bson 4.12.0-java → 4.14.1-java
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- checksums.yaml.gz.sig +0 -0
- data/README.md +4 -7
- data/lib/bson/active_support.rb +1 -0
- data/lib/bson/array.rb +2 -1
- data/lib/bson/big_decimal.rb +67 -0
- data/lib/bson/binary.rb +5 -3
- data/lib/bson/boolean.rb +2 -1
- data/lib/bson/code.rb +2 -1
- data/lib/bson/code_with_scope.rb +2 -1
- data/lib/bson/config.rb +1 -0
- data/lib/bson/date.rb +1 -0
- data/lib/bson/date_time.rb +1 -0
- data/lib/bson/db_pointer.rb +2 -1
- data/lib/bson/dbref.rb +152 -0
- data/lib/bson/decimal128/builder.rb +27 -20
- data/lib/bson/decimal128.rb +27 -12
- data/lib/bson/document.rb +18 -0
- data/lib/bson/environment.rb +1 -0
- data/lib/bson/error.rb +7 -0
- data/lib/bson/ext_json.rb +16 -11
- data/lib/bson/false_class.rb +2 -1
- data/lib/bson/float.rb +21 -32
- data/lib/bson/hash.rb +15 -6
- data/lib/bson/int32.rb +3 -2
- data/lib/bson/int64.rb +3 -2
- data/lib/bson/integer.rb +3 -2
- data/lib/bson/json.rb +1 -0
- data/lib/bson/max_key.rb +3 -2
- data/lib/bson/min_key.rb +3 -2
- data/lib/bson/nil_class.rb +2 -1
- data/lib/bson/object.rb +1 -0
- data/lib/bson/object_id.rb +4 -3
- data/lib/bson/open_struct.rb +1 -0
- data/lib/bson/regexp.rb +17 -6
- data/lib/bson/registry.rb +1 -0
- data/lib/bson/specialized.rb +1 -0
- data/lib/bson/string.rb +3 -2
- data/lib/bson/symbol.rb +2 -1
- data/lib/bson/time.rb +4 -3
- data/lib/bson/time_with_zone.rb +1 -0
- data/lib/bson/timestamp.rb +3 -2
- data/lib/bson/true_class.rb +2 -1
- data/lib/bson/undefined.rb +2 -1
- data/lib/bson/version.rb +2 -1
- data/lib/bson-ruby.jar +0 -0
- data/lib/bson.rb +8 -5
- data/spec/README.md +14 -0
- data/spec/bson/big_decimal_spec.rb +316 -0
- data/spec/bson/dbref_legacy_spec.rb +169 -0
- data/spec/bson/dbref_spec.rb +487 -0
- data/spec/bson/decimal128_spec.rb +16 -0
- data/spec/bson/document_as_spec.rb +46 -0
- data/spec/bson/document_spec.rb +7 -1
- data/spec/bson/ext_json_parse_spec.rb +37 -0
- data/spec/bson/hash_as_spec.rb +57 -0
- data/spec/bson/hash_spec.rb +32 -0
- data/spec/bson/int64_spec.rb +4 -24
- data/spec/bson/raw_spec.rb +7 -1
- data/spec/bson/regexp_spec.rb +52 -0
- data/spec/runners/common_driver.rb +1 -1
- data/spec/shared/LICENSE +20 -0
- data/spec/shared/bin/get-mongodb-download-url +17 -0
- data/spec/shared/bin/s3-copy +45 -0
- data/spec/shared/bin/s3-upload +69 -0
- data/spec/shared/lib/mrss/child_process_helper.rb +80 -0
- data/spec/shared/lib/mrss/cluster_config.rb +231 -0
- data/spec/shared/lib/mrss/constraints.rb +386 -0
- data/spec/shared/lib/mrss/docker_runner.rb +271 -0
- data/spec/shared/lib/mrss/event_subscriber.rb +200 -0
- data/spec/shared/lib/mrss/lite_constraints.rb +191 -0
- data/spec/shared/lib/mrss/server_version_registry.rb +120 -0
- data/spec/shared/lib/mrss/spec_organizer.rb +179 -0
- data/spec/shared/lib/mrss/utils.rb +15 -0
- data/spec/shared/share/Dockerfile.erb +338 -0
- data/spec/shared/share/haproxy-1.conf +16 -0
- data/spec/shared/share/haproxy-2.conf +17 -0
- data/spec/shared/shlib/distro.sh +74 -0
- data/spec/shared/shlib/server.sh +367 -0
- data/spec/shared/shlib/set_env.sh +131 -0
- data/spec/spec_helper.rb +20 -0
- data/spec/spec_tests/common_driver_spec.rb +2 -1
- data/spec/spec_tests/data/corpus/binary.json +18 -1
- data/spec/spec_tests/data/corpus/dbref.json +21 -1
- data/spec/spec_tests/data/corpus/document.json +4 -0
- data/spec/spec_tests/data/corpus/regex.json +2 -2
- data/spec/spec_tests/data/corpus/top.json +20 -9
- data.tar.gz.sig +0 -0
- metadata +141 -89
- metadata.gz.sig +0 -0
@@ -0,0 +1,487 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
# encoding: utf-8
|
3
|
+
|
4
|
+
require 'spec_helper'
|
5
|
+
require 'json'
|
6
|
+
|
7
|
+
describe BSON::DBRef do
|
8
|
+
|
9
|
+
let(:object_id) do
|
10
|
+
BSON::ObjectId.new
|
11
|
+
end
|
12
|
+
|
13
|
+
describe '#as_json' do
|
14
|
+
|
15
|
+
context 'when the database is not provided' do
|
16
|
+
|
17
|
+
let(:dbref) do
|
18
|
+
described_class.new({ '$ref' => 'users', '$id' => object_id })
|
19
|
+
end
|
20
|
+
|
21
|
+
it 'returns the json document without database' do
|
22
|
+
expect(dbref.as_json).to eq({ '$ref' => 'users', '$id' => object_id })
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
context 'when the database is provided' do
|
27
|
+
|
28
|
+
let(:dbref) do
|
29
|
+
described_class.new({ '$ref' => 'users', '$id' => object_id, '$db' => 'database' })
|
30
|
+
end
|
31
|
+
|
32
|
+
it 'returns the json document with database' do
|
33
|
+
expect(dbref.as_json).to eq({
|
34
|
+
'$ref' => 'users',
|
35
|
+
'$id' => object_id,
|
36
|
+
'$db' => 'database'
|
37
|
+
})
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
context 'when other keys are provided' do
|
42
|
+
|
43
|
+
let(:dbref) do
|
44
|
+
described_class.new({ '$ref' => 'users', '$id' => object_id, '$db' => 'database', 'x' => 'y' })
|
45
|
+
end
|
46
|
+
|
47
|
+
it 'returns the json document with the other keys' do
|
48
|
+
expect(dbref.as_json).to eq({
|
49
|
+
'$ref' => 'users',
|
50
|
+
'$id' => object_id,
|
51
|
+
'$db' => 'database',
|
52
|
+
'x' => 'y'
|
53
|
+
})
|
54
|
+
end
|
55
|
+
end
|
56
|
+
end
|
57
|
+
|
58
|
+
describe '#initialize' do
|
59
|
+
|
60
|
+
let(:dbref) do
|
61
|
+
described_class.new(hash)
|
62
|
+
end
|
63
|
+
|
64
|
+
let(:hash) do
|
65
|
+
{ '$ref' => 'users', '$id' => object_id }
|
66
|
+
end
|
67
|
+
|
68
|
+
it 'sets the collection' do
|
69
|
+
expect(dbref.collection).to eq('users')
|
70
|
+
end
|
71
|
+
|
72
|
+
it 'sets the id' do
|
73
|
+
expect(dbref.id).to eq(object_id)
|
74
|
+
end
|
75
|
+
|
76
|
+
context 'when first argument is a hash and two arguments are provided' do
|
77
|
+
|
78
|
+
let(:dbref) do
|
79
|
+
described_class.new({:$ref => 'users', :$id => object_id}, object_id)
|
80
|
+
end
|
81
|
+
|
82
|
+
it 'raises ArgumentError' do
|
83
|
+
lambda do
|
84
|
+
dbref
|
85
|
+
end.should raise_error(ArgumentError)
|
86
|
+
end
|
87
|
+
end
|
88
|
+
|
89
|
+
context 'when first argument is a hash and three arguments are provided' do
|
90
|
+
|
91
|
+
let(:dbref) do
|
92
|
+
described_class.new({:$ref => 'users', :$id => object_id}, object_id, 'db')
|
93
|
+
end
|
94
|
+
|
95
|
+
it 'raises ArgumentError' do
|
96
|
+
lambda do
|
97
|
+
dbref
|
98
|
+
end.should raise_error(ArgumentError)
|
99
|
+
end
|
100
|
+
end
|
101
|
+
|
102
|
+
context 'when a database is provided' do
|
103
|
+
|
104
|
+
let(:hash) do
|
105
|
+
{ '$ref' => 'users', '$id' => object_id, '$db' => 'db' }
|
106
|
+
end
|
107
|
+
|
108
|
+
it 'sets the database' do
|
109
|
+
expect(dbref.database).to eq('db')
|
110
|
+
end
|
111
|
+
end
|
112
|
+
|
113
|
+
context 'when not providing a collection' do
|
114
|
+
let(:hash) do
|
115
|
+
{ '$id' => object_id, '$db' => 'db' }
|
116
|
+
end
|
117
|
+
|
118
|
+
it 'raises an error' do
|
119
|
+
expect do
|
120
|
+
dbref
|
121
|
+
end.to raise_error(ArgumentError, /DBRef must have \$ref/)
|
122
|
+
end
|
123
|
+
end
|
124
|
+
|
125
|
+
context 'when not providing an id' do
|
126
|
+
let(:hash) do
|
127
|
+
{ '$ref' => 'users', '$db' => 'db' }
|
128
|
+
end
|
129
|
+
|
130
|
+
it 'raises an error' do
|
131
|
+
expect do
|
132
|
+
dbref
|
133
|
+
end.to raise_error(ArgumentError, /DBRef must have \$id/)
|
134
|
+
end
|
135
|
+
end
|
136
|
+
|
137
|
+
context 'when providing an invalid type for ref' do
|
138
|
+
let(:hash) do
|
139
|
+
{ '$ref' => 1, '$id' => object_id }
|
140
|
+
end
|
141
|
+
|
142
|
+
it 'raises an error' do
|
143
|
+
expect do
|
144
|
+
dbref
|
145
|
+
end.to raise_error(ArgumentError, /The value for key \$ref must be a string/)
|
146
|
+
end
|
147
|
+
end
|
148
|
+
|
149
|
+
context 'when providing an invalid type for database' do
|
150
|
+
let(:hash) do
|
151
|
+
{ '$ref' => 'users', '$id' => object_id, '$db' => 1 }
|
152
|
+
end
|
153
|
+
|
154
|
+
it 'raises an error' do
|
155
|
+
expect do
|
156
|
+
dbref
|
157
|
+
end.to raise_error(ArgumentError, /The value for key \$db must be a string/)
|
158
|
+
end
|
159
|
+
end
|
160
|
+
|
161
|
+
context 'when providing the fieds as symbols' do
|
162
|
+
let(:hash) do
|
163
|
+
{ :$ref => 'users', :$id => object_id, :$db => 'db' }
|
164
|
+
end
|
165
|
+
|
166
|
+
it 'does not raise an error' do
|
167
|
+
expect do
|
168
|
+
dbref
|
169
|
+
end.to_not raise_error
|
170
|
+
end
|
171
|
+
end
|
172
|
+
|
173
|
+
context 'when testing the ordering of the fields' do
|
174
|
+
context 'when the fields are in order' do
|
175
|
+
let(:hash) do
|
176
|
+
{ '$ref' => 'users', '$id' => object_id, '$db' => 'db' }
|
177
|
+
end
|
178
|
+
|
179
|
+
it 'has the correct order' do
|
180
|
+
expect(dbref.keys).to eq(['$ref', '$id', '$db'])
|
181
|
+
end
|
182
|
+
end
|
183
|
+
|
184
|
+
context 'when the fields are out of order' do
|
185
|
+
let(:hash) do
|
186
|
+
{ '$db' => 'db', '$id' => object_id, '$ref' => 'users' }
|
187
|
+
end
|
188
|
+
|
189
|
+
it 'has the correct order' do
|
190
|
+
expect(dbref.keys).to eq(['$ref', '$id', '$db'])
|
191
|
+
end
|
192
|
+
end
|
193
|
+
|
194
|
+
context 'when there is no db' do
|
195
|
+
let(:hash) do
|
196
|
+
{ '$id' => object_id, '$ref' => 'users' }
|
197
|
+
end
|
198
|
+
|
199
|
+
it 'has the correct order' do
|
200
|
+
expect(dbref.keys).to eq(['$ref', '$id'])
|
201
|
+
end
|
202
|
+
end
|
203
|
+
|
204
|
+
context 'when the there are other fields in order' do
|
205
|
+
let(:hash) do
|
206
|
+
{ '$ref' => 'users', '$id' => object_id, '$db' => 'db', 'x' => 'y', 'y' => 'z' }
|
207
|
+
end
|
208
|
+
|
209
|
+
it 'has the correct order' do
|
210
|
+
expect(dbref.keys).to eq(['$ref', '$id', '$db', 'x', 'y'])
|
211
|
+
end
|
212
|
+
end
|
213
|
+
|
214
|
+
context 'when the there are other fields out of order' do
|
215
|
+
let(:hash) do
|
216
|
+
{ 'y' => 'z', '$db' => 'db', '$id' => object_id, 'x' => 'y', '$ref' => 'users' }
|
217
|
+
end
|
218
|
+
|
219
|
+
it 'has the correct order' do
|
220
|
+
expect(dbref.keys).to eq(['$ref', '$id', '$db', 'y', 'x'])
|
221
|
+
end
|
222
|
+
end
|
223
|
+
end
|
224
|
+
end
|
225
|
+
|
226
|
+
describe '#to_bson' do
|
227
|
+
|
228
|
+
let(:dbref) do
|
229
|
+
described_class.new({ '$ref' => 'users', '$id' => object_id, '$db' => 'database' })
|
230
|
+
end
|
231
|
+
|
232
|
+
it 'converts the underlying document to bson' do
|
233
|
+
expect(dbref.to_bson.to_s).to eq(dbref.as_json.to_bson.to_s)
|
234
|
+
end
|
235
|
+
end
|
236
|
+
|
237
|
+
describe '#to_json' do
|
238
|
+
|
239
|
+
context 'when the database is not provided' do
|
240
|
+
|
241
|
+
let(:dbref) do
|
242
|
+
described_class.new({ '$ref' => 'users', '$id' => object_id })
|
243
|
+
end
|
244
|
+
|
245
|
+
it 'returns the json document without database' do
|
246
|
+
expect(dbref.to_json).to eq("{\"$ref\":\"users\",\"$id\":#{object_id.to_json}}")
|
247
|
+
end
|
248
|
+
end
|
249
|
+
|
250
|
+
context 'when the database is provided' do
|
251
|
+
|
252
|
+
let(:dbref) do
|
253
|
+
described_class.new({ '$ref' => 'users', '$id' => object_id, '$db' => 'database' })
|
254
|
+
end
|
255
|
+
|
256
|
+
it 'returns the json document with database' do
|
257
|
+
expect(dbref.to_json).to eq("{\"$ref\":\"users\",\"$id\":#{object_id.to_json},\"$db\":\"database\"}")
|
258
|
+
end
|
259
|
+
end
|
260
|
+
|
261
|
+
context 'when other keys are provided' do
|
262
|
+
|
263
|
+
let(:dbref) do
|
264
|
+
described_class.new({ '$ref' => 'users', '$id' => object_id, '$db' => 'database', 'x' => 'y' })
|
265
|
+
end
|
266
|
+
|
267
|
+
it 'returns the json document with the other keys' do
|
268
|
+
expect(dbref.to_json).to eq("{\"$ref\":\"users\",\"$id\":#{object_id.to_json},\"$db\":\"database\",\"x\":\"y\"}")
|
269
|
+
end
|
270
|
+
end
|
271
|
+
end
|
272
|
+
|
273
|
+
describe '#from_bson' do
|
274
|
+
|
275
|
+
let(:buffer) do
|
276
|
+
hash.to_bson
|
277
|
+
end
|
278
|
+
|
279
|
+
let(:decoded) do
|
280
|
+
BSON::Document.from_bson(BSON::ByteBuffer.new(buffer.to_s))
|
281
|
+
end
|
282
|
+
|
283
|
+
context 'when a database exists' do
|
284
|
+
|
285
|
+
let(:hash) do
|
286
|
+
{ '$ref' => 'users', '$id' => object_id, '$db' => 'database' }
|
287
|
+
end
|
288
|
+
|
289
|
+
it 'decodes the ref' do
|
290
|
+
expect(decoded.collection).to eq('users')
|
291
|
+
end
|
292
|
+
|
293
|
+
it 'decodes the id' do
|
294
|
+
expect(decoded.id).to eq(object_id)
|
295
|
+
end
|
296
|
+
|
297
|
+
it 'decodes the database' do
|
298
|
+
expect(decoded.database).to eq('database')
|
299
|
+
end
|
300
|
+
|
301
|
+
it 'is of class DBRef' do
|
302
|
+
expect(decoded).to be_a described_class
|
303
|
+
end
|
304
|
+
end
|
305
|
+
|
306
|
+
context 'when no database exists' do
|
307
|
+
|
308
|
+
let(:hash) do
|
309
|
+
{ '$ref' => 'users', '$id' => object_id }
|
310
|
+
end
|
311
|
+
|
312
|
+
it 'decodes the ref' do
|
313
|
+
expect(decoded.collection).to eq('users')
|
314
|
+
end
|
315
|
+
|
316
|
+
it 'decodes the id' do
|
317
|
+
expect(decoded.id).to eq(object_id)
|
318
|
+
end
|
319
|
+
|
320
|
+
it 'sets the database to nil' do
|
321
|
+
expect(decoded.database).to be_nil
|
322
|
+
end
|
323
|
+
|
324
|
+
it 'is of class DBRef' do
|
325
|
+
expect(decoded).to be_a described_class
|
326
|
+
end
|
327
|
+
end
|
328
|
+
|
329
|
+
context 'when other keys exist' do
|
330
|
+
|
331
|
+
let(:hash) do
|
332
|
+
{ '$ref' => 'users', '$id' => object_id, 'x' => 'y' }
|
333
|
+
end
|
334
|
+
|
335
|
+
it 'decodes the key' do
|
336
|
+
expect(decoded['x']).to eq('y')
|
337
|
+
end
|
338
|
+
|
339
|
+
it 'is of class DBRef' do
|
340
|
+
expect(decoded).to be_a described_class
|
341
|
+
end
|
342
|
+
end
|
343
|
+
|
344
|
+
context 'when it is an invalid dbref' do
|
345
|
+
|
346
|
+
shared_examples 'bson document' do
|
347
|
+
it 'should not raise' do
|
348
|
+
expect do
|
349
|
+
decoded
|
350
|
+
end.to_not raise_error
|
351
|
+
end
|
352
|
+
|
353
|
+
it 'has the correct class' do
|
354
|
+
expect(decoded).to be_a BSON::Document
|
355
|
+
expect(decoded).to_not be_a described_class
|
356
|
+
end
|
357
|
+
end
|
358
|
+
|
359
|
+
context 'when the hash has invalid collection type' do
|
360
|
+
let(:hash) do
|
361
|
+
{ '$ref' => 1, '$id' => object_id }
|
362
|
+
end
|
363
|
+
include_examples 'bson document'
|
364
|
+
end
|
365
|
+
|
366
|
+
context 'when the hash has an invalid database type' do
|
367
|
+
let(:hash) do
|
368
|
+
{ '$ref' => 'users', '$id' => object_id, '$db' => 1 }
|
369
|
+
end
|
370
|
+
include_examples 'bson document'
|
371
|
+
end
|
372
|
+
|
373
|
+
context 'when the hash is missing a collection' do
|
374
|
+
let(:hash) do
|
375
|
+
{ '$id' => object_id }
|
376
|
+
end
|
377
|
+
include_examples 'bson document'
|
378
|
+
end
|
379
|
+
|
380
|
+
context 'when the hash is missing an id' do
|
381
|
+
let(:hash) do
|
382
|
+
{ '$ref' => 'users' }
|
383
|
+
end
|
384
|
+
include_examples 'bson document'
|
385
|
+
end
|
386
|
+
end
|
387
|
+
|
388
|
+
context 'when nesting the dbref' do
|
389
|
+
|
390
|
+
context 'when it is a valid dbref' do
|
391
|
+
let(:hash) do
|
392
|
+
{ 'dbref' => { '$ref' => 'users', '$id' => object_id } }
|
393
|
+
end
|
394
|
+
|
395
|
+
it 'should not raise' do
|
396
|
+
expect do
|
397
|
+
buffer
|
398
|
+
end.to_not raise_error
|
399
|
+
end
|
400
|
+
|
401
|
+
it 'has the correct class' do
|
402
|
+
expect(decoded['dbref']).to be_a described_class
|
403
|
+
end
|
404
|
+
end
|
405
|
+
|
406
|
+
context 'when it is an invalid dbref' do
|
407
|
+
|
408
|
+
shared_examples 'nested bson document' do
|
409
|
+
it 'should not raise' do
|
410
|
+
expect do
|
411
|
+
decoded
|
412
|
+
end.to_not raise_error
|
413
|
+
end
|
414
|
+
|
415
|
+
it 'has the correct class' do
|
416
|
+
expect(decoded['dbref']).to be_a BSON::Document
|
417
|
+
expect(decoded['dbref']).to_not be_a described_class
|
418
|
+
end
|
419
|
+
end
|
420
|
+
|
421
|
+
context 'when the hash has invalid collection type' do
|
422
|
+
let(:hash) do
|
423
|
+
{ 'dbref' => { '$ref' => 1, '$id' => object_id } }
|
424
|
+
end
|
425
|
+
include_examples 'nested bson document'
|
426
|
+
end
|
427
|
+
|
428
|
+
context 'when the hash has an invalid database type' do
|
429
|
+
let(:hash) do
|
430
|
+
{ 'dbref' => { '$ref' => 'users', '$id' => object_id, '$db' => 1 } }
|
431
|
+
end
|
432
|
+
include_examples 'nested bson document'
|
433
|
+
end
|
434
|
+
|
435
|
+
context 'when the hash is missing a collection' do
|
436
|
+
let(:hash) do
|
437
|
+
{ 'dbref' => { '$id' => object_id } }
|
438
|
+
end
|
439
|
+
include_examples 'nested bson document'
|
440
|
+
end
|
441
|
+
|
442
|
+
context 'when the hash is missing an id' do
|
443
|
+
let(:hash) do
|
444
|
+
{ 'dbref' => { '$ref' => 'users' } }
|
445
|
+
end
|
446
|
+
include_examples 'nested bson document'
|
447
|
+
end
|
448
|
+
end
|
449
|
+
end
|
450
|
+
|
451
|
+
context 'when nesting a dbref inside a dbref' do
|
452
|
+
context 'when it is a valid dbref' do
|
453
|
+
let(:hash) do
|
454
|
+
{ 'dbref1' => { '$ref' => 'users', '$id' => object_id, 'dbref2' => { '$ref' => 'users', '$id' => object_id } } }
|
455
|
+
end
|
456
|
+
|
457
|
+
it 'should not raise' do
|
458
|
+
expect do
|
459
|
+
buffer
|
460
|
+
end.to_not raise_error
|
461
|
+
end
|
462
|
+
|
463
|
+
it 'has the correct class' do
|
464
|
+
expect(decoded['dbref1']).to be_a described_class
|
465
|
+
expect(decoded['dbref1']['dbref2']).to be_a described_class
|
466
|
+
end
|
467
|
+
end
|
468
|
+
|
469
|
+
context 'when it is an invalid dbref' do
|
470
|
+
let(:hash) do
|
471
|
+
{ 'dbref' => { '$ref' => 'users', '$id' => object_id, 'dbref' => { '$ref' => 1, '$id' => object_id } } }
|
472
|
+
end
|
473
|
+
|
474
|
+
it 'should not raise' do
|
475
|
+
expect do
|
476
|
+
decoded
|
477
|
+
end.to_not raise_error
|
478
|
+
end
|
479
|
+
|
480
|
+
it 'has the correct class' do
|
481
|
+
expect(decoded['dbref']).to be_a described_class
|
482
|
+
expect(decoded['dbref']['dbref']).to be_a BSON::Document
|
483
|
+
end
|
484
|
+
end
|
485
|
+
end
|
486
|
+
end
|
487
|
+
end
|
@@ -309,6 +309,22 @@ describe BSON::Decimal128 do
|
|
309
309
|
it_behaves_like 'an initialized BSON::Decimal128'
|
310
310
|
end
|
311
311
|
end
|
312
|
+
|
313
|
+
context 'when range is exceeded' do
|
314
|
+
it 'raises InvalidRange' do
|
315
|
+
lambda do
|
316
|
+
described_class.new('1e10000')
|
317
|
+
end.should raise_error(BSON::Decimal128::InvalidRange, /Value out of range/)
|
318
|
+
end
|
319
|
+
end
|
320
|
+
|
321
|
+
context 'when precision is exceeded' do
|
322
|
+
it 'raises UnrepresentablePrecision' do
|
323
|
+
lambda do
|
324
|
+
described_class.new('1.000000000000000000000000000000000000000000000000001')
|
325
|
+
end.should raise_error(BSON::Decimal128::UnrepresentablePrecision, /The value contains too much precision/)
|
326
|
+
end
|
327
|
+
end
|
312
328
|
end
|
313
329
|
|
314
330
|
context 'when deserializing' do
|
@@ -0,0 +1,46 @@
|
|
1
|
+
# Copyright (C) 2021 MongoDB Inc.
|
2
|
+
#
|
3
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
4
|
+
# you may not use this file except in compliance with the License.
|
5
|
+
# You may obtain a copy of the License at
|
6
|
+
#
|
7
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
8
|
+
#
|
9
|
+
# Unless required by applicable law or agreed to in writing, software
|
10
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
11
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
12
|
+
# See the License for the specific language governing permissions and
|
13
|
+
# limitations under the License.
|
14
|
+
|
15
|
+
require "spec_helper"
|
16
|
+
|
17
|
+
# BSON::Document ActiveSupport extensions
|
18
|
+
describe BSON::Document do
|
19
|
+
require_active_support
|
20
|
+
|
21
|
+
describe '#symbolize_keys' do
|
22
|
+
context 'string keys' do
|
23
|
+
let(:doc) do
|
24
|
+
described_class.new('foo' => 'bar')
|
25
|
+
end
|
26
|
+
|
27
|
+
it 'works correctly' do
|
28
|
+
doc.symbolize_keys.should == {foo: 'bar'}
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
describe '#symbolize_keys!' do
|
34
|
+
context 'string keys' do
|
35
|
+
let(:doc) do
|
36
|
+
described_class.new('foo' => 'bar')
|
37
|
+
end
|
38
|
+
|
39
|
+
it 'raises ArgumentError' do
|
40
|
+
lambda do
|
41
|
+
doc.symbolize_keys!
|
42
|
+
end.should raise_error(ArgumentError, /symbolize_keys! is not supported on BSON::Document instances/)
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
data/spec/bson/document_spec.rb
CHANGED
@@ -520,7 +520,13 @@ describe BSON::Document do
|
|
520
520
|
context "when the document has been serialized" do
|
521
521
|
|
522
522
|
let(:deserialized) do
|
523
|
-
YAML.
|
523
|
+
if YAML.respond_to?(:unsafe_load)
|
524
|
+
# In psych >= 4.0.0 `load` is basically an alias to `safe_load`,
|
525
|
+
# which will fail here.
|
526
|
+
YAML.unsafe_load(YAML.dump(doc))
|
527
|
+
else
|
528
|
+
YAML.load(YAML.dump(doc))
|
529
|
+
end
|
524
530
|
end
|
525
531
|
|
526
532
|
let!(:enum) do
|
@@ -148,6 +148,7 @@ describe "BSON::ExtJSON.parse" do
|
|
148
148
|
end
|
149
149
|
|
150
150
|
let(:parsed) { BSON::ExtJSON.parse_obj(input, mode: mode) }
|
151
|
+
let(:mode) { :bson }
|
151
152
|
|
152
153
|
context 'when mode is invalid' do
|
153
154
|
let(:mode) { :foo }
|
@@ -158,6 +159,42 @@ describe "BSON::ExtJSON.parse" do
|
|
158
159
|
end.should raise_error(ArgumentError, /Invalid value for :mode option/)
|
159
160
|
end
|
160
161
|
end
|
162
|
+
|
163
|
+
context 'when it contains a string key with a null byte' do
|
164
|
+
let(:input) do
|
165
|
+
{ "key\x00" => 1 }
|
166
|
+
end
|
167
|
+
|
168
|
+
it 'raises an exception' do
|
169
|
+
lambda do
|
170
|
+
parsed
|
171
|
+
end.should raise_error(BSON::Error::ExtJSONParseError, /Hash key cannot contain a null byte/)
|
172
|
+
end
|
173
|
+
end
|
174
|
+
|
175
|
+
context 'when it contains a symbol key with a null byte' do
|
176
|
+
let(:input) do
|
177
|
+
{ "key\x00".to_sym => 1 }
|
178
|
+
end
|
179
|
+
|
180
|
+
it 'raises an exception' do
|
181
|
+
lambda do
|
182
|
+
parsed
|
183
|
+
end.should raise_error(BSON::Error::ExtJSONParseError, /Hash key cannot contain a null byte/)
|
184
|
+
end
|
185
|
+
end
|
186
|
+
|
187
|
+
context 'when it contains an integer key' do
|
188
|
+
let(:input) do
|
189
|
+
{ 0 => 1 }
|
190
|
+
end
|
191
|
+
|
192
|
+
it 'does not raises an exception' do
|
193
|
+
lambda do
|
194
|
+
parsed
|
195
|
+
end.should_not raise_error
|
196
|
+
end
|
197
|
+
end
|
161
198
|
end
|
162
199
|
|
163
200
|
context 'when input is a binary' do
|
@@ -0,0 +1,57 @@
|
|
1
|
+
# Copyright (C) 2021 MongoDB Inc.
|
2
|
+
#
|
3
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
4
|
+
# you may not use this file except in compliance with the License.
|
5
|
+
# You may obtain a copy of the License at
|
6
|
+
#
|
7
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
8
|
+
#
|
9
|
+
# Unless required by applicable law or agreed to in writing, software
|
10
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
11
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
12
|
+
# See the License for the specific language governing permissions and
|
13
|
+
# limitations under the License.
|
14
|
+
|
15
|
+
require "spec_helper"
|
16
|
+
|
17
|
+
describe 'Hash ActiveSupport extensions' do
|
18
|
+
require_active_support
|
19
|
+
|
20
|
+
describe '#symbolize_keys' do
|
21
|
+
let(:symbolized) { hash.symbolize_keys }
|
22
|
+
|
23
|
+
shared_examples 'works correctly' do
|
24
|
+
it 'returns a hash' do
|
25
|
+
symbolized.class.should be Hash
|
26
|
+
end
|
27
|
+
|
28
|
+
it 'works correctly' do
|
29
|
+
hash.symbolize_keys.should == {foo: 'bar'}
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
context 'string keys' do
|
34
|
+
let(:hash) do
|
35
|
+
{'foo' => 'bar'}
|
36
|
+
end
|
37
|
+
|
38
|
+
include_examples 'works correctly'
|
39
|
+
end
|
40
|
+
|
41
|
+
context 'symbol keys' do
|
42
|
+
let(:hash) do
|
43
|
+
{foo: 'bar'}
|
44
|
+
end
|
45
|
+
|
46
|
+
include_examples 'works correctly'
|
47
|
+
end
|
48
|
+
|
49
|
+
context 'both string and symbol keys' do
|
50
|
+
let(:hash) do
|
51
|
+
{'foo' => 42, foo: 'bar'}
|
52
|
+
end
|
53
|
+
|
54
|
+
include_examples 'works correctly'
|
55
|
+
end
|
56
|
+
end
|
57
|
+
end
|