jmongo 1.1.1 → 1.1.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/Rakefile +14 -1
- data/jmongo.gemspec +2 -2
- data/lib/jmongo/collection.rb +32 -50
- data/lib/jmongo/connection.rb +24 -9
- data/lib/jmongo/db.rb +42 -32
- data/lib/jmongo/mongo/bson.rb +26 -2
- data/lib/jmongo/mongo/collection.rb +24 -16
- data/lib/jmongo/mongo/connection.rb +3 -2
- data/lib/jmongo/mongo/db.rb +13 -13
- data/lib/jmongo/mongo/jmongo.rb +21 -1
- data/lib/jmongo/mongo/ruby_ext.rb +6 -0
- data/lib/jmongo/mongo/utils.rb +48 -0
- data/lib/jmongo/version.rb +1 -1
- data/test/collection_test.rb +273 -288
- data/test/cursor_test.rb +130 -144
- data/test/db_api_test.rb +267 -284
- data/test/test_helper.rb +43 -14
- metadata +26 -29
data/test/db_api_test.rb
CHANGED
@@ -1,45 +1,41 @@
|
|
1
1
|
require './test/test_helper'
|
2
2
|
|
3
|
-
|
4
|
-
|
5
|
-
include BSON
|
3
|
+
Cfg.connection :op_timeout => 10
|
4
|
+
Cfg.db
|
6
5
|
|
7
|
-
|
8
|
-
|
9
|
-
@@coll = @@db.collection('test')
|
10
|
-
@@version = @@conn.server_version
|
6
|
+
class DBAPITest < MiniTest::Unit::TestCase
|
7
|
+
include Mongo
|
11
8
|
|
12
9
|
def setup
|
13
|
-
|
10
|
+
Cfg.db.strict = false
|
11
|
+
Cfg.clear_all
|
14
12
|
@r1 = {'a' => 1}
|
15
|
-
|
16
|
-
@@coll_full_name = "#{MONGO_TEST_DB}.test"
|
13
|
+
Cfg.coll.insert(@r1) # collection not created until it's used
|
17
14
|
end
|
18
15
|
|
19
16
|
def teardown
|
20
|
-
|
21
|
-
@@db.get_last_error
|
17
|
+
Cfg.db.get_last_error
|
22
18
|
end
|
23
19
|
|
24
20
|
def test_clear
|
25
|
-
assert_equal 1,
|
26
|
-
|
27
|
-
assert_equal 0,
|
21
|
+
assert_equal 1, Cfg.coll.count
|
22
|
+
Cfg.coll.remove
|
23
|
+
assert_equal 0, Cfg.coll.count
|
28
24
|
end
|
29
25
|
|
30
26
|
def test_insert
|
31
|
-
assert_kind_of BSON::ObjectId,
|
32
|
-
assert_kind_of BSON::ObjectId,
|
27
|
+
assert_kind_of BSON::ObjectId, Cfg.coll.insert('a' => 2)
|
28
|
+
assert_kind_of BSON::ObjectId, Cfg.coll.insert('b' => 3)
|
33
29
|
|
34
|
-
assert_equal 3,
|
35
|
-
docs =
|
30
|
+
assert_equal 3, Cfg.coll.count
|
31
|
+
docs = Cfg.coll.find().to_a
|
36
32
|
assert_equal 3, docs.length
|
37
33
|
assert docs.detect { |row| row['a'] == 1 }
|
38
34
|
assert docs.detect { |row| row['a'] == 2 }
|
39
35
|
assert docs.detect { |row| row['b'] == 3 }
|
40
36
|
|
41
|
-
|
42
|
-
docs =
|
37
|
+
Cfg.coll << {'b' => 4}
|
38
|
+
docs = Cfg.coll.find().to_a
|
43
39
|
assert_equal 4, docs.length
|
44
40
|
assert docs.detect { |row| row['b'] == 4 }
|
45
41
|
end
|
@@ -49,23 +45,23 @@ class DBAPITest < Test::Unit::TestCase
|
|
49
45
|
oh['a'] = -1
|
50
46
|
oh['b'] = 'foo'
|
51
47
|
|
52
|
-
oid =
|
53
|
-
assert_equal 'foo',
|
48
|
+
oid = Cfg.coll.save(oh)
|
49
|
+
assert_equal 'foo', Cfg.coll.find_one(oid)['b']
|
54
50
|
|
55
51
|
oh = BSON::OrderedHash['a' => 1, 'b' => 'foo']
|
56
|
-
oid =
|
57
|
-
assert_equal 'foo',
|
52
|
+
oid = Cfg.coll.save(oh)
|
53
|
+
assert_equal 'foo', Cfg.coll.find_one(oid)['b']
|
58
54
|
end
|
59
55
|
|
60
56
|
def test_insert_multiple
|
61
|
-
ids =
|
57
|
+
ids = Cfg.coll.insert([{'a' => 2}, {'b' => 3}])
|
62
58
|
|
63
59
|
ids.each do |i|
|
64
60
|
assert_kind_of BSON::ObjectId, i
|
65
61
|
end
|
66
62
|
|
67
|
-
assert_equal 3,
|
68
|
-
docs =
|
63
|
+
assert_equal 3, Cfg.coll.count
|
64
|
+
docs = Cfg.coll.find().to_a
|
69
65
|
assert_equal 3, docs.length
|
70
66
|
assert docs.detect { |row| row['a'] == 1 }
|
71
67
|
assert docs.detect { |row| row['a'] == 2 }
|
@@ -73,20 +69,20 @@ class DBAPITest < Test::Unit::TestCase
|
|
73
69
|
end
|
74
70
|
|
75
71
|
def test_count_on_nonexisting
|
76
|
-
|
77
|
-
assert_equal 0,
|
72
|
+
Cfg.db.drop_collection('foo')
|
73
|
+
assert_equal 0, Cfg.db.collection('foo').count()
|
78
74
|
end
|
79
75
|
|
80
76
|
def test_find_simple
|
81
|
-
@r2 =
|
82
|
-
@r3 =
|
77
|
+
@r2 = Cfg.coll.insert('a' => 2)
|
78
|
+
@r3 = Cfg.coll.insert('b' => 3)
|
83
79
|
# Check sizes
|
84
|
-
docs =
|
80
|
+
docs = Cfg.coll.find().to_a
|
85
81
|
assert_equal 3, docs.size
|
86
|
-
assert_equal 3,
|
82
|
+
assert_equal 3, Cfg.coll.count
|
87
83
|
|
88
84
|
# Find by other value
|
89
|
-
docs =
|
85
|
+
docs = Cfg.coll.find('a' => @r1['a']).to_a
|
90
86
|
assert_equal 1, docs.size
|
91
87
|
doc = docs.first
|
92
88
|
# Can't compare _id values because at insert, an _id was added to @r1 by
|
@@ -97,52 +93,52 @@ class DBAPITest < Test::Unit::TestCase
|
|
97
93
|
end
|
98
94
|
|
99
95
|
def test_find_advanced
|
100
|
-
|
101
|
-
|
96
|
+
Cfg.coll.insert('a' => 2)
|
97
|
+
Cfg.coll.insert('b' => 3)
|
102
98
|
|
103
99
|
# Find by advanced query (less than)
|
104
|
-
docs =
|
100
|
+
docs = Cfg.coll.find('a' => { '$lt' => 10 }).to_a
|
105
101
|
assert_equal 2, docs.size
|
106
102
|
assert docs.detect { |row| row['a'] == 1 }
|
107
103
|
assert docs.detect { |row| row['a'] == 2 }
|
108
104
|
|
109
105
|
# Find by advanced query (greater than)
|
110
|
-
docs =
|
106
|
+
docs = Cfg.coll.find('a' => { '$gt' => 1 }).to_a
|
111
107
|
assert_equal 1, docs.size
|
112
108
|
assert docs.detect { |row| row['a'] == 2 }
|
113
109
|
|
114
110
|
# Find by advanced query (less than or equal to)
|
115
|
-
docs =
|
111
|
+
docs = Cfg.coll.find('a' => { '$lte' => 1 }).to_a
|
116
112
|
assert_equal 1, docs.size
|
117
113
|
assert docs.detect { |row| row['a'] == 1 }
|
118
114
|
|
119
115
|
# Find by advanced query (greater than or equal to)
|
120
|
-
docs =
|
116
|
+
docs = Cfg.coll.find('a' => { '$gte' => 1 }).to_a
|
121
117
|
assert_equal 2, docs.size
|
122
118
|
assert docs.detect { |row| row['a'] == 1 }
|
123
119
|
assert docs.detect { |row| row['a'] == 2 }
|
124
120
|
|
125
121
|
# Find by advanced query (between)
|
126
|
-
docs =
|
122
|
+
docs = Cfg.coll.find('a' => { '$gt' => 1, '$lt' => 3 }).to_a
|
127
123
|
assert_equal 1, docs.size
|
128
124
|
assert docs.detect { |row| row['a'] == 2 }
|
129
125
|
|
130
126
|
# Find by advanced query (in clause)
|
131
|
-
docs =
|
127
|
+
docs = Cfg.coll.find('a' => {'$in' => [1,2]}).to_a
|
132
128
|
assert_equal 2, docs.size
|
133
129
|
assert docs.detect { |row| row['a'] == 1 }
|
134
130
|
assert docs.detect { |row| row['a'] == 2 }
|
135
131
|
end
|
136
132
|
|
137
133
|
def test_find_sorting
|
138
|
-
|
139
|
-
|
140
|
-
|
141
|
-
|
142
|
-
|
134
|
+
Cfg.coll.remove
|
135
|
+
Cfg.coll.insert('a' => 1, 'b' => 2)
|
136
|
+
Cfg.coll.insert('a' => 2, 'b' => 1)
|
137
|
+
Cfg.coll.insert('a' => 3, 'b' => 2)
|
138
|
+
Cfg.coll.insert('a' => 4, 'b' => 1)
|
143
139
|
|
144
140
|
# Sorting (ascending)
|
145
|
-
docs =
|
141
|
+
docs = Cfg.coll.find({'a' => { '$lt' => 10 }}, :sort => [['a', 1]]).to_a
|
146
142
|
assert_equal 4, docs.size
|
147
143
|
assert_equal 1, docs[0]['a']
|
148
144
|
assert_equal 2, docs[1]['a']
|
@@ -150,7 +146,7 @@ class DBAPITest < Test::Unit::TestCase
|
|
150
146
|
assert_equal 4, docs[3]['a']
|
151
147
|
|
152
148
|
# Sorting (descending)
|
153
|
-
docs =
|
149
|
+
docs = Cfg.coll.find({'a' => { '$lt' => 10 }}, :sort => [['a', -1]]).to_a
|
154
150
|
assert_equal 4, docs.size
|
155
151
|
assert_equal 4, docs[0]['a']
|
156
152
|
assert_equal 3, docs[1]['a']
|
@@ -158,7 +154,7 @@ class DBAPITest < Test::Unit::TestCase
|
|
158
154
|
assert_equal 1, docs[3]['a']
|
159
155
|
|
160
156
|
# Sorting using array of names; assumes ascending order.
|
161
|
-
docs =
|
157
|
+
docs = Cfg.coll.find({'a' => { '$lt' => 10 }}, :sort => 'a').to_a
|
162
158
|
assert_equal 4, docs.size
|
163
159
|
assert_equal 1, docs[0]['a']
|
164
160
|
assert_equal 2, docs[1]['a']
|
@@ -166,14 +162,14 @@ class DBAPITest < Test::Unit::TestCase
|
|
166
162
|
assert_equal 4, docs[3]['a']
|
167
163
|
|
168
164
|
# Sorting using single name; assumes ascending order.
|
169
|
-
docs =
|
165
|
+
docs = Cfg.coll.find({'a' => { '$lt' => 10 }}, :sort => 'a').to_a
|
170
166
|
assert_equal 4, docs.size
|
171
167
|
assert_equal 1, docs[0]['a']
|
172
168
|
assert_equal 2, docs[1]['a']
|
173
169
|
assert_equal 3, docs[2]['a']
|
174
170
|
assert_equal 4, docs[3]['a']
|
175
171
|
|
176
|
-
docs =
|
172
|
+
docs = Cfg.coll.find({'a' => { '$lt' => 10 }}, :sort => [['b', 'asc'], ['a', 'asc']]).to_a
|
177
173
|
assert_equal 4, docs.size
|
178
174
|
assert_equal 2, docs[0]['a']
|
179
175
|
assert_equal 4, docs[1]['a']
|
@@ -181,357 +177,343 @@ class DBAPITest < Test::Unit::TestCase
|
|
181
177
|
assert_equal 3, docs[3]['a']
|
182
178
|
|
183
179
|
# Sorting using empty array; no order guarantee should not blow up.
|
184
|
-
docs =
|
180
|
+
docs = Cfg.coll.find({'a' => { '$lt' => 10 }}, :sort => []).to_a
|
185
181
|
assert_equal 4, docs.size
|
186
182
|
|
187
183
|
# Sorting using ordered hash. You can use an unordered one, but then the
|
188
184
|
# order of the keys won't be guaranteed thus your sort won't make sense.
|
189
185
|
oh = BSON::OrderedHash.new
|
190
186
|
oh['a'] = -1
|
191
|
-
|
192
|
-
docs =
|
187
|
+
assert_raises InvalidSortValueError do
|
188
|
+
docs = Cfg.coll.find({'a' => { '$lt' => 10 }}, :sort => oh).to_a
|
193
189
|
end
|
194
190
|
end
|
195
191
|
|
196
192
|
def test_find_limits
|
197
|
-
|
198
|
-
|
199
|
-
|
193
|
+
Cfg.coll.insert('b' => 2)
|
194
|
+
Cfg.coll.insert('c' => 3)
|
195
|
+
Cfg.coll.insert('d' => 4)
|
200
196
|
|
201
|
-
docs =
|
197
|
+
docs = Cfg.coll.find({}, :limit => 1).to_a
|
202
198
|
assert_equal 1, docs.size
|
203
|
-
docs =
|
199
|
+
docs = Cfg.coll.find({}, :limit => 2).to_a
|
204
200
|
assert_equal 2, docs.size
|
205
|
-
docs =
|
201
|
+
docs = Cfg.coll.find({}, :limit => 3).to_a
|
206
202
|
assert_equal 3, docs.size
|
207
|
-
docs =
|
203
|
+
docs = Cfg.coll.find({}, :limit => 4).to_a
|
208
204
|
assert_equal 4, docs.size
|
209
|
-
docs =
|
205
|
+
docs = Cfg.coll.find({}).to_a
|
210
206
|
assert_equal 4, docs.size
|
211
|
-
docs =
|
207
|
+
docs = Cfg.coll.find({}, :limit => 99).to_a
|
212
208
|
assert_equal 4, docs.size
|
213
209
|
end
|
214
210
|
|
215
211
|
def test_find_one_no_records
|
216
|
-
|
217
|
-
x =
|
212
|
+
Cfg.coll.remove
|
213
|
+
x = Cfg.coll.find_one('a' => 1)
|
218
214
|
assert_nil x
|
219
215
|
end
|
220
216
|
|
221
217
|
def test_drop_collection
|
222
|
-
assert
|
223
|
-
assert
|
218
|
+
assert Cfg.db.drop_collection(Cfg.coll.name), "drop of collection #{Cfg.coll.name} failed"
|
219
|
+
assert !Cfg.db.collection_names.include?(Cfg.coll.name)
|
224
220
|
end
|
225
221
|
|
226
222
|
def test_other_drop
|
227
|
-
assert
|
228
|
-
|
229
|
-
assert
|
223
|
+
assert Cfg.db.collection_names.include?(Cfg.coll.name)
|
224
|
+
Cfg.coll.drop
|
225
|
+
assert !Cfg.db.collection_names.include?(Cfg.coll.name)
|
230
226
|
end
|
231
227
|
|
232
228
|
def test_collection_names
|
233
|
-
names =
|
229
|
+
names = Cfg.db.collection_names
|
234
230
|
assert names.length >= 1
|
235
|
-
assert names.include?(
|
231
|
+
assert names.include?(Cfg.coll.name)
|
236
232
|
|
237
|
-
coll2 =
|
233
|
+
coll2 = Cfg.db.collection('test2')
|
238
234
|
coll2.insert('a' => 1) # collection not created until it's used
|
239
|
-
names =
|
235
|
+
names = Cfg.db.collection_names
|
240
236
|
assert names.length >= 2
|
241
|
-
assert names.include?(
|
237
|
+
assert names.include?(Cfg.coll.name)
|
242
238
|
assert names.include?('test2')
|
243
|
-
ensure
|
244
|
-
@@db.drop_collection('test2')
|
245
239
|
end
|
246
240
|
|
247
241
|
def test_collections_info
|
248
|
-
cursor =
|
242
|
+
cursor = Cfg.db.collections_info
|
249
243
|
rows = cursor.to_a
|
250
244
|
assert rows.length >= 1
|
251
|
-
row = rows.detect { |r| r['name'] ==
|
245
|
+
row = rows.detect { |r| r['name'] == Cfg.coll_full_name }
|
252
246
|
assert_not_nil row
|
253
247
|
end
|
254
248
|
|
255
249
|
def test_collection_options
|
256
|
-
|
257
|
-
|
250
|
+
Cfg.db.drop_collection('foobar')
|
251
|
+
Cfg.db.strict = true
|
258
252
|
|
259
253
|
begin
|
260
|
-
coll =
|
254
|
+
coll = Cfg.db.create_collection('foobar', :capped => true, :size => 1024)
|
261
255
|
options = coll.options()
|
262
256
|
assert_equal 'foobar', options['create']
|
263
257
|
assert_equal true, options['capped']
|
264
258
|
assert_equal 1024, options['size']
|
265
259
|
rescue => ex
|
266
|
-
|
260
|
+
Cfg.db.drop_collection('foobar')
|
267
261
|
fail "did not expect exception \"#{ex}\""
|
268
262
|
ensure
|
269
|
-
|
263
|
+
Cfg.db.strict = false
|
270
264
|
end
|
271
265
|
end
|
272
266
|
|
273
267
|
def test_collection_options_are_passed_to_the_existing_ones
|
274
|
-
|
268
|
+
Cfg.db.drop_collection('foobar')
|
275
269
|
|
276
|
-
|
270
|
+
Cfg.db.create_collection('foobar')
|
277
271
|
|
278
272
|
opts = {:safe => true}
|
279
|
-
coll =
|
273
|
+
coll = Cfg.db.create_collection('foobar', opts)
|
280
274
|
assert_equal true, coll.safe
|
281
275
|
end
|
282
276
|
|
283
|
-
|
284
277
|
def test_index_information
|
285
|
-
assert_equal
|
278
|
+
assert_equal Cfg.coll.index_information.length, 1
|
286
279
|
|
287
|
-
name =
|
288
|
-
|
289
|
-
assert_equal name, "a_1"
|
290
|
-
assert_equal @@coll.index_information, info
|
291
|
-
assert_equal 2, info.length
|
280
|
+
name = Cfg.coll.create_index('a')
|
281
|
+
assert_equal "a_1", name
|
292
282
|
|
283
|
+
info = Cfg.db.index_information(Cfg.coll.name)
|
284
|
+
assert_equal Cfg.coll.index_information, info
|
285
|
+
assert_equal 2, info.length
|
293
286
|
assert info.has_key?(name)
|
294
287
|
assert_equal info[name]["key"], {"a" => 1}
|
295
|
-
ensure
|
296
|
-
@@db.drop_index(@@coll.name, name)
|
297
288
|
end
|
298
289
|
|
299
290
|
def test_index_create_with_symbol
|
300
|
-
info =
|
291
|
+
info = Cfg.coll.index_information
|
301
292
|
assert_equal info.length, 1
|
302
|
-
|
303
|
-
|
304
|
-
info = @@db.index_information(@@coll.name)
|
305
|
-
assert_equal name, "a_1"
|
306
|
-
assert_equal @@coll.index_information, info
|
307
|
-
assert_equal 2, info.length
|
293
|
+
name = Cfg.coll.create_index([['a', 1]])
|
294
|
+
assert_equal "a_1", name
|
308
295
|
|
296
|
+
info = Cfg.db.index_information(Cfg.coll.name)
|
297
|
+
assert_equal Cfg.coll.index_information, info
|
298
|
+
assert_equal 2, info.length
|
309
299
|
assert info.has_key?(name)
|
310
300
|
assert_equal info[name]['key'], {"a" => 1}
|
311
|
-
ensure
|
312
|
-
@@db.drop_index(@@coll.name, name)
|
313
301
|
end
|
314
302
|
|
315
303
|
def test_multiple_index_cols
|
316
|
-
name =
|
317
|
-
|
318
|
-
assert_equal 2, info.length
|
304
|
+
name = Cfg.coll.create_index([['a', DESCENDING], ['b', ASCENDING], ['c', DESCENDING]])
|
305
|
+
assert_equal 'a_-1_b_1_c_-1', name
|
319
306
|
|
320
|
-
|
307
|
+
info = Cfg.db.index_information(Cfg.coll.name)
|
308
|
+
assert_equal 2, info.length
|
321
309
|
assert info.has_key?(name)
|
322
310
|
assert_equal info[name]['key'], {"a" => -1, "b" => 1, "c" => -1}
|
323
|
-
ensure
|
324
|
-
@@db.drop_index(@@coll.name, name)
|
325
311
|
end
|
326
312
|
|
327
313
|
def test_multiple_index_cols_with_symbols
|
328
|
-
name =
|
329
|
-
info = @@db.index_information(@@coll.name)
|
330
|
-
assert_equal 2, info.length
|
331
|
-
|
314
|
+
name = Cfg.coll.create_index([[:a, DESCENDING], [:b, ASCENDING], [:c, DESCENDING]])
|
332
315
|
assert_equal name, 'a_-1_b_1_c_-1'
|
316
|
+
|
317
|
+
info = Cfg.db.index_information(Cfg.coll.name)
|
318
|
+
assert_equal 2, info.length
|
333
319
|
assert info.has_key?(name)
|
334
320
|
assert_equal info[name]['key'], {"a" => -1, "b" => 1, "c" => -1}
|
335
|
-
ensure
|
336
|
-
@@db.drop_index(@@coll.name, name)
|
337
321
|
end
|
338
322
|
|
339
323
|
def test_unique_index
|
340
|
-
|
341
|
-
test =
|
324
|
+
Cfg.db.drop_collection("blah")
|
325
|
+
test = Cfg.db.collection("blah")
|
342
326
|
test.create_index("hello")
|
343
327
|
|
344
328
|
test.insert("hello" => "world")
|
345
329
|
test.insert("hello" => "mike")
|
346
330
|
test.insert("hello" => "world")
|
347
|
-
assert
|
331
|
+
assert !Cfg.db.error?
|
348
332
|
|
349
|
-
|
350
|
-
test =
|
333
|
+
Cfg.db.drop_collection("blah")
|
334
|
+
test = Cfg.db.collection("blah", :safe => true)
|
351
335
|
test.create_index("hello", :unique => true)
|
352
336
|
|
353
337
|
test.insert("hello" => "world")
|
354
338
|
test.insert("hello" => "mike")
|
355
|
-
|
356
|
-
|
339
|
+
assert_raises Mongo::OperationFailure do
|
340
|
+
test.insert("hello" => "world")
|
341
|
+
end
|
342
|
+
assert Cfg.db.error?
|
357
343
|
end
|
358
344
|
|
359
345
|
def test_index_on_subfield
|
360
|
-
|
361
|
-
test =
|
346
|
+
Cfg.db.drop_collection("blah")
|
347
|
+
test = Cfg.db.collection("blah")
|
362
348
|
|
363
349
|
test.insert("hello" => {"a" => 4, "b" => 5})
|
364
350
|
test.insert("hello" => {"a" => 7, "b" => 2})
|
365
351
|
test.insert("hello" => {"a" => 4, "b" => 10})
|
366
|
-
assert
|
352
|
+
assert !Cfg.db.error?
|
367
353
|
|
368
|
-
|
369
|
-
test =
|
354
|
+
Cfg.db.drop_collection("blah")
|
355
|
+
test = Cfg.db.collection("blah")
|
370
356
|
test.create_index("hello.a", :unique => true)
|
371
357
|
|
372
358
|
test.insert("hello" => {"a" => 4, "b" => 5})
|
373
359
|
test.insert("hello" => {"a" => 7, "b" => 2})
|
374
360
|
test.insert("hello" => {"a" => 4, "b" => 10})
|
375
|
-
assert
|
361
|
+
assert Cfg.db.error?
|
362
|
+
assert 2, test.count
|
363
|
+
|
376
364
|
end
|
377
365
|
|
378
366
|
def test_array
|
379
|
-
|
380
|
-
|
381
|
-
|
382
|
-
rows =
|
367
|
+
Cfg.coll.remove
|
368
|
+
Cfg.coll.insert({'b' => [1, 2, 3]})
|
369
|
+
Cfg.coll.insert({'b' => [1, 2, 3]})
|
370
|
+
rows = Cfg.coll.find({}, {:fields => ['b']}).to_a
|
383
371
|
assert_equal 2, rows.length
|
384
372
|
assert_equal [1, 2, 3], rows[1]['b']
|
385
373
|
end
|
386
374
|
|
387
375
|
def test_regex
|
388
376
|
regex = /foobar/i
|
389
|
-
|
390
|
-
rows =
|
391
|
-
if
|
377
|
+
Cfg.coll << {'b' => regex}
|
378
|
+
rows = Cfg.coll.find({}, {:fields => ['b']}).to_a
|
379
|
+
if Cfg.version < "1.1.3"
|
392
380
|
assert_equal 1, rows.length
|
393
381
|
assert_equal regex, rows[0]['b']
|
394
382
|
else
|
395
383
|
assert_equal 2, rows.length
|
396
|
-
|
384
|
+
ret_regex = rows[1]['b']
|
385
|
+
assert_equal regex, ret_regex
|
386
|
+
assert "fOObar" =~ ret_regex
|
397
387
|
end
|
398
388
|
end
|
399
389
|
|
400
390
|
def test_regex_multi_line
|
401
|
-
|
391
|
+
skip("test is for future version") unless Cfg.version >= "1.9.1"
|
402
392
|
doc = <<HERE
|
403
393
|
the lazy brown
|
404
394
|
fox
|
405
395
|
HERE
|
406
|
-
|
407
|
-
|
408
|
-
|
409
|
-
end
|
396
|
+
Cfg.coll.save({:doc => doc})
|
397
|
+
assert Cfg.coll.find_one({:doc => /n.*x/m})
|
398
|
+
Cfg.coll.remove
|
410
399
|
end
|
411
400
|
|
412
401
|
def test_non_oid_id
|
413
402
|
# Note: can't use Time.new because that will include fractional seconds,
|
414
403
|
# which Mongo does not store.
|
415
404
|
t = Time.at(1234567890)
|
416
|
-
|
417
|
-
rows =
|
405
|
+
Cfg.coll << {'_id' => t}
|
406
|
+
rows = Cfg.coll.find({'_id' => t}).to_a
|
418
407
|
assert_equal 1, rows.length
|
419
408
|
assert_equal t, rows[0]['_id']
|
420
409
|
end
|
421
410
|
|
422
411
|
def test_strict
|
423
|
-
assert
|
424
|
-
|
425
|
-
assert
|
412
|
+
assert !Cfg.db.strict?
|
413
|
+
Cfg.db.strict = true
|
414
|
+
assert Cfg.db.strict?
|
426
415
|
ensure
|
427
|
-
|
416
|
+
Cfg.db.strict = false
|
428
417
|
end
|
429
418
|
|
430
419
|
def test_strict_access_collection
|
431
|
-
|
432
|
-
|
433
|
-
|
434
|
-
fail "expected exception"
|
435
|
-
rescue => ex
|
436
|
-
assert_equal Mongo::MongoDBError, ex.class
|
437
|
-
assert_equal "Collection does-not-exist doesn't exist. Currently in strict mode.", ex.to_s
|
438
|
-
ensure
|
439
|
-
@@db.strict = false
|
440
|
-
@@db.drop_collection('does-not-exist')
|
420
|
+
Cfg.db.strict = true
|
421
|
+
assert_raises Mongo::MongoDBError do
|
422
|
+
Cfg.db.collection('does-not-exist')
|
441
423
|
end
|
424
|
+
Cfg.db.strict = false
|
442
425
|
end
|
443
426
|
|
444
427
|
def test_strict_create_collection
|
445
|
-
|
446
|
-
|
428
|
+
Cfg.db.drop_collection('foobar')
|
429
|
+
Cfg.db.strict = true
|
447
430
|
|
448
431
|
begin
|
449
|
-
|
432
|
+
Cfg.db.create_collection('foobar')
|
450
433
|
assert true
|
451
434
|
rescue => ex
|
452
435
|
fail "did not expect exception \"#{ex}\""
|
453
436
|
end
|
454
437
|
|
455
438
|
# Now the collection exists. This time we should see an exception.
|
456
|
-
|
457
|
-
|
439
|
+
assert_raises Mongo::MongoDBError do
|
440
|
+
Cfg.db.create_collection('foobar')
|
458
441
|
end
|
459
|
-
|
460
|
-
|
442
|
+
Cfg.db.strict = false
|
443
|
+
Cfg.db.drop_collection('foobar')
|
461
444
|
|
462
445
|
# Now we're not in strict mode - should succeed
|
463
|
-
|
464
|
-
|
465
|
-
|
446
|
+
Cfg.db.create_collection('foobar')
|
447
|
+
Cfg.db.create_collection('foobar')
|
448
|
+
Cfg.db.drop_collection('foobar')
|
466
449
|
end
|
467
450
|
|
468
451
|
def test_where
|
469
|
-
|
470
|
-
|
452
|
+
Cfg.coll.insert('a' => 2)
|
453
|
+
Cfg.coll.insert('a' => 3)
|
471
454
|
|
472
|
-
assert_equal 3,
|
473
|
-
assert_equal 1,
|
474
|
-
assert_equal 2,
|
455
|
+
assert_equal 3, Cfg.coll.count
|
456
|
+
assert_equal 1, Cfg.coll.find('$where' => BSON::Code.new('this.a > 2')).count()
|
457
|
+
assert_equal 2, Cfg.coll.find('$where' => BSON::Code.new('this.a > i', {'i' => 1})).count()
|
475
458
|
end
|
476
459
|
|
477
460
|
def test_eval
|
478
|
-
assert_equal 3,
|
461
|
+
assert_equal 3, Cfg.db.eval('function (x) {return x;}', 3)
|
479
462
|
|
480
|
-
assert_equal nil,
|
481
|
-
assert_equal 5,
|
463
|
+
assert_equal nil, Cfg.db.eval("function (x) {db.test_eval.save({y:x});}", 5)
|
464
|
+
assert_equal 5, Cfg.db.collection('test_eval').find_one['y']
|
482
465
|
|
483
|
-
assert_equal 5,
|
484
|
-
assert_equal 5,
|
485
|
-
assert_equal 5,
|
466
|
+
assert_equal 5, Cfg.db.eval("function (x, y) {return x + y;}", 2, 3)
|
467
|
+
assert_equal 5, Cfg.db.eval("function () {return 5;}")
|
468
|
+
assert_equal 5, Cfg.db.eval("2 + 3;")
|
486
469
|
|
487
|
-
assert_equal 5,
|
488
|
-
assert_equal 2,
|
489
|
-
assert_equal 5,
|
470
|
+
assert_equal 5, Cfg.db.eval(Code.new("2 + 3;"))
|
471
|
+
assert_equal 2, Cfg.db.eval(Code.new("return i;", {"i" => 2}))
|
472
|
+
assert_equal 5, Cfg.db.eval(Code.new("i + 3;", {"i" => 2}))
|
490
473
|
|
491
|
-
|
492
|
-
|
474
|
+
assert_raises OperationFailure do
|
475
|
+
Cfg.db.eval("5 ++ 5;")
|
493
476
|
end
|
494
477
|
end
|
495
478
|
|
496
479
|
def test_hint
|
497
|
-
|
480
|
+
coll = Cfg.coll
|
481
|
+
name = coll.create_index('a')
|
498
482
|
begin
|
499
|
-
assert_nil
|
500
|
-
assert_equal 1,
|
501
|
-
assert_equal 1,
|
502
|
-
assert_equal 1,
|
503
|
-
|
504
|
-
|
505
|
-
assert_equal({'a' => 1},
|
506
|
-
assert_equal 1,
|
507
|
-
|
508
|
-
|
509
|
-
assert_equal({'a' => 1},
|
510
|
-
assert_equal 1,
|
511
|
-
|
512
|
-
|
513
|
-
assert_equal({'a' => 1},
|
514
|
-
assert_equal 1,
|
515
|
-
|
516
|
-
|
517
|
-
assert_nil
|
518
|
-
assert_equal 1,
|
519
|
-
ensure
|
520
|
-
@@coll.drop_index(name)
|
483
|
+
assert_nil coll.hint
|
484
|
+
assert_equal 1, coll.find({'a' => 1}, :hint => 'a').to_a.size
|
485
|
+
assert_equal 1, coll.find({'a' => 1}, :hint => ['a']).to_a.size
|
486
|
+
assert_equal 1, coll.find({'a' => 1}, :hint => {'a' => 1}).to_a.size
|
487
|
+
|
488
|
+
coll.hint = 'a'
|
489
|
+
assert_equal({'a' => 1}, coll.hint)
|
490
|
+
assert_equal 1, coll.find('a' => 1).to_a.size
|
491
|
+
|
492
|
+
coll.hint = ['a']
|
493
|
+
assert_equal({'a' => 1}, coll.hint)
|
494
|
+
assert_equal 1, coll.find('a' => 1).to_a.size
|
495
|
+
|
496
|
+
coll.hint = {'a' => 1}
|
497
|
+
assert_equal({'a' => 1}, coll.hint)
|
498
|
+
assert_equal 1, coll.find('a' => 1).to_a.size
|
499
|
+
|
500
|
+
coll.hint = nil
|
501
|
+
assert_nil coll.hint
|
502
|
+
assert_equal 1, coll.find('a' => 1).to_a.size
|
521
503
|
end
|
522
504
|
end
|
523
505
|
|
524
506
|
def test_hash_default_value_id
|
525
507
|
val = Hash.new(0)
|
526
508
|
val["x"] = 5
|
527
|
-
|
528
|
-
id =
|
509
|
+
Cfg.coll.insert val
|
510
|
+
id = Cfg.coll.find_one("x" => 5)["_id"]
|
529
511
|
assert id != 0
|
530
512
|
end
|
531
513
|
|
532
514
|
def test_group
|
533
|
-
|
534
|
-
test =
|
515
|
+
Cfg.db.drop_collection("test")
|
516
|
+
test = Cfg.db.collection("test")
|
535
517
|
|
536
518
|
assert_equal [], test.group(:initial => {"count" => 0}, :reduce => "function (obj, prev) { prev.count++; }")
|
537
519
|
assert_equal [], test.group(:initial => {"count" => 0}, :reduce => "function (obj, prev) { prev.count++; }")
|
@@ -559,135 +541,135 @@ HERE
|
|
559
541
|
{"a" => 1, "count" => 1}]
|
560
542
|
assert_equal expected, test.group(:key => ["a"], :initial => {"count" => 0},
|
561
543
|
:reduce => "function (obj, prev) { prev.count++; }")
|
562
|
-
assert_equal expected, test.group(:key => :a, :initial => {"count" => 0},
|
544
|
+
assert_equal expected, test.group(:key => [:a], :initial => {"count" => 0},
|
563
545
|
:reduce => "function (obj, prev) { prev.count++; }")
|
564
546
|
|
565
|
-
|
547
|
+
assert_raises OperationFailure do
|
566
548
|
test.group(:initial => {}, :reduce => "5 ++ 5")
|
567
549
|
end
|
568
550
|
end
|
569
551
|
|
570
552
|
def test_deref
|
571
|
-
|
572
|
-
|
573
|
-
assert_equal nil,
|
574
|
-
|
575
|
-
key =
|
576
|
-
assert_equal "hello",
|
577
|
-
|
578
|
-
assert_equal nil,
|
553
|
+
Cfg.coll.remove
|
554
|
+
dbref = DBRef.new("test", ObjectId.new)
|
555
|
+
assert_equal nil, Cfg.db.dereference(dbref)
|
556
|
+
Cfg.coll.insert({"x" => "hello"})
|
557
|
+
key = Cfg.coll.find_one()["_id"]
|
558
|
+
assert_equal "hello", Cfg.db.dereference(DBRef.new("test", key))["x"]
|
559
|
+
|
560
|
+
assert_equal nil, Cfg.db.dereference(DBRef.new("test", 4))
|
579
561
|
obj = {"_id" => 4}
|
580
|
-
|
581
|
-
assert_equal obj,
|
562
|
+
Cfg.coll.insert(obj)
|
563
|
+
assert_equal obj, Cfg.db.dereference(DBRef.new("test", 4))
|
582
564
|
|
583
|
-
|
584
|
-
|
585
|
-
assert_equal nil,
|
565
|
+
Cfg.coll.remove
|
566
|
+
Cfg.coll.insert({"x" => "hello"})
|
567
|
+
assert_equal nil, Cfg.db.dereference(DBRef.new("test", nil))
|
586
568
|
end
|
587
569
|
|
588
570
|
def test_save
|
589
|
-
|
571
|
+
Cfg.coll.remove
|
590
572
|
|
591
573
|
a = {"hello" => "world"}
|
592
574
|
|
593
|
-
id =
|
575
|
+
id = Cfg.coll.save(a)
|
594
576
|
assert_kind_of ObjectId, id
|
595
|
-
assert_equal 1,
|
577
|
+
assert_equal 1, Cfg.coll.count
|
596
578
|
|
597
|
-
assert_equal id,
|
598
|
-
assert_equal 1,
|
579
|
+
assert_equal id, Cfg.coll.save(a)
|
580
|
+
assert_equal 1, Cfg.coll.count
|
599
581
|
|
600
|
-
assert_equal "world",
|
582
|
+
assert_equal "world", Cfg.coll.find_one()["hello"]
|
601
583
|
|
602
584
|
a["hello"] = "mike"
|
603
|
-
|
604
|
-
assert_equal 1,
|
585
|
+
Cfg.coll.save(a)
|
586
|
+
assert_equal 1, Cfg.coll.count
|
605
587
|
|
606
|
-
assert_equal "mike",
|
588
|
+
assert_equal "mike", Cfg.coll.find_one()["hello"]
|
607
589
|
|
608
|
-
|
609
|
-
assert_equal 2,
|
590
|
+
Cfg.coll.save({"hello" => "world"})
|
591
|
+
assert_equal 2, Cfg.coll.count
|
610
592
|
end
|
611
593
|
|
612
594
|
def test_save_long
|
613
|
-
|
614
|
-
|
615
|
-
assert_equal 9223372036854775807,
|
595
|
+
Cfg.coll.remove
|
596
|
+
Cfg.coll.insert("x" => 9223372036854775807)
|
597
|
+
assert_equal 9223372036854775807, Cfg.coll.find_one()["x"]
|
616
598
|
end
|
617
599
|
|
618
600
|
def test_find_by_oid
|
619
|
-
|
601
|
+
Cfg.coll.remove
|
620
602
|
|
621
|
-
|
622
|
-
id =
|
603
|
+
Cfg.coll.save("hello" => "mike")
|
604
|
+
id = Cfg.coll.save("hello" => "world")
|
623
605
|
assert_kind_of ObjectId, id
|
624
606
|
|
625
|
-
assert_equal "world",
|
626
|
-
|
607
|
+
assert_equal "world", Cfg.coll.find_one(:_id => id)["hello"]
|
608
|
+
Cfg.coll.find(:_id => id).to_a.each do |doc|
|
627
609
|
assert_equal "world", doc["hello"]
|
628
610
|
end
|
629
611
|
|
630
612
|
id = ObjectId.from_string(id.to_s)
|
631
|
-
assert_equal "world",
|
613
|
+
assert_equal "world", Cfg.coll.find_one(:_id => id)["hello"]
|
632
614
|
end
|
633
615
|
|
634
616
|
def test_save_with_object_that_has_id_but_does_not_actually_exist_in_collection
|
635
|
-
|
617
|
+
Cfg.coll.remove
|
636
618
|
|
637
619
|
a = {'_id' => '1', 'hello' => 'world'}
|
638
|
-
|
639
|
-
assert_equal(1,
|
640
|
-
assert_equal("world",
|
620
|
+
Cfg.coll.save(a)
|
621
|
+
assert_equal(1, Cfg.coll.count)
|
622
|
+
assert_equal("world", Cfg.coll.find_one()["hello"])
|
641
623
|
|
642
624
|
a["hello"] = "mike"
|
643
|
-
|
644
|
-
assert_equal(1,
|
645
|
-
assert_equal("mike",
|
625
|
+
Cfg.coll.save(a)
|
626
|
+
assert_equal(1, Cfg.coll.count)
|
627
|
+
assert_equal("mike", Cfg.coll.find_one()["hello"])
|
646
628
|
end
|
647
629
|
|
648
630
|
def test_collection_names_errors
|
649
|
-
|
650
|
-
|
631
|
+
assert_raises TypeError do
|
632
|
+
Cfg.db.collection(5)
|
651
633
|
end
|
652
|
-
|
653
|
-
|
634
|
+
assert_raises Mongo::InvalidNSName do
|
635
|
+
Cfg.db.collection("")
|
654
636
|
end
|
655
|
-
|
656
|
-
|
637
|
+
assert_raises Mongo::InvalidNSName do
|
638
|
+
Cfg.db.collection("te$t")
|
657
639
|
end
|
658
|
-
|
659
|
-
|
640
|
+
assert_raises Mongo::InvalidNSName do
|
641
|
+
Cfg.db.collection(".test")
|
660
642
|
end
|
661
|
-
|
662
|
-
|
643
|
+
assert_raises Mongo::InvalidNSName do
|
644
|
+
Cfg.db.collection("test.")
|
663
645
|
end
|
664
|
-
|
665
|
-
|
646
|
+
assert_raises Mongo::InvalidNSName do
|
647
|
+
Cfg.db.collection("tes..t")
|
666
648
|
end
|
667
649
|
end
|
668
650
|
|
669
651
|
def test_rename_collection
|
670
|
-
|
671
|
-
|
672
|
-
a =
|
673
|
-
b =
|
652
|
+
Cfg.db.drop_collection("foo")
|
653
|
+
Cfg.db.drop_collection("bar")
|
654
|
+
a = Cfg.db.collection("foo")
|
655
|
+
b = Cfg.db.collection("bar")
|
674
656
|
|
675
|
-
|
657
|
+
assert_raises TypeError do
|
676
658
|
a.rename(5)
|
677
659
|
end
|
678
|
-
|
660
|
+
assert_raises Mongo::InvalidNSName do
|
679
661
|
a.rename("")
|
680
662
|
end
|
681
|
-
|
663
|
+
assert_raises Mongo::InvalidNSName do
|
682
664
|
a.rename("te$t")
|
683
665
|
end
|
684
|
-
|
666
|
+
assert_raises Mongo::InvalidNSName do
|
685
667
|
a.rename(".test")
|
686
668
|
end
|
687
|
-
|
669
|
+
assert_raises Mongo::InvalidNSName do
|
688
670
|
a.rename("test.")
|
689
671
|
end
|
690
|
-
|
672
|
+
assert_raises Mongo::InvalidNSName do
|
691
673
|
a.rename("tes..t")
|
692
674
|
end
|
693
675
|
|
@@ -706,9 +688,10 @@ HERE
|
|
706
688
|
|
707
689
|
# doesn't really test functionality, just that the option is set correctly
|
708
690
|
def test_snapshot
|
709
|
-
|
710
|
-
|
711
|
-
|
691
|
+
skip("i am pointless")
|
692
|
+
Cfg.db.collection("test").find({}, :snapshot => true).to_a
|
693
|
+
assert_raises OperationFailure do
|
694
|
+
Cfg.db.collection("test").find({}, :snapshot => true, :sort => 'a').to_a
|
712
695
|
end
|
713
696
|
end
|
714
697
|
|
@@ -727,9 +710,9 @@ HERE
|
|
727
710
|
assert_equal "UTF-8", utf8.encoding.name
|
728
711
|
assert_equal "ISO-8859-1", iso8859.encoding.name
|
729
712
|
|
730
|
-
|
731
|
-
|
732
|
-
doc =
|
713
|
+
Cfg.coll.remove
|
714
|
+
Cfg.coll.save("ascii" => ascii, "utf8" => utf8, "iso8859" => iso8859)
|
715
|
+
doc = Cfg.coll.find_one()
|
733
716
|
|
734
717
|
assert_equal "UTF-8", doc["ascii"].encoding.name
|
735
718
|
assert_equal "UTF-8", doc["utf8"].encoding.name
|