bson 4.12.0-java → 4.14.1-java
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- checksums.yaml.gz.sig +0 -0
- data/README.md +4 -7
- data/lib/bson/active_support.rb +1 -0
- data/lib/bson/array.rb +2 -1
- data/lib/bson/big_decimal.rb +67 -0
- data/lib/bson/binary.rb +5 -3
- data/lib/bson/boolean.rb +2 -1
- data/lib/bson/code.rb +2 -1
- data/lib/bson/code_with_scope.rb +2 -1
- data/lib/bson/config.rb +1 -0
- data/lib/bson/date.rb +1 -0
- data/lib/bson/date_time.rb +1 -0
- data/lib/bson/db_pointer.rb +2 -1
- data/lib/bson/dbref.rb +152 -0
- data/lib/bson/decimal128/builder.rb +27 -20
- data/lib/bson/decimal128.rb +27 -12
- data/lib/bson/document.rb +18 -0
- data/lib/bson/environment.rb +1 -0
- data/lib/bson/error.rb +7 -0
- data/lib/bson/ext_json.rb +16 -11
- data/lib/bson/false_class.rb +2 -1
- data/lib/bson/float.rb +21 -32
- data/lib/bson/hash.rb +15 -6
- data/lib/bson/int32.rb +3 -2
- data/lib/bson/int64.rb +3 -2
- data/lib/bson/integer.rb +3 -2
- data/lib/bson/json.rb +1 -0
- data/lib/bson/max_key.rb +3 -2
- data/lib/bson/min_key.rb +3 -2
- data/lib/bson/nil_class.rb +2 -1
- data/lib/bson/object.rb +1 -0
- data/lib/bson/object_id.rb +4 -3
- data/lib/bson/open_struct.rb +1 -0
- data/lib/bson/regexp.rb +17 -6
- data/lib/bson/registry.rb +1 -0
- data/lib/bson/specialized.rb +1 -0
- data/lib/bson/string.rb +3 -2
- data/lib/bson/symbol.rb +2 -1
- data/lib/bson/time.rb +4 -3
- data/lib/bson/time_with_zone.rb +1 -0
- data/lib/bson/timestamp.rb +3 -2
- data/lib/bson/true_class.rb +2 -1
- data/lib/bson/undefined.rb +2 -1
- data/lib/bson/version.rb +2 -1
- data/lib/bson-ruby.jar +0 -0
- data/lib/bson.rb +8 -5
- data/spec/README.md +14 -0
- data/spec/bson/big_decimal_spec.rb +316 -0
- data/spec/bson/dbref_legacy_spec.rb +169 -0
- data/spec/bson/dbref_spec.rb +487 -0
- data/spec/bson/decimal128_spec.rb +16 -0
- data/spec/bson/document_as_spec.rb +46 -0
- data/spec/bson/document_spec.rb +7 -1
- data/spec/bson/ext_json_parse_spec.rb +37 -0
- data/spec/bson/hash_as_spec.rb +57 -0
- data/spec/bson/hash_spec.rb +32 -0
- data/spec/bson/int64_spec.rb +4 -24
- data/spec/bson/raw_spec.rb +7 -1
- data/spec/bson/regexp_spec.rb +52 -0
- data/spec/runners/common_driver.rb +1 -1
- data/spec/shared/LICENSE +20 -0
- data/spec/shared/bin/get-mongodb-download-url +17 -0
- data/spec/shared/bin/s3-copy +45 -0
- data/spec/shared/bin/s3-upload +69 -0
- data/spec/shared/lib/mrss/child_process_helper.rb +80 -0
- data/spec/shared/lib/mrss/cluster_config.rb +231 -0
- data/spec/shared/lib/mrss/constraints.rb +386 -0
- data/spec/shared/lib/mrss/docker_runner.rb +271 -0
- data/spec/shared/lib/mrss/event_subscriber.rb +200 -0
- data/spec/shared/lib/mrss/lite_constraints.rb +191 -0
- data/spec/shared/lib/mrss/server_version_registry.rb +120 -0
- data/spec/shared/lib/mrss/spec_organizer.rb +179 -0
- data/spec/shared/lib/mrss/utils.rb +15 -0
- data/spec/shared/share/Dockerfile.erb +338 -0
- data/spec/shared/share/haproxy-1.conf +16 -0
- data/spec/shared/share/haproxy-2.conf +17 -0
- data/spec/shared/shlib/distro.sh +74 -0
- data/spec/shared/shlib/server.sh +367 -0
- data/spec/shared/shlib/set_env.sh +131 -0
- data/spec/spec_helper.rb +20 -0
- data/spec/spec_tests/common_driver_spec.rb +2 -1
- data/spec/spec_tests/data/corpus/binary.json +18 -1
- data/spec/spec_tests/data/corpus/dbref.json +21 -1
- data/spec/spec_tests/data/corpus/document.json +4 -0
- data/spec/spec_tests/data/corpus/regex.json +2 -2
- data/spec/spec_tests/data/corpus/top.json +20 -9
- data.tar.gz.sig +0 -0
- metadata +141 -89
- metadata.gz.sig +0 -0
data/lib/bson.rb
CHANGED
@@ -1,3 +1,4 @@
|
|
1
|
+
# frozen_string_literal: true
|
1
2
|
# Copyright (C) 2009-2020 MongoDB Inc.
|
2
3
|
#
|
3
4
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
@@ -38,22 +39,22 @@ module BSON
|
|
38
39
|
# Constant for binary string encoding.
|
39
40
|
#
|
40
41
|
# @since 2.0.0
|
41
|
-
BINARY = "BINARY"
|
42
|
+
BINARY = "BINARY"
|
42
43
|
|
43
44
|
# Constant for bson types that don't actually serialize a value.
|
44
45
|
#
|
45
46
|
# @since 2.0.0
|
46
|
-
NO_VALUE =
|
47
|
+
NO_VALUE = ::String.new(encoding: BINARY).freeze
|
47
48
|
|
48
49
|
# Constant for a null byte (0x00).
|
49
50
|
#
|
50
51
|
# @since 2.0.0
|
51
|
-
NULL_BYTE = 0.chr
|
52
|
+
NULL_BYTE = ::String.new(0.chr, encoding: BINARY).freeze
|
52
53
|
|
53
54
|
# Constant for UTF-8 string encoding.
|
54
55
|
#
|
55
56
|
# @since 2.0.0
|
56
|
-
UTF8 = "UTF-8"
|
57
|
+
UTF8 = "UTF-8"
|
57
58
|
end
|
58
59
|
|
59
60
|
require "bson/config"
|
@@ -73,11 +74,13 @@ require "bson/date"
|
|
73
74
|
require "bson/date_time"
|
74
75
|
require "bson/db_pointer"
|
75
76
|
require "bson/decimal128"
|
77
|
+
require "bson/big_decimal"
|
76
78
|
require "bson/document"
|
77
79
|
require "bson/ext_json"
|
78
80
|
require "bson/false_class"
|
79
81
|
require "bson/float"
|
80
82
|
require "bson/hash"
|
83
|
+
require "bson/dbref"
|
81
84
|
require "bson/open_struct"
|
82
85
|
require "bson/max_key"
|
83
86
|
require "bson/min_key"
|
@@ -100,7 +103,7 @@ require "bson/version"
|
|
100
103
|
begin
|
101
104
|
if BSON::Environment.jruby?
|
102
105
|
require "bson-ruby.jar"
|
103
|
-
org.bson.NativeService
|
106
|
+
JRuby::Util.load_ext("org.bson.NativeService")
|
104
107
|
else
|
105
108
|
require "bson_native"
|
106
109
|
end
|
data/spec/README.md
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
# Running BSON Ruby Tests
|
2
|
+
|
3
|
+
## Quick Start
|
4
|
+
|
5
|
+
The test suite requires shared tooling that is stored in a separate repository
|
6
|
+
and is referenced as a submodule. After checking out the desired bson-ruby
|
7
|
+
branch, check out the matching submodules:
|
8
|
+
|
9
|
+
git submodule init
|
10
|
+
git submodule update
|
11
|
+
|
12
|
+
Then, to run the test suite:
|
13
|
+
|
14
|
+
rake
|
@@ -0,0 +1,316 @@
|
|
1
|
+
# Copyright (C) 2016-2021 MongoDB Inc.
|
2
|
+
#
|
3
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
4
|
+
# you may not use this file except in compliance with the License.
|
5
|
+
# You may obtain a copy of the License at
|
6
|
+
#
|
7
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
8
|
+
#
|
9
|
+
# Unless required by applicable law or agreed to in writing, software
|
10
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
11
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
12
|
+
# See the License for the specific language governing permissions and
|
13
|
+
# limitations under the License.
|
14
|
+
|
15
|
+
require "spec_helper"
|
16
|
+
|
17
|
+
describe BSON::BigDecimal do
|
18
|
+
|
19
|
+
describe '#from_bson' do
|
20
|
+
shared_examples_for 'a BSON::BigDecimal deserializer' do
|
21
|
+
|
22
|
+
let(:decimal128) do
|
23
|
+
BSON::Decimal128.new(argument)
|
24
|
+
end
|
25
|
+
|
26
|
+
let(:deserialized_big_decimal) do
|
27
|
+
BigDecimal.from_bson(decimal128.to_bson)
|
28
|
+
end
|
29
|
+
|
30
|
+
let(:deserialized_decimal128) do
|
31
|
+
BSON::Decimal128.from_bson(decimal128.to_bson)
|
32
|
+
end
|
33
|
+
|
34
|
+
it 'deserializes Decimal128 encoded bson correctly' do
|
35
|
+
if deserialized_decimal128.to_s == "NaN"
|
36
|
+
expect(deserialized_big_decimal.nan?).to be true
|
37
|
+
else
|
38
|
+
expect(deserialized_big_decimal).to eq(deserialized_decimal128.to_big_decimal)
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
context 'when Infinity is passed' do
|
44
|
+
|
45
|
+
let(:argument) { "Infinity" }
|
46
|
+
|
47
|
+
it_behaves_like 'a BSON::BigDecimal deserializer'
|
48
|
+
end
|
49
|
+
|
50
|
+
context 'when -Infinity is passed' do
|
51
|
+
|
52
|
+
let(:argument) { "-Infinity" }
|
53
|
+
|
54
|
+
it_behaves_like 'a BSON::BigDecimal deserializer'
|
55
|
+
end
|
56
|
+
|
57
|
+
context 'when NaN is passed' do
|
58
|
+
|
59
|
+
let(:argument) { "NaN" }
|
60
|
+
|
61
|
+
it_behaves_like 'a BSON::BigDecimal deserializer'
|
62
|
+
end
|
63
|
+
|
64
|
+
context 'when -NaN is passed' do
|
65
|
+
let(:argument) { "-NaN" }
|
66
|
+
|
67
|
+
it_behaves_like 'a BSON::BigDecimal deserializer'
|
68
|
+
end
|
69
|
+
|
70
|
+
context 'when SNaN is passed' do
|
71
|
+
let(:argument) { "SNaN" }
|
72
|
+
|
73
|
+
it_behaves_like 'a BSON::BigDecimal deserializer'
|
74
|
+
end
|
75
|
+
|
76
|
+
context 'when -SNaN is passed' do
|
77
|
+
let(:argument) { "SNaN" }
|
78
|
+
|
79
|
+
it_behaves_like 'a BSON::BigDecimal deserializer'
|
80
|
+
end
|
81
|
+
|
82
|
+
context 'when -0 is passed' do
|
83
|
+
let(:argument) { "-0" }
|
84
|
+
|
85
|
+
it_behaves_like 'a BSON::BigDecimal deserializer'
|
86
|
+
end
|
87
|
+
|
88
|
+
context 'when a positive integer is passed' do
|
89
|
+
let(:argument) { "12" }
|
90
|
+
|
91
|
+
it_behaves_like 'a BSON::BigDecimal deserializer'
|
92
|
+
end
|
93
|
+
|
94
|
+
context 'when a negative integer is passed' do
|
95
|
+
let(:argument) { "-12" }
|
96
|
+
|
97
|
+
it_behaves_like 'a BSON::BigDecimal deserializer'
|
98
|
+
end
|
99
|
+
|
100
|
+
context 'when a positive float is passed' do
|
101
|
+
let(:argument) { "0.12345" }
|
102
|
+
|
103
|
+
it_behaves_like 'a BSON::BigDecimal deserializer'
|
104
|
+
end
|
105
|
+
|
106
|
+
context 'when a negative float is passed' do
|
107
|
+
let(:argument) { "-0.12345" }
|
108
|
+
|
109
|
+
it_behaves_like 'a BSON::BigDecimal deserializer'
|
110
|
+
end
|
111
|
+
|
112
|
+
context 'when a large positive integer is passed' do
|
113
|
+
let(:argument) { "1234567890123456789012345678901234" }
|
114
|
+
|
115
|
+
it_behaves_like 'a BSON::BigDecimal deserializer'
|
116
|
+
end
|
117
|
+
|
118
|
+
context 'when a large negative integer is passed' do
|
119
|
+
let(:argument) { "-1234567890123456789012345678901234" }
|
120
|
+
|
121
|
+
it_behaves_like 'a BSON::BigDecimal deserializer'
|
122
|
+
end
|
123
|
+
end
|
124
|
+
|
125
|
+
describe "#to_bson" do
|
126
|
+
shared_examples_for 'a BSON::BigDecimal serializer' do
|
127
|
+
|
128
|
+
let(:decimal128) do
|
129
|
+
BSON::Decimal128.new(BigDecimal(argument).to_s)
|
130
|
+
end
|
131
|
+
|
132
|
+
let(:decimal_128_bson) do
|
133
|
+
decimal128.to_bson
|
134
|
+
end
|
135
|
+
|
136
|
+
let(:big_decimal_bson) do
|
137
|
+
BigDecimal(argument).to_bson
|
138
|
+
end
|
139
|
+
|
140
|
+
it 'serializes BigDecimals correctly' do
|
141
|
+
expect(decimal_128_bson.to_s).to eq(big_decimal_bson.to_s)
|
142
|
+
end
|
143
|
+
end
|
144
|
+
|
145
|
+
context 'when Infinity is passed' do
|
146
|
+
|
147
|
+
let(:argument) { "Infinity" }
|
148
|
+
|
149
|
+
it_behaves_like 'a BSON::BigDecimal serializer'
|
150
|
+
end
|
151
|
+
|
152
|
+
context 'when -Infinity is passed' do
|
153
|
+
|
154
|
+
let(:argument) { "-Infinity" }
|
155
|
+
|
156
|
+
it_behaves_like 'a BSON::BigDecimal serializer'
|
157
|
+
end
|
158
|
+
|
159
|
+
context 'when NaN is passed' do
|
160
|
+
|
161
|
+
let(:argument) { "NaN" }
|
162
|
+
|
163
|
+
it_behaves_like 'a BSON::BigDecimal serializer'
|
164
|
+
end
|
165
|
+
|
166
|
+
context 'when -0 is passed' do
|
167
|
+
let(:argument) { "-0" }
|
168
|
+
|
169
|
+
it_behaves_like 'a BSON::BigDecimal serializer'
|
170
|
+
end
|
171
|
+
|
172
|
+
context 'when a positive integer is passed' do
|
173
|
+
let(:argument) { "12" }
|
174
|
+
|
175
|
+
it_behaves_like 'a BSON::BigDecimal serializer'
|
176
|
+
end
|
177
|
+
|
178
|
+
context 'when a negative integer is passed' do
|
179
|
+
let(:argument) { "-12" }
|
180
|
+
|
181
|
+
it_behaves_like 'a BSON::BigDecimal serializer'
|
182
|
+
end
|
183
|
+
|
184
|
+
context 'when a positive float is passed' do
|
185
|
+
let(:argument) { "0.12345" }
|
186
|
+
|
187
|
+
it_behaves_like 'a BSON::BigDecimal serializer'
|
188
|
+
end
|
189
|
+
|
190
|
+
context 'when a negative float is passed' do
|
191
|
+
let(:argument) { "-0.12345" }
|
192
|
+
|
193
|
+
it_behaves_like 'a BSON::BigDecimal serializer'
|
194
|
+
end
|
195
|
+
|
196
|
+
context 'when a large positive integer is passed' do
|
197
|
+
let(:argument) { "1234567890123456789012345678901234" }
|
198
|
+
|
199
|
+
it_behaves_like 'a BSON::BigDecimal serializer'
|
200
|
+
end
|
201
|
+
|
202
|
+
context 'when a large negative integer is passed' do
|
203
|
+
let(:argument) { "-1234567890123456789012345678901234" }
|
204
|
+
|
205
|
+
it_behaves_like 'a BSON::BigDecimal serializer'
|
206
|
+
end
|
207
|
+
|
208
|
+
context "when passing an out of range Decimal128" do
|
209
|
+
let(:argument) { "1E1000000" }
|
210
|
+
|
211
|
+
it "raises an error" do
|
212
|
+
expect do
|
213
|
+
BigDecimal(argument).to_bson
|
214
|
+
end.to raise_error(BSON::Decimal128::InvalidRange)
|
215
|
+
end
|
216
|
+
end
|
217
|
+
|
218
|
+
context "when passing a number with too much precision for Decimal128" do
|
219
|
+
let(:argument) { "1.000000000000000000000000000000000000000000000000001" }
|
220
|
+
|
221
|
+
it "raises an error" do
|
222
|
+
expect do
|
223
|
+
BigDecimal(argument).to_bson
|
224
|
+
end.to raise_error(BSON::Decimal128::UnrepresentablePrecision)
|
225
|
+
end
|
226
|
+
end
|
227
|
+
end
|
228
|
+
|
229
|
+
describe "#from_bson/#to_bson" do
|
230
|
+
shared_examples_for 'a BSON::BigDecimal round trip' do
|
231
|
+
|
232
|
+
let(:big_decimal) do
|
233
|
+
BigDecimal(argument)
|
234
|
+
end
|
235
|
+
|
236
|
+
let(:big_decimal_bson) do
|
237
|
+
big_decimal.to_bson
|
238
|
+
end
|
239
|
+
|
240
|
+
let(:deserialized_big_decimal) do
|
241
|
+
BigDecimal.from_bson(big_decimal_bson)
|
242
|
+
end
|
243
|
+
|
244
|
+
it 'serializes BigDecimals correctly' do
|
245
|
+
if big_decimal.nan?
|
246
|
+
expect(deserialized_big_decimal.nan?).to be true
|
247
|
+
else
|
248
|
+
expect(deserialized_big_decimal).to eq(big_decimal)
|
249
|
+
end
|
250
|
+
end
|
251
|
+
end
|
252
|
+
|
253
|
+
context 'when Infinity is passed' do
|
254
|
+
|
255
|
+
let(:argument) { "Infinity" }
|
256
|
+
|
257
|
+
it_behaves_like 'a BSON::BigDecimal round trip'
|
258
|
+
end
|
259
|
+
|
260
|
+
context 'when -Infinity is passed' do
|
261
|
+
|
262
|
+
let(:argument) { "-Infinity" }
|
263
|
+
|
264
|
+
it_behaves_like 'a BSON::BigDecimal round trip'
|
265
|
+
end
|
266
|
+
|
267
|
+
context 'when NaN is passed' do
|
268
|
+
|
269
|
+
let(:argument) { "NaN" }
|
270
|
+
|
271
|
+
it_behaves_like 'a BSON::BigDecimal round trip'
|
272
|
+
end
|
273
|
+
|
274
|
+
context 'when -0 is passed' do
|
275
|
+
let(:argument) { "-0" }
|
276
|
+
|
277
|
+
it_behaves_like 'a BSON::BigDecimal round trip'
|
278
|
+
end
|
279
|
+
|
280
|
+
context 'when a positive integer is passed' do
|
281
|
+
let(:argument) { "12" }
|
282
|
+
|
283
|
+
it_behaves_like 'a BSON::BigDecimal round trip'
|
284
|
+
end
|
285
|
+
|
286
|
+
context 'when a negative integer is passed' do
|
287
|
+
let(:argument) { "-12" }
|
288
|
+
|
289
|
+
it_behaves_like 'a BSON::BigDecimal round trip'
|
290
|
+
end
|
291
|
+
|
292
|
+
context 'when a positive float is passed' do
|
293
|
+
let(:argument) { "0.12345" }
|
294
|
+
|
295
|
+
it_behaves_like 'a BSON::BigDecimal round trip'
|
296
|
+
end
|
297
|
+
|
298
|
+
context 'when a negative float is passed' do
|
299
|
+
let(:argument) { "-0.12345" }
|
300
|
+
|
301
|
+
it_behaves_like 'a BSON::BigDecimal round trip'
|
302
|
+
end
|
303
|
+
|
304
|
+
context 'when a large positive integer is passed' do
|
305
|
+
let(:argument) { "1234567890123456789012345678901234" }
|
306
|
+
|
307
|
+
it_behaves_like 'a BSON::BigDecimal round trip'
|
308
|
+
end
|
309
|
+
|
310
|
+
context 'when a large negative integer is passed' do
|
311
|
+
let(:argument) { "-1234567890123456789012345678901234" }
|
312
|
+
|
313
|
+
it_behaves_like 'a BSON::BigDecimal round trip'
|
314
|
+
end
|
315
|
+
end
|
316
|
+
end
|
@@ -0,0 +1,169 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
# encoding: utf-8
|
3
|
+
|
4
|
+
require 'spec_helper'
|
5
|
+
require 'json'
|
6
|
+
|
7
|
+
# These tests are copied from driver when the driver implemented Mongo:DBRef
|
8
|
+
# class, and are intended to verify that the current DBRef implementation is
|
9
|
+
# compatible with the legacy driver DBRef interface.
|
10
|
+
|
11
|
+
describe BSON::DBRef do
|
12
|
+
|
13
|
+
let(:object_id) do
|
14
|
+
BSON::ObjectId.new
|
15
|
+
end
|
16
|
+
|
17
|
+
describe '#as_json' do
|
18
|
+
|
19
|
+
context 'when the database is not provided' do
|
20
|
+
|
21
|
+
let(:dbref) do
|
22
|
+
described_class.new('users', object_id)
|
23
|
+
end
|
24
|
+
|
25
|
+
it 'returns the json document without database' do
|
26
|
+
expect(dbref.as_json).to eq({ '$ref' => 'users', '$id' => object_id })
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
context 'when the database is provided' do
|
31
|
+
|
32
|
+
let(:dbref) do
|
33
|
+
described_class.new('users', object_id, 'database')
|
34
|
+
end
|
35
|
+
|
36
|
+
it 'returns the json document with database' do
|
37
|
+
expect(dbref.as_json).to eq({
|
38
|
+
'$ref' => 'users',
|
39
|
+
'$id' => object_id,
|
40
|
+
'$db' => 'database'
|
41
|
+
})
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
describe '#initialize' do
|
47
|
+
|
48
|
+
let(:dbref) do
|
49
|
+
described_class.new('users', object_id)
|
50
|
+
end
|
51
|
+
|
52
|
+
it 'sets the collection' do
|
53
|
+
expect(dbref.collection).to eq('users')
|
54
|
+
end
|
55
|
+
|
56
|
+
it 'sets the id' do
|
57
|
+
expect(dbref.id).to eq(object_id)
|
58
|
+
end
|
59
|
+
|
60
|
+
context 'when a database is provided' do
|
61
|
+
|
62
|
+
let(:dbref) do
|
63
|
+
described_class.new('users', object_id, 'db')
|
64
|
+
end
|
65
|
+
|
66
|
+
it 'sets the database' do
|
67
|
+
expect(dbref.database).to eq('db')
|
68
|
+
end
|
69
|
+
|
70
|
+
context 'when id is not provided' do
|
71
|
+
|
72
|
+
let(:dbref) do
|
73
|
+
described_class.new('users', nil, 'db')
|
74
|
+
end
|
75
|
+
|
76
|
+
it 'raises ArgumentError' do
|
77
|
+
lambda do
|
78
|
+
dbref
|
79
|
+
end.should raise_error(ArgumentError)
|
80
|
+
end
|
81
|
+
end
|
82
|
+
end
|
83
|
+
end
|
84
|
+
|
85
|
+
describe '#to_bson' do
|
86
|
+
|
87
|
+
let(:dbref) do
|
88
|
+
described_class.new('users', object_id, 'database')
|
89
|
+
end
|
90
|
+
|
91
|
+
it 'converts the underlying document to bson' do
|
92
|
+
expect(dbref.to_bson.to_s).to eq(dbref.as_json.to_bson.to_s)
|
93
|
+
end
|
94
|
+
end
|
95
|
+
|
96
|
+
describe '#to_json' do
|
97
|
+
|
98
|
+
context 'when the database is not provided' do
|
99
|
+
|
100
|
+
let(:dbref) do
|
101
|
+
described_class.new('users', object_id)
|
102
|
+
end
|
103
|
+
|
104
|
+
it 'returns the json document without database' do
|
105
|
+
expect(dbref.to_json).to eq("{\"$ref\":\"users\",\"$id\":#{object_id.to_json}}")
|
106
|
+
end
|
107
|
+
end
|
108
|
+
|
109
|
+
context 'when the database is provided' do
|
110
|
+
|
111
|
+
let(:dbref) do
|
112
|
+
described_class.new('users', object_id, 'database')
|
113
|
+
end
|
114
|
+
|
115
|
+
it 'returns the json document with database' do
|
116
|
+
expect(dbref.to_json).to eq("{\"$ref\":\"users\",\"$id\":#{object_id.to_json},\"$db\":\"database\"}")
|
117
|
+
end
|
118
|
+
end
|
119
|
+
end
|
120
|
+
|
121
|
+
describe '#from_bson' do
|
122
|
+
|
123
|
+
let(:buffer) do
|
124
|
+
dbref.to_bson
|
125
|
+
end
|
126
|
+
|
127
|
+
let(:decoded) do
|
128
|
+
BSON::Document.from_bson(BSON::ByteBuffer.new(buffer.to_s))
|
129
|
+
end
|
130
|
+
|
131
|
+
context 'when a database exists' do
|
132
|
+
|
133
|
+
let(:dbref) do
|
134
|
+
described_class.new('users', object_id, 'database')
|
135
|
+
end
|
136
|
+
|
137
|
+
it 'decodes the ref' do
|
138
|
+
expect(decoded.collection).to eq('users')
|
139
|
+
end
|
140
|
+
|
141
|
+
it 'decodes the id' do
|
142
|
+
expect(decoded.id).to eq(object_id)
|
143
|
+
end
|
144
|
+
|
145
|
+
it 'decodes the database' do
|
146
|
+
expect(decoded.database).to eq('database')
|
147
|
+
end
|
148
|
+
end
|
149
|
+
|
150
|
+
context 'when no database exists' do
|
151
|
+
|
152
|
+
let(:dbref) do
|
153
|
+
described_class.new('users', object_id)
|
154
|
+
end
|
155
|
+
|
156
|
+
it 'decodes the ref' do
|
157
|
+
expect(decoded.collection).to eq('users')
|
158
|
+
end
|
159
|
+
|
160
|
+
it 'decodes the id' do
|
161
|
+
expect(decoded.id).to eq(object_id)
|
162
|
+
end
|
163
|
+
|
164
|
+
it 'sets the database to nil' do
|
165
|
+
expect(decoded.database).to be_nil
|
166
|
+
end
|
167
|
+
end
|
168
|
+
end
|
169
|
+
end
|