bson 4.2.2 → 4.12.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (169) hide show
  1. checksums.yaml +5 -5
  2. checksums.yaml.gz.sig +0 -0
  3. data.tar.gz.sig +0 -0
  4. data/README.md +25 -7
  5. data/Rakefile +16 -9
  6. data/ext/bson/{native-endian.h → bson-endian.h} +5 -99
  7. data/ext/bson/bson-native.h +125 -0
  8. data/ext/bson/bytebuf.c +133 -0
  9. data/ext/bson/endian.c +117 -0
  10. data/ext/bson/init.c +355 -0
  11. data/ext/bson/libbson-utf8.c +230 -0
  12. data/ext/bson/read.c +411 -0
  13. data/ext/bson/util.c +95 -0
  14. data/ext/bson/write.c +680 -0
  15. data/lib/bson.rb +6 -3
  16. data/lib/bson/active_support.rb +17 -0
  17. data/lib/bson/array.rb +57 -17
  18. data/lib/bson/binary.rb +185 -13
  19. data/lib/bson/boolean.rb +12 -3
  20. data/lib/bson/code.rb +16 -2
  21. data/lib/bson/code_with_scope.rb +32 -5
  22. data/lib/bson/config.rb +1 -1
  23. data/lib/bson/date.rb +12 -2
  24. data/lib/bson/date_time.rb +2 -2
  25. data/lib/bson/db_pointer.rb +110 -0
  26. data/lib/bson/decimal128.rb +17 -3
  27. data/lib/bson/decimal128/builder.rb +1 -1
  28. data/lib/bson/document.rb +152 -5
  29. data/lib/bson/environment.rb +2 -1
  30. data/lib/bson/error.rb +27 -0
  31. data/lib/bson/ext_json.rb +383 -0
  32. data/lib/bson/false_class.rb +1 -1
  33. data/lib/bson/float.rb +48 -2
  34. data/lib/bson/hash.rb +68 -17
  35. data/lib/bson/int32.rb +52 -13
  36. data/lib/bson/int64.rb +59 -15
  37. data/lib/bson/integer.rb +36 -2
  38. data/lib/bson/json.rb +1 -1
  39. data/lib/bson/max_key.rb +13 -1
  40. data/lib/bson/min_key.rb +13 -1
  41. data/lib/bson/nil_class.rb +4 -2
  42. data/lib/bson/object.rb +28 -1
  43. data/lib/bson/object_id.rb +16 -2
  44. data/lib/bson/open_struct.rb +1 -1
  45. data/lib/bson/regexp.rb +27 -4
  46. data/lib/bson/registry.rb +3 -3
  47. data/lib/bson/specialized.rb +4 -2
  48. data/lib/bson/string.rb +5 -3
  49. data/lib/bson/symbol.rb +99 -7
  50. data/lib/bson/time.rb +63 -4
  51. data/lib/bson/time_with_zone.rb +54 -0
  52. data/lib/bson/timestamp.rb +44 -6
  53. data/lib/bson/true_class.rb +1 -1
  54. data/lib/bson/undefined.rb +12 -1
  55. data/lib/bson/version.rb +2 -2
  56. data/spec/bson/array_spec.rb +18 -1
  57. data/spec/bson/binary_spec.rb +100 -3
  58. data/spec/bson/binary_uuid_spec.rb +189 -0
  59. data/spec/bson/boolean_spec.rb +1 -1
  60. data/spec/bson/byte_buffer_read_spec.rb +197 -0
  61. data/spec/bson/byte_buffer_spec.rb +121 -381
  62. data/spec/bson/byte_buffer_write_spec.rb +854 -0
  63. data/spec/bson/code_spec.rb +1 -1
  64. data/spec/bson/code_with_scope_spec.rb +1 -1
  65. data/spec/bson/date_spec.rb +1 -1
  66. data/spec/bson/date_time_spec.rb +54 -1
  67. data/spec/bson/decimal128_spec.rb +35 -35
  68. data/spec/bson/document_as_spec.rb +46 -0
  69. data/spec/bson/document_spec.rb +197 -30
  70. data/spec/bson/ext_json_parse_spec.rb +308 -0
  71. data/spec/bson/false_class_spec.rb +1 -1
  72. data/spec/bson/float_spec.rb +37 -1
  73. data/spec/bson/hash_as_spec.rb +57 -0
  74. data/spec/bson/hash_spec.rb +209 -1
  75. data/spec/bson/int32_spec.rb +180 -6
  76. data/spec/bson/int64_spec.rb +199 -6
  77. data/spec/bson/integer_spec.rb +29 -3
  78. data/spec/bson/json_spec.rb +1 -1
  79. data/spec/bson/max_key_spec.rb +1 -1
  80. data/spec/bson/min_key_spec.rb +1 -1
  81. data/spec/bson/nil_class_spec.rb +1 -1
  82. data/spec/bson/object_id_spec.rb +1 -1
  83. data/spec/bson/object_spec.rb +1 -1
  84. data/spec/bson/open_struct_spec.rb +1 -1
  85. data/spec/bson/raw_spec.rb +34 -2
  86. data/spec/bson/regexp_spec.rb +1 -1
  87. data/spec/bson/registry_spec.rb +1 -1
  88. data/spec/bson/string_spec.rb +19 -1
  89. data/spec/bson/symbol_raw_spec.rb +45 -0
  90. data/spec/bson/symbol_spec.rb +63 -3
  91. data/spec/bson/time_spec.rb +205 -2
  92. data/spec/bson/time_with_zone_spec.rb +68 -0
  93. data/spec/bson/timestamp_spec.rb +56 -1
  94. data/spec/bson/true_class_spec.rb +1 -1
  95. data/spec/bson/undefined_spec.rb +1 -1
  96. data/spec/bson_spec.rb +1 -1
  97. data/spec/{support → runners}/common_driver.rb +1 -1
  98. data/spec/runners/corpus.rb +185 -0
  99. data/spec/{support/corpus.rb → runners/corpus_legacy.rb} +41 -59
  100. data/spec/spec_helper.rb +40 -3
  101. data/spec/{bson/driver_bson_spec.rb → spec_tests/common_driver_spec.rb} +1 -0
  102. data/spec/{bson/corpus_spec.rb → spec_tests/corpus_legacy_spec.rb} +10 -7
  103. data/spec/spec_tests/corpus_spec.rb +124 -0
  104. data/spec/spec_tests/data/corpus/README.md +15 -0
  105. data/spec/spec_tests/data/corpus/array.json +49 -0
  106. data/spec/spec_tests/data/corpus/binary.json +113 -0
  107. data/spec/spec_tests/data/corpus/boolean.json +27 -0
  108. data/spec/spec_tests/data/corpus/code.json +67 -0
  109. data/spec/spec_tests/data/corpus/code_w_scope.json +78 -0
  110. data/spec/spec_tests/data/corpus/datetime.json +42 -0
  111. data/spec/spec_tests/data/corpus/dbpointer.json +56 -0
  112. data/spec/spec_tests/data/corpus/dbref.json +31 -0
  113. data/spec/spec_tests/data/corpus/decimal128-1.json +317 -0
  114. data/spec/spec_tests/data/corpus/decimal128-2.json +793 -0
  115. data/spec/spec_tests/data/corpus/decimal128-3.json +1771 -0
  116. data/spec/spec_tests/data/corpus/decimal128-4.json +117 -0
  117. data/spec/spec_tests/data/corpus/decimal128-5.json +402 -0
  118. data/spec/spec_tests/data/corpus/decimal128-6.json +119 -0
  119. data/spec/spec_tests/data/corpus/decimal128-7.json +323 -0
  120. data/spec/spec_tests/data/corpus/document.json +36 -0
  121. data/spec/spec_tests/data/corpus/double.json +87 -0
  122. data/spec/spec_tests/data/corpus/int32.json +43 -0
  123. data/spec/spec_tests/data/corpus/int64.json +43 -0
  124. data/spec/spec_tests/data/corpus/maxkey.json +12 -0
  125. data/spec/spec_tests/data/corpus/minkey.json +12 -0
  126. data/spec/spec_tests/data/corpus/multi-type-deprecated.json +15 -0
  127. data/spec/spec_tests/data/corpus/multi-type.json +11 -0
  128. data/spec/spec_tests/data/corpus/null.json +12 -0
  129. data/spec/spec_tests/data/corpus/oid.json +28 -0
  130. data/spec/spec_tests/data/corpus/regex.json +65 -0
  131. data/spec/spec_tests/data/corpus/string.json +72 -0
  132. data/spec/spec_tests/data/corpus/symbol.json +80 -0
  133. data/spec/spec_tests/data/corpus/timestamp.json +34 -0
  134. data/spec/spec_tests/data/corpus/top.json +236 -0
  135. data/spec/spec_tests/data/corpus/undefined.json +15 -0
  136. data/spec/{support/corpus-tests → spec_tests/data/corpus_legacy}/array.json +8 -2
  137. data/spec/{support/corpus-tests/failures → spec_tests/data/corpus_legacy}/binary.json +0 -0
  138. data/spec/{support/corpus-tests → spec_tests/data/corpus_legacy}/boolean.json +0 -0
  139. data/spec/{support/corpus-tests → spec_tests/data/corpus_legacy}/code.json +1 -1
  140. data/spec/{support/corpus-tests → spec_tests/data/corpus_legacy}/code_w_scope.json +1 -1
  141. data/spec/{support/corpus-tests → spec_tests/data/corpus_legacy}/document.json +1 -1
  142. data/spec/{support/corpus-tests → spec_tests/data/corpus_legacy}/double.json +1 -1
  143. data/spec/{support/corpus-tests → spec_tests/data/corpus_legacy}/failures/datetime.json +0 -0
  144. data/spec/{support/corpus-tests → spec_tests/data/corpus_legacy}/failures/dbpointer.json +0 -0
  145. data/spec/{support/corpus-tests → spec_tests/data/corpus_legacy}/failures/int64.json +0 -0
  146. data/spec/{support/corpus-tests → spec_tests/data/corpus_legacy}/failures/symbol.json +0 -0
  147. data/spec/{support/corpus-tests → spec_tests/data/corpus_legacy}/int32.json +1 -1
  148. data/spec/{support/corpus-tests → spec_tests/data/corpus_legacy}/maxkey.json +1 -1
  149. data/spec/{support/corpus-tests → spec_tests/data/corpus_legacy}/minkey.json +1 -1
  150. data/spec/{support/corpus-tests → spec_tests/data/corpus_legacy}/null.json +1 -1
  151. data/spec/{support/corpus-tests → spec_tests/data/corpus_legacy}/oid.json +0 -0
  152. data/spec/{support/corpus-tests → spec_tests/data/corpus_legacy}/regex.json +1 -1
  153. data/spec/{support/corpus-tests → spec_tests/data/corpus_legacy}/string.json +0 -0
  154. data/spec/{support/corpus-tests → spec_tests/data/corpus_legacy}/timestamp.json +1 -1
  155. data/spec/{support/corpus-tests → spec_tests/data/corpus_legacy}/top.json +0 -0
  156. data/spec/{support/corpus-tests/failures → spec_tests/data/corpus_legacy}/undefined.json +0 -0
  157. data/spec/{support/driver-spec-tests → spec_tests/data}/decimal128/decimal128-1.json +0 -0
  158. data/spec/{support/driver-spec-tests → spec_tests/data}/decimal128/decimal128-2.json +0 -0
  159. data/spec/{support/driver-spec-tests → spec_tests/data}/decimal128/decimal128-3.json +0 -0
  160. data/spec/{support/driver-spec-tests → spec_tests/data}/decimal128/decimal128-4.json +0 -0
  161. data/spec/{support/driver-spec-tests → spec_tests/data}/decimal128/decimal128-5.json +0 -0
  162. data/spec/{support/driver-spec-tests → spec_tests/data}/decimal128/decimal128-6.json +0 -0
  163. data/spec/{support/driver-spec-tests → spec_tests/data}/decimal128/decimal128-7.json +0 -0
  164. data/spec/support/shared_examples.rb +3 -5
  165. data/spec/support/spec_config.rb +16 -0
  166. data/spec/support/utils.rb +10 -0
  167. metadata +227 -124
  168. metadata.gz.sig +0 -0
  169. data/ext/bson/bson_native.c +0 -762
@@ -1,4 +1,4 @@
1
- # Copyright (C) 2009-2014 MongoDB Inc.
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.
@@ -62,10 +62,36 @@ describe Integer do
62
62
  describe "#to_bson_key" do
63
63
 
64
64
  let(:obj) { Integer::MAX_32BIT - 1 }
65
- let(:encoded) { obj.to_s }
65
+ let(:encoded) { obj }
66
66
 
67
- it "returns the key as a string" do
67
+ it "returns the key as an 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
@@ -1,4 +1,4 @@
1
- # Copyright (C) 2009-2014 MongoDB Inc.
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.
@@ -1,4 +1,4 @@
1
- # Copyright (C) 2009-2014 MongoDB Inc.
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.
@@ -1,4 +1,4 @@
1
- # Copyright (C) 2009-2014 MongoDB Inc.
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.
@@ -1,4 +1,4 @@
1
- # Copyright (C) 2009-2014 MongoDB Inc.
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.
@@ -1,4 +1,4 @@
1
- # Copyright (C) 2009-2014 MongoDB Inc.
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.
@@ -1,4 +1,4 @@
1
- # Copyright (C) 2009-2014 MongoDB Inc.
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.
@@ -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.
@@ -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' }
@@ -559,4 +580,15 @@ describe Regexp::Raw do
559
580
  end
560
581
  end
561
582
  end
562
- end
583
+
584
+ describe 'yaml loading' do
585
+ let(:regexp) { described_class.new('hello.world', 's') }
586
+
587
+ it 'round-trips' do
588
+ actual = YAML.load(regexp.to_yaml)
589
+ actual.pattern.should == 'hello.world'
590
+ actual.options.should == 's'
591
+ actual.compile.should =~ "hello\nworld"
592
+ end
593
+ end
594
+ end
@@ -1,4 +1,4 @@
1
- # Copyright (C) 2009-2014 MongoDB Inc.
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.
@@ -1,4 +1,4 @@
1
- # Copyright (C) 2009-2014 MongoDB Inc.
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.
@@ -1,6 +1,6 @@
1
1
  # encoding: utf-8
2
2
 
3
- # Copyright (C) 2009-2014 MongoDB Inc.
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.
@@ -130,4 +130,22 @@ describe String do
130
130
  end
131
131
  end
132
132
  end
133
+
134
+ describe '#to_bson' do
135
+ context 'when string is not valid utf-8' do
136
+ let(:string) do
137
+ "\xfe\x00\xff".force_encoding('BINARY')
138
+ end
139
+
140
+ let(:expected_message) do
141
+ /from ASCII-8BIT to UTF-8/
142
+ end
143
+
144
+ it 'raises EncodingError' do
145
+ expect do
146
+ string.to_bson
147
+ end.to raise_error(EncodingError, expected_message)
148
+ end
149
+ end
150
+ end
133
151
  end
@@ -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
@@ -1,4 +1,4 @@
1
- # Copyright (C) 2009-2014 MongoDB Inc.
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,12 +32,72 @@ 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
38
98
 
39
99
  let(:symbol) { :test }
40
- let(:encoded) { symbol.to_s }
100
+ let(:encoded) { symbol }
41
101
 
42
102
  it "returns the encoded string" do
43
103
  expect(symbol.to_bson_key).to eq(encoded)
@@ -66,7 +126,7 @@ describe Symbol do
66
126
  end
67
127
 
68
128
  it "allows invalid keys" do
69
- expect(symbol.to_bson_key).to eq(symbol.to_s)
129
+ expect(symbol.to_bson_key).to eq(symbol)
70
130
  end
71
131
  end
72
132
  end
@@ -1,4 +1,4 @@
1
- # Copyright (C) 2009-2014 MongoDB Inc.
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 is pre epoch" do
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