bson 4.2.2 → 4.12.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -5
- checksums.yaml.gz.sig +0 -0
- data.tar.gz.sig +0 -0
- data/README.md +25 -7
- data/Rakefile +16 -9
- data/ext/bson/{native-endian.h → bson-endian.h} +5 -99
- data/ext/bson/bson-native.h +125 -0
- data/ext/bson/bytebuf.c +133 -0
- data/ext/bson/endian.c +117 -0
- data/ext/bson/init.c +355 -0
- data/ext/bson/libbson-utf8.c +230 -0
- data/ext/bson/read.c +411 -0
- data/ext/bson/util.c +95 -0
- data/ext/bson/write.c +680 -0
- data/lib/bson.rb +6 -3
- data/lib/bson/active_support.rb +17 -0
- data/lib/bson/array.rb +57 -17
- data/lib/bson/binary.rb +185 -13
- data/lib/bson/boolean.rb +12 -3
- data/lib/bson/code.rb +16 -2
- data/lib/bson/code_with_scope.rb +32 -5
- data/lib/bson/config.rb +1 -1
- data/lib/bson/date.rb +12 -2
- data/lib/bson/date_time.rb +2 -2
- data/lib/bson/db_pointer.rb +110 -0
- data/lib/bson/decimal128.rb +17 -3
- data/lib/bson/decimal128/builder.rb +1 -1
- data/lib/bson/document.rb +152 -5
- data/lib/bson/environment.rb +2 -1
- data/lib/bson/error.rb +27 -0
- data/lib/bson/ext_json.rb +383 -0
- data/lib/bson/false_class.rb +1 -1
- data/lib/bson/float.rb +48 -2
- data/lib/bson/hash.rb +68 -17
- data/lib/bson/int32.rb +52 -13
- data/lib/bson/int64.rb +59 -15
- data/lib/bson/integer.rb +36 -2
- data/lib/bson/json.rb +1 -1
- data/lib/bson/max_key.rb +13 -1
- data/lib/bson/min_key.rb +13 -1
- data/lib/bson/nil_class.rb +4 -2
- data/lib/bson/object.rb +28 -1
- data/lib/bson/object_id.rb +16 -2
- data/lib/bson/open_struct.rb +1 -1
- data/lib/bson/regexp.rb +27 -4
- data/lib/bson/registry.rb +3 -3
- data/lib/bson/specialized.rb +4 -2
- data/lib/bson/string.rb +5 -3
- data/lib/bson/symbol.rb +99 -7
- data/lib/bson/time.rb +63 -4
- data/lib/bson/time_with_zone.rb +54 -0
- data/lib/bson/timestamp.rb +44 -6
- data/lib/bson/true_class.rb +1 -1
- data/lib/bson/undefined.rb +12 -1
- data/lib/bson/version.rb +2 -2
- data/spec/bson/array_spec.rb +18 -1
- data/spec/bson/binary_spec.rb +100 -3
- data/spec/bson/binary_uuid_spec.rb +189 -0
- data/spec/bson/boolean_spec.rb +1 -1
- data/spec/bson/byte_buffer_read_spec.rb +197 -0
- data/spec/bson/byte_buffer_spec.rb +121 -381
- data/spec/bson/byte_buffer_write_spec.rb +854 -0
- data/spec/bson/code_spec.rb +1 -1
- data/spec/bson/code_with_scope_spec.rb +1 -1
- data/spec/bson/date_spec.rb +1 -1
- data/spec/bson/date_time_spec.rb +54 -1
- data/spec/bson/decimal128_spec.rb +35 -35
- data/spec/bson/document_as_spec.rb +46 -0
- data/spec/bson/document_spec.rb +197 -30
- data/spec/bson/ext_json_parse_spec.rb +308 -0
- data/spec/bson/false_class_spec.rb +1 -1
- data/spec/bson/float_spec.rb +37 -1
- data/spec/bson/hash_as_spec.rb +57 -0
- data/spec/bson/hash_spec.rb +209 -1
- data/spec/bson/int32_spec.rb +180 -6
- data/spec/bson/int64_spec.rb +199 -6
- data/spec/bson/integer_spec.rb +29 -3
- data/spec/bson/json_spec.rb +1 -1
- data/spec/bson/max_key_spec.rb +1 -1
- data/spec/bson/min_key_spec.rb +1 -1
- data/spec/bson/nil_class_spec.rb +1 -1
- data/spec/bson/object_id_spec.rb +1 -1
- data/spec/bson/object_spec.rb +1 -1
- data/spec/bson/open_struct_spec.rb +1 -1
- data/spec/bson/raw_spec.rb +34 -2
- data/spec/bson/regexp_spec.rb +1 -1
- data/spec/bson/registry_spec.rb +1 -1
- data/spec/bson/string_spec.rb +19 -1
- data/spec/bson/symbol_raw_spec.rb +45 -0
- data/spec/bson/symbol_spec.rb +63 -3
- data/spec/bson/time_spec.rb +205 -2
- data/spec/bson/time_with_zone_spec.rb +68 -0
- data/spec/bson/timestamp_spec.rb +56 -1
- data/spec/bson/true_class_spec.rb +1 -1
- data/spec/bson/undefined_spec.rb +1 -1
- data/spec/bson_spec.rb +1 -1
- data/spec/{support → runners}/common_driver.rb +1 -1
- data/spec/runners/corpus.rb +185 -0
- data/spec/{support/corpus.rb → runners/corpus_legacy.rb} +41 -59
- data/spec/spec_helper.rb +40 -3
- data/spec/{bson/driver_bson_spec.rb → spec_tests/common_driver_spec.rb} +1 -0
- data/spec/{bson/corpus_spec.rb → spec_tests/corpus_legacy_spec.rb} +10 -7
- data/spec/spec_tests/corpus_spec.rb +124 -0
- data/spec/spec_tests/data/corpus/README.md +15 -0
- data/spec/spec_tests/data/corpus/array.json +49 -0
- data/spec/spec_tests/data/corpus/binary.json +113 -0
- data/spec/spec_tests/data/corpus/boolean.json +27 -0
- data/spec/spec_tests/data/corpus/code.json +67 -0
- data/spec/spec_tests/data/corpus/code_w_scope.json +78 -0
- data/spec/spec_tests/data/corpus/datetime.json +42 -0
- data/spec/spec_tests/data/corpus/dbpointer.json +56 -0
- data/spec/spec_tests/data/corpus/dbref.json +31 -0
- data/spec/spec_tests/data/corpus/decimal128-1.json +317 -0
- data/spec/spec_tests/data/corpus/decimal128-2.json +793 -0
- data/spec/spec_tests/data/corpus/decimal128-3.json +1771 -0
- data/spec/spec_tests/data/corpus/decimal128-4.json +117 -0
- data/spec/spec_tests/data/corpus/decimal128-5.json +402 -0
- data/spec/spec_tests/data/corpus/decimal128-6.json +119 -0
- data/spec/spec_tests/data/corpus/decimal128-7.json +323 -0
- data/spec/spec_tests/data/corpus/document.json +36 -0
- data/spec/spec_tests/data/corpus/double.json +87 -0
- data/spec/spec_tests/data/corpus/int32.json +43 -0
- data/spec/spec_tests/data/corpus/int64.json +43 -0
- data/spec/spec_tests/data/corpus/maxkey.json +12 -0
- data/spec/spec_tests/data/corpus/minkey.json +12 -0
- data/spec/spec_tests/data/corpus/multi-type-deprecated.json +15 -0
- data/spec/spec_tests/data/corpus/multi-type.json +11 -0
- data/spec/spec_tests/data/corpus/null.json +12 -0
- data/spec/spec_tests/data/corpus/oid.json +28 -0
- data/spec/spec_tests/data/corpus/regex.json +65 -0
- data/spec/spec_tests/data/corpus/string.json +72 -0
- data/spec/spec_tests/data/corpus/symbol.json +80 -0
- data/spec/spec_tests/data/corpus/timestamp.json +34 -0
- data/spec/spec_tests/data/corpus/top.json +236 -0
- data/spec/spec_tests/data/corpus/undefined.json +15 -0
- data/spec/{support/corpus-tests → spec_tests/data/corpus_legacy}/array.json +8 -2
- data/spec/{support/corpus-tests/failures → spec_tests/data/corpus_legacy}/binary.json +0 -0
- data/spec/{support/corpus-tests → spec_tests/data/corpus_legacy}/boolean.json +0 -0
- data/spec/{support/corpus-tests → spec_tests/data/corpus_legacy}/code.json +1 -1
- data/spec/{support/corpus-tests → spec_tests/data/corpus_legacy}/code_w_scope.json +1 -1
- data/spec/{support/corpus-tests → spec_tests/data/corpus_legacy}/document.json +1 -1
- data/spec/{support/corpus-tests → spec_tests/data/corpus_legacy}/double.json +1 -1
- data/spec/{support/corpus-tests → spec_tests/data/corpus_legacy}/failures/datetime.json +0 -0
- data/spec/{support/corpus-tests → spec_tests/data/corpus_legacy}/failures/dbpointer.json +0 -0
- data/spec/{support/corpus-tests → spec_tests/data/corpus_legacy}/failures/int64.json +0 -0
- data/spec/{support/corpus-tests → spec_tests/data/corpus_legacy}/failures/symbol.json +0 -0
- data/spec/{support/corpus-tests → spec_tests/data/corpus_legacy}/int32.json +1 -1
- data/spec/{support/corpus-tests → spec_tests/data/corpus_legacy}/maxkey.json +1 -1
- data/spec/{support/corpus-tests → spec_tests/data/corpus_legacy}/minkey.json +1 -1
- data/spec/{support/corpus-tests → spec_tests/data/corpus_legacy}/null.json +1 -1
- data/spec/{support/corpus-tests → spec_tests/data/corpus_legacy}/oid.json +0 -0
- data/spec/{support/corpus-tests → spec_tests/data/corpus_legacy}/regex.json +1 -1
- data/spec/{support/corpus-tests → spec_tests/data/corpus_legacy}/string.json +0 -0
- data/spec/{support/corpus-tests → spec_tests/data/corpus_legacy}/timestamp.json +1 -1
- data/spec/{support/corpus-tests → spec_tests/data/corpus_legacy}/top.json +0 -0
- data/spec/{support/corpus-tests/failures → spec_tests/data/corpus_legacy}/undefined.json +0 -0
- data/spec/{support/driver-spec-tests → spec_tests/data}/decimal128/decimal128-1.json +0 -0
- data/spec/{support/driver-spec-tests → spec_tests/data}/decimal128/decimal128-2.json +0 -0
- data/spec/{support/driver-spec-tests → spec_tests/data}/decimal128/decimal128-3.json +0 -0
- data/spec/{support/driver-spec-tests → spec_tests/data}/decimal128/decimal128-4.json +0 -0
- data/spec/{support/driver-spec-tests → spec_tests/data}/decimal128/decimal128-5.json +0 -0
- data/spec/{support/driver-spec-tests → spec_tests/data}/decimal128/decimal128-6.json +0 -0
- data/spec/{support/driver-spec-tests → spec_tests/data}/decimal128/decimal128-7.json +0 -0
- data/spec/support/shared_examples.rb +3 -5
- data/spec/support/spec_config.rb +16 -0
- data/spec/support/utils.rb +10 -0
- metadata +227 -124
- metadata.gz.sig +0 -0
- data/ext/bson/bson_native.c +0 -762
data/spec/bson/code_spec.rb
CHANGED
data/spec/bson/date_spec.rb
CHANGED
data/spec/bson/date_time_spec.rb
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
# Copyright (C) 2009-
|
1
|
+
# Copyright (C) 2009-2020 MongoDB Inc.
|
2
2
|
#
|
3
3
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
4
4
|
# you may not use this file except in compliance with the License.
|
@@ -35,5 +35,58 @@ describe DateTime do
|
|
35
35
|
|
36
36
|
it_behaves_like "a serializable bson element"
|
37
37
|
end
|
38
|
+
|
39
|
+
context "when the dates don't both use Gregorian" do
|
40
|
+
|
41
|
+
let(:shakespeare_datetime) do
|
42
|
+
DateTime.iso8601('1616-04-23', Date::ENGLAND)
|
43
|
+
end
|
44
|
+
|
45
|
+
let(:gregorian_datetime) do
|
46
|
+
DateTime.iso8601('1616-04-23', Date::GREGORIAN)
|
47
|
+
end
|
48
|
+
|
49
|
+
context "when putting to bson" do
|
50
|
+
|
51
|
+
let(:shakespeare) do
|
52
|
+
{ a: shakespeare_datetime }.to_bson
|
53
|
+
end
|
54
|
+
|
55
|
+
let(:gregorian) do
|
56
|
+
{ a: gregorian_datetime }.to_bson
|
57
|
+
end
|
58
|
+
|
59
|
+
it "does not equal each other" do
|
60
|
+
expect(shakespeare.to_s).to_not eq(gregorian.to_s)
|
61
|
+
end
|
62
|
+
|
63
|
+
it "the english date is 10 days later" do
|
64
|
+
expect(shakespeare.to_s).to eq({ a: DateTime.iso8601('1616-05-03', Date::GREGORIAN) }.to_bson.to_s)
|
65
|
+
end
|
66
|
+
end
|
67
|
+
|
68
|
+
context "when putting and receiving from bson" do
|
69
|
+
|
70
|
+
let(:shakespeare) do
|
71
|
+
Hash.from_bson(BSON::ByteBuffer.new({ a: shakespeare_datetime }.to_bson.to_s))
|
72
|
+
end
|
73
|
+
|
74
|
+
let(:gregorian) do
|
75
|
+
Hash.from_bson(BSON::ByteBuffer.new({ a: gregorian_datetime }.to_bson.to_s))
|
76
|
+
end
|
77
|
+
|
78
|
+
it "does not equal each other" do
|
79
|
+
expect(shakespeare).to_not eq(gregorian)
|
80
|
+
end
|
81
|
+
|
82
|
+
it "the english date is 10 days later" do
|
83
|
+
expect(shakespeare[:a]).to eq(DateTime.iso8601('1616-05-03', Date::GREGORIAN).to_time)
|
84
|
+
end
|
85
|
+
|
86
|
+
it "the gregorian date is the same" do
|
87
|
+
expect(gregorian[:a]).to eq(DateTime.iso8601('1616-04-23', Date::GREGORIAN).to_time)
|
88
|
+
end
|
89
|
+
end
|
90
|
+
end
|
38
91
|
end
|
39
92
|
end
|
@@ -1,4 +1,4 @@
|
|
1
|
-
# Copyright (C) 2016 MongoDB Inc.
|
1
|
+
# Copyright (C) 2016-2020 MongoDB Inc.
|
2
2
|
#
|
3
3
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
4
4
|
# you may not use this file except in compliance with the License.
|
@@ -78,7 +78,7 @@ describe BSON::Decimal128 do
|
|
78
78
|
|
79
79
|
context 'when a BigDecimal is passed' do
|
80
80
|
|
81
|
-
let(:argument) { BigDecimal
|
81
|
+
let(:argument) { BigDecimal("Infinity") }
|
82
82
|
|
83
83
|
it_behaves_like 'an initialized BSON::Decimal128'
|
84
84
|
end
|
@@ -98,7 +98,7 @@ describe BSON::Decimal128 do
|
|
98
98
|
|
99
99
|
context 'when a BigDecimal is passed' do
|
100
100
|
|
101
|
-
let(:argument) { BigDecimal
|
101
|
+
let(:argument) { BigDecimal("-Infinity") }
|
102
102
|
|
103
103
|
it_behaves_like 'an initialized BSON::Decimal128'
|
104
104
|
end
|
@@ -118,7 +118,7 @@ describe BSON::Decimal128 do
|
|
118
118
|
|
119
119
|
context 'when a BigDecimal is passed' do
|
120
120
|
|
121
|
-
let(:argument) { BigDecimal
|
121
|
+
let(:argument) { BigDecimal("NaN") }
|
122
122
|
|
123
123
|
it_behaves_like 'an initialized BSON::Decimal128'
|
124
124
|
end
|
@@ -177,7 +177,7 @@ describe BSON::Decimal128 do
|
|
177
177
|
|
178
178
|
context 'when a BigDecimal is passed' do
|
179
179
|
|
180
|
-
let(:argument) { BigDecimal
|
180
|
+
let(:argument) { BigDecimal("-0") }
|
181
181
|
|
182
182
|
it_behaves_like 'an initialized BSON::Decimal128'
|
183
183
|
end
|
@@ -197,7 +197,7 @@ describe BSON::Decimal128 do
|
|
197
197
|
|
198
198
|
context 'when a BigDecimal is passed' do
|
199
199
|
|
200
|
-
let(:argument) { BigDecimal
|
200
|
+
let(:argument) { BigDecimal(12) }
|
201
201
|
|
202
202
|
it_behaves_like 'an initialized BSON::Decimal128'
|
203
203
|
end
|
@@ -217,7 +217,7 @@ describe BSON::Decimal128 do
|
|
217
217
|
|
218
218
|
context 'when a BigDecimal is passed' do
|
219
219
|
|
220
|
-
let(:argument) { BigDecimal
|
220
|
+
let(:argument) { BigDecimal(-12) }
|
221
221
|
|
222
222
|
it_behaves_like 'an initialized BSON::Decimal128'
|
223
223
|
end
|
@@ -237,7 +237,7 @@ describe BSON::Decimal128 do
|
|
237
237
|
|
238
238
|
context 'when a BigDecimal is passed' do
|
239
239
|
|
240
|
-
let(:argument) { BigDecimal
|
240
|
+
let(:argument) { BigDecimal(0.12345, 5) }
|
241
241
|
|
242
242
|
it_behaves_like 'an initialized BSON::Decimal128'
|
243
243
|
end
|
@@ -257,7 +257,7 @@ describe BSON::Decimal128 do
|
|
257
257
|
|
258
258
|
context 'when a BigDecimal is passed' do
|
259
259
|
|
260
|
-
let(:argument) { BigDecimal
|
260
|
+
let(:argument) { BigDecimal(-0.12345, 5) }
|
261
261
|
|
262
262
|
it_behaves_like 'an initialized BSON::Decimal128'
|
263
263
|
end
|
@@ -277,7 +277,7 @@ describe BSON::Decimal128 do
|
|
277
277
|
|
278
278
|
context 'when a BigDecimal is passed' do
|
279
279
|
|
280
|
-
let(:argument) { BigDecimal
|
280
|
+
let(:argument) { BigDecimal(1234567890123456789012345678901234) }
|
281
281
|
|
282
282
|
it_behaves_like 'an initialized BSON::Decimal128'
|
283
283
|
end
|
@@ -297,7 +297,7 @@ describe BSON::Decimal128 do
|
|
297
297
|
|
298
298
|
context 'when a BigDecimal is passed' do
|
299
299
|
|
300
|
-
let(:argument) { BigDecimal
|
300
|
+
let(:argument) { BigDecimal(-1234567890123456789012345678901234) }
|
301
301
|
|
302
302
|
it_behaves_like 'an initialized BSON::Decimal128'
|
303
303
|
end
|
@@ -1183,7 +1183,7 @@ describe BSON::Decimal128 do
|
|
1183
1183
|
end
|
1184
1184
|
|
1185
1185
|
it "returns false" do
|
1186
|
-
expect(decimal128).to_not eq(described_class.new(BigDecimal
|
1186
|
+
expect(decimal128).to_not eq(described_class.new(BigDecimal('2.00')))
|
1187
1187
|
end
|
1188
1188
|
end
|
1189
1189
|
|
@@ -1198,7 +1198,7 @@ describe BSON::Decimal128 do
|
|
1198
1198
|
describe "#===" do
|
1199
1199
|
|
1200
1200
|
let(:decimal128) do
|
1201
|
-
described_class.new(BigDecimal
|
1201
|
+
described_class.new(BigDecimal('1.23'))
|
1202
1202
|
end
|
1203
1203
|
|
1204
1204
|
context "when comparing with another decimal128" do
|
@@ -1217,7 +1217,7 @@ describe BSON::Decimal128 do
|
|
1217
1217
|
context "when the high and low bits are not equal" do
|
1218
1218
|
|
1219
1219
|
let(:other) do
|
1220
|
-
described_class.new(BigDecimal
|
1220
|
+
described_class.new(BigDecimal('1000.003'))
|
1221
1221
|
end
|
1222
1222
|
|
1223
1223
|
it "returns false" do
|
@@ -1251,7 +1251,7 @@ describe BSON::Decimal128 do
|
|
1251
1251
|
describe "#as_json" do
|
1252
1252
|
|
1253
1253
|
let(:object) do
|
1254
|
-
described_class.new(BigDecimal
|
1254
|
+
described_class.new(BigDecimal('1.23'))
|
1255
1255
|
end
|
1256
1256
|
|
1257
1257
|
it "returns the decimal128 with $numberDecimal key" do
|
@@ -1271,7 +1271,7 @@ describe BSON::Decimal128 do
|
|
1271
1271
|
describe "#bson_type" do
|
1272
1272
|
|
1273
1273
|
let(:code) do
|
1274
|
-
described_class.new(BigDecimal
|
1274
|
+
described_class.new(BigDecimal('1.23'))
|
1275
1275
|
end
|
1276
1276
|
|
1277
1277
|
it "returns 0x13" do
|
@@ -1311,7 +1311,7 @@ describe BSON::Decimal128 do
|
|
1311
1311
|
end
|
1312
1312
|
|
1313
1313
|
it "returns false" do
|
1314
|
-
expect(decimal128).to_not eql(described_class.new(BigDecimal
|
1314
|
+
expect(decimal128).to_not eql(described_class.new(BigDecimal('2')))
|
1315
1315
|
end
|
1316
1316
|
end
|
1317
1317
|
|
@@ -1326,7 +1326,7 @@ describe BSON::Decimal128 do
|
|
1326
1326
|
describe "#hash" do
|
1327
1327
|
|
1328
1328
|
let(:decimal128) do
|
1329
|
-
described_class.new(BigDecimal
|
1329
|
+
described_class.new(BigDecimal('-1234E+33'))
|
1330
1330
|
end
|
1331
1331
|
|
1332
1332
|
it "returns a hash of the high and low bits" do
|
@@ -1337,7 +1337,7 @@ describe BSON::Decimal128 do
|
|
1337
1337
|
describe "#inspect" do
|
1338
1338
|
|
1339
1339
|
let(:decimal128) do
|
1340
|
-
described_class.new(BigDecimal
|
1340
|
+
described_class.new(BigDecimal('1.23'))
|
1341
1341
|
end
|
1342
1342
|
|
1343
1343
|
it "returns the inspection with the decimal128 to_s" do
|
@@ -1363,7 +1363,7 @@ describe BSON::Decimal128 do
|
|
1363
1363
|
context 'when the value is Infinity' do
|
1364
1364
|
|
1365
1365
|
let(:big_decimal) do
|
1366
|
-
BigDecimal
|
1366
|
+
BigDecimal('Infinity')
|
1367
1367
|
end
|
1368
1368
|
|
1369
1369
|
let(:expected_big_decimal) do
|
@@ -1376,7 +1376,7 @@ describe BSON::Decimal128 do
|
|
1376
1376
|
context 'when the value is -Infinity' do
|
1377
1377
|
|
1378
1378
|
let(:big_decimal) do
|
1379
|
-
BigDecimal
|
1379
|
+
BigDecimal('-Infinity')
|
1380
1380
|
end
|
1381
1381
|
|
1382
1382
|
let(:expected_big_decimal) do
|
@@ -1392,7 +1392,7 @@ describe BSON::Decimal128 do
|
|
1392
1392
|
context 'when the value is 1' do
|
1393
1393
|
|
1394
1394
|
let(:big_decimal) do
|
1395
|
-
BigDecimal
|
1395
|
+
BigDecimal(1)
|
1396
1396
|
end
|
1397
1397
|
|
1398
1398
|
let(:expected_big_decimal) do
|
@@ -1405,7 +1405,7 @@ describe BSON::Decimal128 do
|
|
1405
1405
|
context 'when the value is -1' do
|
1406
1406
|
|
1407
1407
|
let(:big_decimal) do
|
1408
|
-
BigDecimal
|
1408
|
+
BigDecimal(-1)
|
1409
1409
|
end
|
1410
1410
|
|
1411
1411
|
let(:expected_big_decimal) do
|
@@ -1418,7 +1418,7 @@ describe BSON::Decimal128 do
|
|
1418
1418
|
context 'when the value is 20' do
|
1419
1419
|
|
1420
1420
|
let(:big_decimal) do
|
1421
|
-
BigDecimal
|
1421
|
+
BigDecimal(20)
|
1422
1422
|
end
|
1423
1423
|
|
1424
1424
|
let(:expected_big_decimal) do
|
@@ -1431,7 +1431,7 @@ describe BSON::Decimal128 do
|
|
1431
1431
|
context 'when the value is -20' do
|
1432
1432
|
|
1433
1433
|
let(:big_decimal) do
|
1434
|
-
BigDecimal
|
1434
|
+
BigDecimal(-20)
|
1435
1435
|
end
|
1436
1436
|
|
1437
1437
|
let(:expected_big_decimal) do
|
@@ -1444,7 +1444,7 @@ describe BSON::Decimal128 do
|
|
1444
1444
|
context 'when the value is 12345678901234567' do
|
1445
1445
|
|
1446
1446
|
let(:big_decimal) do
|
1447
|
-
BigDecimal
|
1447
|
+
BigDecimal(12345678901234567)
|
1448
1448
|
end
|
1449
1449
|
|
1450
1450
|
let(:expected_big_decimal) do
|
@@ -1457,7 +1457,7 @@ describe BSON::Decimal128 do
|
|
1457
1457
|
context 'when the value is -12345678901234567' do
|
1458
1458
|
|
1459
1459
|
let(:big_decimal) do
|
1460
|
-
BigDecimal
|
1460
|
+
BigDecimal(-12345678901234567)
|
1461
1461
|
end
|
1462
1462
|
|
1463
1463
|
let(:expected_big_decimal) do
|
@@ -1470,7 +1470,7 @@ describe BSON::Decimal128 do
|
|
1470
1470
|
context 'when the value is 12345689012345789012345' do
|
1471
1471
|
|
1472
1472
|
let(:big_decimal) do
|
1473
|
-
BigDecimal
|
1473
|
+
BigDecimal(12345689012345789012345)
|
1474
1474
|
end
|
1475
1475
|
|
1476
1476
|
let(:expected_big_decimal) do
|
@@ -1483,7 +1483,7 @@ describe BSON::Decimal128 do
|
|
1483
1483
|
context 'when the value is -12345689012345789012345' do
|
1484
1484
|
|
1485
1485
|
let(:big_decimal) do
|
1486
|
-
BigDecimal
|
1486
|
+
BigDecimal(-12345689012345789012345)
|
1487
1487
|
end
|
1488
1488
|
|
1489
1489
|
let(:expected_big_decimal) do
|
@@ -1499,7 +1499,7 @@ describe BSON::Decimal128 do
|
|
1499
1499
|
context 'when the value is 0.1' do
|
1500
1500
|
|
1501
1501
|
let(:big_decimal) do
|
1502
|
-
BigDecimal
|
1502
|
+
BigDecimal(0.1, 1)
|
1503
1503
|
end
|
1504
1504
|
|
1505
1505
|
let(:expected_big_decimal) do
|
@@ -1512,7 +1512,7 @@ describe BSON::Decimal128 do
|
|
1512
1512
|
context 'when the value is -0.1' do
|
1513
1513
|
|
1514
1514
|
let(:big_decimal) do
|
1515
|
-
BigDecimal
|
1515
|
+
BigDecimal(-0.1, 1)
|
1516
1516
|
end
|
1517
1517
|
|
1518
1518
|
let(:expected_big_decimal) do
|
@@ -1525,7 +1525,7 @@ describe BSON::Decimal128 do
|
|
1525
1525
|
context 'when the value is 0.123' do
|
1526
1526
|
|
1527
1527
|
let(:big_decimal) do
|
1528
|
-
BigDecimal
|
1528
|
+
BigDecimal(0.123, 3)
|
1529
1529
|
end
|
1530
1530
|
|
1531
1531
|
let(:expected_big_decimal) do
|
@@ -1538,7 +1538,7 @@ describe BSON::Decimal128 do
|
|
1538
1538
|
context 'when the value is -0.123' do
|
1539
1539
|
|
1540
1540
|
let(:big_decimal) do
|
1541
|
-
BigDecimal
|
1541
|
+
BigDecimal(-0.123, 3)
|
1542
1542
|
end
|
1543
1543
|
|
1544
1544
|
let(:expected_big_decimal) do
|
@@ -1552,7 +1552,7 @@ describe BSON::Decimal128 do
|
|
1552
1552
|
context 'when the value has leading zeros' do
|
1553
1553
|
|
1554
1554
|
let(:big_decimal) do
|
1555
|
-
BigDecimal
|
1555
|
+
BigDecimal(0.001234, 4)
|
1556
1556
|
end
|
1557
1557
|
|
1558
1558
|
let(:expected_big_decimal) do
|
@@ -1565,7 +1565,7 @@ describe BSON::Decimal128 do
|
|
1565
1565
|
context 'when the value has trailing zeros' do
|
1566
1566
|
|
1567
1567
|
let(:big_decimal) do
|
1568
|
-
BigDecimal
|
1568
|
+
BigDecimal(2.000, 4)
|
1569
1569
|
end
|
1570
1570
|
|
1571
1571
|
let(:expected_big_decimal) 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
@@ -1,6 +1,6 @@
|
|
1
1
|
# encoding: utf-8
|
2
2
|
|
3
|
-
# Copyright (C) 2009-
|
3
|
+
# Copyright (C) 2009-2020 MongoDB Inc.
|
4
4
|
#
|
5
5
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
6
6
|
# you may not use this file except in compliance with the License.
|
@@ -49,6 +49,68 @@ describe BSON::Document do
|
|
49
49
|
end
|
50
50
|
end
|
51
51
|
|
52
|
+
describe "#fetch" do
|
53
|
+
|
54
|
+
let(:document) do
|
55
|
+
described_class["key", "value", "key2", "value"]
|
56
|
+
end
|
57
|
+
|
58
|
+
context "when provided string keys" do
|
59
|
+
|
60
|
+
it "returns the value" do
|
61
|
+
expect(document.fetch("key")).to eq("value")
|
62
|
+
end
|
63
|
+
end
|
64
|
+
|
65
|
+
context "when provided symbol keys" do
|
66
|
+
|
67
|
+
it "returns the value" do
|
68
|
+
expect(document.fetch(:key)).to eq("value")
|
69
|
+
end
|
70
|
+
end
|
71
|
+
|
72
|
+
context "when key does not exist" do
|
73
|
+
|
74
|
+
it "raises KeyError" do
|
75
|
+
expect do
|
76
|
+
document.fetch(:non_existent_key)
|
77
|
+
end.to raise_exception(KeyError)
|
78
|
+
end
|
79
|
+
|
80
|
+
context "and default value is provided" do
|
81
|
+
|
82
|
+
it "returns default value" do
|
83
|
+
expect(document.fetch(:non_existent_key, false)).to eq(false)
|
84
|
+
end
|
85
|
+
end
|
86
|
+
|
87
|
+
context "and block is passed" do
|
88
|
+
|
89
|
+
it "returns result of the block" do
|
90
|
+
expect(document.fetch(:non_existent_key, &:to_s))
|
91
|
+
.to eq("non_existent_key")
|
92
|
+
end
|
93
|
+
end
|
94
|
+
end
|
95
|
+
|
96
|
+
context "when key exists" do
|
97
|
+
|
98
|
+
context "and default value is provided" do
|
99
|
+
|
100
|
+
it "returns the value" do
|
101
|
+
expect(document.fetch(:key, "other")).to eq("value")
|
102
|
+
end
|
103
|
+
end
|
104
|
+
|
105
|
+
context "and block is passed" do
|
106
|
+
|
107
|
+
it "returns the value" do
|
108
|
+
expect(document.fetch(:key, &:to_s)).to eq("value")
|
109
|
+
end
|
110
|
+
end
|
111
|
+
end
|
112
|
+
end
|
113
|
+
|
52
114
|
describe "#[]" do
|
53
115
|
|
54
116
|
let(:document) do
|
@@ -68,6 +130,13 @@ describe BSON::Document do
|
|
68
130
|
expect(document[:key]).to eq("value")
|
69
131
|
end
|
70
132
|
end
|
133
|
+
|
134
|
+
context "when key does not exist" do
|
135
|
+
|
136
|
+
it "returns nil" do
|
137
|
+
expect(document[:non_existent_key]).to be nil
|
138
|
+
end
|
139
|
+
end
|
71
140
|
end
|
72
141
|
|
73
142
|
describe "#[]=" do
|
@@ -94,8 +163,108 @@ describe BSON::Document do
|
|
94
163
|
it "sets the value" do
|
95
164
|
expect(doc[key]).to eq(val)
|
96
165
|
end
|
166
|
+
|
167
|
+
context 'when value is a hash' do
|
168
|
+
let(:val) do
|
169
|
+
{'foo' => {'bar' => 'baz'}}
|
170
|
+
end
|
171
|
+
|
172
|
+
it 'converts value to indifferent access' do
|
173
|
+
expect(doc[key][:foo][:bar]).to eq('baz')
|
174
|
+
end
|
175
|
+
end
|
176
|
+
|
177
|
+
context 'when value is an array with hash element' do
|
178
|
+
let(:val) do
|
179
|
+
[42, {'foo' => {'bar' => 'baz'}}]
|
180
|
+
end
|
181
|
+
|
182
|
+
it 'converts hash element to indifferent access' do
|
183
|
+
expect(doc[key][1][:foo][:bar]).to eq('baz')
|
184
|
+
end
|
185
|
+
end
|
186
|
+
end
|
187
|
+
|
188
|
+
if described_class.instance_methods.include?(:dig)
|
189
|
+
describe "#dig" do
|
190
|
+
let(:document) do
|
191
|
+
described_class.new("key1" => { :key2 => "value" })
|
192
|
+
end
|
193
|
+
|
194
|
+
context "when provided string keys" do
|
195
|
+
|
196
|
+
it "returns the value" do
|
197
|
+
expect(document.dig("key1", "key2")).to eq("value")
|
198
|
+
end
|
199
|
+
end
|
200
|
+
|
201
|
+
context "when provided symbol keys" do
|
202
|
+
|
203
|
+
it "returns the value" do
|
204
|
+
expect(document.dig(:key1, :key2)).to eq("value")
|
205
|
+
end
|
206
|
+
end
|
207
|
+
end
|
97
208
|
end
|
98
209
|
|
210
|
+
if described_class.instance_methods.include?(:slice)
|
211
|
+
describe "#slice" do
|
212
|
+
let(:document) do
|
213
|
+
described_class.new("key1" => "value1", key2: "value2")
|
214
|
+
end
|
215
|
+
|
216
|
+
context "when provided string keys" do
|
217
|
+
|
218
|
+
it "is a BSON Document" do
|
219
|
+
expect(document.slice("key1")).to be_a(BSON::Document)
|
220
|
+
end
|
221
|
+
|
222
|
+
it "returns the partial document" do
|
223
|
+
expect(document.slice("key1")).to contain_exactly(['key1', 'value1'])
|
224
|
+
end
|
225
|
+
end
|
226
|
+
|
227
|
+
context "when provided symbol keys" do
|
228
|
+
|
229
|
+
it "is a BSON Document" do
|
230
|
+
expect(document.slice(:key1)).to be_a(BSON::Document)
|
231
|
+
end
|
232
|
+
|
233
|
+
it "returns the partial document" do
|
234
|
+
expect(document.slice(:key1)).to contain_exactly(['key1', 'value1'])
|
235
|
+
end
|
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
|
264
|
+
end
|
265
|
+
end
|
266
|
+
|
267
|
+
|
99
268
|
describe "#delete" do
|
100
269
|
|
101
270
|
shared_examples_for "a document with deletable pairs" do
|
@@ -756,6 +925,24 @@ describe BSON::Document do
|
|
756
925
|
end
|
757
926
|
end
|
758
927
|
|
928
|
+
context "when the hash contains an array of hashes" do
|
929
|
+
let(:obj) do
|
930
|
+
described_class["key",[{"a" => 1}, {"b" => 2}]]
|
931
|
+
end
|
932
|
+
|
933
|
+
let(:bson) do
|
934
|
+
"#{45.to_bson}#{Array::BSON_TYPE}key#{BSON::NULL_BYTE}" +
|
935
|
+
"#{35.to_bson}"+
|
936
|
+
"#{BSON::Document::BSON_TYPE}0#{BSON::NULL_BYTE}#{12.to_bson}#{BSON::Int32::BSON_TYPE}a#{BSON::NULL_BYTE}#{1.to_bson}#{BSON::NULL_BYTE}" +
|
937
|
+
"#{BSON::Document::BSON_TYPE}1#{BSON::NULL_BYTE}#{12.to_bson}#{BSON::Int32::BSON_TYPE}b#{BSON::NULL_BYTE}#{2.to_bson}#{BSON::NULL_BYTE}" +
|
938
|
+
"#{BSON::NULL_BYTE}" +
|
939
|
+
"#{BSON::NULL_BYTE}"
|
940
|
+
end
|
941
|
+
|
942
|
+
it_behaves_like "a serializable bson element"
|
943
|
+
it_behaves_like "a deserializable bson element"
|
944
|
+
end
|
945
|
+
|
759
946
|
context "when the hash is a single level" do
|
760
947
|
|
761
948
|
let(:obj) do
|
@@ -867,51 +1054,31 @@ describe BSON::Document do
|
|
867
1054
|
it_behaves_like "a document able to handle utf-8"
|
868
1055
|
end
|
869
1056
|
|
870
|
-
context "
|
1057
|
+
context "given a utf-8-encodable string in another encoding" do
|
871
1058
|
|
872
1059
|
let(:string) { "gültig" }
|
873
1060
|
let(:document) do
|
874
1061
|
described_class["type", string.encode("iso-8859-1")]
|
875
1062
|
end
|
876
1063
|
|
877
|
-
it
|
878
|
-
expect {
|
879
|
-
document.to_bson
|
880
|
-
}.to raise_error(ArgumentError)
|
881
|
-
end
|
882
|
-
|
883
|
-
it 'converts the values', if: BSON::Environment.jruby? do
|
884
|
-
expect(
|
885
|
-
BSON::Document.from_bson(BSON::ByteBuffer.new(document.to_bson.to_s))
|
886
|
-
).to eq({ "type" => string })
|
887
|
-
end
|
888
|
-
end
|
889
|
-
|
890
|
-
context "when binary strings with utf-8 values exist", if: BSON::Environment.jruby? && (JRUBY_VERSION < '9') do
|
891
|
-
|
892
|
-
let(:string) { "europäisch" }
|
893
|
-
let(:document) do
|
894
|
-
described_class["type", string.encode("binary")]
|
895
|
-
end
|
896
|
-
|
897
|
-
it "encodes and decodes the document properly" do
|
1064
|
+
it 'converts the values to utf-8' do
|
898
1065
|
expect(
|
899
1066
|
BSON::Document.from_bson(BSON::ByteBuffer.new(document.to_bson.to_s))
|
900
1067
|
).to eq({ "type" => string })
|
901
1068
|
end
|
902
1069
|
end
|
903
1070
|
|
904
|
-
context "
|
1071
|
+
context "given a binary string with utf-8 values" do
|
905
1072
|
|
906
|
-
let(:string) { "europäisch" }
|
1073
|
+
let(:string) { "europäisch".force_encoding('binary') }
|
907
1074
|
let(:document) do
|
908
|
-
described_class["type", string
|
1075
|
+
described_class["type", string]
|
909
1076
|
end
|
910
1077
|
|
911
|
-
it "
|
912
|
-
expect
|
913
|
-
|
914
|
-
|
1078
|
+
it "raises encoding error" do
|
1079
|
+
expect do
|
1080
|
+
document.to_bson
|
1081
|
+
end.to raise_error(Encoding::UndefinedConversionError, /from ASCII-8BIT to UTF-8/)
|
915
1082
|
end
|
916
1083
|
end
|
917
1084
|
end
|