kbaum-mongo 0.18.3p
Sign up to get free protection for your applications and to get access to all the features.
- data/LICENSE.txt +202 -0
- data/README.rdoc +339 -0
- data/Rakefile +138 -0
- data/bin/bson_benchmark.rb +59 -0
- data/bin/fail_if_no_c.rb +11 -0
- data/examples/admin.rb +42 -0
- data/examples/capped.rb +22 -0
- data/examples/cursor.rb +48 -0
- data/examples/gridfs.rb +88 -0
- data/examples/index_test.rb +126 -0
- data/examples/info.rb +31 -0
- data/examples/queries.rb +70 -0
- data/examples/simple.rb +24 -0
- data/examples/strict.rb +35 -0
- data/examples/types.rb +36 -0
- data/lib/mongo/collection.rb +609 -0
- data/lib/mongo/connection.rb +672 -0
- data/lib/mongo/cursor.rb +403 -0
- data/lib/mongo/db.rb +555 -0
- data/lib/mongo/exceptions.rb +66 -0
- data/lib/mongo/gridfs/chunk.rb +91 -0
- data/lib/mongo/gridfs/grid.rb +79 -0
- data/lib/mongo/gridfs/grid_file_system.rb +101 -0
- data/lib/mongo/gridfs/grid_io.rb +338 -0
- data/lib/mongo/gridfs/grid_store.rb +580 -0
- data/lib/mongo/gridfs.rb +25 -0
- data/lib/mongo/types/binary.rb +52 -0
- data/lib/mongo/types/code.rb +36 -0
- data/lib/mongo/types/dbref.rb +40 -0
- data/lib/mongo/types/min_max_keys.rb +58 -0
- data/lib/mongo/types/objectid.rb +180 -0
- data/lib/mongo/types/regexp_of_holding.rb +45 -0
- data/lib/mongo/util/bson_c.rb +18 -0
- data/lib/mongo/util/bson_ruby.rb +606 -0
- data/lib/mongo/util/byte_buffer.rb +222 -0
- data/lib/mongo/util/conversions.rb +87 -0
- data/lib/mongo/util/ordered_hash.rb +140 -0
- data/lib/mongo/util/server_version.rb +69 -0
- data/lib/mongo/util/support.rb +26 -0
- data/lib/mongo.rb +63 -0
- data/mongo-ruby-driver.gemspec +28 -0
- data/test/auxillary/autoreconnect_test.rb +42 -0
- data/test/binary_test.rb +15 -0
- data/test/bson_test.rb +427 -0
- data/test/byte_buffer_test.rb +81 -0
- data/test/chunk_test.rb +82 -0
- data/test/collection_test.rb +515 -0
- data/test/connection_test.rb +160 -0
- data/test/conversions_test.rb +120 -0
- data/test/cursor_test.rb +379 -0
- data/test/db_api_test.rb +780 -0
- data/test/db_connection_test.rb +16 -0
- data/test/db_test.rb +272 -0
- data/test/grid_file_system_test.rb +210 -0
- data/test/grid_io_test.rb +78 -0
- data/test/grid_store_test.rb +334 -0
- data/test/grid_test.rb +87 -0
- data/test/objectid_test.rb +125 -0
- data/test/ordered_hash_test.rb +172 -0
- data/test/replica/count_test.rb +34 -0
- data/test/replica/insert_test.rb +50 -0
- data/test/replica/pooled_insert_test.rb +54 -0
- data/test/replica/query_test.rb +39 -0
- data/test/slave_connection_test.rb +36 -0
- data/test/test_helper.rb +42 -0
- data/test/threading/test_threading_large_pool.rb +90 -0
- data/test/threading_test.rb +87 -0
- data/test/unit/collection_test.rb +61 -0
- data/test/unit/connection_test.rb +117 -0
- data/test/unit/cursor_test.rb +93 -0
- data/test/unit/db_test.rb +98 -0
- metadata +127 -0
data/test/bson_test.rb
ADDED
@@ -0,0 +1,427 @@
|
|
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 Regexp, r
|
142
|
+
|
143
|
+
r = RegexpOfHolding.new('st', 0, 'zywcab')
|
144
|
+
assert_equal 'zywcab', r.extra_options_str
|
145
|
+
|
146
|
+
doc = {'doc' => r}
|
147
|
+
bson_doc = BSON.serialize(doc)
|
148
|
+
doc2 = nil
|
149
|
+
doc2 = BSON.deserialize(bson_doc)
|
150
|
+
assert_equal doc, doc2
|
151
|
+
|
152
|
+
r = doc2['doc']
|
153
|
+
assert_kind_of RegexpOfHolding, r
|
154
|
+
assert_equal 'abcwyz', r.extra_options_str # must be sorted
|
155
|
+
end
|
156
|
+
|
157
|
+
def test_boolean
|
158
|
+
doc = {'doc' => true}
|
159
|
+
bson = BSON.serialize(doc)
|
160
|
+
assert_equal doc, BSON.deserialize(bson)
|
161
|
+
end
|
162
|
+
|
163
|
+
def test_date
|
164
|
+
doc = {'date' => Time.now}
|
165
|
+
bson = BSON.serialize(doc)
|
166
|
+
doc2 = BSON.deserialize(bson)
|
167
|
+
# Mongo only stores up to the millisecond
|
168
|
+
assert_in_delta doc['date'], doc2['date'], 0.001
|
169
|
+
end
|
170
|
+
|
171
|
+
def test_date_returns_as_utc
|
172
|
+
doc = {'date' => Time.now}
|
173
|
+
bson = BSON.serialize(doc)
|
174
|
+
doc2 = BSON.deserialize(bson)
|
175
|
+
assert doc2['date'].utc?
|
176
|
+
end
|
177
|
+
|
178
|
+
def test_date_before_epoch
|
179
|
+
begin
|
180
|
+
doc = {'date' => Time.utc(1600)}
|
181
|
+
bson = BSON.serialize(doc)
|
182
|
+
doc2 = BSON.deserialize(bson)
|
183
|
+
# Mongo only stores up to the millisecond
|
184
|
+
assert_in_delta doc['date'], doc2['date'], 0.001
|
185
|
+
rescue ArgumentError
|
186
|
+
# some versions of Ruby won't let you create pre-epoch Time instances
|
187
|
+
#
|
188
|
+
# TODO figure out how that will work if somebady has saved data
|
189
|
+
# w/ early dates already and is just querying for it.
|
190
|
+
end
|
191
|
+
end
|
192
|
+
|
193
|
+
def test_exeption_on_using_unsupported_date_class
|
194
|
+
[DateTime.now, Date.today, ActiveSupport::TimeWithZone.new].each do |invalid_date|
|
195
|
+
doc = {:date => invalid_date}
|
196
|
+
begin
|
197
|
+
bson = BSON.serialize(doc)
|
198
|
+
rescue => e
|
199
|
+
ensure
|
200
|
+
assert_equal InvalidDocument, e.class
|
201
|
+
assert_match /UTC Time/, e.message
|
202
|
+
end
|
203
|
+
end
|
204
|
+
end
|
205
|
+
|
206
|
+
def test_dbref
|
207
|
+
oid = ObjectID.new
|
208
|
+
doc = {}
|
209
|
+
doc['dbref'] = DBRef.new('namespace', oid)
|
210
|
+
bson = BSON.serialize(doc)
|
211
|
+
doc2 = BSON.deserialize(bson)
|
212
|
+
assert_equal 'namespace', doc2['dbref'].namespace
|
213
|
+
assert_equal oid, doc2['dbref'].object_id
|
214
|
+
end
|
215
|
+
|
216
|
+
def test_symbol
|
217
|
+
doc = {'sym' => :foo}
|
218
|
+
bson = BSON.serialize(doc)
|
219
|
+
doc2 = BSON.deserialize(bson)
|
220
|
+
assert_equal :foo, doc2['sym']
|
221
|
+
end
|
222
|
+
|
223
|
+
def test_binary
|
224
|
+
bin = Binary.new
|
225
|
+
'binstring'.each_byte { |b| bin.put(b) }
|
226
|
+
|
227
|
+
doc = {'bin' => bin}
|
228
|
+
bson = BSON.serialize(doc)
|
229
|
+
doc2 = BSON.deserialize(bson)
|
230
|
+
bin2 = doc2['bin']
|
231
|
+
assert_kind_of Binary, bin2
|
232
|
+
assert_equal 'binstring', bin2.to_s
|
233
|
+
assert_equal Binary::SUBTYPE_BYTES, bin2.subtype
|
234
|
+
end
|
235
|
+
|
236
|
+
def test_binary_type
|
237
|
+
bin = Binary.new([1, 2, 3, 4, 5], Binary::SUBTYPE_USER_DEFINED)
|
238
|
+
|
239
|
+
doc = {'bin' => bin}
|
240
|
+
bson = BSON.serialize(doc)
|
241
|
+
doc2 = BSON.deserialize(bson)
|
242
|
+
bin2 = doc2['bin']
|
243
|
+
assert_kind_of Binary, bin2
|
244
|
+
assert_equal [1, 2, 3, 4, 5], bin2.to_a
|
245
|
+
assert_equal Binary::SUBTYPE_USER_DEFINED, bin2.subtype
|
246
|
+
end
|
247
|
+
|
248
|
+
def test_binary_byte_buffer
|
249
|
+
bb = ByteBuffer.new
|
250
|
+
5.times { |i| bb.put(i + 1) }
|
251
|
+
|
252
|
+
doc = {'bin' => bb}
|
253
|
+
bson = BSON.serialize(doc)
|
254
|
+
doc2 = BSON.deserialize(bson)
|
255
|
+
bin2 = doc2['bin']
|
256
|
+
assert_kind_of Binary, bin2
|
257
|
+
assert_equal [1, 2, 3, 4, 5], bin2.to_a
|
258
|
+
assert_equal Binary::SUBTYPE_BYTES, bin2.subtype
|
259
|
+
end
|
260
|
+
|
261
|
+
def test_put_id_first
|
262
|
+
val = OrderedHash.new
|
263
|
+
val['not_id'] = 1
|
264
|
+
val['_id'] = 2
|
265
|
+
roundtrip = BSON.deserialize(BSON.serialize(val, false, true).to_a)
|
266
|
+
assert_kind_of OrderedHash, roundtrip
|
267
|
+
assert_equal '_id', roundtrip.keys.first
|
268
|
+
|
269
|
+
val = {'a' => 'foo', 'b' => 'bar', :_id => 42, 'z' => 'hello'}
|
270
|
+
roundtrip = BSON.deserialize(BSON.serialize(val, false, true).to_a)
|
271
|
+
assert_kind_of OrderedHash, roundtrip
|
272
|
+
assert_equal '_id', roundtrip.keys.first
|
273
|
+
end
|
274
|
+
|
275
|
+
def test_nil_id
|
276
|
+
doc = {"_id" => nil}
|
277
|
+
assert_equal doc, BSON.deserialize(bson = BSON.serialize(doc, false, true).to_a)
|
278
|
+
end
|
279
|
+
|
280
|
+
def test_timestamp
|
281
|
+
val = {"test" => [4, 20]}
|
282
|
+
assert_equal val, BSON.deserialize([0x13, 0x00, 0x00, 0x00,
|
283
|
+
0x11, 0x74, 0x65, 0x73,
|
284
|
+
0x74, 0x00, 0x04, 0x00,
|
285
|
+
0x00, 0x00, 0x14, 0x00,
|
286
|
+
0x00, 0x00, 0x00])
|
287
|
+
end
|
288
|
+
|
289
|
+
def test_overflow
|
290
|
+
doc = {"x" => 2**75}
|
291
|
+
assert_raise RangeError do
|
292
|
+
bson = BSON.serialize(doc)
|
293
|
+
end
|
294
|
+
|
295
|
+
doc = {"x" => 9223372036854775}
|
296
|
+
assert_equal doc, BSON.deserialize(BSON.serialize(doc).to_a)
|
297
|
+
|
298
|
+
doc = {"x" => 9223372036854775807}
|
299
|
+
assert_equal doc, BSON.deserialize(BSON.serialize(doc).to_a)
|
300
|
+
|
301
|
+
doc["x"] = doc["x"] + 1
|
302
|
+
assert_raise RangeError do
|
303
|
+
bson = BSON.serialize(doc)
|
304
|
+
end
|
305
|
+
|
306
|
+
doc = {"x" => -9223372036854775}
|
307
|
+
assert_equal doc, BSON.deserialize(BSON.serialize(doc).to_a)
|
308
|
+
|
309
|
+
doc = {"x" => -9223372036854775808}
|
310
|
+
assert_equal doc, BSON.deserialize(BSON.serialize(doc).to_a)
|
311
|
+
|
312
|
+
doc["x"] = doc["x"] - 1
|
313
|
+
assert_raise RangeError do
|
314
|
+
bson = BSON.serialize(doc)
|
315
|
+
end
|
316
|
+
end
|
317
|
+
|
318
|
+
def test_invalid_numeric_types
|
319
|
+
[BigDecimal.new("1.0"), Complex(0, 1), Rational(2, 3)].each do |type|
|
320
|
+
doc = {"x" => type}
|
321
|
+
begin
|
322
|
+
BSON.serialize(doc)
|
323
|
+
rescue => e
|
324
|
+
ensure
|
325
|
+
assert_equal InvalidDocument, e.class
|
326
|
+
assert_match /Cannot serialize/, e.message
|
327
|
+
end
|
328
|
+
end
|
329
|
+
end
|
330
|
+
|
331
|
+
def test_do_not_change_original_object
|
332
|
+
val = OrderedHash.new
|
333
|
+
val['not_id'] = 1
|
334
|
+
val['_id'] = 2
|
335
|
+
assert val.keys.include?('_id')
|
336
|
+
BSON.serialize(val)
|
337
|
+
assert val.keys.include?('_id')
|
338
|
+
|
339
|
+
val = {'a' => 'foo', 'b' => 'bar', :_id => 42, 'z' => 'hello'}
|
340
|
+
assert val.keys.include?(:_id)
|
341
|
+
BSON.serialize(val)
|
342
|
+
assert val.keys.include?(:_id)
|
343
|
+
end
|
344
|
+
|
345
|
+
# note we only test for _id here because in the general case we will
|
346
|
+
# write duplicates for :key and "key". _id is a special case because
|
347
|
+
# we call has_key? to check for it's existance rather than just iterating
|
348
|
+
# over it like we do for the rest of the keys. thus, things like
|
349
|
+
# HashWithIndifferentAccess can cause problems for _id but not for other
|
350
|
+
# keys. rather than require rails to test with HWIA directly, we do this
|
351
|
+
# somewhat hacky test.
|
352
|
+
def test_no_duplicate_id
|
353
|
+
dup = {"_id" => "foo", :_id => "foo"}
|
354
|
+
one = {"_id" => "foo"}
|
355
|
+
|
356
|
+
assert_equal BSON.serialize(one).to_a, BSON.serialize(dup).to_a
|
357
|
+
end
|
358
|
+
|
359
|
+
def test_null_character
|
360
|
+
doc = {"a" => "\x00"}
|
361
|
+
|
362
|
+
assert_equal doc, BSON.deserialize(BSON.serialize(doc).to_a)
|
363
|
+
|
364
|
+
assert_raise InvalidDocument do
|
365
|
+
BSON.serialize({"\x00" => "a"})
|
366
|
+
end
|
367
|
+
|
368
|
+
assert_raise InvalidDocument do
|
369
|
+
BSON.serialize({"a" => (Regexp.compile "ab\x00c")})
|
370
|
+
end
|
371
|
+
end
|
372
|
+
|
373
|
+
def test_max_key
|
374
|
+
doc = {"a" => MaxKey.new}
|
375
|
+
|
376
|
+
assert_equal doc, BSON.deserialize(BSON.serialize(doc).to_a)
|
377
|
+
end
|
378
|
+
|
379
|
+
def test_min_key
|
380
|
+
doc = {"a" => MinKey.new}
|
381
|
+
|
382
|
+
assert_equal doc, BSON.deserialize(BSON.serialize(doc).to_a)
|
383
|
+
end
|
384
|
+
|
385
|
+
def test_invalid_object
|
386
|
+
o = Object.new
|
387
|
+
assert_raise InvalidDocument do
|
388
|
+
BSON.serialize({:foo => o})
|
389
|
+
end
|
390
|
+
|
391
|
+
assert_raise InvalidDocument do
|
392
|
+
BSON.serialize({:foo => Date.today})
|
393
|
+
end
|
394
|
+
end
|
395
|
+
|
396
|
+
def test_move_id
|
397
|
+
a = OrderedHash.new
|
398
|
+
a['text'] = 'abc'
|
399
|
+
a['key'] = 'abc'
|
400
|
+
a['_id'] = 1
|
401
|
+
|
402
|
+
assert_equal ")\000\000\000\020_id\000\001\000\000\000\002text" +
|
403
|
+
"\000\004\000\000\000abc\000\002key\000\004\000\000\000abc\000\000",
|
404
|
+
BSON.serialize(a, false, true).to_s
|
405
|
+
assert_equal ")\000\000\000\002text\000\004\000\000\000abc\000\002key" +
|
406
|
+
"\000\004\000\000\000abc\000\020_id\000\001\000\000\000\000",
|
407
|
+
BSON.serialize(a, false, false).to_s
|
408
|
+
end
|
409
|
+
|
410
|
+
def test_move_id_with_nested_doc
|
411
|
+
b = OrderedHash.new
|
412
|
+
b['text'] = 'abc'
|
413
|
+
b['_id'] = 2
|
414
|
+
c = OrderedHash.new
|
415
|
+
c['text'] = 'abc'
|
416
|
+
c['hash'] = b
|
417
|
+
c['_id'] = 3
|
418
|
+
assert_equal ">\000\000\000\020_id\000\003\000\000\000\002text" +
|
419
|
+
"\000\004\000\000\000abc\000\003hash\000\034\000\000" +
|
420
|
+
"\000\002text\000\004\000\000\000abc\000\020_id\000\002\000\000\000\000\000",
|
421
|
+
BSON.serialize(c, false, true).to_s
|
422
|
+
assert_equal ">\000\000\000\002text\000\004\000\000\000abc\000\003hash" +
|
423
|
+
"\000\034\000\000\000\002text\000\004\000\000\000abc\000\020_id" +
|
424
|
+
"\000\002\000\000\000\000\020_id\000\003\000\000\000\000",
|
425
|
+
BSON.serialize(c, false, false).to_s
|
426
|
+
end
|
427
|
+
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
|
data/test/chunk_test.rb
ADDED
@@ -0,0 +1,82 @@
|
|
1
|
+
require 'test/test_helper'
|
2
|
+
require 'mongo/gridfs'
|
3
|
+
|
4
|
+
class ChunkTest < Test::Unit::TestCase
|
5
|
+
|
6
|
+
include Mongo
|
7
|
+
include GridFS
|
8
|
+
|
9
|
+
@@db = Connection.new(ENV['MONGO_RUBY_DRIVER_HOST'] || 'localhost',
|
10
|
+
ENV['MONGO_RUBY_DRIVER_PORT'] || Connection::DEFAULT_PORT).db('ruby-mongo-utils-test')
|
11
|
+
@@files = @@db.collection('gridfs.files')
|
12
|
+
@@chunks = @@db.collection('gridfs.chunks')
|
13
|
+
|
14
|
+
def setup
|
15
|
+
@@chunks.remove
|
16
|
+
@@files.remove
|
17
|
+
|
18
|
+
@f = GridStore.new(@@db, 'foobar', 'w')
|
19
|
+
@c = @f.instance_variable_get('@curr_chunk')
|
20
|
+
end
|
21
|
+
|
22
|
+
def teardown
|
23
|
+
@@chunks.remove
|
24
|
+
@@files.remove
|
25
|
+
@@db.error
|
26
|
+
end
|
27
|
+
|
28
|
+
def test_pos
|
29
|
+
assert_equal 0, @c.pos
|
30
|
+
assert @c.eof? # since data is empty
|
31
|
+
|
32
|
+
b = ByteBuffer.new
|
33
|
+
3.times { |i| b.put(i) }
|
34
|
+
c = Chunk.new(@f, 'data' => b)
|
35
|
+
assert !c.eof?
|
36
|
+
end
|
37
|
+
|
38
|
+
def test_getc
|
39
|
+
b = ByteBuffer.new
|
40
|
+
3.times { |i| b.put(i) }
|
41
|
+
c = Chunk.new(@f, 'data' => b)
|
42
|
+
|
43
|
+
assert !c.eof?
|
44
|
+
assert_equal 0, c.getc
|
45
|
+
assert !c.eof?
|
46
|
+
assert_equal 1, c.getc
|
47
|
+
assert !c.eof?
|
48
|
+
assert_equal 2, c.getc
|
49
|
+
assert c.eof?
|
50
|
+
end
|
51
|
+
|
52
|
+
def test_putc
|
53
|
+
3.times { |i| @c.putc(i) }
|
54
|
+
@c.pos = 0
|
55
|
+
|
56
|
+
assert !@c.eof?
|
57
|
+
assert_equal 0, @c.getc
|
58
|
+
assert !@c.eof?
|
59
|
+
assert_equal 1, @c.getc
|
60
|
+
assert !@c.eof?
|
61
|
+
assert_equal 2, @c.getc
|
62
|
+
assert @c.eof?
|
63
|
+
end
|
64
|
+
|
65
|
+
def test_truncate
|
66
|
+
10.times { |i| @c.putc(i) }
|
67
|
+
assert_equal 10, @c.size
|
68
|
+
@c.pos = 3
|
69
|
+
@c.truncate
|
70
|
+
assert_equal 3, @c.size
|
71
|
+
|
72
|
+
@c.pos = 0
|
73
|
+
assert !@c.eof?
|
74
|
+
assert_equal 0, @c.getc
|
75
|
+
assert !@c.eof?
|
76
|
+
assert_equal 1, @c.getc
|
77
|
+
assert !@c.eof?
|
78
|
+
assert_equal 2, @c.getc
|
79
|
+
assert @c.eof?
|
80
|
+
end
|
81
|
+
|
82
|
+
end
|