bson 1.0.9 → 1.1
Sign up to get free protection for your applications and to get access to all the features.
Potentially problematic release.
This version of bson might be problematic. Click here for more details.
- data/Rakefile +23 -0
- data/bson.gemspec +1 -0
- data/ext/java/jar/bson.jar +0 -0
- data/ext/java/jar/jbson.jar +0 -0
- data/ext/java/jar/jruby.jar +0 -0
- data/ext/java/jar/mongo.jar +0 -0
- data/lib/bson.rb +32 -22
- data/lib/bson/bson_java.rb +21 -0
- data/lib/bson/bson_ruby.rb +7 -8
- data/lib/bson/ordered_hash.rb +2 -2
- data/lib/bson/types/code.rb +16 -3
- data/lib/bson/types/dbref.rb +4 -0
- data/lib/bson/types/object_id.rb +9 -0
- metadata +44 -63
- data/lib/bson/types/objectid.rb +0 -187
- data/test/mongo_bson/basic_test.rb +0 -99
- data/test/mongo_bson/binary_test.rb +0 -15
- data/test/mongo_bson/bson_test.rb +0 -543
- data/test/mongo_bson/byte_buffer_test.rb +0 -190
- data/test/mongo_bson/jruby_bson_test.rb +0 -227
- data/test/mongo_bson/jruby_encode_test.rb +0 -396
- data/test/mongo_bson/json_test.rb +0 -16
- data/test/mongo_bson/object_id_test.rb +0 -132
- data/test/mongo_bson/ordered_hash_test.rb +0 -197
@@ -1,99 +0,0 @@
|
|
1
|
-
# encoding:utf-8
|
2
|
-
require './test/test_helper'
|
3
|
-
require 'complex'
|
4
|
-
require 'bigdecimal'
|
5
|
-
require 'rational'
|
6
|
-
require 'benchmark'
|
7
|
-
|
8
|
-
MEDIUM = {
|
9
|
-
'integer' => 5,
|
10
|
-
'number' => 5.05,
|
11
|
-
'boolean' => false,
|
12
|
-
'array' => ['test', 'benchmark']
|
13
|
-
}
|
14
|
-
|
15
|
-
|
16
|
-
LARGE = {
|
17
|
-
'base_url' => 'http://www.example.com/test-me',
|
18
|
-
'total_word_count' => 6743,
|
19
|
-
'access_time' => Time.now,
|
20
|
-
'meta_tags' => {
|
21
|
-
'description' => 'i am a long description string',
|
22
|
-
'author' => 'Holly Man',
|
23
|
-
'dynamically_created_meta_tag' => 'who know\n what'
|
24
|
-
},
|
25
|
-
'page_structure' => {
|
26
|
-
'counted_tags' => 3450,
|
27
|
-
'no_of_js_attached' => 10,
|
28
|
-
'no_of_images' => 6
|
29
|
-
},
|
30
|
-
'harvested_words' => ['10gen','web','open','source','application','paas',
|
31
|
-
'platform-as-a-service','technology','helps',
|
32
|
-
'developers','focus','building','mongodb','mongo'] * 20
|
33
|
-
}
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
begin
|
38
|
-
require 'active_support/core_ext'
|
39
|
-
require 'active_support/hash_with_indifferent_access'
|
40
|
-
Time.zone = "Pacific Time (US & Canada)"
|
41
|
-
Zone = Time.zone.now
|
42
|
-
rescue LoadError
|
43
|
-
warn 'Could not test BSON with HashWithIndifferentAccess.'
|
44
|
-
module ActiveSupport
|
45
|
-
class TimeWithZone
|
46
|
-
end
|
47
|
-
end
|
48
|
-
Zone = ActiveSupport::TimeWithZone.new
|
49
|
-
end
|
50
|
-
|
51
|
-
class BSONTest < Test::Unit::TestCase
|
52
|
-
include BSON
|
53
|
-
|
54
|
-
def test_string
|
55
|
-
doc = {'doc' => 'hello, world'}
|
56
|
-
bson = bson = BSON::BSON_CODER.serialize(doc)
|
57
|
-
assert_equal doc, BSON::BSON_CODER.deserialize(bson)
|
58
|
-
end
|
59
|
-
|
60
|
-
def test_object
|
61
|
-
doc = {'doc' => {'age' => 42, 'name' => 'Spongebob', 'shoe_size' => 9.5}}
|
62
|
-
bson = BSON::BSON_CODER.serialize(doc)
|
63
|
-
assert_equal doc, BSON::BSON_CODER.deserialize(bson)
|
64
|
-
end
|
65
|
-
|
66
|
-
def test_oid
|
67
|
-
doc = {'doc' => ObjectID.new}
|
68
|
-
bson = BSON::BSON_CODER.serialize(doc)
|
69
|
-
assert_equal doc, BSON::BSON_CODER.deserialize(bson)
|
70
|
-
end
|
71
|
-
|
72
|
-
def test_array
|
73
|
-
doc = {'doc' => [1, 2, "a", "b"]}
|
74
|
-
bson = BSON::BSON_CODER.serialize(doc)
|
75
|
-
assert_equal doc, BSON::BSON_CODER.deserialize(bson)
|
76
|
-
end
|
77
|
-
|
78
|
-
def test_speed
|
79
|
-
|
80
|
-
Benchmark.bm do |x|
|
81
|
-
x.report('serialize obj') do
|
82
|
-
1000.times do
|
83
|
-
BSON::BSON_CODER.serialize(LARGE)
|
84
|
-
end
|
85
|
-
end
|
86
|
-
end
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
Benchmark.bm do |x|
|
91
|
-
b = BSON::BSON_CODER.serialize(LARGE)
|
92
|
-
x.report('deserialize obj') do
|
93
|
-
1000.times do
|
94
|
-
BSON::BSON_CODER.deserialize(b)
|
95
|
-
end
|
96
|
-
end
|
97
|
-
end
|
98
|
-
end
|
99
|
-
end
|
@@ -1,15 +0,0 @@
|
|
1
|
-
# encoding:utf-8
|
2
|
-
require './test/test_helper'
|
3
|
-
|
4
|
-
class BinaryTest < Test::Unit::TestCase
|
5
|
-
context "Inspecting" do
|
6
|
-
setup do
|
7
|
-
@data = ("THIS IS BINARY " * 50).unpack("c*")
|
8
|
-
end
|
9
|
-
|
10
|
-
should "not display actual data" do
|
11
|
-
binary = BSON::Binary.new(@data)
|
12
|
-
assert_equal "<BSON::Binary:#{binary.object_id}>", binary.inspect
|
13
|
-
end
|
14
|
-
end
|
15
|
-
end
|
@@ -1,543 +0,0 @@
|
|
1
|
-
# encoding:utf-8
|
2
|
-
require './test/test_helper'
|
3
|
-
require 'complex'
|
4
|
-
require 'bigdecimal'
|
5
|
-
require 'rational'
|
6
|
-
|
7
|
-
begin
|
8
|
-
require 'active_support/core_ext'
|
9
|
-
Time.zone = "Pacific Time (US & Canada)"
|
10
|
-
Zone = Time.zone.now
|
11
|
-
rescue LoadError
|
12
|
-
warn 'Mocking time with zone'
|
13
|
-
module ActiveSupport
|
14
|
-
class TimeWithZone
|
15
|
-
end
|
16
|
-
end
|
17
|
-
Zone = ActiveSupport::TimeWithZone.new
|
18
|
-
end
|
19
|
-
|
20
|
-
class BSONTest < Test::Unit::TestCase
|
21
|
-
|
22
|
-
include BSON
|
23
|
-
|
24
|
-
def test_require_hash
|
25
|
-
assert_raise_error InvalidDocument, "takes a Hash" do
|
26
|
-
BSON.serialize('foo')
|
27
|
-
end
|
28
|
-
|
29
|
-
assert_raise_error InvalidDocument, "takes a Hash" do
|
30
|
-
BSON.serialize(Object.new)
|
31
|
-
end
|
32
|
-
|
33
|
-
assert_raise_error InvalidDocument, "takes a Hash" do
|
34
|
-
BSON.serialize(Set.new)
|
35
|
-
end
|
36
|
-
end
|
37
|
-
|
38
|
-
def test_read_bson_io_document
|
39
|
-
doc = {'doc' => 'hello, world'}
|
40
|
-
bson = BSON.serialize(doc)
|
41
|
-
io = StringIO.new
|
42
|
-
io.write(bson.to_s)
|
43
|
-
io.rewind
|
44
|
-
assert_equal BSON.deserialize(bson), BSON.read_bson_document(io)
|
45
|
-
end
|
46
|
-
|
47
|
-
def test_serialize_returns_byte_buffer
|
48
|
-
doc = {'doc' => 'hello, world'}
|
49
|
-
bson = BSON.serialize(doc)
|
50
|
-
assert bson.is_a?(ByteBuffer)
|
51
|
-
end
|
52
|
-
|
53
|
-
def test_deserialize_from_string
|
54
|
-
doc = {'doc' => 'hello, world'}
|
55
|
-
bson = BSON.serialize(doc)
|
56
|
-
string = bson.to_s
|
57
|
-
assert_equal doc, BSON.deserialize(string)
|
58
|
-
end
|
59
|
-
|
60
|
-
def test_deprecated_bson_module
|
61
|
-
doc = {'doc' => 'hello, world'}
|
62
|
-
bson = BSON.serialize(doc)
|
63
|
-
assert_equal doc, BSON.deserialize(bson)
|
64
|
-
end
|
65
|
-
|
66
|
-
def test_string
|
67
|
-
doc = {'doc' => 'hello, world'}
|
68
|
-
bson = bson = BSON::BSON_CODER.serialize(doc)
|
69
|
-
assert_equal doc, BSON::BSON_CODER.deserialize(bson)
|
70
|
-
end
|
71
|
-
|
72
|
-
def test_valid_utf8_string
|
73
|
-
doc = {'doc' => 'aé'}
|
74
|
-
bson = bson = BSON::BSON_CODER.serialize(doc)
|
75
|
-
assert_equal doc, BSON::BSON_CODER.deserialize(bson)
|
76
|
-
end
|
77
|
-
|
78
|
-
def test_valid_utf8_key
|
79
|
-
doc = {'aé' => 'hello'}
|
80
|
-
bson = bson = BSON::BSON_CODER.serialize(doc)
|
81
|
-
assert_equal doc, BSON::BSON_CODER.deserialize(bson)
|
82
|
-
end
|
83
|
-
|
84
|
-
def test_document_length
|
85
|
-
doc = {'name' => 'a' * 5 * 1024 * 1024}
|
86
|
-
assert_raise InvalidDocument do
|
87
|
-
assert BSON::BSON_CODER.serialize(doc)
|
88
|
-
end
|
89
|
-
end
|
90
|
-
|
91
|
-
# In 1.8 we test that other string encodings raise an exception.
|
92
|
-
# In 1.9 we test that they get auto-converted.
|
93
|
-
if RUBY_VERSION < '1.9'
|
94
|
-
require 'iconv'
|
95
|
-
def test_invalid_string
|
96
|
-
string = Iconv.conv('iso-8859-1', 'utf-8', 'aé')
|
97
|
-
doc = {'doc' => string}
|
98
|
-
assert_raise InvalidStringEncoding do
|
99
|
-
BSON::BSON_CODER.serialize(doc)
|
100
|
-
end
|
101
|
-
end
|
102
|
-
|
103
|
-
def test_invalid_key
|
104
|
-
key = Iconv.conv('iso-8859-1', 'utf-8', 'aé')
|
105
|
-
doc = {key => 'hello'}
|
106
|
-
assert_raise InvalidStringEncoding do
|
107
|
-
BSON::BSON_CODER.serialize(doc)
|
108
|
-
end
|
109
|
-
end
|
110
|
-
else
|
111
|
-
def test_non_utf8_string
|
112
|
-
bson = BSON::BSON_CODER.serialize({'str' => 'aé'.encode('iso-8859-1')})
|
113
|
-
result = BSON::BSON_CODER.deserialize(bson)['str']
|
114
|
-
assert_equal 'aé', result
|
115
|
-
assert_equal 'UTF-8', result.encoding.name
|
116
|
-
end
|
117
|
-
|
118
|
-
def test_non_utf8_key
|
119
|
-
bson = BSON::BSON_CODER.serialize({'aé'.encode('iso-8859-1') => 'hello'})
|
120
|
-
assert_equal 'hello', BSON::BSON_CODER.deserialize(bson)['aé']
|
121
|
-
end
|
122
|
-
|
123
|
-
# Based on a test from sqlite3-ruby
|
124
|
-
def test_default_internal_is_honored
|
125
|
-
before_enc = Encoding.default_internal
|
126
|
-
|
127
|
-
str = "壁に耳あり、障子に目あり"
|
128
|
-
bson = BSON::BSON_CODER.serialize("x" => str)
|
129
|
-
|
130
|
-
Encoding.default_internal = 'EUC-JP'
|
131
|
-
out = BSON::BSON_CODER.deserialize(bson)["x"]
|
132
|
-
|
133
|
-
assert_equal Encoding.default_internal, out.encoding
|
134
|
-
assert_equal str.encode('EUC-JP'), out
|
135
|
-
assert_equal str, out.encode(str.encoding)
|
136
|
-
ensure
|
137
|
-
Encoding.default_internal = before_enc
|
138
|
-
end
|
139
|
-
end
|
140
|
-
|
141
|
-
def test_code
|
142
|
-
doc = {'$where' => Code.new('this.a.b < this.b')}
|
143
|
-
bson = BSON::BSON_CODER.serialize(doc)
|
144
|
-
assert_equal doc, BSON::BSON_CODER.deserialize(bson)
|
145
|
-
end
|
146
|
-
|
147
|
-
def test_code_with_scope
|
148
|
-
doc = {'$where' => Code.new('this.a.b < this.b', {'foo' => 1})}
|
149
|
-
bson = BSON::BSON_CODER.serialize(doc)
|
150
|
-
assert_equal doc, BSON::BSON_CODER.deserialize(bson)
|
151
|
-
end
|
152
|
-
|
153
|
-
def test_number
|
154
|
-
doc = {'doc' => 41.99}
|
155
|
-
bson = BSON::BSON_CODER.serialize(doc)
|
156
|
-
assert_equal doc, BSON::BSON_CODER.deserialize(bson)
|
157
|
-
end
|
158
|
-
|
159
|
-
def test_int
|
160
|
-
doc = {'doc' => 42}
|
161
|
-
bson = BSON::BSON_CODER.serialize(doc)
|
162
|
-
assert_equal doc, BSON::BSON_CODER.deserialize(bson)
|
163
|
-
|
164
|
-
doc = {"doc" => -5600}
|
165
|
-
bson = BSON::BSON_CODER.serialize(doc)
|
166
|
-
assert_equal doc, BSON::BSON_CODER.deserialize(bson)
|
167
|
-
|
168
|
-
doc = {"doc" => 2147483647}
|
169
|
-
bson = BSON::BSON_CODER.serialize(doc)
|
170
|
-
assert_equal doc, BSON::BSON_CODER.deserialize(bson)
|
171
|
-
|
172
|
-
doc = {"doc" => -2147483648}
|
173
|
-
bson = BSON::BSON_CODER.serialize(doc)
|
174
|
-
assert_equal doc, BSON::BSON_CODER.deserialize(bson)
|
175
|
-
end
|
176
|
-
|
177
|
-
def test_ordered_hash
|
178
|
-
doc = BSON::OrderedHash.new
|
179
|
-
doc["b"] = 1
|
180
|
-
doc["a"] = 2
|
181
|
-
doc["c"] = 3
|
182
|
-
doc["d"] = 4
|
183
|
-
bson = BSON::BSON_CODER.serialize(doc)
|
184
|
-
assert_equal doc, BSON::BSON_CODER.deserialize(bson)
|
185
|
-
end
|
186
|
-
|
187
|
-
def test_object
|
188
|
-
doc = {'doc' => {'age' => 42, 'name' => 'Spongebob', 'shoe_size' => 9.5}}
|
189
|
-
bson = BSON::BSON_CODER.serialize(doc)
|
190
|
-
assert_equal doc, BSON::BSON_CODER.deserialize(bson)
|
191
|
-
end
|
192
|
-
|
193
|
-
def test_oid
|
194
|
-
doc = {'doc' => ObjectId.new}
|
195
|
-
bson = BSON::BSON_CODER.serialize(doc)
|
196
|
-
assert_equal doc, BSON::BSON_CODER.deserialize(bson)
|
197
|
-
end
|
198
|
-
|
199
|
-
def test_array
|
200
|
-
doc = {'doc' => [1, 2, 'a', 'b']}
|
201
|
-
bson = BSON::BSON_CODER.serialize(doc)
|
202
|
-
assert_equal doc, BSON::BSON_CODER.deserialize(bson)
|
203
|
-
end
|
204
|
-
|
205
|
-
def test_regex
|
206
|
-
doc = {'doc' => /foobar/i}
|
207
|
-
bson = BSON::BSON_CODER.serialize(doc)
|
208
|
-
doc2 = BSON::BSON_CODER.deserialize(bson)
|
209
|
-
assert_equal doc, doc2
|
210
|
-
|
211
|
-
r = doc2['doc']
|
212
|
-
assert_kind_of Regexp, r
|
213
|
-
|
214
|
-
doc = {'doc' => r}
|
215
|
-
bson_doc = BSON::BSON_CODER.serialize(doc)
|
216
|
-
doc2 = nil
|
217
|
-
doc2 = BSON::BSON_CODER.deserialize(bson_doc)
|
218
|
-
assert_equal doc, doc2
|
219
|
-
end
|
220
|
-
|
221
|
-
def test_boolean
|
222
|
-
doc = {'doc' => true}
|
223
|
-
bson = BSON::BSON_CODER.serialize(doc)
|
224
|
-
assert_equal doc, BSON::BSON_CODER.deserialize(bson)
|
225
|
-
end
|
226
|
-
|
227
|
-
def test_date
|
228
|
-
doc = {'date' => Time.now}
|
229
|
-
bson = BSON::BSON_CODER.serialize(doc)
|
230
|
-
doc2 = BSON::BSON_CODER.deserialize(bson)
|
231
|
-
# Mongo only stores up to the millisecond
|
232
|
-
assert_in_delta doc['date'], doc2['date'], 0.001
|
233
|
-
end
|
234
|
-
|
235
|
-
def test_date_returns_as_utc
|
236
|
-
doc = {'date' => Time.now}
|
237
|
-
bson = BSON::BSON_CODER.serialize(doc)
|
238
|
-
doc2 = BSON::BSON_CODER.deserialize(bson)
|
239
|
-
assert doc2['date'].utc?
|
240
|
-
end
|
241
|
-
|
242
|
-
def test_date_before_epoch
|
243
|
-
begin
|
244
|
-
doc = {'date' => Time.utc(1600)}
|
245
|
-
bson = BSON::BSON_CODER.serialize(doc)
|
246
|
-
doc2 = BSON::BSON_CODER.deserialize(bson)
|
247
|
-
# Mongo only stores up to the millisecond
|
248
|
-
assert_in_delta doc['date'], doc2['date'], 0.001
|
249
|
-
rescue ArgumentError
|
250
|
-
# some versions of Ruby won't let you create pre-epoch Time instances
|
251
|
-
#
|
252
|
-
# TODO figure out how that will work if somebady has saved data
|
253
|
-
# w/ early dates already and is just querying for it.
|
254
|
-
end
|
255
|
-
end
|
256
|
-
|
257
|
-
def test_exeption_on_using_unsupported_date_class
|
258
|
-
[DateTime.now, Date.today, Zone].each do |invalid_date|
|
259
|
-
doc = {:date => invalid_date}
|
260
|
-
begin
|
261
|
-
bson = BSON::BSON_CODER.serialize(doc)
|
262
|
-
rescue => e
|
263
|
-
ensure
|
264
|
-
if !invalid_date.is_a? Time
|
265
|
-
assert_equal InvalidDocument, e.class
|
266
|
-
assert_match /UTC Time/, e.message
|
267
|
-
end
|
268
|
-
end
|
269
|
-
end
|
270
|
-
end
|
271
|
-
|
272
|
-
def test_dbref
|
273
|
-
oid = ObjectId.new
|
274
|
-
doc = {}
|
275
|
-
doc['dbref'] = DBRef.new('namespace', oid)
|
276
|
-
bson = BSON::BSON_CODER.serialize(doc)
|
277
|
-
doc2 = BSON::BSON_CODER.deserialize(bson)
|
278
|
-
assert_equal 'namespace', doc2['dbref'].namespace
|
279
|
-
assert_equal oid, doc2['dbref'].object_id
|
280
|
-
end
|
281
|
-
|
282
|
-
def test_symbol
|
283
|
-
doc = {'sym' => :foo}
|
284
|
-
bson = BSON::BSON_CODER.serialize(doc)
|
285
|
-
doc2 = BSON::BSON_CODER.deserialize(bson)
|
286
|
-
assert_equal :foo, doc2['sym']
|
287
|
-
end
|
288
|
-
|
289
|
-
def test_binary
|
290
|
-
bin = Binary.new
|
291
|
-
'binstring'.each_byte { |b| bin.put(b) }
|
292
|
-
|
293
|
-
doc = {'bin' => bin}
|
294
|
-
bson = BSON::BSON_CODER.serialize(doc)
|
295
|
-
doc2 = BSON::BSON_CODER.deserialize(bson)
|
296
|
-
bin2 = doc2['bin']
|
297
|
-
assert_kind_of Binary, bin2
|
298
|
-
assert_equal 'binstring', bin2.to_s
|
299
|
-
assert_equal Binary::SUBTYPE_BYTES, bin2.subtype
|
300
|
-
end
|
301
|
-
|
302
|
-
def test_binary_with_string
|
303
|
-
b = Binary.new('somebinarystring')
|
304
|
-
doc = {'bin' => b}
|
305
|
-
bson = BSON::BSON_CODER.serialize(doc)
|
306
|
-
doc2 = BSON::BSON_CODER.deserialize(bson)
|
307
|
-
bin2 = doc2['bin']
|
308
|
-
assert_kind_of Binary, bin2
|
309
|
-
assert_equal 'somebinarystring', bin2.to_s
|
310
|
-
assert_equal Binary::SUBTYPE_BYTES, bin2.subtype
|
311
|
-
end
|
312
|
-
|
313
|
-
def test_binary_type
|
314
|
-
bin = Binary.new([1, 2, 3, 4, 5], Binary::SUBTYPE_USER_DEFINED)
|
315
|
-
|
316
|
-
doc = {'bin' => bin}
|
317
|
-
bson = BSON::BSON_CODER.serialize(doc)
|
318
|
-
doc2 = BSON::BSON_CODER.deserialize(bson)
|
319
|
-
bin2 = doc2['bin']
|
320
|
-
assert_kind_of Binary, bin2
|
321
|
-
assert_equal [1, 2, 3, 4, 5], bin2.to_a
|
322
|
-
assert_equal Binary::SUBTYPE_USER_DEFINED, bin2.subtype
|
323
|
-
end
|
324
|
-
|
325
|
-
def test_binary_binary_subtype_0
|
326
|
-
bin = Binary.new([1, 2, 3, 4, 5], Binary::SUBTYPE_SIMPLE)
|
327
|
-
|
328
|
-
doc = {'bin' => bin}
|
329
|
-
bson = BSON::BSON_CODER.serialize(doc)
|
330
|
-
doc2 = BSON::BSON_CODER.deserialize(bson)
|
331
|
-
bin2 = doc2['bin']
|
332
|
-
assert_kind_of Binary, bin2
|
333
|
-
assert_equal [1, 2, 3, 4, 5], bin2.to_a
|
334
|
-
assert_equal Binary::SUBTYPE_SIMPLE, bin2.subtype
|
335
|
-
end
|
336
|
-
|
337
|
-
def test_binary_byte_buffer
|
338
|
-
bb = Binary.new
|
339
|
-
5.times { |i| bb.put(i + 1) }
|
340
|
-
|
341
|
-
doc = {'bin' => bb}
|
342
|
-
bson = BSON::BSON_CODER.serialize(doc)
|
343
|
-
doc2 = BSON::BSON_CODER.deserialize(bson)
|
344
|
-
bin2 = doc2['bin']
|
345
|
-
assert_kind_of Binary, bin2
|
346
|
-
assert_equal [1, 2, 3, 4, 5], bin2.to_a
|
347
|
-
assert_equal Binary::SUBTYPE_BYTES, bin2.subtype
|
348
|
-
end
|
349
|
-
|
350
|
-
def test_put_id_first
|
351
|
-
val = BSON::OrderedHash.new
|
352
|
-
val['not_id'] = 1
|
353
|
-
val['_id'] = 2
|
354
|
-
roundtrip = BSON::BSON_CODER.deserialize(BSON::BSON_CODER.serialize(val, false, true).to_a)
|
355
|
-
assert_kind_of BSON::OrderedHash, roundtrip
|
356
|
-
assert_equal '_id', roundtrip.keys.first
|
357
|
-
|
358
|
-
val = {'a' => 'foo', 'b' => 'bar', :_id => 42, 'z' => 'hello'}
|
359
|
-
roundtrip = BSON::BSON_CODER.deserialize(BSON::BSON_CODER.serialize(val, false, true).to_a)
|
360
|
-
assert_kind_of BSON::OrderedHash, roundtrip
|
361
|
-
assert_equal '_id', roundtrip.keys.first
|
362
|
-
end
|
363
|
-
|
364
|
-
def test_nil_id
|
365
|
-
doc = {"_id" => nil}
|
366
|
-
assert_equal doc, BSON::BSON_CODER.deserialize(bson = BSON::BSON_CODER.serialize(doc, false, true).to_a)
|
367
|
-
end
|
368
|
-
|
369
|
-
def test_timestamp
|
370
|
-
val = {"test" => [4, 20]}
|
371
|
-
assert_equal val, BSON::BSON_CODER.deserialize([0x13, 0x00, 0x00, 0x00,
|
372
|
-
0x11, 0x74, 0x65, 0x73,
|
373
|
-
0x74, 0x00, 0x04, 0x00,
|
374
|
-
0x00, 0x00, 0x14, 0x00,
|
375
|
-
0x00, 0x00, 0x00])
|
376
|
-
end
|
377
|
-
|
378
|
-
def test_overflow
|
379
|
-
doc = {"x" => 2**75}
|
380
|
-
assert_raise RangeError do
|
381
|
-
bson = BSON::BSON_CODER.serialize(doc)
|
382
|
-
end
|
383
|
-
|
384
|
-
doc = {"x" => 9223372036854775}
|
385
|
-
assert_equal doc, BSON::BSON_CODER.deserialize(BSON::BSON_CODER.serialize(doc).to_a)
|
386
|
-
|
387
|
-
doc = {"x" => 9223372036854775807}
|
388
|
-
assert_equal doc, BSON::BSON_CODER.deserialize(BSON::BSON_CODER.serialize(doc).to_a)
|
389
|
-
|
390
|
-
doc["x"] = doc["x"] + 1
|
391
|
-
assert_raise RangeError do
|
392
|
-
bson = BSON::BSON_CODER.serialize(doc)
|
393
|
-
end
|
394
|
-
|
395
|
-
doc = {"x" => -9223372036854775}
|
396
|
-
assert_equal doc, BSON::BSON_CODER.deserialize(BSON::BSON_CODER.serialize(doc).to_a)
|
397
|
-
|
398
|
-
doc = {"x" => -9223372036854775808}
|
399
|
-
assert_equal doc, BSON::BSON_CODER.deserialize(BSON::BSON_CODER.serialize(doc).to_a)
|
400
|
-
|
401
|
-
doc["x"] = doc["x"] - 1
|
402
|
-
assert_raise RangeError do
|
403
|
-
bson = BSON::BSON_CODER.serialize(doc)
|
404
|
-
end
|
405
|
-
end
|
406
|
-
|
407
|
-
def test_invalid_numeric_types
|
408
|
-
[BigDecimal.new("1.0"), Complex(0, 1), Rational(2, 3)].each do |type|
|
409
|
-
doc = {"x" => type}
|
410
|
-
begin
|
411
|
-
BSON::BSON_CODER.serialize(doc)
|
412
|
-
rescue => e
|
413
|
-
ensure
|
414
|
-
assert_equal InvalidDocument, e.class
|
415
|
-
assert_match /Cannot serialize/, e.message
|
416
|
-
end
|
417
|
-
end
|
418
|
-
end
|
419
|
-
|
420
|
-
def test_do_not_change_original_object
|
421
|
-
val = BSON::OrderedHash.new
|
422
|
-
val['not_id'] = 1
|
423
|
-
val['_id'] = 2
|
424
|
-
assert val.keys.include?('_id')
|
425
|
-
BSON::BSON_CODER.serialize(val)
|
426
|
-
assert val.keys.include?('_id')
|
427
|
-
|
428
|
-
val = {'a' => 'foo', 'b' => 'bar', :_id => 42, 'z' => 'hello'}
|
429
|
-
assert val.keys.include?(:_id)
|
430
|
-
BSON::BSON_CODER.serialize(val)
|
431
|
-
assert val.keys.include?(:_id)
|
432
|
-
end
|
433
|
-
|
434
|
-
# note we only test for _id here because in the general case we will
|
435
|
-
# write duplicates for :key and "key". _id is a special case because
|
436
|
-
# we call has_key? to check for it's existence rather than just iterating
|
437
|
-
# over it like we do for the rest of the keys. thus, things like
|
438
|
-
# HashWithIndifferentAccess can cause problems for _id but not for other
|
439
|
-
# keys. rather than require rails to test with HWIA directly, we do this
|
440
|
-
# somewhat hacky test.
|
441
|
-
def test_no_duplicate_id
|
442
|
-
dup = {"_id" => "foo", :_id => "foo"}
|
443
|
-
one = {"_id" => "foo"}
|
444
|
-
|
445
|
-
assert_equal BSON::BSON_CODER.serialize(one).to_a, BSON::BSON_CODER.serialize(dup).to_a
|
446
|
-
end
|
447
|
-
|
448
|
-
def test_no_duplicate_id_when_moving_id
|
449
|
-
dup = {"_id" => "foo", :_id => "foo"}
|
450
|
-
one = {:_id => "foo"}
|
451
|
-
|
452
|
-
assert_equal BSON::BSON_CODER.serialize(one, false, true).to_s, BSON::BSON_CODER.serialize(dup, false, true).to_s
|
453
|
-
end
|
454
|
-
|
455
|
-
def test_null_character
|
456
|
-
doc = {"a" => "\x00"}
|
457
|
-
|
458
|
-
assert_equal doc, BSON::BSON_CODER.deserialize(BSON::BSON_CODER.serialize(doc).to_a)
|
459
|
-
|
460
|
-
assert_raise InvalidDocument do
|
461
|
-
BSON::BSON_CODER.serialize({"\x00" => "a"})
|
462
|
-
end
|
463
|
-
|
464
|
-
assert_raise InvalidDocument do
|
465
|
-
BSON::BSON_CODER.serialize({"a" => (Regexp.compile "ab\x00c")})
|
466
|
-
end
|
467
|
-
end
|
468
|
-
|
469
|
-
def test_max_key
|
470
|
-
doc = {"a" => MaxKey.new}
|
471
|
-
|
472
|
-
assert_equal doc, BSON::BSON_CODER.deserialize(BSON::BSON_CODER.serialize(doc).to_a)
|
473
|
-
end
|
474
|
-
|
475
|
-
def test_min_key
|
476
|
-
doc = {"a" => MinKey.new}
|
477
|
-
|
478
|
-
assert_equal doc, BSON::BSON_CODER.deserialize(BSON::BSON_CODER.serialize(doc).to_a)
|
479
|
-
end
|
480
|
-
|
481
|
-
def test_invalid_object
|
482
|
-
o = Object.new
|
483
|
-
assert_raise InvalidDocument do
|
484
|
-
BSON::BSON_CODER.serialize({:foo => o})
|
485
|
-
end
|
486
|
-
|
487
|
-
assert_raise InvalidDocument do
|
488
|
-
BSON::BSON_CODER.serialize({:foo => Date.today})
|
489
|
-
end
|
490
|
-
end
|
491
|
-
|
492
|
-
def test_move_id
|
493
|
-
a = BSON::OrderedHash.new
|
494
|
-
a['text'] = 'abc'
|
495
|
-
a['key'] = 'abc'
|
496
|
-
a['_id'] = 1
|
497
|
-
|
498
|
-
|
499
|
-
assert_equal ")\000\000\000\020_id\000\001\000\000\000\002text" +
|
500
|
-
"\000\004\000\000\000abc\000\002key\000\004\000\000\000abc\000\000",
|
501
|
-
BSON::BSON_CODER.serialize(a, false, true).to_s
|
502
|
-
assert_equal ")\000\000\000\002text\000\004\000\000\000abc\000\002key" +
|
503
|
-
"\000\004\000\000\000abc\000\020_id\000\001\000\000\000\000",
|
504
|
-
BSON::BSON_CODER.serialize(a, false, false).to_s
|
505
|
-
end
|
506
|
-
|
507
|
-
def test_move_id_with_nested_doc
|
508
|
-
b = BSON::OrderedHash.new
|
509
|
-
b['text'] = 'abc'
|
510
|
-
b['_id'] = 2
|
511
|
-
c = BSON::OrderedHash.new
|
512
|
-
c['text'] = 'abc'
|
513
|
-
c['hash'] = b
|
514
|
-
c['_id'] = 3
|
515
|
-
assert_equal ">\000\000\000\020_id\000\003\000\000\000\002text" +
|
516
|
-
"\000\004\000\000\000abc\000\003hash\000\034\000\000" +
|
517
|
-
"\000\002text\000\004\000\000\000abc\000\020_id\000\002\000\000\000\000\000",
|
518
|
-
BSON::BSON_CODER.serialize(c, false, true).to_s
|
519
|
-
assert_equal ">\000\000\000\002text\000\004\000\000\000abc\000\003hash" +
|
520
|
-
"\000\034\000\000\000\002text\000\004\000\000\000abc\000\020_id" +
|
521
|
-
"\000\002\000\000\000\000\020_id\000\003\000\000\000\000",
|
522
|
-
BSON::BSON_CODER.serialize(c, false, false).to_s
|
523
|
-
end
|
524
|
-
|
525
|
-
# Mocking this class for testing
|
526
|
-
class ::HashWithIndifferentAccess < Hash; end
|
527
|
-
|
528
|
-
def test_keep_id_with_hash_with_indifferent_access
|
529
|
-
doc = HashWithIndifferentAccess.new
|
530
|
-
embedded = HashWithIndifferentAccess.new
|
531
|
-
embedded['_id'] = ObjectId.new
|
532
|
-
doc['_id'] = ObjectId.new
|
533
|
-
doc['embedded'] = [embedded]
|
534
|
-
BSON::BSON_CODER.serialize(doc, false, true).to_a
|
535
|
-
assert doc.has_key?("_id")
|
536
|
-
assert doc['embedded'][0].has_key?("_id")
|
537
|
-
|
538
|
-
doc['_id'] = ObjectId.new
|
539
|
-
BSON::BSON_CODER.serialize(doc, false, true).to_a
|
540
|
-
assert doc.has_key?("_id")
|
541
|
-
end
|
542
|
-
|
543
|
-
end
|