bson 4.7.1 → 4.9.1
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.tar.gz.sig +2 -1
- data/README.md +6 -2
- data/ext/bson/bson-endian.h +1 -1
- data/ext/bson/bson-native.h +15 -5
- data/ext/bson/bytebuf.c +1 -1
- data/ext/bson/endian.c +1 -1
- data/ext/bson/init.c +26 -3
- data/ext/bson/read.c +127 -33
- data/ext/bson/util.c +41 -1
- data/ext/bson/write.c +18 -14
- data/lib/bson.rb +4 -1
- data/lib/bson/active_support.rb +1 -1
- data/lib/bson/array.rb +34 -5
- data/lib/bson/binary.rb +42 -4
- 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 +1 -1
- data/lib/bson/date_time.rb +1 -1
- data/lib/bson/db_pointer.rb +110 -0
- data/lib/bson/decimal128.rb +16 -2
- data/lib/bson/decimal128/builder.rb +1 -1
- data/lib/bson/document.rb +1 -1
- data/lib/bson/environment.rb +2 -1
- data/lib/bson/error.rb +21 -0
- data/lib/bson/ext_json.rb +375 -0
- data/lib/bson/false_class.rb +1 -1
- data/lib/bson/float.rb +48 -2
- data/lib/bson/hash.rb +36 -5
- data/lib/bson/int32.rb +22 -2
- data/lib/bson/int64.rb +29 -4
- data/lib/bson/integer.rb +35 -1
- 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 +20 -3
- data/lib/bson/registry.rb +1 -1
- data/lib/bson/specialized.rb +4 -2
- data/lib/bson/string.rb +4 -2
- data/lib/bson/symbol.rb +93 -4
- data/lib/bson/time.rb +63 -4
- data/lib/bson/time_with_zone.rb +1 -1
- data/lib/bson/timestamp.rb +16 -2
- 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 +1 -1
- data/spec/bson/binary_spec.rb +34 -4
- data/spec/bson/binary_uuid_spec.rb +1 -1
- data/spec/bson/boolean_spec.rb +1 -1
- 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 +1 -1
- data/spec/bson/decimal128_spec.rb +1 -1
- data/spec/bson/document_spec.rb +1 -1
- 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_spec.rb +71 -1
- data/spec/bson/int32_spec.rb +21 -1
- data/spec/bson/int64_spec.rb +39 -1
- data/spec/bson/integer_spec.rb +27 -1
- 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 +22 -1
- data/spec/bson/regexp_spec.rb +1 -1
- data/spec/bson/registry_spec.rb +1 -1
- data/spec/bson/string_spec.rb +1 -1
- data/spec/bson/symbol_raw_spec.rb +45 -0
- data/spec/bson/symbol_spec.rb +61 -1
- data/spec/bson/time_spec.rb +205 -2
- data/spec/bson/time_with_zone_spec.rb +1 -1
- data/spec/bson/timestamp_spec.rb +1 -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 +10 -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} +4 -4
- 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 +85 -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 +24 -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 +0 -0
- 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 +1 -1
- metadata +176 -102
- metadata.gz.sig +0 -0
data/spec/bson/int32_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.
|
@@ -253,4 +253,24 @@ describe BSON::Int32 do
|
|
253
253
|
expect(obj.value).to eq(12345)
|
254
254
|
end
|
255
255
|
end
|
256
|
+
|
257
|
+
describe '#as_extended_json' do
|
258
|
+
context 'canonical mode' do
|
259
|
+
it 'returns $numberInt' do
|
260
|
+
described_class.new(42).as_extended_json.should == {'$numberInt' => '42'}
|
261
|
+
end
|
262
|
+
end
|
263
|
+
|
264
|
+
context 'relaxed mode' do
|
265
|
+
it 'returns integer' do
|
266
|
+
described_class.new(42).as_extended_json(mode: :relaxed).should == 42
|
267
|
+
end
|
268
|
+
end
|
269
|
+
|
270
|
+
context 'legacy mode' do
|
271
|
+
it 'returns integer' do
|
272
|
+
described_class.new(42).as_extended_json(mode: :legacy).should be 42
|
273
|
+
end
|
274
|
+
end
|
275
|
+
end
|
256
276
|
end
|
data/spec/bson/int64_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.
|
@@ -71,6 +71,25 @@ describe BSON::Int64 do
|
|
71
71
|
it_behaves_like "a bson element"
|
72
72
|
it_behaves_like "a deserializable bson element"
|
73
73
|
|
74
|
+
context 'canonical deserialization' do
|
75
|
+
let(:integer) { 42 }
|
76
|
+
|
77
|
+
let(:bson) do
|
78
|
+
BSON::ByteBuffer.new(BSON::Int64.new(integer).to_bson.to_s)
|
79
|
+
end
|
80
|
+
|
81
|
+
let(:deserialized) do
|
82
|
+
described_class.from_bson(bson, mode: :bson)
|
83
|
+
end
|
84
|
+
|
85
|
+
it 'deserializes to BSON::Int64' do
|
86
|
+
deserialized.class.should be BSON::Int64
|
87
|
+
end
|
88
|
+
|
89
|
+
it 'has the correct value' do
|
90
|
+
deserialized.value.should == 42
|
91
|
+
end
|
92
|
+
end
|
74
93
|
|
75
94
|
context "when the integer is within the MRI Fixnum range" do
|
76
95
|
|
@@ -299,4 +318,23 @@ describe BSON::Int64 do
|
|
299
318
|
end
|
300
319
|
end
|
301
320
|
|
321
|
+
describe '#as_extended_json' do
|
322
|
+
context 'canonical mode' do
|
323
|
+
it 'returns $numberLong' do
|
324
|
+
described_class.new(42).as_extended_json.should == {'$numberLong' => '42'}
|
325
|
+
end
|
326
|
+
end
|
327
|
+
|
328
|
+
context 'relaxed mode' do
|
329
|
+
it 'returns integer' do
|
330
|
+
described_class.new(42).as_extended_json(mode: :relaxed).should == 42
|
331
|
+
end
|
332
|
+
end
|
333
|
+
|
334
|
+
context 'legacy mode' do
|
335
|
+
it 'returns integer' do
|
336
|
+
described_class.new(42).as_extended_json(mode: :legacy).should be 42
|
337
|
+
end
|
338
|
+
end
|
339
|
+
end
|
302
340
|
end
|
data/spec/bson/integer_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.
|
@@ -68,4 +68,30 @@ describe Integer do
|
|
68
68
|
expect(obj.to_bson_key).to eq(encoded)
|
69
69
|
end
|
70
70
|
end
|
71
|
+
|
72
|
+
describe '#to_json' do
|
73
|
+
it 'returns integer' do
|
74
|
+
42.to_json.should == '42'
|
75
|
+
end
|
76
|
+
end
|
77
|
+
|
78
|
+
describe '#as_extended_json' do
|
79
|
+
context 'canonical mode' do
|
80
|
+
it 'returns $numberInt' do
|
81
|
+
42.as_extended_json.should == {'$numberInt' => '42'}
|
82
|
+
end
|
83
|
+
end
|
84
|
+
|
85
|
+
context 'relaxed mode' do
|
86
|
+
it 'returns integer' do
|
87
|
+
42.as_extended_json(mode: :relaxed).should be 42
|
88
|
+
end
|
89
|
+
end
|
90
|
+
|
91
|
+
context 'legacy mode' do
|
92
|
+
it 'returns integer' do
|
93
|
+
42.as_extended_json(mode: :legacy).should be 42
|
94
|
+
end
|
95
|
+
end
|
96
|
+
end
|
71
97
|
end
|
data/spec/bson/json_spec.rb
CHANGED
data/spec/bson/max_key_spec.rb
CHANGED
data/spec/bson/min_key_spec.rb
CHANGED
data/spec/bson/nil_class_spec.rb
CHANGED
data/spec/bson/object_id_spec.rb
CHANGED
data/spec/bson/object_spec.rb
CHANGED
data/spec/bson/raw_spec.rb
CHANGED
@@ -12,13 +12,34 @@ describe Regexp::Raw do
|
|
12
12
|
described_class.new(pattern, 'im')
|
13
13
|
end
|
14
14
|
|
15
|
-
it "returns the regex pattern and options" do
|
15
|
+
it "returns the legacy serialization including regex pattern and options" do
|
16
16
|
expect(object.as_json).to eq({ "$regex" => "\\W+", "$options" => "im" })
|
17
17
|
end
|
18
18
|
|
19
19
|
it_behaves_like "a JSON serializable object"
|
20
20
|
end
|
21
21
|
|
22
|
+
describe '#as_extended_json' do
|
23
|
+
|
24
|
+
let(:object) do
|
25
|
+
described_class.new(pattern, 'im')
|
26
|
+
end
|
27
|
+
|
28
|
+
context 'legacy mode' do
|
29
|
+
it "returns the legacy serialization including regex pattern and options" do
|
30
|
+
expect(object.as_extended_json(mode: :legacy)).to eq({ "$regex" => "\\W+", "$options" => "im" })
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
context 'canonical/relaxed mode' do
|
35
|
+
it "returns the extended json 2.0 serialization" do
|
36
|
+
expect(object.as_extended_json).to eq(
|
37
|
+
"$regularExpression" => {'pattern' => "\\W+", "options" => "im"}
|
38
|
+
)
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
22
43
|
describe "#to_bson/#from_bson" do
|
23
44
|
|
24
45
|
let(:options) { 'ilmsux' }
|
data/spec/bson/regexp_spec.rb
CHANGED
data/spec/bson/registry_spec.rb
CHANGED
data/spec/bson/string_spec.rb
CHANGED
@@ -0,0 +1,45 @@
|
|
1
|
+
# Copyright (C) 2020 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 BSON::Symbol::Raw do
|
18
|
+
describe '#==' do
|
19
|
+
let(:one) { described_class.new('foo') }
|
20
|
+
let(:two) { described_class.new('foo') }
|
21
|
+
let(:three) { described_class.new('bar') }
|
22
|
+
|
23
|
+
it 'compares equal' do
|
24
|
+
one.should == two
|
25
|
+
end
|
26
|
+
|
27
|
+
it 'compares not equal' do
|
28
|
+
one.should_not == three
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
describe '#eql?' do
|
33
|
+
let(:one) { described_class.new('foo') }
|
34
|
+
let(:two) { described_class.new('foo') }
|
35
|
+
let(:three) { described_class.new('bar') }
|
36
|
+
|
37
|
+
it 'compares equal' do
|
38
|
+
one.should be_eql(two)
|
39
|
+
end
|
40
|
+
|
41
|
+
it 'compares not equal' do
|
42
|
+
one.should_not be_eql(three)
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
data/spec/bson/symbol_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.
|
@@ -32,6 +32,66 @@ describe Symbol do
|
|
32
32
|
it_behaves_like "a bson element"
|
33
33
|
it_behaves_like "a serializable bson element"
|
34
34
|
it_behaves_like "a deserializable bson element"
|
35
|
+
|
36
|
+
context 'canonical deserialization' do
|
37
|
+
let(:bson) do
|
38
|
+
BSON::ByteBuffer.new(BSON::Symbol::Raw.new(obj).to_bson.to_s)
|
39
|
+
end
|
40
|
+
|
41
|
+
let(:deserialized) do
|
42
|
+
described_class.from_bson(bson, mode: :bson)
|
43
|
+
end
|
44
|
+
|
45
|
+
it 'deserializes to BSON::Symbol::Raw' do
|
46
|
+
deserialized.class.should be BSON::Symbol::Raw
|
47
|
+
end
|
48
|
+
|
49
|
+
it 'has the correct value' do
|
50
|
+
deserialized.to_sym.should be obj
|
51
|
+
end
|
52
|
+
end
|
53
|
+
|
54
|
+
context 'when changing bson_type' do
|
55
|
+
def perform_test(bson_type_to_use)
|
56
|
+
Symbol.class_eval do
|
57
|
+
alias_method :bson_type_orig, :bson_type
|
58
|
+
define_method(:bson_type) do
|
59
|
+
bson_type_to_use
|
60
|
+
end
|
61
|
+
end
|
62
|
+
|
63
|
+
begin
|
64
|
+
yield
|
65
|
+
ensure
|
66
|
+
Symbol.class_eval do
|
67
|
+
alias_method :bson_type, :bson_type_orig
|
68
|
+
remove_method :bson_type_orig
|
69
|
+
end
|
70
|
+
end
|
71
|
+
end
|
72
|
+
|
73
|
+
let(:value) { :foo }
|
74
|
+
|
75
|
+
let(:serialized) do
|
76
|
+
value.to_bson.to_s
|
77
|
+
end
|
78
|
+
|
79
|
+
context 'when bson_type is set to symbol' do
|
80
|
+
it 'serializes to BSON string' do
|
81
|
+
perform_test(BSON::Symbol::BSON_TYPE) do
|
82
|
+
serialized
|
83
|
+
end.should == "\x04\x00\x00\x00foo\x00".force_encoding('binary')
|
84
|
+
end
|
85
|
+
end
|
86
|
+
|
87
|
+
context 'when bson_type is set to string' do
|
88
|
+
it 'serializes to BSON string' do
|
89
|
+
perform_test(BSON::String::BSON_TYPE) do
|
90
|
+
serialized
|
91
|
+
end.should == "\x04\x00\x00\x00foo\x00".force_encoding('binary')
|
92
|
+
end
|
93
|
+
end
|
94
|
+
end
|
35
95
|
end
|
36
96
|
|
37
97
|
describe "#to_bson_key" do
|
data/spec/bson/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.
|
@@ -43,7 +43,7 @@ describe Time do
|
|
43
43
|
end
|
44
44
|
end
|
45
45
|
|
46
|
-
context "when the time
|
46
|
+
context "when the time precedes epoch" do
|
47
47
|
|
48
48
|
let(:obj) { Time.utc(1969, 1, 1, 0, 0, 0) }
|
49
49
|
let(:bson) { [ (obj.to_f * 1000).to_i ].pack(BSON::Int64::PACK) }
|
@@ -51,5 +51,208 @@ describe Time do
|
|
51
51
|
it_behaves_like "a serializable bson element"
|
52
52
|
it_behaves_like "a deserializable bson element"
|
53
53
|
end
|
54
|
+
|
55
|
+
context 'when value has sub-millisecond precision' do
|
56
|
+
let(:obj) { Time.utc(2012, 1, 1, 0, 0, 0, 999_999) }
|
57
|
+
|
58
|
+
let(:expected_round_tripped_obj) do
|
59
|
+
Time.utc(2012, 1, 1, 0, 0, 0, 999_000)
|
60
|
+
end
|
61
|
+
|
62
|
+
let(:round_tripped_obj) do
|
63
|
+
Time.from_bson(obj.to_bson)
|
64
|
+
end
|
65
|
+
|
66
|
+
it 'truncates to milliseconds when round-tripping' do
|
67
|
+
round_tripped_obj.should == expected_round_tripped_obj
|
68
|
+
end
|
69
|
+
end
|
70
|
+
end
|
71
|
+
|
72
|
+
describe '#as_extended_json' do
|
73
|
+
|
74
|
+
context 'canonical mode' do
|
75
|
+
context 'when value has sub-millisecond precision' do
|
76
|
+
let(:obj) { Time.utc(2012, 1, 1, 0, 0, 0, 999_999) }
|
77
|
+
|
78
|
+
let(:expected_serialization) do
|
79
|
+
{'$date' => {'$numberLong' => '1325376000999'}}
|
80
|
+
end
|
81
|
+
|
82
|
+
let(:serialization) do
|
83
|
+
obj.as_extended_json
|
84
|
+
end
|
85
|
+
|
86
|
+
shared_examples_for 'truncates to milliseconds when serializing' do
|
87
|
+
it 'truncates to milliseconds when serializing' do
|
88
|
+
serialization.should == expected_serialization
|
89
|
+
end
|
90
|
+
end
|
91
|
+
|
92
|
+
it_behaves_like 'truncates to milliseconds when serializing'
|
93
|
+
|
94
|
+
context 'when value has sub-microsecond precision' do
|
95
|
+
let(:obj) { Time.utc(2012, 1, 1, 0, 0, 0, 999_999_999/1000r) }
|
96
|
+
|
97
|
+
it_behaves_like 'truncates to milliseconds when serializing'
|
98
|
+
end
|
99
|
+
|
100
|
+
context "when the time precedes epoch" do
|
101
|
+
let(:obj) { Time.utc(1960, 1, 1, 0, 0, 0, 999_999) }
|
102
|
+
|
103
|
+
let(:expected_serialization) do
|
104
|
+
{'$date' => {'$numberLong' => '-315619199001'}}
|
105
|
+
end
|
106
|
+
|
107
|
+
it_behaves_like 'truncates to milliseconds when serializing'
|
108
|
+
end
|
109
|
+
end
|
110
|
+
end
|
111
|
+
|
112
|
+
context 'relaxed mode' do
|
113
|
+
context 'when value has sub-millisecond precision' do
|
114
|
+
let(:obj) { Time.utc(2012, 1, 1, 0, 0, 0, 999_999) }
|
115
|
+
|
116
|
+
let(:expected_serialization) do
|
117
|
+
{'$date' => '2012-01-01T00:00:00.999Z'}
|
118
|
+
end
|
119
|
+
|
120
|
+
let(:serialization) do
|
121
|
+
obj.as_extended_json(mode: :relaxed)
|
122
|
+
end
|
123
|
+
|
124
|
+
shared_examples_for 'truncates to milliseconds when serializing' do
|
125
|
+
it 'truncates to milliseconds when serializing' do
|
126
|
+
serialization.should == expected_serialization
|
127
|
+
end
|
128
|
+
end
|
129
|
+
|
130
|
+
it_behaves_like 'truncates to milliseconds when serializing'
|
131
|
+
|
132
|
+
context 'when value has sub-microsecond precision' do
|
133
|
+
let(:obj) { Time.utc(2012, 1, 1, 0, 0, 0, 999_999_999/1000r) }
|
134
|
+
|
135
|
+
it_behaves_like 'truncates to milliseconds when serializing'
|
136
|
+
end
|
137
|
+
|
138
|
+
context "when the time precedes epoch" do
|
139
|
+
let(:obj) { Time.utc(1960, 1, 1, 0, 0, 0, 999_999) }
|
140
|
+
|
141
|
+
let(:expected_serialization) do
|
142
|
+
{'$date' => {'$numberLong' => '-315619199001'}}
|
143
|
+
end
|
144
|
+
|
145
|
+
it_behaves_like 'truncates to milliseconds when serializing'
|
146
|
+
end
|
147
|
+
end
|
148
|
+
end
|
149
|
+
end
|
150
|
+
|
151
|
+
describe '#to_extended_json' do
|
152
|
+
|
153
|
+
context 'canonical mode' do
|
154
|
+
context 'when value has sub-millisecond precision' do
|
155
|
+
let(:obj) { Time.utc(2012, 1, 1, 0, 0, 0, 999_999) }
|
156
|
+
|
157
|
+
let(:expected_serialization) do
|
158
|
+
%q`{"$date":{"$numberLong":"1325376000999"}}`
|
159
|
+
end
|
160
|
+
|
161
|
+
let(:serialization) do
|
162
|
+
obj.to_extended_json
|
163
|
+
end
|
164
|
+
|
165
|
+
shared_examples_for 'truncates to milliseconds when serializing' do
|
166
|
+
it 'truncates to milliseconds when serializing' do
|
167
|
+
serialization.should == expected_serialization
|
168
|
+
end
|
169
|
+
end
|
170
|
+
|
171
|
+
it_behaves_like 'truncates to milliseconds when serializing'
|
172
|
+
|
173
|
+
context 'when value has sub-microsecond precision' do
|
174
|
+
let(:obj) { Time.utc(2012, 1, 1, 0, 0, 0, 999_999_999/1000r) }
|
175
|
+
|
176
|
+
it_behaves_like 'truncates to milliseconds when serializing'
|
177
|
+
end
|
178
|
+
|
179
|
+
context "when the time precedes epoch" do
|
180
|
+
let(:obj) { Time.utc(1960, 1, 1, 0, 0, 0, 999_999) }
|
181
|
+
|
182
|
+
let(:expected_serialization) do
|
183
|
+
%q`{"$date":{"$numberLong":"-315619199001"}}`
|
184
|
+
end
|
185
|
+
|
186
|
+
it_behaves_like 'truncates to milliseconds when serializing'
|
187
|
+
end
|
188
|
+
end
|
189
|
+
end
|
190
|
+
|
191
|
+
context 'relaxed mode' do
|
192
|
+
context 'when value has sub-millisecond precision' do
|
193
|
+
let(:obj) { Time.utc(2012, 1, 1, 0, 0, 0, 999_999) }
|
194
|
+
|
195
|
+
let(:expected_serialization) do
|
196
|
+
%q`{"$date":"2012-01-01T00:00:00.999Z"}`
|
197
|
+
end
|
198
|
+
|
199
|
+
let(:serialization) do
|
200
|
+
obj.to_extended_json(mode: :relaxed)
|
201
|
+
end
|
202
|
+
|
203
|
+
shared_examples_for 'truncates to milliseconds when serializing' do
|
204
|
+
it 'truncates to milliseconds when serializing' do
|
205
|
+
serialization.should == expected_serialization
|
206
|
+
end
|
207
|
+
end
|
208
|
+
|
209
|
+
it_behaves_like 'truncates to milliseconds when serializing'
|
210
|
+
|
211
|
+
context 'when value has sub-microsecond precision' do
|
212
|
+
let(:obj) { Time.utc(2012, 1, 1, 0, 0, 0, 999_999_999/1000r) }
|
213
|
+
|
214
|
+
it_behaves_like 'truncates to milliseconds when serializing'
|
215
|
+
end
|
216
|
+
end
|
217
|
+
end
|
218
|
+
end
|
219
|
+
|
220
|
+
describe '#to_json' do
|
221
|
+
|
222
|
+
context 'when value has sub-millisecond precision' do
|
223
|
+
let(:obj) { Time.utc(2012, 1, 1, 0, 0, 0, 999_999) }
|
224
|
+
|
225
|
+
let(:expected_serialization) do
|
226
|
+
%q`"2012-01-01 00:00:00 UTC"`
|
227
|
+
end
|
228
|
+
|
229
|
+
let(:serialization) do
|
230
|
+
obj.to_json
|
231
|
+
end
|
232
|
+
|
233
|
+
shared_examples_for 'truncates to milliseconds when serializing' do
|
234
|
+
it 'truncates to milliseconds when serializing' do
|
235
|
+
serialization.should == expected_serialization
|
236
|
+
end
|
237
|
+
end
|
238
|
+
|
239
|
+
it_behaves_like 'truncates to milliseconds when serializing'
|
240
|
+
|
241
|
+
context 'when value has sub-microsecond precision' do
|
242
|
+
let(:obj) { Time.utc(2012, 1, 1, 0, 0, 0, 999_999_999/1000r) }
|
243
|
+
|
244
|
+
it_behaves_like 'truncates to milliseconds when serializing'
|
245
|
+
end
|
246
|
+
|
247
|
+
context "when the time precedes epoch" do
|
248
|
+
let(:obj) { Time.utc(1960, 1, 1, 0, 0, 0, 999_999) }
|
249
|
+
|
250
|
+
let(:expected_serialization) do
|
251
|
+
%q`"1960-01-01 00:00:00 UTC"`
|
252
|
+
end
|
253
|
+
|
254
|
+
it_behaves_like 'truncates to milliseconds when serializing'
|
255
|
+
end
|
256
|
+
end
|
54
257
|
end
|
55
258
|
end
|