bson 4.15.0 → 5.0.0
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 -4
- data/Rakefile +5 -0
- data/ext/bson/bson-native.h +12 -4
- data/ext/bson/extconf.rb +8 -3
- data/ext/bson/init.c +11 -11
- data/ext/bson/read.c +21 -6
- data/ext/bson/util.c +168 -16
- data/ext/bson/write.c +30 -39
- data/lib/bson/active_support.rb +1 -0
- data/lib/bson/array.rb +57 -31
- data/lib/bson/big_decimal.rb +16 -6
- data/lib/bson/binary.rb +255 -128
- data/lib/bson/boolean.rb +1 -0
- data/lib/bson/code.rb +9 -11
- data/lib/bson/code_with_scope.rb +8 -10
- data/lib/bson/config.rb +1 -27
- data/lib/bson/date.rb +2 -1
- data/lib/bson/date_time.rb +2 -1
- data/lib/bson/db_pointer.rb +11 -12
- data/lib/bson/dbref.rb +11 -9
- data/lib/bson/decimal128/builder.rb +9 -8
- data/lib/bson/decimal128.rb +24 -110
- data/lib/bson/document.rb +1 -0
- data/lib/bson/environment.rb +1 -0
- data/lib/bson/error/bson_decode_error.rb +11 -0
- data/lib/bson/error/ext_json_parse_error.rb +11 -0
- data/lib/bson/error/illegal_key.rb +23 -0
- data/lib/bson/error/invalid_binary_type.rb +37 -0
- data/lib/bson/error/invalid_dbref_argument.rb +12 -0
- data/lib/bson/error/invalid_decimal128_argument.rb +25 -0
- data/lib/bson/error/invalid_decimal128_range.rb +27 -0
- data/lib/bson/error/invalid_decimal128_string.rb +26 -0
- data/lib/bson/error/invalid_key.rb +24 -0
- data/lib/bson/error/invalid_object_id.rb +11 -0
- data/lib/bson/error/invalid_regexp_pattern.rb +13 -0
- data/lib/bson/error/unrepresentable_precision.rb +19 -0
- data/lib/bson/error/unserializable_class.rb +13 -0
- data/lib/bson/error/unsupported_binary_subtype.rb +12 -0
- data/lib/bson/error/unsupported_type.rb +11 -0
- data/lib/bson/error.rb +16 -28
- data/lib/bson/ext_json.rb +1 -0
- data/lib/bson/false_class.rb +2 -1
- data/lib/bson/float.rb +2 -1
- data/lib/bson/hash.rb +127 -72
- data/lib/bson/int32.rb +16 -4
- data/lib/bson/int64.rb +16 -4
- data/lib/bson/integer.rb +3 -4
- data/lib/bson/json.rb +1 -0
- data/lib/bson/max_key.rb +7 -9
- data/lib/bson/min_key.rb +7 -9
- data/lib/bson/nil_class.rb +1 -0
- data/lib/bson/object.rb +5 -25
- data/lib/bson/object_id.rb +75 -121
- data/lib/bson/open_struct.rb +3 -2
- data/lib/bson/regexp.rb +35 -64
- data/lib/bson/registry.rb +2 -6
- data/lib/bson/specialized.rb +2 -1
- data/lib/bson/string.rb +4 -27
- data/lib/bson/symbol.rb +22 -19
- data/lib/bson/time.rb +2 -1
- data/lib/bson/time_with_zone.rb +13 -1
- data/lib/bson/timestamp.rb +2 -1
- data/lib/bson/true_class.rb +2 -1
- data/lib/bson/undefined.rb +14 -0
- data/lib/bson/version.rb +2 -1
- data/lib/bson.rb +3 -2
- data/spec/bson/array_spec.rb +19 -60
- data/spec/bson/big_decimal_spec.rb +16 -4
- data/spec/bson/binary_spec.rb +83 -74
- data/spec/bson/binary_uuid_spec.rb +1 -0
- data/spec/bson/boolean_spec.rb +1 -0
- data/spec/bson/byte_buffer_read_spec.rb +1 -0
- data/spec/bson/byte_buffer_spec.rb +1 -0
- data/spec/bson/byte_buffer_write_spec.rb +1 -0
- data/spec/bson/code_spec.rb +5 -3
- data/spec/bson/code_with_scope_spec.rb +5 -3
- data/spec/bson/config_spec.rb +1 -35
- data/spec/bson/date_spec.rb +1 -0
- data/spec/bson/date_time_spec.rb +1 -0
- data/spec/bson/dbref_legacy_spec.rb +20 -3
- data/spec/bson/dbref_spec.rb +9 -9
- data/spec/bson/decimal128_spec.rb +40 -20
- data/spec/bson/document_as_spec.rb +1 -0
- data/spec/bson/document_spec.rb +1 -1
- data/spec/bson/ext_json_parse_spec.rb +1 -0
- data/spec/bson/false_class_spec.rb +8 -0
- data/spec/bson/float_spec.rb +8 -3
- data/spec/bson/hash_as_spec.rb +1 -0
- data/spec/bson/hash_spec.rb +87 -75
- data/spec/bson/int32_spec.rb +21 -6
- data/spec/bson/int64_spec.rb +21 -6
- data/spec/bson/integer_spec.rb +45 -13
- data/spec/bson/json_spec.rb +1 -0
- data/spec/bson/max_key_spec.rb +5 -3
- data/spec/bson/min_key_spec.rb +5 -3
- data/spec/bson/nil_class_spec.rb +1 -0
- data/spec/bson/object_id_spec.rb +43 -4
- data/spec/bson/object_spec.rb +2 -1
- data/spec/bson/open_struct_spec.rb +14 -71
- data/spec/bson/raw_spec.rb +9 -15
- data/spec/bson/regexp_spec.rb +4 -3
- data/spec/bson/registry_spec.rb +2 -1
- data/spec/bson/string_spec.rb +13 -38
- data/spec/bson/symbol_raw_spec.rb +25 -0
- data/spec/bson/symbol_spec.rb +15 -18
- data/spec/bson/time_spec.rb +1 -0
- data/spec/bson/time_with_zone_spec.rb +1 -0
- data/spec/bson/timestamp_spec.rb +1 -0
- data/spec/bson/true_class_spec.rb +8 -0
- data/spec/bson/undefined_spec.rb +27 -0
- data/spec/bson_spec.rb +1 -0
- data/spec/runners/common_driver.rb +6 -5
- data/spec/runners/corpus.rb +6 -0
- data/spec/runners/corpus_legacy.rb +1 -0
- data/spec/shared/lib/mrss/constraints.rb +8 -16
- data/spec/shared/lib/mrss/docker_runner.rb +30 -3
- data/spec/shared/lib/mrss/eg_config_utils.rb +51 -0
- data/spec/shared/lib/mrss/event_subscriber.rb +15 -5
- data/spec/shared/lib/mrss/lite_constraints.rb +48 -1
- data/spec/shared/lib/mrss/server_version_registry.rb +16 -23
- data/spec/shared/lib/mrss/session_registry.rb +69 -0
- data/spec/shared/lib/mrss/session_registry_legacy.rb +60 -0
- data/spec/shared/lib/mrss/utils.rb +28 -6
- data/spec/shared/share/Dockerfile.erb +68 -85
- data/spec/shared/shlib/config.sh +27 -0
- data/spec/shared/shlib/server.sh +73 -24
- data/spec/shared/shlib/set_env.sh +39 -1
- data/spec/spec_helper.rb +1 -0
- data/spec/spec_tests/common_driver_spec.rb +9 -4
- data/spec/spec_tests/corpus_legacy_spec.rb +1 -0
- data/spec/spec_tests/corpus_spec.rb +13 -3
- data/spec/spec_tests/data/corpus/binary.json +5 -0
- data/spec/spec_tests/data/corpus/code.json +13 -13
- data/spec/spec_tests/data/corpus/decimal128-4.json +48 -0
- data/spec/spec_tests/data/corpus/decimal128-6.json +12 -0
- data/spec/spec_tests/data/corpus/decimal128-7.json +4 -0
- data/spec/spec_tests/data/corpus/document.json +20 -0
- data/spec/spec_tests/data/corpus/symbol.json +7 -7
- data/spec/spec_tests/data/corpus/top.json +18 -3
- data/spec/support/shared_examples.rb +28 -5
- data/spec/support/spec_config.rb +1 -0
- data/spec/support/utils.rb +49 -1
- data.tar.gz.sig +0 -0
- metadata +167 -144
- metadata.gz.sig +2 -1
data/lib/bson/string.rb
CHANGED
@@ -1,3 +1,4 @@
|
|
1
|
+
# rubocop:todo all
|
1
2
|
# -*- coding: utf-8 -*-
|
2
3
|
# frozen_string_literal: true
|
3
4
|
# Copyright (C) 2009-2020 MongoDB Inc.
|
@@ -46,7 +47,7 @@ module BSON
|
|
46
47
|
# @see http://bsonspec.org/#/specification
|
47
48
|
#
|
48
49
|
# @since 2.0.0
|
49
|
-
def to_bson(buffer = ByteBuffer.new
|
50
|
+
def to_bson(buffer = ByteBuffer.new)
|
50
51
|
buffer.put_string(self)
|
51
52
|
end
|
52
53
|
|
@@ -57,18 +58,12 @@ module BSON
|
|
57
58
|
#
|
58
59
|
# @raise [ EncodingError ] If the string is not UTF-8.
|
59
60
|
#
|
60
|
-
# @raise [ IllegalKey ] If validating keys and it contains a '.' or starts
|
61
|
-
# with '$'.
|
62
|
-
#
|
63
61
|
# @return [ String ] The encoded string.
|
64
62
|
#
|
65
63
|
# @see http://bsonspec.org/#/specification
|
66
64
|
#
|
67
65
|
# @since 2.0.0
|
68
|
-
def to_bson_key
|
69
|
-
if validating_keys
|
70
|
-
raise IllegalKey.new(self) if ILLEGAL_KEY =~ self
|
71
|
-
end
|
66
|
+
def to_bson_key
|
72
67
|
self
|
73
68
|
end
|
74
69
|
|
@@ -80,7 +75,7 @@ module BSON
|
|
80
75
|
#
|
81
76
|
# @note This is used for repairing legacy bson data.
|
82
77
|
#
|
83
|
-
# @raise [ BSON::
|
78
|
+
# @raise [ BSON::Error::InvalidObjectId ] If the string is not 12 elements.
|
84
79
|
#
|
85
80
|
# @return [ String ] The raw object id bytes.
|
86
81
|
#
|
@@ -101,24 +96,6 @@ module BSON
|
|
101
96
|
unpack("H*")[0]
|
102
97
|
end
|
103
98
|
|
104
|
-
# Raised when validating keys and a key is illegal in MongoDB
|
105
|
-
#
|
106
|
-
# @since 4.1.0
|
107
|
-
class IllegalKey < RuntimeError
|
108
|
-
|
109
|
-
# Instantiate the exception.
|
110
|
-
#
|
111
|
-
# @example Instantiate the exception.
|
112
|
-
# BSON::Object::IllegalKey.new(string)
|
113
|
-
#
|
114
|
-
# @param [ String ] string The illegal string.
|
115
|
-
#
|
116
|
-
# @since 4.1.0
|
117
|
-
def initialize(string)
|
118
|
-
super("'#{string}' is an illegal key in MongoDB. Keys may not start with '$' or contain a '.'.")
|
119
|
-
end
|
120
|
-
end
|
121
|
-
|
122
99
|
module ClassMethods
|
123
100
|
|
124
101
|
# Deserialize a string from BSON.
|
data/lib/bson/symbol.rb
CHANGED
@@ -1,4 +1,5 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
|
+
# rubocop:todo all
|
2
3
|
# Copyright (C) 2009-2020 MongoDB Inc.
|
3
4
|
#
|
4
5
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
@@ -57,7 +58,7 @@ module BSON
|
|
57
58
|
# @see http://bsonspec.org/#/specification
|
58
59
|
#
|
59
60
|
# @since 2.0.0
|
60
|
-
def to_bson(buffer = ByteBuffer.new
|
61
|
+
def to_bson(buffer = ByteBuffer.new)
|
61
62
|
buffer.put_symbol(self)
|
62
63
|
end
|
63
64
|
|
@@ -71,10 +72,7 @@ module BSON
|
|
71
72
|
# @see http://bsonspec.org/#/specification
|
72
73
|
#
|
73
74
|
# @since 2.0.0
|
74
|
-
def to_bson_key
|
75
|
-
if validating_keys
|
76
|
-
raise BSON::String::IllegalKey.new(self) if BSON::String::ILLEGAL_KEY =~ self
|
77
|
-
end
|
75
|
+
def to_bson_key
|
78
76
|
self
|
79
77
|
end
|
80
78
|
|
@@ -93,15 +91,14 @@ module BSON
|
|
93
91
|
# Converts this object to a representation directly serializable to
|
94
92
|
# Extended JSON (https://github.com/mongodb/specifications/blob/master/source/extended-json.rst).
|
95
93
|
#
|
96
|
-
# @option options [ true | false ] :relaxed Whether to produce relaxed
|
97
|
-
# extended JSON representation.
|
98
|
-
#
|
99
94
|
# @return [ Hash ] The extended json representation.
|
100
|
-
def as_extended_json(**
|
95
|
+
def as_extended_json(**_options)
|
101
96
|
{ "$symbol" => to_s }
|
102
97
|
end
|
103
98
|
|
104
99
|
class Raw
|
100
|
+
include JSON
|
101
|
+
|
105
102
|
# Create a BSON Symbol
|
106
103
|
#
|
107
104
|
# @param [ String | Symbol ] str_or_sym The symbol represented by this
|
@@ -148,7 +145,7 @@ module BSON
|
|
148
145
|
# @return [ BSON::ByteBuffer ] The buffer with the encoded object.
|
149
146
|
#
|
150
147
|
# @see http://bsonspec.org/#/specification
|
151
|
-
def to_bson(buffer = ByteBuffer.new
|
148
|
+
def to_bson(buffer = ByteBuffer.new)
|
152
149
|
buffer.put_string(to_s)
|
153
150
|
end
|
154
151
|
|
@@ -156,18 +153,24 @@ module BSON
|
|
156
153
|
Symbol::BSON_TYPE
|
157
154
|
end
|
158
155
|
|
159
|
-
#
|
160
|
-
#
|
156
|
+
# Return a string representation of the raw symbol for use in
|
157
|
+
# application-level JSON serialization. This method is intentionally
|
158
|
+
# different from #as_extended_json.
|
161
159
|
#
|
162
|
-
#
|
163
|
-
#
|
160
|
+
# @example Get the raw symbol as a JSON-serializable object.
|
161
|
+
# raw_symbol.as_json
|
164
162
|
#
|
165
|
-
# @
|
166
|
-
|
163
|
+
# @return [ String ] The raw symbol as a String.
|
164
|
+
def as_json(*args)
|
165
|
+
to_s
|
166
|
+
end
|
167
|
+
|
168
|
+
# Converts this object to a representation directly serializable to
|
169
|
+
# Extended JSON (https://github.com/mongodb/specifications/blob/master/source/extended-json.rst).
|
167
170
|
#
|
168
|
-
# @return [ Hash
|
169
|
-
def as_extended_json(**
|
170
|
-
{'$symbol' => to_s}
|
171
|
+
# @return [ Hash ] The extended json representation.
|
172
|
+
def as_extended_json(**_options)
|
173
|
+
{ '$symbol' => to_s }
|
171
174
|
end
|
172
175
|
end
|
173
176
|
|
data/lib/bson/time.rb
CHANGED
@@ -1,4 +1,5 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
|
+
# rubocop:todo all
|
2
3
|
# Copyright (C) 2009-2020 MongoDB Inc.
|
3
4
|
#
|
4
5
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
@@ -54,7 +55,7 @@ module BSON
|
|
54
55
|
# @see http://bsonspec.org/#/specification
|
55
56
|
#
|
56
57
|
# @since 2.0.0
|
57
|
-
def to_bson(buffer = ByteBuffer.new
|
58
|
+
def to_bson(buffer = ByteBuffer.new)
|
58
59
|
value = _bson_to_i * 1000 + usec.divmod(1000).first
|
59
60
|
buffer.put_int64(value)
|
60
61
|
end
|
data/lib/bson/time_with_zone.rb
CHANGED
@@ -1,4 +1,5 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
|
+
# rubocop:todo all
|
2
3
|
# Copyright (C) 2018-2020 MongoDB Inc.
|
3
4
|
#
|
4
5
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
@@ -35,7 +36,7 @@ module BSON
|
|
35
36
|
# @see http://bsonspec.org/#/specification
|
36
37
|
#
|
37
38
|
# @since 4.4.0
|
38
|
-
def to_bson(buffer = ByteBuffer.new
|
39
|
+
def to_bson(buffer = ByteBuffer.new)
|
39
40
|
buffer.put_int64((to_i * 1000) + (usec / 1000))
|
40
41
|
end
|
41
42
|
|
@@ -46,6 +47,17 @@ module BSON
|
|
46
47
|
def bson_type
|
47
48
|
::Time::BSON_TYPE
|
48
49
|
end
|
50
|
+
|
51
|
+
# @api private
|
52
|
+
def _bson_to_i
|
53
|
+
# Workaround for JRuby's #to_i rounding negative timestamps up
|
54
|
+
# rather than down (https://github.com/jruby/jruby/issues/6104)
|
55
|
+
if BSON::Environment.jruby?
|
56
|
+
(self - usec.to_r/1000000).to_i
|
57
|
+
else
|
58
|
+
to_i
|
59
|
+
end
|
60
|
+
end
|
49
61
|
end
|
50
62
|
|
51
63
|
# Enrich the ActiveSupport::TimeWithZone class with this module.
|
data/lib/bson/timestamp.rb
CHANGED
@@ -1,4 +1,5 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
|
+
# rubocop:todo all
|
2
3
|
# Copyright (C) 2009-2020 MongoDB Inc.
|
3
4
|
#
|
4
5
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
@@ -124,7 +125,7 @@ module BSON
|
|
124
125
|
# @see http://bsonspec.org/#/specification
|
125
126
|
#
|
126
127
|
# @since 2.0.0
|
127
|
-
def to_bson(buffer = ByteBuffer.new
|
128
|
+
def to_bson(buffer = ByteBuffer.new)
|
128
129
|
buffer.put_uint32(increment)
|
129
130
|
buffer.put_uint32(seconds)
|
130
131
|
end
|
data/lib/bson/true_class.rb
CHANGED
@@ -1,4 +1,5 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
|
+
# rubocop:todo all
|
2
3
|
# Copyright (C) 2009-2020 MongoDB Inc.
|
3
4
|
#
|
4
5
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
@@ -50,7 +51,7 @@ module BSON
|
|
50
51
|
# @see http://bsonspec.org/#/specification
|
51
52
|
#
|
52
53
|
# @since 2.0.0
|
53
|
-
def to_bson(buffer = ByteBuffer.new
|
54
|
+
def to_bson(buffer = ByteBuffer.new)
|
54
55
|
buffer.put_byte(TRUE_BYTE)
|
55
56
|
end
|
56
57
|
end
|
data/lib/bson/undefined.rb
CHANGED
@@ -1,4 +1,5 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
|
+
# rubocop:todo all
|
2
3
|
# Copyright (C) 2009-2020 MongoDB Inc.
|
3
4
|
#
|
4
5
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
@@ -21,6 +22,7 @@ module BSON
|
|
21
22
|
#
|
22
23
|
# @since 2.0.0
|
23
24
|
class Undefined
|
25
|
+
include JSON
|
24
26
|
include Specialized
|
25
27
|
|
26
28
|
# Undefined is type 0x06 in the BSON spec.
|
@@ -42,6 +44,18 @@ module BSON
|
|
42
44
|
self.class == other.class
|
43
45
|
end
|
44
46
|
|
47
|
+
# Return a string representation of the BSON::Undefined for use in
|
48
|
+
# application-level JSON serialization. This method is intentionally
|
49
|
+
# different from #as_extended_json.
|
50
|
+
#
|
51
|
+
# @example Get the undefined as a JSON-serializable object.
|
52
|
+
# undefined.as_json
|
53
|
+
#
|
54
|
+
# @return [ nil ] The undefined as nil.
|
55
|
+
def as_json(*args)
|
56
|
+
nil
|
57
|
+
end
|
58
|
+
|
45
59
|
# Converts this object to a representation directly serializable to
|
46
60
|
# Extended JSON (https://github.com/mongodb/specifications/blob/master/source/extended-json.rst).
|
47
61
|
#
|
data/lib/bson/version.rb
CHANGED
@@ -1,4 +1,5 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
|
+
# rubocop:todo all
|
2
3
|
# Copyright (C) 2009-2020 MongoDB Inc.
|
3
4
|
#
|
4
5
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
@@ -14,5 +15,5 @@
|
|
14
15
|
# limitations under the License.
|
15
16
|
|
16
17
|
module BSON
|
17
|
-
VERSION = "
|
18
|
+
VERSION = "5.0.0"
|
18
19
|
end
|
data/lib/bson.rb
CHANGED
@@ -1,4 +1,5 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
|
+
# rubocop:todo all
|
2
3
|
# Copyright (C) 2009-2020 MongoDB Inc.
|
3
4
|
#
|
4
5
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
@@ -27,7 +28,7 @@ module BSON
|
|
27
28
|
#
|
28
29
|
# @param [ String ] string The string to create the id from.
|
29
30
|
#
|
30
|
-
# @raise [ BSON::
|
31
|
+
# @raise [ BSON::Error::InvalidObjectId ] If the provided string is invalid.
|
31
32
|
#
|
32
33
|
# @return [ BSON::ObjectId ] The new object id.
|
33
34
|
#
|
@@ -103,7 +104,7 @@ require "bson/version"
|
|
103
104
|
begin
|
104
105
|
if BSON::Environment.jruby?
|
105
106
|
require "bson-ruby.jar"
|
106
|
-
JRuby::Util.load_ext("org.
|
107
|
+
JRuby::Util.load_ext("org.bson_ruby.NativeService")
|
107
108
|
else
|
108
109
|
require "bson_native"
|
109
110
|
end
|
data/spec/bson/array_spec.rb
CHANGED
@@ -1,3 +1,4 @@
|
|
1
|
+
# rubocop:todo all
|
1
2
|
# Copyright (C) 2009-2020 MongoDB Inc.
|
2
3
|
#
|
3
4
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
@@ -34,65 +35,6 @@ describe Array do
|
|
34
35
|
[ { "$testing" => "value" } ]
|
35
36
|
end
|
36
37
|
|
37
|
-
context "when validating keys" do
|
38
|
-
|
39
|
-
context "when validating globally" do
|
40
|
-
|
41
|
-
before do
|
42
|
-
BSON::Config.validating_keys = true
|
43
|
-
end
|
44
|
-
|
45
|
-
after do
|
46
|
-
BSON::Config.validating_keys = false
|
47
|
-
end
|
48
|
-
|
49
|
-
it "raises an error" do
|
50
|
-
expect {
|
51
|
-
obj.to_bson
|
52
|
-
}.to raise_error(BSON::String::IllegalKey)
|
53
|
-
end
|
54
|
-
end
|
55
|
-
|
56
|
-
context "when validating locally" do
|
57
|
-
|
58
|
-
it "raises an error" do
|
59
|
-
expect {
|
60
|
-
obj.to_bson(BSON::ByteBuffer.new, true)
|
61
|
-
}.to raise_error(BSON::String::IllegalKey)
|
62
|
-
end
|
63
|
-
|
64
|
-
context "when serializing different types" do
|
65
|
-
|
66
|
-
let(:obj) do
|
67
|
-
[ BSON::Binary.new("testing", :generic),
|
68
|
-
BSON::Code.new("this.value = 5"),
|
69
|
-
BSON::CodeWithScope.new("this.value = val", "test"),
|
70
|
-
Date.new(2012, 1, 1),
|
71
|
-
Time.utc(2012, 1, 1),
|
72
|
-
DateTime.new(2012, 1, 1, 0, 0, 0),
|
73
|
-
false,
|
74
|
-
1.2332,
|
75
|
-
Integer::MAX_32BIT - 1,
|
76
|
-
BSON::ObjectId.new,
|
77
|
-
/\W+/i,
|
78
|
-
'a string',
|
79
|
-
:a_symbol,
|
80
|
-
Time.utc(2012, 1, 1, 0, 0, 0),
|
81
|
-
BSON::Timestamp.new(1, 10),
|
82
|
-
true,
|
83
|
-
{ "$testing" => "value" }
|
84
|
-
]
|
85
|
-
end
|
86
|
-
|
87
|
-
it "raises an error" do
|
88
|
-
expect {
|
89
|
-
obj.to_bson(BSON::ByteBuffer.new, true)
|
90
|
-
}.to raise_error(BSON::String::IllegalKey)
|
91
|
-
end
|
92
|
-
end
|
93
|
-
end
|
94
|
-
end
|
95
|
-
|
96
38
|
context "when not validating keys" do
|
97
39
|
|
98
40
|
let(:bson) do
|
@@ -186,8 +128,25 @@ describe Array do
|
|
186
128
|
it "raises an exception" do
|
187
129
|
expect {
|
188
130
|
[ 1 ].to_bson_object_id
|
189
|
-
}.to raise_error(BSON::
|
131
|
+
}.to raise_error(BSON::Error::InvalidObjectId)
|
190
132
|
end
|
191
133
|
end
|
192
134
|
end
|
135
|
+
|
136
|
+
describe '#as_extended_json' do
|
137
|
+
|
138
|
+
let(:object) do
|
139
|
+
['one', :two, 3, 4.0, nil]
|
140
|
+
end
|
141
|
+
|
142
|
+
let(:expected) do
|
143
|
+
["one", { "$symbol" => "two" }, { "$numberInt" => "3" }, { "$numberDouble"=> "4.0" }, nil]
|
144
|
+
end
|
145
|
+
|
146
|
+
it 'returns the extended serialization' do
|
147
|
+
expect(object.as_extended_json).to eq(expected)
|
148
|
+
end
|
149
|
+
|
150
|
+
it_behaves_like 'an Extended JSON serializable object'
|
151
|
+
end
|
193
152
|
end
|
@@ -1,3 +1,4 @@
|
|
1
|
+
# rubocop:todo all
|
1
2
|
# Copyright (C) 2016-2021 MongoDB Inc.
|
2
3
|
#
|
3
4
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
@@ -14,7 +15,7 @@
|
|
14
15
|
|
15
16
|
require "spec_helper"
|
16
17
|
|
17
|
-
describe
|
18
|
+
describe BigDecimal do
|
18
19
|
|
19
20
|
describe '#from_bson' do
|
20
21
|
shared_examples_for 'a BSON::BigDecimal deserializer' do
|
@@ -35,7 +36,7 @@ describe BSON::BigDecimal do
|
|
35
36
|
if deserialized_decimal128.to_s == "NaN"
|
36
37
|
expect(deserialized_big_decimal.nan?).to be true
|
37
38
|
else
|
38
|
-
expect(deserialized_big_decimal).to eq(deserialized_decimal128.
|
39
|
+
expect(deserialized_big_decimal).to eq(deserialized_decimal128.to_d)
|
39
40
|
end
|
40
41
|
end
|
41
42
|
end
|
@@ -211,7 +212,7 @@ describe BSON::BigDecimal do
|
|
211
212
|
it "raises an error" do
|
212
213
|
expect do
|
213
214
|
BigDecimal(argument).to_bson
|
214
|
-
end.to raise_error(BSON::
|
215
|
+
end.to raise_error(BSON::Error::InvalidDecimal128Range)
|
215
216
|
end
|
216
217
|
end
|
217
218
|
|
@@ -221,7 +222,7 @@ describe BSON::BigDecimal do
|
|
221
222
|
it "raises an error" do
|
222
223
|
expect do
|
223
224
|
BigDecimal(argument).to_bson
|
224
|
-
end.to raise_error(BSON::
|
225
|
+
end.to raise_error(BSON::Error::UnrepresentablePrecision)
|
225
226
|
end
|
226
227
|
end
|
227
228
|
end
|
@@ -313,4 +314,15 @@ describe BSON::BigDecimal do
|
|
313
314
|
it_behaves_like 'a BSON::BigDecimal round trip'
|
314
315
|
end
|
315
316
|
end
|
317
|
+
|
318
|
+
context "when the class is loaded" do
|
319
|
+
|
320
|
+
let(:registered) do
|
321
|
+
BSON::Registry.get(described_class::BSON_TYPE, 'field')
|
322
|
+
end
|
323
|
+
|
324
|
+
it "registers the type" do
|
325
|
+
expect(registered).to eq(described_class)
|
326
|
+
end
|
327
|
+
end
|
316
328
|
end
|
data/spec/bson/binary_spec.rb
CHANGED
@@ -1,3 +1,4 @@
|
|
1
|
+
# rubocop:todo all
|
1
2
|
# Copyright (C) 2009-2020 MongoDB Inc.
|
2
3
|
#
|
3
4
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
@@ -55,19 +56,20 @@ describe BSON::Binary do
|
|
55
56
|
expect(hash[not_testing]).to be_nil
|
56
57
|
end
|
57
58
|
|
58
|
-
describe "#
|
59
|
+
describe "#as_extended_json" do
|
59
60
|
|
60
61
|
let(:object) do
|
61
62
|
described_class.new("testing", :user)
|
62
63
|
end
|
63
64
|
|
64
65
|
it "returns the binary data plus type" do
|
65
|
-
expect(object.
|
66
|
+
expect(object.as_extended_json).to eq(
|
66
67
|
{ "$binary" => {'base64' => Base64.encode64("testing").strip, "subType" => '80' }}
|
67
68
|
)
|
68
69
|
end
|
69
70
|
|
70
|
-
it_behaves_like
|
71
|
+
it_behaves_like 'an Extended JSON serializable object'
|
72
|
+
it_behaves_like '#as_json calls #as_extended_json'
|
71
73
|
end
|
72
74
|
|
73
75
|
describe "#initialize" do
|
@@ -80,17 +82,26 @@ describe BSON::Binary do
|
|
80
82
|
end
|
81
83
|
end
|
82
84
|
|
83
|
-
context "when
|
85
|
+
context "when the type is invalid" do
|
84
86
|
|
85
87
|
it "raises an error" do
|
86
88
|
expect {
|
87
89
|
described_class.new("testing", :error)
|
88
90
|
}.to raise_error { |error|
|
89
|
-
expect(error).to be_a(BSON::
|
91
|
+
expect(error).to be_a(BSON::Error::InvalidBinaryType)
|
90
92
|
expect(error.message).to match /is not a valid binary type/
|
91
93
|
}
|
92
94
|
end
|
93
95
|
end
|
96
|
+
|
97
|
+
context 'when initialized via legacy YAML' do
|
98
|
+
let(:yaml) { "--- !ruby/object:BSON::Binary\ndata: hello\ntype: :generic\n" }
|
99
|
+
let(:deserialized) { YAML.safe_load(yaml, permitted_classes: [ Symbol, BSON::Binary ]) }
|
100
|
+
|
101
|
+
it 'correctly sets the raw_type' do
|
102
|
+
expect(deserialized.raw_type).to be == BSON::Binary::SUBTYPES[:generic]
|
103
|
+
end
|
104
|
+
end
|
94
105
|
end
|
95
106
|
|
96
107
|
describe '#inspect' do
|
@@ -162,75 +173,73 @@ describe BSON::Binary do
|
|
162
173
|
|
163
174
|
it_behaves_like "a bson element"
|
164
175
|
|
165
|
-
|
166
|
-
|
167
|
-
|
168
|
-
|
169
|
-
|
170
|
-
|
171
|
-
|
172
|
-
|
173
|
-
|
174
|
-
|
175
|
-
|
176
|
-
|
177
|
-
|
178
|
-
|
179
|
-
|
180
|
-
|
181
|
-
|
182
|
-
|
183
|
-
|
184
|
-
|
185
|
-
|
186
|
-
|
187
|
-
|
188
|
-
|
189
|
-
|
190
|
-
|
191
|
-
|
192
|
-
|
193
|
-
|
194
|
-
|
195
|
-
|
196
|
-
|
197
|
-
|
198
|
-
|
199
|
-
|
200
|
-
|
201
|
-
|
202
|
-
|
203
|
-
|
204
|
-
|
205
|
-
|
206
|
-
|
207
|
-
|
208
|
-
|
209
|
-
|
210
|
-
|
211
|
-
|
212
|
-
|
213
|
-
|
214
|
-
|
215
|
-
|
216
|
-
|
217
|
-
|
218
|
-
|
219
|
-
|
220
|
-
|
221
|
-
|
222
|
-
|
223
|
-
|
224
|
-
|
225
|
-
|
226
|
-
|
227
|
-
|
228
|
-
|
229
|
-
|
230
|
-
|
231
|
-
|
232
|
-
it_behaves_like "a serializable bson element"
|
233
|
-
it_behaves_like "a deserializable bson element"
|
176
|
+
[
|
177
|
+
{
|
178
|
+
types: [ nil, 0, 0.chr, :generic, 'generic' ],
|
179
|
+
bson: "#{7.to_bson}#{0.chr}testing",
|
180
|
+
type: :generic,
|
181
|
+
},
|
182
|
+
{
|
183
|
+
types: [ 1, 1.chr, :function, 'function' ],
|
184
|
+
bson: "#{7.to_bson}#{1.chr}testing",
|
185
|
+
type: :function,
|
186
|
+
},
|
187
|
+
{
|
188
|
+
types: [ 2, 2.chr, :old, 'old' ],
|
189
|
+
bson: "#{11.to_bson}#{2.chr}#{7.to_bson}testing",
|
190
|
+
type: :old,
|
191
|
+
},
|
192
|
+
{
|
193
|
+
types: [ 3, 3.chr, :uuid_old, 'uuid_old' ],
|
194
|
+
bson: "#{7.to_bson}#{3.chr}testing",
|
195
|
+
type: :uuid_old,
|
196
|
+
},
|
197
|
+
{
|
198
|
+
types: [ 4, 4.chr, :uuid, 'uuid' ],
|
199
|
+
bson: "#{7.to_bson}#{4.chr}testing",
|
200
|
+
type: :uuid,
|
201
|
+
},
|
202
|
+
{
|
203
|
+
types: [ 5, 5.chr, :md5, 'md5' ],
|
204
|
+
bson: "#{7.to_bson}#{5.chr}testing",
|
205
|
+
type: :md5,
|
206
|
+
},
|
207
|
+
{
|
208
|
+
types: [ 6, 6.chr, :ciphertext, 'ciphertext' ],
|
209
|
+
bson: "#{7.to_bson}#{6.chr}testing",
|
210
|
+
type: :ciphertext,
|
211
|
+
},
|
212
|
+
{
|
213
|
+
types: [ 0x80, 0x80.chr, :user, 'user' ],
|
214
|
+
bson: "#{7.to_bson}#{128.chr}testing",
|
215
|
+
type: :user,
|
216
|
+
},
|
217
|
+
{
|
218
|
+
types: [ 0xFF, 0xFF.chr ],
|
219
|
+
bson: "#{7.to_bson}#{0xFF.chr}testing",
|
220
|
+
type: :user,
|
221
|
+
},
|
222
|
+
].each do |defn|
|
223
|
+
defn[:types].each do |type|
|
224
|
+
context "when the type is #{type ? type.inspect : 'not provided'}" do
|
225
|
+
let(:obj) do
|
226
|
+
if type
|
227
|
+
described_class.new("testing", type)
|
228
|
+
else
|
229
|
+
described_class.new("testing")
|
230
|
+
end
|
231
|
+
end
|
232
|
+
|
233
|
+
let(:bson) { defn[:bson] }
|
234
|
+
|
235
|
+
it_behaves_like "a serializable bson element"
|
236
|
+
it_behaves_like "a deserializable bson element"
|
237
|
+
|
238
|
+
it "reports its type as #{defn[:type].inspect}" do
|
239
|
+
expect(obj.type).to be == defn[:type]
|
240
|
+
end
|
241
|
+
end
|
242
|
+
end
|
234
243
|
end
|
235
244
|
|
236
245
|
context 'when given binary string' do
|
data/spec/bson/boolean_spec.rb
CHANGED