bson 4.11.0-java → 4.13.0-java

Sign up to get free protection for your applications and to get access to all the features.
Files changed (84) hide show
  1. checksums.yaml +4 -4
  2. checksums.yaml.gz.sig +0 -0
  3. data/README.md +4 -7
  4. data/lib/bson/active_support.rb +1 -0
  5. data/lib/bson/array.rb +2 -1
  6. data/lib/bson/binary.rb +8 -5
  7. data/lib/bson/boolean.rb +2 -1
  8. data/lib/bson/code.rb +2 -1
  9. data/lib/bson/code_with_scope.rb +2 -1
  10. data/lib/bson/config.rb +1 -0
  11. data/lib/bson/date.rb +1 -0
  12. data/lib/bson/date_time.rb +1 -0
  13. data/lib/bson/db_pointer.rb +2 -1
  14. data/lib/bson/dbref.rb +125 -0
  15. data/lib/bson/decimal128/builder.rb +18 -15
  16. data/lib/bson/decimal128.rb +10 -9
  17. data/lib/bson/document.rb +61 -18
  18. data/lib/bson/environment.rb +1 -0
  19. data/lib/bson/error.rb +7 -0
  20. data/lib/bson/ext_json.rb +24 -11
  21. data/lib/bson/false_class.rb +2 -1
  22. data/lib/bson/float.rb +20 -31
  23. data/lib/bson/hash.rb +15 -6
  24. data/lib/bson/int32.rb +3 -2
  25. data/lib/bson/int64.rb +3 -2
  26. data/lib/bson/integer.rb +3 -2
  27. data/lib/bson/json.rb +1 -0
  28. data/lib/bson/max_key.rb +3 -2
  29. data/lib/bson/min_key.rb +3 -2
  30. data/lib/bson/nil_class.rb +2 -1
  31. data/lib/bson/object.rb +1 -0
  32. data/lib/bson/object_id.rb +4 -3
  33. data/lib/bson/open_struct.rb +1 -0
  34. data/lib/bson/regexp.rb +17 -6
  35. data/lib/bson/registry.rb +1 -0
  36. data/lib/bson/specialized.rb +1 -0
  37. data/lib/bson/string.rb +3 -2
  38. data/lib/bson/symbol.rb +2 -1
  39. data/lib/bson/time.rb +4 -3
  40. data/lib/bson/time_with_zone.rb +1 -0
  41. data/lib/bson/timestamp.rb +3 -2
  42. data/lib/bson/true_class.rb +2 -1
  43. data/lib/bson/undefined.rb +2 -1
  44. data/lib/bson/version.rb +2 -1
  45. data/lib/bson-ruby.jar +0 -0
  46. data/lib/bson.rb +6 -4
  47. data/spec/README.md +14 -0
  48. data/spec/bson/binary_spec.rb +1 -1
  49. data/spec/bson/binary_uuid_spec.rb +12 -0
  50. data/spec/bson/byte_buffer_spec.rb +80 -1
  51. data/spec/bson/dbref_spec.rb +461 -0
  52. data/spec/bson/document_as_spec.rb +46 -0
  53. data/spec/bson/document_spec.rb +43 -1
  54. data/spec/bson/ext_json_parse_spec.rb +37 -0
  55. data/spec/bson/hash_as_spec.rb +57 -0
  56. data/spec/bson/int64_spec.rb +4 -24
  57. data/spec/bson/raw_spec.rb +7 -1
  58. data/spec/bson/regexp_spec.rb +52 -0
  59. data/spec/runners/common_driver.rb +1 -1
  60. data/spec/shared/LICENSE +20 -0
  61. data/spec/shared/bin/get-mongodb-download-url +17 -0
  62. data/spec/shared/lib/mrss/child_process_helper.rb +80 -0
  63. data/spec/shared/lib/mrss/cluster_config.rb +221 -0
  64. data/spec/shared/lib/mrss/constraints.rb +346 -0
  65. data/spec/shared/lib/mrss/docker_runner.rb +265 -0
  66. data/spec/shared/lib/mrss/lite_constraints.rb +191 -0
  67. data/spec/shared/lib/mrss/server_version_registry.rb +115 -0
  68. data/spec/shared/lib/mrss/spec_organizer.rb +152 -0
  69. data/spec/shared/lib/mrss/utils.rb +15 -0
  70. data/spec/shared/share/Dockerfile.erb +231 -0
  71. data/spec/shared/shlib/distro.sh +73 -0
  72. data/spec/shared/shlib/server.sh +290 -0
  73. data/spec/shared/shlib/set_env.sh +128 -0
  74. data/spec/spec_helper.rb +13 -0
  75. data/spec/spec_tests/data/corpus/binary.json +33 -0
  76. data/spec/spec_tests/data/corpus/dbref.json +21 -1
  77. data/spec/spec_tests/data/corpus/document.json +4 -0
  78. data/spec/spec_tests/data/corpus/regex.json +2 -2
  79. data/spec/spec_tests/data/corpus/top.json +20 -9
  80. data/spec/support/spec_config.rb +2 -1
  81. data.tar.gz.sig +0 -0
  82. metadata +136 -100
  83. metadata.gz.sig +0 -0
  84. data/lib/bson_native.bundle +0 -0
@@ -136,6 +136,10 @@ describe "BSON::Binary - UUID spec tests" do
136
136
  it 'decodes as python legacy' do
137
137
  expect(binary.to_uuid(:python_legacy).gsub('-', '').upcase).not_to eq("00112233445566778899AABBCCDDEEFF")
138
138
  end
139
+
140
+ it 'expects four dashes when output as String' do
141
+ expect(binary.to_uuid(:csharp_legacy)).to eq("00112233-4455-6677-8899-aabbccddeeff")
142
+ end
139
143
  end
140
144
 
141
145
  context ':uuid_old, java legacy encoded' do
@@ -154,6 +158,10 @@ describe "BSON::Binary - UUID spec tests" do
154
158
  it 'decodes as python legacy' do
155
159
  expect(binary.to_uuid(:python_legacy).gsub('-', '').upcase).not_to eq("00112233445566778899AABBCCDDEEFF")
156
160
  end
161
+
162
+ it 'expects four dashes when output as String' do
163
+ expect(binary.to_uuid(:java_legacy)).to eq("00112233-4455-6677-8899-aabbccddeeff")
164
+ end
157
165
  end
158
166
 
159
167
  context ':uuid_old, python legacy encoded' do
@@ -172,6 +180,10 @@ describe "BSON::Binary - UUID spec tests" do
172
180
  it 'decodes as python legacy' do
173
181
  expect(binary.to_uuid(:python_legacy).gsub('-', '').upcase).to eq("00112233445566778899AABBCCDDEEFF")
174
182
  end
183
+
184
+ it 'expects four dashes when output as String' do
185
+ expect(binary.to_uuid(:python_legacy)).to eq("00112233-4455-6677-8899-aabbccddeeff")
186
+ end
175
187
  end
176
188
  end
177
189
  end
@@ -42,7 +42,6 @@ describe BSON::ByteBuffer do
42
42
  end
43
43
  end
44
44
 
45
-
46
45
  context 'when the byte buffer is initialized with some bytes' do
47
46
 
48
47
  let(:buffer) do
@@ -53,6 +52,50 @@ describe BSON::ByteBuffer do
53
52
  expect(buffer.length).to eq(2)
54
53
  end
55
54
  end
55
+
56
+ context 'after the byte buffer was read from' do
57
+
58
+ let(:buffer) do
59
+ described_class.new({}.to_bson.to_s)
60
+ end
61
+
62
+ it 'returns the number of bytes remaining in the buffer' do
63
+ expect(buffer.length).to eq(5)
64
+ buffer.get_int32
65
+ expect(buffer.length).to eq(1)
66
+ end
67
+ end
68
+
69
+ context 'after the byte buffer was converted to string' do
70
+
71
+ shared_examples 'returns the total buffer length' do
72
+ it 'returns the total buffer length' do
73
+ expect(buffer.length).to eq(5)
74
+ buffer.to_s.length.should == 5
75
+ expect(buffer.length).to eq(5)
76
+ end
77
+ end
78
+
79
+ context 'read buffer' do
80
+
81
+ let(:buffer) do
82
+ described_class.new({}.to_bson.to_s)
83
+ end
84
+
85
+ include_examples 'returns the total buffer length'
86
+ end
87
+
88
+ context 'write buffer' do
89
+
90
+ let(:buffer) do
91
+ described_class.new.tap do |buffer|
92
+ buffer.put_bytes('hello')
93
+ end
94
+ end
95
+
96
+ include_examples 'returns the total buffer length'
97
+ end
98
+ end
56
99
  end
57
100
 
58
101
  describe '#rewind!' do
@@ -148,4 +191,40 @@ describe BSON::ByteBuffer do
148
191
  end
149
192
  end
150
193
  end
194
+
195
+ describe '#to_s' do
196
+ context 'read buffer' do
197
+ let(:buffer) do
198
+ described_class.new("\x18\x00\x00\x00*\x00\x00\x00")
199
+ end
200
+
201
+ it 'returns the data' do
202
+ buffer.to_s.should == "\x18\x00\x00\x00*\x00\x00\x00"
203
+ end
204
+
205
+ it 'returns the remaining buffer contents after a read' do
206
+ buffer.to_s.should == "\x18\x00\x00\x00*\x00\x00\x00"
207
+ buffer.get_int32.should == 24
208
+ buffer.to_s.should == "*\x00\x00\x00"
209
+ end
210
+ end
211
+
212
+ context 'write buffer' do
213
+ let(:buffer) do
214
+ described_class.new.tap do |buffer|
215
+ buffer.put_int32(24)
216
+ end
217
+ end
218
+
219
+ it 'returns the data' do
220
+ buffer.to_s.should == "\x18\x00\x00\x00".force_encoding('binary')
221
+ end
222
+
223
+ it 'returns the complete buffer contents after a write' do
224
+ buffer.to_s.should == "\x18\x00\x00\x00".force_encoding('binary')
225
+ buffer.put_int32(42)
226
+ buffer.to_s.should == "\x18\x00\x00\x00*\x00\x00\x00".force_encoding('binary')
227
+ end
228
+ end
229
+ end
151
230
  end
@@ -0,0 +1,461 @@
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 a database is provided' do
77
+
78
+ let(:hash) do
79
+ { '$ref' => 'users', '$id' => object_id, '$db' => 'db' }
80
+ end
81
+
82
+ it 'sets the database' do
83
+ expect(dbref.database).to eq('db')
84
+ end
85
+ end
86
+
87
+ context 'when not providing a collection' do
88
+ let(:hash) do
89
+ { '$id' => object_id, '$db' => 'db' }
90
+ end
91
+
92
+ it 'raises an error' do
93
+ expect do
94
+ dbref
95
+ end.to raise_error(ArgumentError, /DBRef must have \$ref/)
96
+ end
97
+ end
98
+
99
+ context 'when not providing an id' do
100
+ let(:hash) do
101
+ { '$ref' => 'users', '$db' => 'db' }
102
+ end
103
+
104
+ it 'raises an error' do
105
+ expect do
106
+ dbref
107
+ end.to raise_error(ArgumentError, /DBRef must have \$id/)
108
+ end
109
+ end
110
+
111
+ context 'when providing an invalid type for ref' do
112
+ let(:hash) do
113
+ { '$ref' => 1, '$id' => object_id }
114
+ end
115
+
116
+ it 'raises an error' do
117
+ expect do
118
+ dbref
119
+ end.to raise_error(ArgumentError, /The value for key \$ref must be a string/)
120
+ end
121
+ end
122
+
123
+ context 'when providing an invalid type for database' do
124
+ let(:hash) do
125
+ { '$ref' => 'users', '$id' => object_id, '$db' => 1 }
126
+ end
127
+
128
+ it 'raises an error' do
129
+ expect do
130
+ dbref
131
+ end.to raise_error(ArgumentError, /The value for key \$db must be a string/)
132
+ end
133
+ end
134
+
135
+ context 'when providing the fieds as symbols' do
136
+ let(:hash) do
137
+ { :$ref => 'users', :$id => object_id, :$db => 'db' }
138
+ end
139
+
140
+ it 'does not raise an error' do
141
+ expect do
142
+ dbref
143
+ end.to_not raise_error
144
+ end
145
+ end
146
+
147
+ context 'when testing the ordering of the fields' do
148
+ context 'when the fields are in order' do
149
+ let(:hash) do
150
+ { '$ref' => 'users', '$id' => object_id, '$db' => 'db' }
151
+ end
152
+
153
+ it 'has the correct order' do
154
+ expect(dbref.keys).to eq(['$ref', '$id', '$db'])
155
+ end
156
+ end
157
+
158
+ context 'when the fields are out of order' do
159
+ let(:hash) do
160
+ { '$db' => 'db', '$id' => object_id, '$ref' => 'users' }
161
+ end
162
+
163
+ it 'has the correct order' do
164
+ expect(dbref.keys).to eq(['$ref', '$id', '$db'])
165
+ end
166
+ end
167
+
168
+ context 'when there is no db' do
169
+ let(:hash) do
170
+ { '$id' => object_id, '$ref' => 'users' }
171
+ end
172
+
173
+ it 'has the correct order' do
174
+ expect(dbref.keys).to eq(['$ref', '$id'])
175
+ end
176
+ end
177
+
178
+ context 'when the there are other fields in order' do
179
+ let(:hash) do
180
+ { '$ref' => 'users', '$id' => object_id, '$db' => 'db', 'x' => 'y', 'y' => 'z' }
181
+ end
182
+
183
+ it 'has the correct order' do
184
+ expect(dbref.keys).to eq(['$ref', '$id', '$db', 'x', 'y'])
185
+ end
186
+ end
187
+
188
+ context 'when the there are other fields out of order' do
189
+ let(:hash) do
190
+ { 'y' => 'z', '$db' => 'db', '$id' => object_id, 'x' => 'y', '$ref' => 'users' }
191
+ end
192
+
193
+ it 'has the correct order' do
194
+ expect(dbref.keys).to eq(['$ref', '$id', '$db', 'y', 'x'])
195
+ end
196
+ end
197
+ end
198
+ end
199
+
200
+ describe '#to_bson' do
201
+
202
+ let(:dbref) do
203
+ described_class.new({ '$ref' => 'users', '$id' => object_id, '$db' => 'database' })
204
+ end
205
+
206
+ it 'converts the underlying document to bson' do
207
+ expect(dbref.to_bson.to_s).to eq(dbref.as_json.to_bson.to_s)
208
+ end
209
+ end
210
+
211
+ describe '#to_json' do
212
+
213
+ context 'when the database is not provided' do
214
+
215
+ let(:dbref) do
216
+ described_class.new({ '$ref' => 'users', '$id' => object_id })
217
+ end
218
+
219
+ it 'returns the json document without database' do
220
+ expect(dbref.to_json).to eq("{\"$ref\":\"users\",\"$id\":#{object_id.to_json}}")
221
+ end
222
+ end
223
+
224
+ context 'when the database is provided' do
225
+
226
+ let(:dbref) do
227
+ described_class.new({ '$ref' => 'users', '$id' => object_id, '$db' => 'database' })
228
+ end
229
+
230
+ it 'returns the json document with database' do
231
+ expect(dbref.to_json).to eq("{\"$ref\":\"users\",\"$id\":#{object_id.to_json},\"$db\":\"database\"}")
232
+ end
233
+ end
234
+
235
+ context 'when other keys are provided' do
236
+
237
+ let(:dbref) do
238
+ described_class.new({ '$ref' => 'users', '$id' => object_id, '$db' => 'database', 'x' => 'y' })
239
+ end
240
+
241
+ it 'returns the json document with the other keys' do
242
+ expect(dbref.to_json).to eq("{\"$ref\":\"users\",\"$id\":#{object_id.to_json},\"$db\":\"database\",\"x\":\"y\"}")
243
+ end
244
+ end
245
+ end
246
+
247
+ describe '#from_bson' do
248
+
249
+ let(:buffer) do
250
+ hash.to_bson
251
+ end
252
+
253
+ let(:decoded) do
254
+ BSON::Document.from_bson(BSON::ByteBuffer.new(buffer.to_s))
255
+ end
256
+
257
+ context 'when a database exists' do
258
+
259
+ let(:hash) do
260
+ { '$ref' => 'users', '$id' => object_id, '$db' => 'database' }
261
+ end
262
+
263
+ it 'decodes the ref' do
264
+ expect(decoded.collection).to eq('users')
265
+ end
266
+
267
+ it 'decodes the id' do
268
+ expect(decoded.id).to eq(object_id)
269
+ end
270
+
271
+ it 'decodes the database' do
272
+ expect(decoded.database).to eq('database')
273
+ end
274
+
275
+ it 'is of class DBRef' do
276
+ expect(decoded).to be_a described_class
277
+ end
278
+ end
279
+
280
+ context 'when no database exists' do
281
+
282
+ let(:hash) do
283
+ { '$ref' => 'users', '$id' => object_id }
284
+ end
285
+
286
+ it 'decodes the ref' do
287
+ expect(decoded.collection).to eq('users')
288
+ end
289
+
290
+ it 'decodes the id' do
291
+ expect(decoded.id).to eq(object_id)
292
+ end
293
+
294
+ it 'sets the database to nil' do
295
+ expect(decoded.database).to be_nil
296
+ end
297
+
298
+ it 'is of class DBRef' do
299
+ expect(decoded).to be_a described_class
300
+ end
301
+ end
302
+
303
+ context 'when other keys exist' do
304
+
305
+ let(:hash) do
306
+ { '$ref' => 'users', '$id' => object_id, 'x' => 'y' }
307
+ end
308
+
309
+ it 'decodes the key' do
310
+ expect(decoded['x']).to eq('y')
311
+ end
312
+
313
+ it 'is of class DBRef' do
314
+ expect(decoded).to be_a described_class
315
+ end
316
+ end
317
+
318
+ context 'when it is an invalid dbref' do
319
+
320
+ shared_examples 'bson document' do
321
+ it 'should not raise' do
322
+ expect do
323
+ decoded
324
+ end.to_not raise_error
325
+ end
326
+
327
+ it 'has the correct class' do
328
+ expect(decoded).to be_a BSON::Document
329
+ expect(decoded).to_not be_a described_class
330
+ end
331
+ end
332
+
333
+ context 'when the hash has invalid collection type' do
334
+ let(:hash) do
335
+ { '$ref' => 1, '$id' => object_id }
336
+ end
337
+ include_examples 'bson document'
338
+ end
339
+
340
+ context 'when the hash has an invalid database type' do
341
+ let(:hash) do
342
+ { '$ref' => 'users', '$id' => object_id, '$db' => 1 }
343
+ end
344
+ include_examples 'bson document'
345
+ end
346
+
347
+ context 'when the hash is missing a collection' do
348
+ let(:hash) do
349
+ { '$id' => object_id }
350
+ end
351
+ include_examples 'bson document'
352
+ end
353
+
354
+ context 'when the hash is missing an id' do
355
+ let(:hash) do
356
+ { '$ref' => 'users' }
357
+ end
358
+ include_examples 'bson document'
359
+ end
360
+ end
361
+
362
+ context 'when nesting the dbref' do
363
+
364
+ context 'when it is a valid dbref' do
365
+ let(:hash) do
366
+ { 'dbref' => { '$ref' => 'users', '$id' => object_id } }
367
+ end
368
+
369
+ it 'should not raise' do
370
+ expect do
371
+ buffer
372
+ end.to_not raise_error
373
+ end
374
+
375
+ it 'has the correct class' do
376
+ expect(decoded['dbref']).to be_a described_class
377
+ end
378
+ end
379
+
380
+ context 'when it is an invalid dbref' do
381
+
382
+ shared_examples 'nested bson document' do
383
+ it 'should not raise' do
384
+ expect do
385
+ decoded
386
+ end.to_not raise_error
387
+ end
388
+
389
+ it 'has the correct class' do
390
+ expect(decoded['dbref']).to be_a BSON::Document
391
+ expect(decoded['dbref']).to_not be_a described_class
392
+ end
393
+ end
394
+
395
+ context 'when the hash has invalid collection type' do
396
+ let(:hash) do
397
+ { 'dbref' => { '$ref' => 1, '$id' => object_id } }
398
+ end
399
+ include_examples 'nested bson document'
400
+ end
401
+
402
+ context 'when the hash has an invalid database type' do
403
+ let(:hash) do
404
+ { 'dbref' => { '$ref' => 'users', '$id' => object_id, '$db' => 1 } }
405
+ end
406
+ include_examples 'nested bson document'
407
+ end
408
+
409
+ context 'when the hash is missing a collection' do
410
+ let(:hash) do
411
+ { 'dbref' => { '$id' => object_id } }
412
+ end
413
+ include_examples 'nested bson document'
414
+ end
415
+
416
+ context 'when the hash is missing an id' do
417
+ let(:hash) do
418
+ { 'dbref' => { '$ref' => 'users' } }
419
+ end
420
+ include_examples 'nested bson document'
421
+ end
422
+ end
423
+ end
424
+
425
+ context 'when nesting a dbref inside a dbref' do
426
+ context 'when it is a valid dbref' do
427
+ let(:hash) do
428
+ { 'dbref1' => { '$ref' => 'users', '$id' => object_id, 'dbref2' => { '$ref' => 'users', '$id' => object_id } } }
429
+ end
430
+
431
+ it 'should not raise' do
432
+ expect do
433
+ buffer
434
+ end.to_not raise_error
435
+ end
436
+
437
+ it 'has the correct class' do
438
+ expect(decoded['dbref1']).to be_a described_class
439
+ expect(decoded['dbref1']['dbref2']).to be_a described_class
440
+ end
441
+ end
442
+
443
+ context 'when it is an invalid dbref' do
444
+ let(:hash) do
445
+ { 'dbref' => { '$ref' => 'users', '$id' => object_id, 'dbref' => { '$ref' => 1, '$id' => object_id } } }
446
+ end
447
+
448
+ it 'should not raise' do
449
+ expect do
450
+ decoded
451
+ end.to_not raise_error
452
+ end
453
+
454
+ it 'has the correct class' do
455
+ expect(decoded['dbref']).to be_a described_class
456
+ expect(decoded['dbref']['dbref']).to be_a BSON::Document
457
+ end
458
+ end
459
+ end
460
+ end
461
+ end
@@ -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
@@ -215,6 +215,10 @@ describe BSON::Document do
215
215
 
216
216
  context "when provided string keys" do
217
217
 
218
+ it "is a BSON Document" do
219
+ expect(document.slice("key1")).to be_a(BSON::Document)
220
+ end
221
+
218
222
  it "returns the partial document" do
219
223
  expect(document.slice("key1")).to contain_exactly(['key1', 'value1'])
220
224
  end
@@ -222,13 +226,45 @@ describe BSON::Document do
222
226
 
223
227
  context "when provided symbol keys" do
224
228
 
229
+ it "is a BSON Document" do
230
+ expect(document.slice(:key1)).to be_a(BSON::Document)
231
+ end
232
+
225
233
  it "returns the partial document" do
226
234
  expect(document.slice(:key1)).to contain_exactly(['key1', 'value1'])
227
235
  end
228
236
  end
237
+
238
+ context "when provided keys that do not exist in the document" do
239
+
240
+ it "returns only the keys that exist in the document" do
241
+ expect(document.slice(:key1, :key3)).to contain_exactly(['key1', 'value1'])
242
+ end
243
+ end
244
+ end
245
+ end
246
+
247
+ describe "#except" do
248
+ let(:document) do
249
+ described_class.new("key1" => "value1", key2: "value2")
250
+ end
251
+
252
+ context "when provided string keys" do
253
+
254
+ it "returns the partial document" do
255
+ expect(document.except("key1")).to contain_exactly(['key2', 'value2'])
256
+ end
257
+ end
258
+
259
+ context "when provided symbol keys" do
260
+
261
+ it "returns the partial document" do
262
+ expect(document.except(:key1)).to contain_exactly(['key2', 'value2'])
263
+ end
229
264
  end
230
265
  end
231
266
 
267
+
232
268
  describe "#delete" do
233
269
 
234
270
  shared_examples_for "a document with deletable pairs" do
@@ -484,7 +520,13 @@ describe BSON::Document do
484
520
  context "when the document has been serialized" do
485
521
 
486
522
  let(:deserialized) do
487
- YAML.load(YAML.dump(doc))
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
488
530
  end
489
531
 
490
532
  let!(:enum) do