bson 4.15.0 → 5.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (147) hide show
  1. checksums.yaml +4 -4
  2. checksums.yaml.gz.sig +0 -0
  3. data/README.md +4 -4
  4. data/Rakefile +5 -0
  5. data/ext/bson/bson-native.h +12 -4
  6. data/ext/bson/extconf.rb +8 -3
  7. data/ext/bson/init.c +11 -11
  8. data/ext/bson/read.c +21 -6
  9. data/ext/bson/util.c +168 -16
  10. data/ext/bson/write.c +30 -39
  11. data/lib/bson/active_support.rb +1 -0
  12. data/lib/bson/array.rb +57 -31
  13. data/lib/bson/big_decimal.rb +16 -6
  14. data/lib/bson/binary.rb +255 -128
  15. data/lib/bson/boolean.rb +1 -0
  16. data/lib/bson/code.rb +9 -11
  17. data/lib/bson/code_with_scope.rb +8 -10
  18. data/lib/bson/config.rb +1 -27
  19. data/lib/bson/date.rb +2 -1
  20. data/lib/bson/date_time.rb +2 -1
  21. data/lib/bson/db_pointer.rb +11 -12
  22. data/lib/bson/dbref.rb +11 -9
  23. data/lib/bson/decimal128/builder.rb +9 -8
  24. data/lib/bson/decimal128.rb +24 -110
  25. data/lib/bson/document.rb +1 -0
  26. data/lib/bson/environment.rb +1 -0
  27. data/lib/bson/error/bson_decode_error.rb +11 -0
  28. data/lib/bson/error/ext_json_parse_error.rb +11 -0
  29. data/lib/bson/error/illegal_key.rb +23 -0
  30. data/lib/bson/error/invalid_binary_type.rb +37 -0
  31. data/lib/bson/error/invalid_dbref_argument.rb +12 -0
  32. data/lib/bson/error/invalid_decimal128_argument.rb +25 -0
  33. data/lib/bson/error/invalid_decimal128_range.rb +27 -0
  34. data/lib/bson/error/invalid_decimal128_string.rb +26 -0
  35. data/lib/bson/error/invalid_key.rb +24 -0
  36. data/lib/bson/error/invalid_object_id.rb +11 -0
  37. data/lib/bson/error/invalid_regexp_pattern.rb +13 -0
  38. data/lib/bson/error/unrepresentable_precision.rb +19 -0
  39. data/lib/bson/error/unserializable_class.rb +13 -0
  40. data/lib/bson/error/unsupported_binary_subtype.rb +12 -0
  41. data/lib/bson/error/unsupported_type.rb +11 -0
  42. data/lib/bson/error.rb +16 -28
  43. data/lib/bson/ext_json.rb +1 -0
  44. data/lib/bson/false_class.rb +2 -1
  45. data/lib/bson/float.rb +2 -1
  46. data/lib/bson/hash.rb +127 -72
  47. data/lib/bson/int32.rb +16 -4
  48. data/lib/bson/int64.rb +16 -4
  49. data/lib/bson/integer.rb +3 -4
  50. data/lib/bson/json.rb +1 -0
  51. data/lib/bson/max_key.rb +7 -9
  52. data/lib/bson/min_key.rb +7 -9
  53. data/lib/bson/nil_class.rb +1 -0
  54. data/lib/bson/object.rb +5 -25
  55. data/lib/bson/object_id.rb +75 -121
  56. data/lib/bson/open_struct.rb +3 -2
  57. data/lib/bson/regexp.rb +35 -64
  58. data/lib/bson/registry.rb +2 -6
  59. data/lib/bson/specialized.rb +2 -1
  60. data/lib/bson/string.rb +4 -27
  61. data/lib/bson/symbol.rb +22 -19
  62. data/lib/bson/time.rb +2 -1
  63. data/lib/bson/time_with_zone.rb +13 -1
  64. data/lib/bson/timestamp.rb +2 -1
  65. data/lib/bson/true_class.rb +2 -1
  66. data/lib/bson/undefined.rb +14 -0
  67. data/lib/bson/version.rb +2 -1
  68. data/lib/bson.rb +3 -2
  69. data/spec/bson/array_spec.rb +19 -60
  70. data/spec/bson/big_decimal_spec.rb +16 -4
  71. data/spec/bson/binary_spec.rb +83 -74
  72. data/spec/bson/binary_uuid_spec.rb +1 -0
  73. data/spec/bson/boolean_spec.rb +1 -0
  74. data/spec/bson/byte_buffer_read_spec.rb +1 -0
  75. data/spec/bson/byte_buffer_spec.rb +1 -0
  76. data/spec/bson/byte_buffer_write_spec.rb +1 -0
  77. data/spec/bson/code_spec.rb +5 -3
  78. data/spec/bson/code_with_scope_spec.rb +5 -3
  79. data/spec/bson/config_spec.rb +1 -35
  80. data/spec/bson/date_spec.rb +1 -0
  81. data/spec/bson/date_time_spec.rb +1 -0
  82. data/spec/bson/dbref_legacy_spec.rb +20 -3
  83. data/spec/bson/dbref_spec.rb +9 -9
  84. data/spec/bson/decimal128_spec.rb +40 -20
  85. data/spec/bson/document_as_spec.rb +1 -0
  86. data/spec/bson/document_spec.rb +1 -1
  87. data/spec/bson/ext_json_parse_spec.rb +1 -0
  88. data/spec/bson/false_class_spec.rb +8 -0
  89. data/spec/bson/float_spec.rb +8 -3
  90. data/spec/bson/hash_as_spec.rb +1 -0
  91. data/spec/bson/hash_spec.rb +87 -75
  92. data/spec/bson/int32_spec.rb +21 -6
  93. data/spec/bson/int64_spec.rb +21 -6
  94. data/spec/bson/integer_spec.rb +45 -13
  95. data/spec/bson/json_spec.rb +1 -0
  96. data/spec/bson/max_key_spec.rb +5 -3
  97. data/spec/bson/min_key_spec.rb +5 -3
  98. data/spec/bson/nil_class_spec.rb +1 -0
  99. data/spec/bson/object_id_spec.rb +43 -4
  100. data/spec/bson/object_spec.rb +2 -1
  101. data/spec/bson/open_struct_spec.rb +14 -71
  102. data/spec/bson/raw_spec.rb +9 -15
  103. data/spec/bson/regexp_spec.rb +4 -3
  104. data/spec/bson/registry_spec.rb +2 -1
  105. data/spec/bson/string_spec.rb +13 -38
  106. data/spec/bson/symbol_raw_spec.rb +25 -0
  107. data/spec/bson/symbol_spec.rb +15 -18
  108. data/spec/bson/time_spec.rb +1 -0
  109. data/spec/bson/time_with_zone_spec.rb +1 -0
  110. data/spec/bson/timestamp_spec.rb +1 -0
  111. data/spec/bson/true_class_spec.rb +8 -0
  112. data/spec/bson/undefined_spec.rb +27 -0
  113. data/spec/bson_spec.rb +1 -0
  114. data/spec/runners/common_driver.rb +6 -5
  115. data/spec/runners/corpus.rb +6 -0
  116. data/spec/runners/corpus_legacy.rb +1 -0
  117. data/spec/shared/lib/mrss/constraints.rb +8 -16
  118. data/spec/shared/lib/mrss/docker_runner.rb +30 -3
  119. data/spec/shared/lib/mrss/eg_config_utils.rb +51 -0
  120. data/spec/shared/lib/mrss/event_subscriber.rb +15 -5
  121. data/spec/shared/lib/mrss/lite_constraints.rb +48 -1
  122. data/spec/shared/lib/mrss/server_version_registry.rb +16 -23
  123. data/spec/shared/lib/mrss/session_registry.rb +69 -0
  124. data/spec/shared/lib/mrss/session_registry_legacy.rb +60 -0
  125. data/spec/shared/lib/mrss/utils.rb +28 -6
  126. data/spec/shared/share/Dockerfile.erb +68 -85
  127. data/spec/shared/shlib/config.sh +27 -0
  128. data/spec/shared/shlib/server.sh +73 -24
  129. data/spec/shared/shlib/set_env.sh +39 -1
  130. data/spec/spec_helper.rb +1 -0
  131. data/spec/spec_tests/common_driver_spec.rb +9 -4
  132. data/spec/spec_tests/corpus_legacy_spec.rb +1 -0
  133. data/spec/spec_tests/corpus_spec.rb +13 -3
  134. data/spec/spec_tests/data/corpus/binary.json +5 -0
  135. data/spec/spec_tests/data/corpus/code.json +13 -13
  136. data/spec/spec_tests/data/corpus/decimal128-4.json +48 -0
  137. data/spec/spec_tests/data/corpus/decimal128-6.json +12 -0
  138. data/spec/spec_tests/data/corpus/decimal128-7.json +4 -0
  139. data/spec/spec_tests/data/corpus/document.json +20 -0
  140. data/spec/spec_tests/data/corpus/symbol.json +7 -7
  141. data/spec/spec_tests/data/corpus/top.json +18 -3
  142. data/spec/support/shared_examples.rb +28 -5
  143. data/spec/support/spec_config.rb +1 -0
  144. data/spec/support/utils.rb +49 -1
  145. data.tar.gz.sig +0 -0
  146. metadata +167 -144
  147. 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, validating_keys = Config.validating_keys?)
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(validating_keys = Config.validating_keys?)
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::ObjectId::Invalid ] If the string is not 12 elements.
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, validating_keys = Config.validating_keys?)
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(validating_keys = Config.validating_keys?)
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(**options)
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, validating_keys = Config.validating_keys?)
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
- # Converts this object to a representation directly serializable to
160
- # Extended JSON (https://github.com/mongodb/specifications/blob/master/source/extended-json.rst).
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
- # This method returns the integer value if relaxed representation is
163
- # requested, otherwise a $numberLong hash.
160
+ # @example Get the raw symbol as a JSON-serializable object.
161
+ # raw_symbol.as_json
164
162
  #
165
- # @option options [ true | false ] :relaxed Whether to produce relaxed
166
- # extended JSON representation.
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 | Integer ] The extended json representation.
169
- def as_extended_json(**options)
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, validating_keys = Config.validating_keys?)
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
@@ -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, validating_keys = Config.validating_keys?)
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.
@@ -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, validating_keys = Config.validating_keys?)
128
+ def to_bson(buffer = ByteBuffer.new)
128
129
  buffer.put_uint32(increment)
129
130
  buffer.put_uint32(seconds)
130
131
  end
@@ -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, validating_keys = Config.validating_keys?)
54
+ def to_bson(buffer = ByteBuffer.new)
54
55
  buffer.put_byte(TRUE_BYTE)
55
56
  end
56
57
  end
@@ -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 = "4.15.0"
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::ObjectId::Invalid ] If the provided string is invalid.
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.bson.NativeService")
107
+ JRuby::Util.load_ext("org.bson_ruby.NativeService")
107
108
  else
108
109
  require "bson_native"
109
110
  end
@@ -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::ObjectId::Invalid)
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 BSON::BigDecimal do
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.to_big_decimal)
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::Decimal128::InvalidRange)
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::Decimal128::UnrepresentablePrecision)
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
@@ -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 "#as_json" do
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.as_json).to eq(
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 "a JSON serializable object"
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 he type is invalid" do
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::Binary::InvalidType)
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
- context "when the type is :generic" do
166
-
167
- let(:obj) { described_class.new("testing") }
168
- let(:bson) { "#{7.to_bson}#{0.chr}testing" }
169
-
170
- it_behaves_like "a serializable bson element"
171
- it_behaves_like "a deserializable bson element"
172
- end
173
-
174
- context "when the type is :function" do
175
-
176
- let(:obj) { described_class.new("testing", :function) }
177
- let(:bson) { "#{7.to_bson}#{1.chr}testing" }
178
-
179
- it_behaves_like "a serializable bson element"
180
- it_behaves_like "a deserializable bson element"
181
- end
182
-
183
- context "when the type is :old" do
184
-
185
- let(:obj) { described_class.new("testing", :old) }
186
- let(:bson) { "#{11.to_bson}#{2.chr}#{7.to_bson}testing" }
187
-
188
- it_behaves_like "a serializable bson element"
189
- it_behaves_like "a deserializable bson element"
190
- end
191
-
192
- context "when the type is :uuid_old" do
193
-
194
- let(:obj) { described_class.new("testing", :uuid_old) }
195
- let(:bson) { "#{7.to_bson}#{3.chr}testing" }
196
-
197
- it_behaves_like "a serializable bson element"
198
- it_behaves_like "a deserializable bson element"
199
- end
200
-
201
- context "when the type is :uuid" do
202
-
203
- let(:obj) { described_class.new("testing", :uuid) }
204
- let(:bson) { "#{7.to_bson}#{4.chr}testing" }
205
-
206
- it_behaves_like "a serializable bson element"
207
- it_behaves_like "a deserializable bson element"
208
- end
209
-
210
- context "when the type is :md5" do
211
-
212
- let(:obj) { described_class.new("testing", :md5) }
213
- let(:bson) { "#{7.to_bson}#{5.chr}testing" }
214
-
215
- it_behaves_like "a serializable bson element"
216
- it_behaves_like "a deserializable bson element"
217
- end
218
-
219
- context "when the type is :user" do
220
-
221
- let(:obj) { described_class.new("testing", :user) }
222
- let(:bson) { "#{7.to_bson}#{128.chr}testing" }
223
-
224
- it_behaves_like "a serializable bson element"
225
- it_behaves_like "a deserializable bson element"
226
- end
227
-
228
- context "when the type is :cyphertext" do
229
- let(:obj) { described_class.new("testing", :ciphertext) }
230
- let(:bson) { "#{7.to_bson}#{6.chr}testing" }
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
@@ -1,3 +1,4 @@
1
+ # rubocop:todo all
1
2
  # Copyright (C) 2019-2020 MongoDB Inc.
2
3
  #
3
4
  # Licensed under the Apache License, Version 2.0 (the "License");
@@ -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");
@@ -1,3 +1,4 @@
1
+ # rubocop:todo all
1
2
  require 'spec_helper'
2
3
 
3
4
  describe BSON::ByteBuffer do