mongo-find_replace 0.18.3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (68) hide show
  1. data/LICENSE.txt +202 -0
  2. data/README.rdoc +358 -0
  3. data/Rakefile +133 -0
  4. data/bin/bson_benchmark.rb +59 -0
  5. data/bin/fail_if_no_c.rb +11 -0
  6. data/examples/admin.rb +42 -0
  7. data/examples/capped.rb +22 -0
  8. data/examples/cursor.rb +48 -0
  9. data/examples/gridfs.rb +88 -0
  10. data/examples/index_test.rb +126 -0
  11. data/examples/info.rb +31 -0
  12. data/examples/queries.rb +70 -0
  13. data/examples/simple.rb +24 -0
  14. data/examples/strict.rb +35 -0
  15. data/examples/types.rb +36 -0
  16. data/lib/mongo.rb +61 -0
  17. data/lib/mongo/admin.rb +95 -0
  18. data/lib/mongo/collection.rb +664 -0
  19. data/lib/mongo/connection.rb +555 -0
  20. data/lib/mongo/cursor.rb +393 -0
  21. data/lib/mongo/db.rb +527 -0
  22. data/lib/mongo/exceptions.rb +60 -0
  23. data/lib/mongo/gridfs.rb +22 -0
  24. data/lib/mongo/gridfs/chunk.rb +90 -0
  25. data/lib/mongo/gridfs/grid_store.rb +555 -0
  26. data/lib/mongo/types/binary.rb +48 -0
  27. data/lib/mongo/types/code.rb +36 -0
  28. data/lib/mongo/types/dbref.rb +38 -0
  29. data/lib/mongo/types/min_max_keys.rb +58 -0
  30. data/lib/mongo/types/objectid.rb +219 -0
  31. data/lib/mongo/types/regexp_of_holding.rb +45 -0
  32. data/lib/mongo/util/bson_c.rb +18 -0
  33. data/lib/mongo/util/bson_ruby.rb +595 -0
  34. data/lib/mongo/util/byte_buffer.rb +222 -0
  35. data/lib/mongo/util/conversions.rb +97 -0
  36. data/lib/mongo/util/ordered_hash.rb +135 -0
  37. data/lib/mongo/util/server_version.rb +69 -0
  38. data/lib/mongo/util/support.rb +26 -0
  39. data/lib/mongo/util/xml_to_ruby.rb +112 -0
  40. data/mongo-ruby-driver.gemspec +28 -0
  41. data/test/replica/count_test.rb +34 -0
  42. data/test/replica/insert_test.rb +50 -0
  43. data/test/replica/pooled_insert_test.rb +54 -0
  44. data/test/replica/query_test.rb +39 -0
  45. data/test/test_admin.rb +67 -0
  46. data/test/test_bson.rb +397 -0
  47. data/test/test_byte_buffer.rb +81 -0
  48. data/test/test_chunk.rb +82 -0
  49. data/test/test_collection.rb +534 -0
  50. data/test/test_connection.rb +160 -0
  51. data/test/test_conversions.rb +120 -0
  52. data/test/test_cursor.rb +386 -0
  53. data/test/test_db.rb +254 -0
  54. data/test/test_db_api.rb +783 -0
  55. data/test/test_db_connection.rb +16 -0
  56. data/test/test_grid_store.rb +306 -0
  57. data/test/test_helper.rb +42 -0
  58. data/test/test_objectid.rb +156 -0
  59. data/test/test_ordered_hash.rb +168 -0
  60. data/test/test_round_trip.rb +114 -0
  61. data/test/test_slave_connection.rb +36 -0
  62. data/test/test_threading.rb +87 -0
  63. data/test/threading/test_threading_large_pool.rb +90 -0
  64. data/test/unit/collection_test.rb +52 -0
  65. data/test/unit/connection_test.rb +59 -0
  66. data/test/unit/cursor_test.rb +94 -0
  67. data/test/unit/db_test.rb +97 -0
  68. metadata +123 -0
data/test/test_bson.rb ADDED
@@ -0,0 +1,397 @@
1
+ # encoding:utf-8
2
+ require 'test/test_helper'
3
+ require 'complex'
4
+ require 'bigdecimal'
5
+ require 'rational'
6
+
7
+ # Need to simulating this class
8
+ # without actually loading it.
9
+ module ActiveSupport
10
+ class TimeWithZone
11
+ end
12
+ end
13
+
14
+ class BSONTest < Test::Unit::TestCase
15
+
16
+ include Mongo
17
+
18
+ def test_string
19
+ doc = {'doc' => 'hello, world'}
20
+ bson = bson = BSON.serialize(doc)
21
+ assert_equal doc, BSON.deserialize(bson)
22
+ end
23
+
24
+ def test_valid_utf8_string
25
+ doc = {'doc' => 'aé'}
26
+ bson = bson = BSON.serialize(doc)
27
+ assert_equal doc, BSON.deserialize(bson)
28
+ end
29
+
30
+ def test_valid_utf8_key
31
+ doc = {'aé' => 'hello'}
32
+ bson = bson = BSON.serialize(doc)
33
+ assert_equal doc, BSON.deserialize(bson)
34
+ end
35
+
36
+ def test_document_length
37
+ doc = {'name' => 'a' * 5 * 1024 * 1024}
38
+ assert_raise InvalidDocument do
39
+ assert BSON.serialize(doc)
40
+ end
41
+ end
42
+
43
+ # In 1.8 we test that other string encodings raise an exception.
44
+ # In 1.9 we test that they get auto-converted.
45
+ if RUBY_VERSION < '1.9'
46
+ require 'iconv'
47
+ def test_invalid_string
48
+ string = Iconv.conv('iso-8859-1', 'utf-8', 'aé')
49
+ doc = {'doc' => string}
50
+ assert_raise InvalidStringEncoding do
51
+ BSON.serialize(doc)
52
+ end
53
+ end
54
+
55
+ def test_invalid_key
56
+ key = Iconv.conv('iso-8859-1', 'utf-8', 'aé')
57
+ doc = {key => 'hello'}
58
+ assert_raise InvalidStringEncoding do
59
+ BSON.serialize(doc)
60
+ end
61
+ end
62
+ else
63
+ def test_non_utf8_string
64
+ bson = BSON.serialize({'str' => 'aé'.encode('iso-8859-1')})
65
+ result = BSON.deserialize(bson)['str']
66
+ assert_equal 'aé', result
67
+ assert_equal 'UTF-8', result.encoding.name
68
+ end
69
+
70
+ def test_non_utf8_key
71
+ bson = BSON.serialize({'aé'.encode('iso-8859-1') => 'hello'})
72
+ assert_equal 'hello', BSON.deserialize(bson)['aé']
73
+ end
74
+ end
75
+
76
+ def test_code
77
+ doc = {'$where' => Code.new('this.a.b < this.b')}
78
+ bson = BSON.serialize(doc)
79
+ assert_equal doc, BSON.deserialize(bson)
80
+ end
81
+
82
+ def test_number
83
+ doc = {'doc' => 41.99}
84
+ bson = BSON.serialize(doc)
85
+ assert_equal doc, BSON.deserialize(bson)
86
+ end
87
+
88
+ def test_int
89
+ doc = {'doc' => 42}
90
+ bson = BSON.serialize(doc)
91
+ assert_equal doc, BSON.deserialize(bson)
92
+
93
+ doc = {"doc" => -5600}
94
+ bson = BSON.serialize(doc)
95
+ assert_equal doc, BSON.deserialize(bson)
96
+
97
+ doc = {"doc" => 2147483647}
98
+ bson = BSON.serialize(doc)
99
+ assert_equal doc, BSON.deserialize(bson)
100
+
101
+ doc = {"doc" => -2147483648}
102
+ bson = BSON.serialize(doc)
103
+ assert_equal doc, BSON.deserialize(bson)
104
+ end
105
+
106
+ def test_ordered_hash
107
+ doc = OrderedHash.new
108
+ doc["b"] = 1
109
+ doc["a"] = 2
110
+ doc["c"] = 3
111
+ doc["d"] = 4
112
+ bson = BSON.serialize(doc)
113
+ assert_equal doc, BSON.deserialize(bson)
114
+ end
115
+
116
+ def test_object
117
+ doc = {'doc' => {'age' => 42, 'name' => 'Spongebob', 'shoe_size' => 9.5}}
118
+ bson = BSON.serialize(doc)
119
+ assert_equal doc, BSON.deserialize(bson)
120
+ end
121
+
122
+ def test_oid
123
+ doc = {'doc' => ObjectID.new}
124
+ bson = BSON.serialize(doc)
125
+ assert_equal doc, BSON.deserialize(bson)
126
+ end
127
+
128
+ def test_array
129
+ doc = {'doc' => [1, 2, 'a', 'b']}
130
+ bson = BSON.serialize(doc)
131
+ assert_equal doc, BSON.deserialize(bson)
132
+ end
133
+
134
+ def test_regex
135
+ doc = {'doc' => /foobar/i}
136
+ bson = BSON.serialize(doc)
137
+ doc2 = BSON.deserialize(bson)
138
+ assert_equal doc, doc2
139
+
140
+ r = doc2['doc']
141
+ assert_kind_of RegexpOfHolding, r
142
+ assert_equal '', r.extra_options_str
143
+
144
+ r.extra_options_str << 'zywcab'
145
+ assert_equal 'zywcab', r.extra_options_str
146
+
147
+ doc = {'doc' => r}
148
+ bson_doc = BSON.serialize(doc)
149
+ doc2 = nil
150
+ doc2 = BSON.deserialize(bson_doc)
151
+ assert_equal doc, doc2
152
+
153
+ r = doc2['doc']
154
+ assert_kind_of RegexpOfHolding, r
155
+ assert_equal 'abcwyz', r.extra_options_str # must be sorted
156
+ end
157
+
158
+ def test_boolean
159
+ doc = {'doc' => true}
160
+ bson = BSON.serialize(doc)
161
+ assert_equal doc, BSON.deserialize(bson)
162
+ end
163
+
164
+ def test_date
165
+ doc = {'date' => Time.now}
166
+ bson = BSON.serialize(doc)
167
+ doc2 = BSON.deserialize(bson)
168
+ # Mongo only stores up to the millisecond
169
+ assert_in_delta doc['date'], doc2['date'], 0.001
170
+ end
171
+
172
+ def test_date_returns_as_utc
173
+ doc = {'date' => Time.now}
174
+ bson = BSON.serialize(doc)
175
+ doc2 = BSON.deserialize(bson)
176
+ assert doc2['date'].utc?
177
+ end
178
+
179
+ def test_date_before_epoch
180
+ begin
181
+ doc = {'date' => Time.utc(1600)}
182
+ bson = BSON.serialize(doc)
183
+ doc2 = BSON.deserialize(bson)
184
+ # Mongo only stores up to the millisecond
185
+ assert_in_delta doc['date'], doc2['date'], 0.001
186
+ rescue ArgumentError
187
+ # some versions of Ruby won't let you create pre-epoch Time instances
188
+ #
189
+ # TODO figure out how that will work if somebady has saved data
190
+ # w/ early dates already and is just querying for it.
191
+ end
192
+ end
193
+
194
+ def test_exeption_on_using_unsupported_date_class
195
+ [DateTime.now, Date.today, ActiveSupport::TimeWithZone.new].each do |invalid_date|
196
+ doc = {:date => invalid_date}
197
+ begin
198
+ bson = BSON.serialize(doc)
199
+ rescue => e
200
+ ensure
201
+ assert_equal InvalidDocument, e.class
202
+ assert_match /UTC Time/, e.message
203
+ end
204
+ end
205
+ end
206
+
207
+ def test_dbref
208
+ oid = ObjectID.new
209
+ doc = {}
210
+ doc['dbref'] = DBRef.new('namespace', oid)
211
+ bson = BSON.serialize(doc)
212
+ doc2 = BSON.deserialize(bson)
213
+ assert_equal 'namespace', doc2['dbref'].namespace
214
+ assert_equal oid, doc2['dbref'].object_id
215
+ end
216
+
217
+ def test_symbol
218
+ doc = {'sym' => :foo}
219
+ bson = BSON.serialize(doc)
220
+ doc2 = BSON.deserialize(bson)
221
+ assert_equal :foo, doc2['sym']
222
+ end
223
+
224
+ def test_binary
225
+ bin = Binary.new
226
+ 'binstring'.each_byte { |b| bin.put(b) }
227
+
228
+ doc = {'bin' => bin}
229
+ bson = BSON.serialize(doc)
230
+ doc2 = BSON.deserialize(bson)
231
+ bin2 = doc2['bin']
232
+ assert_kind_of Binary, bin2
233
+ assert_equal 'binstring', bin2.to_s
234
+ assert_equal Binary::SUBTYPE_BYTES, bin2.subtype
235
+ end
236
+
237
+ def test_binary_type
238
+ bin = Binary.new([1, 2, 3, 4, 5], Binary::SUBTYPE_USER_DEFINED)
239
+
240
+ doc = {'bin' => bin}
241
+ bson = BSON.serialize(doc)
242
+ doc2 = BSON.deserialize(bson)
243
+ bin2 = doc2['bin']
244
+ assert_kind_of Binary, bin2
245
+ assert_equal [1, 2, 3, 4, 5], bin2.to_a
246
+ assert_equal Binary::SUBTYPE_USER_DEFINED, bin2.subtype
247
+ end
248
+
249
+ def test_binary_byte_buffer
250
+ bb = ByteBuffer.new
251
+ 5.times { |i| bb.put(i + 1) }
252
+
253
+ doc = {'bin' => bb}
254
+ bson = BSON.serialize(doc)
255
+ doc2 = BSON.deserialize(bson)
256
+ bin2 = doc2['bin']
257
+ assert_kind_of Binary, bin2
258
+ assert_equal [1, 2, 3, 4, 5], bin2.to_a
259
+ assert_equal Binary::SUBTYPE_BYTES, bin2.subtype
260
+ end
261
+
262
+ def test_put_id_first
263
+ val = OrderedHash.new
264
+ val['not_id'] = 1
265
+ val['_id'] = 2
266
+ roundtrip = BSON.deserialize(BSON.serialize(val).to_a)
267
+ assert_kind_of OrderedHash, roundtrip
268
+ assert_equal '_id', roundtrip.keys.first
269
+
270
+ val = {'a' => 'foo', 'b' => 'bar', :_id => 42, 'z' => 'hello'}
271
+ roundtrip = BSON.deserialize(BSON.serialize(val).to_a)
272
+ assert_kind_of OrderedHash, roundtrip
273
+ assert_equal '_id', roundtrip.keys.first
274
+ end
275
+
276
+ def test_nil_id
277
+ doc = {"_id" => nil}
278
+ assert_equal doc, BSON.deserialize(bson = BSON.serialize(doc).to_a)
279
+ end
280
+
281
+ def test_timestamp
282
+ val = {"test" => [4, 20]}
283
+ assert_equal val, BSON.deserialize([0x13, 0x00, 0x00, 0x00,
284
+ 0x11, 0x74, 0x65, 0x73,
285
+ 0x74, 0x00, 0x04, 0x00,
286
+ 0x00, 0x00, 0x14, 0x00,
287
+ 0x00, 0x00, 0x00])
288
+ end
289
+
290
+ def test_overflow
291
+ doc = {"x" => 2**75}
292
+ assert_raise RangeError do
293
+ bson = BSON.serialize(doc)
294
+ end
295
+
296
+ doc = {"x" => 9223372036854775}
297
+ assert_equal doc, BSON.deserialize(BSON.serialize(doc).to_a)
298
+
299
+ doc = {"x" => 9223372036854775807}
300
+ assert_equal doc, BSON.deserialize(BSON.serialize(doc).to_a)
301
+
302
+ doc["x"] = doc["x"] + 1
303
+ assert_raise RangeError do
304
+ bson = BSON.serialize(doc)
305
+ end
306
+
307
+ doc = {"x" => -9223372036854775}
308
+ assert_equal doc, BSON.deserialize(BSON.serialize(doc).to_a)
309
+
310
+ doc = {"x" => -9223372036854775808}
311
+ assert_equal doc, BSON.deserialize(BSON.serialize(doc).to_a)
312
+
313
+ doc["x"] = doc["x"] - 1
314
+ assert_raise RangeError do
315
+ bson = BSON.serialize(doc)
316
+ end
317
+ end
318
+
319
+ def test_invalid_numeric_types
320
+ [BigDecimal.new("1.0"), Complex(0, 1), Rational(2, 3)].each do |type|
321
+ doc = {"x" => type}
322
+ begin
323
+ BSON.serialize(doc)
324
+ rescue => e
325
+ ensure
326
+ assert_equal InvalidDocument, e.class
327
+ assert_match /Cannot serialize/, e.message
328
+ end
329
+ end
330
+ end
331
+
332
+ def test_do_not_change_original_object
333
+ val = OrderedHash.new
334
+ val['not_id'] = 1
335
+ val['_id'] = 2
336
+ assert val.keys.include?('_id')
337
+ BSON.serialize(val)
338
+ assert val.keys.include?('_id')
339
+
340
+ val = {'a' => 'foo', 'b' => 'bar', :_id => 42, 'z' => 'hello'}
341
+ assert val.keys.include?(:_id)
342
+ BSON.serialize(val)
343
+ assert val.keys.include?(:_id)
344
+ end
345
+
346
+ # note we only test for _id here because in the general case we will
347
+ # write duplicates for :key and "key". _id is a special case because
348
+ # we call has_key? to check for it's existance rather than just iterating
349
+ # over it like we do for the rest of the keys. thus, things like
350
+ # HashWithIndifferentAccess can cause problems for _id but not for other
351
+ # keys. rather than require rails to test with HWIA directly, we do this
352
+ # somewhat hacky test.
353
+ def test_no_duplicate_id
354
+ dup = {"_id" => "foo", :_id => "foo"}
355
+ one = {"_id" => "foo"}
356
+
357
+ assert_equal BSON.serialize(one).to_a, BSON.serialize(dup).to_a
358
+ end
359
+
360
+ def test_null_character
361
+ doc = {"a" => "\x00"}
362
+
363
+ assert_equal doc, BSON.deserialize(BSON.serialize(doc).to_a)
364
+
365
+ assert_raise InvalidDocument do
366
+ BSON.serialize({"\x00" => "a"})
367
+ end
368
+
369
+ assert_raise InvalidDocument do
370
+ BSON.serialize({"a" => (Regexp.compile "ab\x00c")})
371
+ end
372
+ end
373
+
374
+ def test_max_key
375
+ doc = {"a" => MaxKey.new}
376
+
377
+ assert_equal doc, BSON.deserialize(BSON.serialize(doc).to_a)
378
+ end
379
+
380
+ def test_min_key
381
+ doc = {"a" => MinKey.new}
382
+
383
+ assert_equal doc, BSON.deserialize(BSON.serialize(doc).to_a)
384
+ end
385
+
386
+ def test_invalid_object
387
+ o = Object.new
388
+ assert_raise InvalidDocument do
389
+ BSON.serialize({:foo => o})
390
+ end
391
+
392
+ assert_raise InvalidDocument do
393
+ BSON.serialize({:foo => Date.today})
394
+ end
395
+ end
396
+
397
+ end
@@ -0,0 +1,81 @@
1
+ require 'test/test_helper'
2
+
3
+ class ByteBufferTest < Test::Unit::TestCase
4
+
5
+ def setup
6
+ @buf = ByteBuffer.new
7
+ end
8
+
9
+ def test_nil_get_returns_one_byte
10
+ @buf.put_array([1, 2, 3, 4])
11
+ @buf.rewind
12
+ assert_equal 1, @buf.get
13
+ end
14
+
15
+ def test_one_get_returns_array_length_one
16
+ @buf.put_array([1, 2, 3, 4])
17
+ @buf.rewind
18
+ assert_equal [1], @buf.get(1)
19
+ end
20
+
21
+ def test_zero_get_returns_empty_array
22
+ @buf.put_array([1, 2, 3, 4])
23
+ @buf.rewind
24
+ assert_equal [], @buf.get(0)
25
+ end
26
+
27
+ def test_empty
28
+ assert_equal 0, @buf.length
29
+ end
30
+
31
+ def test_length
32
+ @buf.put_int 3
33
+ assert_equal 4, @buf.length
34
+ end
35
+
36
+ def test_default_order
37
+ assert_equal :little_endian, @buf.order
38
+ end
39
+
40
+ def test_long_length
41
+ @buf.put_long 1027
42
+ assert_equal 8, @buf.length
43
+ end
44
+
45
+ def test_get_long
46
+ @buf.put_long 1027
47
+ @buf.rewind
48
+ assert_equal 1027, @buf.get_long
49
+ end
50
+
51
+ def test_get_double
52
+ @buf.put_double 41.2
53
+ @buf.rewind
54
+ assert_equal 41.2, @buf.get_double
55
+ end
56
+
57
+ def test_rewrite
58
+ @buf.put_int(0)
59
+ @buf.rewind
60
+ @buf.put_int(1027)
61
+ assert_equal 4, @buf.length
62
+ @buf.rewind
63
+ assert_equal 1027, @buf.get_int
64
+ assert_equal 4, @buf.position
65
+ end
66
+
67
+ def test_prepend_byte_buffer
68
+ @buf.put_int(4)
69
+ new_buf = ByteBuffer.new([5, 0, 0, 0])
70
+ @buf.prepend!(new_buf)
71
+ assert_equal [5, 0, 0, 0, 4, 0, 0, 0], @buf.to_a
72
+ end
73
+
74
+ def test_append_byte_buffer
75
+ @buf.put_int(4)
76
+ new_buf = ByteBuffer.new([5, 0, 0, 0])
77
+ @buf.append!(new_buf)
78
+ assert_equal [4, 0, 0, 0, 5, 0, 0, 0], @buf.to_a
79
+ end
80
+
81
+ end