bson 1.0.7 → 1.0.9
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/lib/bson.rb +2 -2
- data/lib/bson/bson_c.rb +1 -7
- data/lib/bson/bson_ruby.rb +12 -9
- data/lib/bson/byte_buffer.rb +110 -64
- data/lib/bson/ordered_hash.rb +1 -8
- data/lib/bson/types/binary.rb +2 -3
- data/test/mongo_bson/basic_test.rb +1 -1
- data/test/mongo_bson/binary_test.rb +1 -1
- data/test/mongo_bson/bson_test.rb +6 -6
- data/test/mongo_bson/byte_buffer_test.rb +114 -6
- data/test/mongo_bson/jruby_encode_test.rb +14 -12
- data/test/mongo_bson/json_test.rb +2 -2
- data/test/mongo_bson/object_id_test.rb +1 -1
- data/test/mongo_bson/ordered_hash_test.rb +1 -1
- metadata +4 -6
- data/test/mongo_bson/objectid_test.rb +0 -132
data/lib/bson.rb
CHANGED
@@ -2,10 +2,10 @@
|
|
2
2
|
|
3
3
|
$:.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
|
4
4
|
|
5
|
-
MINIMUM_BSON_EXT_VERSION = "1.0.
|
5
|
+
MINIMUM_BSON_EXT_VERSION = "1.0.9"
|
6
6
|
|
7
7
|
module BSON
|
8
|
-
VERSION = "1.0.
|
8
|
+
VERSION = "1.0.9"
|
9
9
|
def self.serialize(obj, check_keys=false, move_id=false)
|
10
10
|
BSON_CODER.serialize(obj, check_keys, move_id)
|
11
11
|
end
|
data/lib/bson/bson_c.rb
CHANGED
@@ -25,13 +25,7 @@ module BSON
|
|
25
25
|
end
|
26
26
|
|
27
27
|
def self.deserialize(buf=nil)
|
28
|
-
|
29
|
-
buf = ByteBuffer.new(buf.unpack("C*")) if buf
|
30
|
-
else
|
31
|
-
buf = ByteBuffer.new(buf.to_a) if buf
|
32
|
-
end
|
33
|
-
buf.rewind
|
34
|
-
CBson.deserialize(buf.to_s)
|
28
|
+
CBson.deserialize(ByteBuffer.new(buf).to_s)
|
35
29
|
end
|
36
30
|
|
37
31
|
end
|
data/lib/bson/bson_ruby.rb
CHANGED
@@ -47,22 +47,29 @@ module BSON
|
|
47
47
|
end
|
48
48
|
|
49
49
|
if RUBY_VERSION >= '1.9'
|
50
|
-
|
51
|
-
|
50
|
+
NULL_BYTE = "\0".force_encoding('binary').freeze
|
51
|
+
UTF8_ENCODING = Encoding.find('utf-8')
|
52
|
+
BINARY_ENCODING = Encoding.find('binary')
|
53
|
+
|
54
|
+
def self.to_utf8_binary(str)
|
55
|
+
str.encode(UTF8_ENCODING).force_encoding(BINARY_ENCODING)
|
52
56
|
end
|
53
57
|
else
|
54
|
-
|
58
|
+
NULL_BYTE = "\0"
|
59
|
+
|
60
|
+
def self.to_utf8_binary(str)
|
55
61
|
begin
|
56
62
|
str.unpack("U*")
|
57
63
|
rescue => ex
|
58
|
-
raise InvalidStringEncoding, "String not valid utf-8: #{str}"
|
64
|
+
raise InvalidStringEncoding, "String not valid utf-8: #{str.inspect}"
|
59
65
|
end
|
60
66
|
str
|
61
67
|
end
|
62
68
|
end
|
63
69
|
|
64
70
|
def self.serialize_cstr(buf, val)
|
65
|
-
buf.
|
71
|
+
buf.put_binary(to_utf8_binary(val.to_s))
|
72
|
+
buf.put_binary(NULL_BYTE)
|
66
73
|
end
|
67
74
|
|
68
75
|
def self.serialize_key(buf, key)
|
@@ -271,7 +278,6 @@ module BSON
|
|
271
278
|
|
272
279
|
def deserialize_date_data(buf)
|
273
280
|
unsigned = buf.get_long()
|
274
|
-
# see note for deserialize_number_long_data below
|
275
281
|
milliseconds = unsigned >= 2 ** 64 / 2 ? unsigned - 2**64 : unsigned
|
276
282
|
Time.at(milliseconds.to_f / 1000.0).utc # at() takes fractional seconds
|
277
283
|
end
|
@@ -285,14 +291,11 @@ module BSON
|
|
285
291
|
end
|
286
292
|
|
287
293
|
def deserialize_number_int_data(buf)
|
288
|
-
# sometimes ruby makes me angry... why would the same code pack as signed
|
289
|
-
# but unpack as unsigned
|
290
294
|
unsigned = buf.get_int
|
291
295
|
unsigned >= 2**32 / 2 ? unsigned - 2**32 : unsigned
|
292
296
|
end
|
293
297
|
|
294
298
|
def deserialize_number_long_data(buf)
|
295
|
-
# same note as above applies here...
|
296
299
|
unsigned = buf.get_long
|
297
300
|
unsigned >= 2 ** 64 / 2 ? unsigned - 2**64 : unsigned
|
298
301
|
end
|
data/lib/bson/byte_buffer.rb
CHANGED
@@ -20,47 +20,48 @@
|
|
20
20
|
module BSON
|
21
21
|
class ByteBuffer
|
22
22
|
|
23
|
-
# Commonly-used integers.
|
24
|
-
INT_LOOKUP = {
|
25
|
-
0 => [0, 0, 0, 0],
|
26
|
-
1 => [1, 0, 0, 0],
|
27
|
-
2 => [2, 0, 0, 0],
|
28
|
-
3 => [3, 0, 0, 0],
|
29
|
-
4 => [4, 0, 0, 0],
|
30
|
-
2001 => [209, 7, 0, 0],
|
31
|
-
2002 => [210, 7, 0, 0],
|
32
|
-
2004 => [212, 7, 0, 0],
|
33
|
-
2005 => [213, 7, 0, 0],
|
34
|
-
2006 => [214, 7, 0, 0]
|
35
|
-
}
|
36
|
-
|
37
23
|
attr_reader :order
|
38
24
|
|
39
|
-
def initialize(initial_data=
|
40
|
-
|
41
|
-
|
25
|
+
def initialize(initial_data="")
|
26
|
+
if initial_data.is_a?(String)
|
27
|
+
if initial_data.respond_to?(:force_encoding)
|
28
|
+
@str = initial_data.force_encoding('binary')
|
29
|
+
else
|
30
|
+
@str = initial_data
|
31
|
+
end
|
32
|
+
else
|
33
|
+
@str = initial_data.pack('C*')
|
34
|
+
end
|
35
|
+
@cursor = @str.length
|
42
36
|
@order = :little_endian
|
43
37
|
@int_pack_order = 'V'
|
44
38
|
@double_pack_order = 'E'
|
45
39
|
end
|
46
40
|
|
47
41
|
if RUBY_VERSION >= '1.9'
|
48
|
-
|
49
|
-
|
42
|
+
NULL_BYTE = "\0".force_encoding('binary').freeze
|
43
|
+
UTF8_ENCODING = Encoding.find('utf-8')
|
44
|
+
BINARY_ENCODING = Encoding.find('binary')
|
45
|
+
|
46
|
+
def self.to_utf8_binary(str)
|
47
|
+
str.encode(UTF8_ENCODING).force_encoding(BINARY_ENCODING)
|
50
48
|
end
|
51
49
|
else
|
52
|
-
|
50
|
+
NULL_BYTE = "\0"
|
51
|
+
|
52
|
+
def self.to_utf8_binary(str)
|
53
53
|
begin
|
54
54
|
str.unpack("U*")
|
55
55
|
rescue => ex
|
56
|
-
raise InvalidStringEncoding, "String not valid utf-8: #{str}"
|
56
|
+
raise InvalidStringEncoding, "String not valid utf-8: #{str.inspect}"
|
57
57
|
end
|
58
58
|
str
|
59
59
|
end
|
60
60
|
end
|
61
61
|
|
62
62
|
def self.serialize_cstr(buf, val)
|
63
|
-
buf.
|
63
|
+
buf.append!(to_utf8_binary(val.to_s))
|
64
|
+
buf.append!(NULL_BYTE)
|
64
65
|
end
|
65
66
|
|
66
67
|
# +endianness+ should be :little_endian or :big_endian. Default is :little_endian
|
@@ -83,45 +84,73 @@ module BSON
|
|
83
84
|
end
|
84
85
|
|
85
86
|
def clear
|
86
|
-
@
|
87
|
+
@str = ""
|
88
|
+
@str.force_encoding('binary') if @str.respond_to?(:force_encoding)
|
87
89
|
rewind
|
88
90
|
end
|
89
91
|
|
90
92
|
def size
|
91
|
-
@
|
93
|
+
@str.size
|
92
94
|
end
|
93
95
|
alias_method :length, :size
|
94
96
|
|
95
97
|
# Appends a second ByteBuffer object, +buffer+, to the current buffer.
|
96
98
|
def append!(buffer)
|
97
|
-
@
|
99
|
+
@str << buffer.to_s
|
98
100
|
self
|
99
101
|
end
|
100
102
|
|
101
103
|
# Prepends a second ByteBuffer object, +buffer+, to the current buffer.
|
102
104
|
def prepend!(buffer)
|
103
|
-
@
|
105
|
+
@str = buffer.to_s + @str
|
104
106
|
self
|
105
107
|
end
|
106
108
|
|
107
109
|
def put(byte, offset=nil)
|
108
110
|
@cursor = offset if offset
|
109
|
-
|
111
|
+
if more?
|
112
|
+
@str[@cursor] = chr(byte)
|
113
|
+
else
|
114
|
+
ensure_length(@cursor)
|
115
|
+
@str << chr(byte)
|
116
|
+
end
|
110
117
|
@cursor += 1
|
111
118
|
end
|
112
|
-
|
119
|
+
|
120
|
+
def put_binary(data, offset=nil)
|
121
|
+
@cursor = offset if offset
|
122
|
+
if defined?(BINARY_ENCODING)
|
123
|
+
data = data.dup.force_encoding(BINARY_ENCODING)
|
124
|
+
end
|
125
|
+
if more?
|
126
|
+
@str[@cursor, data.length] = data
|
127
|
+
else
|
128
|
+
ensure_length(@cursor)
|
129
|
+
@str << data
|
130
|
+
end
|
131
|
+
@cursor += data.length
|
132
|
+
end
|
133
|
+
|
113
134
|
def put_array(array, offset=nil)
|
114
135
|
@cursor = offset if offset
|
115
|
-
|
136
|
+
if more?
|
137
|
+
@str[@cursor, array.length] = array.pack("C*")
|
138
|
+
else
|
139
|
+
ensure_length(@cursor)
|
140
|
+
@str << array.pack("C*")
|
141
|
+
end
|
116
142
|
@cursor += array.length
|
117
143
|
end
|
118
144
|
|
119
145
|
def put_int(i, offset=nil)
|
120
|
-
|
121
|
-
|
122
|
-
[i].pack(@int_pack_order)
|
146
|
+
@cursor = offset if offset
|
147
|
+
if more?
|
148
|
+
@str[@cursor, 4] = [i].pack(@int_pack_order)
|
149
|
+
else
|
150
|
+
ensure_length(@cursor)
|
151
|
+
@str << [i].pack(@int_pack_order)
|
123
152
|
end
|
124
|
-
|
153
|
+
@cursor += 4
|
125
154
|
end
|
126
155
|
|
127
156
|
def put_long(i, offset=nil)
|
@@ -143,27 +172,37 @@ module BSON
|
|
143
172
|
|
144
173
|
# If +size+ == nil, returns one byte. Else returns array of bytes of length
|
145
174
|
# # +size+.
|
146
|
-
|
147
|
-
|
148
|
-
|
149
|
-
|
150
|
-
|
151
|
-
|
152
|
-
|
153
|
-
|
154
|
-
|
155
|
-
if @buf.respond_to? "unpack"
|
156
|
-
@buf[start, len].unpack("C*")
|
175
|
+
if "x"[0].is_a?(Integer)
|
176
|
+
def get(len=nil)
|
177
|
+
one_byte = len.nil?
|
178
|
+
len ||= 1
|
179
|
+
check_read_length(len)
|
180
|
+
start = @cursor
|
181
|
+
@cursor += len
|
182
|
+
if one_byte
|
183
|
+
@str[start]
|
157
184
|
else
|
158
|
-
@
|
185
|
+
@str[start, len].unpack("C*")
|
186
|
+
end
|
187
|
+
end
|
188
|
+
else
|
189
|
+
def get(len=nil)
|
190
|
+
one_byte = len.nil?
|
191
|
+
len ||= 1
|
192
|
+
check_read_length(len)
|
193
|
+
start = @cursor
|
194
|
+
@cursor += len
|
195
|
+
if one_byte
|
196
|
+
@str[start, 1].ord
|
197
|
+
else
|
198
|
+
@str[start, len].unpack("C*")
|
159
199
|
end
|
160
200
|
end
|
161
201
|
end
|
162
202
|
|
163
203
|
def get_int
|
164
204
|
check_read_length(4)
|
165
|
-
vals =
|
166
|
-
(@cursor..@cursor+3).each { |i| vals << @buf[i].chr }
|
205
|
+
vals = @str[@cursor..@cursor+3]
|
167
206
|
@cursor += 4
|
168
207
|
vals.unpack(@int_pack_order)[0]
|
169
208
|
end
|
@@ -180,22 +219,17 @@ module BSON
|
|
180
219
|
|
181
220
|
def get_double
|
182
221
|
check_read_length(8)
|
183
|
-
vals =
|
184
|
-
(@cursor..@cursor+7).each { |i| vals << @buf[i].chr }
|
222
|
+
vals = @str[@cursor..@cursor+7]
|
185
223
|
@cursor += 8
|
186
224
|
vals.unpack(@double_pack_order)[0]
|
187
225
|
end
|
188
226
|
|
189
227
|
def more?
|
190
|
-
@cursor < @
|
228
|
+
@cursor < @str.size
|
191
229
|
end
|
192
230
|
|
193
231
|
def to_a
|
194
|
-
|
195
|
-
@buf.unpack("C*")
|
196
|
-
else
|
197
|
-
@buf
|
198
|
-
end
|
232
|
+
@str.unpack("C*")
|
199
233
|
end
|
200
234
|
|
201
235
|
def unpack(args)
|
@@ -203,23 +237,35 @@ module BSON
|
|
203
237
|
end
|
204
238
|
|
205
239
|
def to_s
|
206
|
-
|
207
|
-
@buf.fast_pack
|
208
|
-
elsif @buf.respond_to? "pack"
|
209
|
-
@buf.pack("C*")
|
210
|
-
else
|
211
|
-
@buf
|
212
|
-
end
|
240
|
+
@str
|
213
241
|
end
|
214
242
|
|
215
243
|
def dump
|
216
|
-
|
244
|
+
i = 0
|
245
|
+
@str.each_byte do |c, i|
|
246
|
+
$stderr.puts "#{'%04d' % i}: #{'%02x' % c} #{'%03o' % c} #{'%s' % c.chr} #{'%3d' % c}"
|
247
|
+
i += 1
|
248
|
+
end
|
217
249
|
end
|
218
250
|
|
219
251
|
private
|
220
252
|
|
253
|
+
def ensure_length(length)
|
254
|
+
if @str.size < length
|
255
|
+
@str << NULL_BYTE * (length - @str.size)
|
256
|
+
end
|
257
|
+
end
|
258
|
+
|
259
|
+
def chr(byte)
|
260
|
+
if byte < 0
|
261
|
+
[byte].pack('c')
|
262
|
+
else
|
263
|
+
byte.chr
|
264
|
+
end
|
265
|
+
end
|
266
|
+
|
221
267
|
def check_read_length(len)
|
222
|
-
raise "attempt to read past end of buffer" if @cursor + len > @
|
268
|
+
raise "attempt to read past end of buffer" if @cursor + len > @str.length
|
223
269
|
end
|
224
270
|
|
225
271
|
end
|
data/lib/bson/ordered_hash.rb
CHANGED
@@ -20,7 +20,6 @@
|
|
20
20
|
#
|
21
21
|
# Under Ruby 1.9 and greater, this class has no added methods because Ruby's
|
22
22
|
# Hash already keeps its keys ordered by order of insertion.
|
23
|
-
require 'set'
|
24
23
|
|
25
24
|
module BSON
|
26
25
|
class OrderedHash < Hash
|
@@ -60,7 +59,6 @@ module BSON
|
|
60
59
|
def initialize(*a, &b)
|
61
60
|
super
|
62
61
|
@ordered_keys = []
|
63
|
-
@ordered_set = Set.new
|
64
62
|
end
|
65
63
|
|
66
64
|
def keys
|
@@ -69,10 +67,8 @@ module BSON
|
|
69
67
|
|
70
68
|
def []=(key, value)
|
71
69
|
@ordered_keys ||= []
|
72
|
-
|
73
|
-
unless @ordered_set.member?(key)
|
70
|
+
unless has_key?(key)
|
74
71
|
@ordered_keys << key
|
75
|
-
@ordered_set.add(key)
|
76
72
|
end
|
77
73
|
super(key, value)
|
78
74
|
end
|
@@ -103,7 +99,6 @@ module BSON
|
|
103
99
|
@ordered_keys ||= []
|
104
100
|
@ordered_keys += other.keys # unordered if not an BSON::OrderedHash
|
105
101
|
@ordered_keys.uniq!
|
106
|
-
@ordered_set = Set.new(@ordered_keys)
|
107
102
|
super(other)
|
108
103
|
end
|
109
104
|
|
@@ -117,7 +112,6 @@ module BSON
|
|
117
112
|
|
118
113
|
def delete(key, &block)
|
119
114
|
@ordered_keys.delete(key) if @ordered_keys
|
120
|
-
@ordered_set.delete(key) if @ordered_set
|
121
115
|
super
|
122
116
|
end
|
123
117
|
|
@@ -137,7 +131,6 @@ module BSON
|
|
137
131
|
|
138
132
|
def clear
|
139
133
|
super
|
140
|
-
@ordered_set.clear
|
141
134
|
@ordered_keys = []
|
142
135
|
end
|
143
136
|
|
data/lib/bson/types/binary.rb
CHANGED
@@ -37,14 +37,13 @@ module BSON
|
|
37
37
|
|
38
38
|
# Create a buffer for storing binary data in MongoDB.
|
39
39
|
#
|
40
|
-
# @param [Array, String] data to story as BSON binary. If a string is given, the
|
41
|
-
#
|
40
|
+
# @param [Array, String] data to story as BSON binary. If a string is given, the on
|
41
|
+
# Ruby 1.9 it will be forced to the binary encoding.
|
42
42
|
# @param [Fixnum] one of four values specifying a BSON binary subtype. Possible values are
|
43
43
|
# SUBTYPE_BYTES, SUBTYPE_UUID, SUBTYPE_MD5, and SUBTYPE_USER_DEFINED.
|
44
44
|
#
|
45
45
|
# @see http://www.mongodb.org/display/DOCS/BSON#BSON-noteondatabinary BSON binary subtypes.
|
46
46
|
def initialize(data=[], subtype=SUBTYPE_BYTES)
|
47
|
-
data = data.unpack("c*") if data.is_a?(String)
|
48
47
|
super(data)
|
49
48
|
@subtype = subtype
|
50
49
|
end
|
@@ -1,5 +1,5 @@
|
|
1
1
|
# encoding:utf-8
|
2
|
-
require 'test/test_helper'
|
2
|
+
require './test/test_helper'
|
3
3
|
require 'complex'
|
4
4
|
require 'bigdecimal'
|
5
5
|
require 'rational'
|
@@ -191,7 +191,7 @@ class BSONTest < Test::Unit::TestCase
|
|
191
191
|
end
|
192
192
|
|
193
193
|
def test_oid
|
194
|
-
doc = {'doc' =>
|
194
|
+
doc = {'doc' => ObjectId.new}
|
195
195
|
bson = BSON::BSON_CODER.serialize(doc)
|
196
196
|
assert_equal doc, BSON::BSON_CODER.deserialize(bson)
|
197
197
|
end
|
@@ -270,7 +270,7 @@ class BSONTest < Test::Unit::TestCase
|
|
270
270
|
end
|
271
271
|
|
272
272
|
def test_dbref
|
273
|
-
oid =
|
273
|
+
oid = ObjectId.new
|
274
274
|
doc = {}
|
275
275
|
doc['dbref'] = DBRef.new('namespace', oid)
|
276
276
|
bson = BSON::BSON_CODER.serialize(doc)
|
@@ -528,14 +528,14 @@ class BSONTest < Test::Unit::TestCase
|
|
528
528
|
def test_keep_id_with_hash_with_indifferent_access
|
529
529
|
doc = HashWithIndifferentAccess.new
|
530
530
|
embedded = HashWithIndifferentAccess.new
|
531
|
-
embedded['_id'] =
|
532
|
-
doc['_id'] =
|
531
|
+
embedded['_id'] = ObjectId.new
|
532
|
+
doc['_id'] = ObjectId.new
|
533
533
|
doc['embedded'] = [embedded]
|
534
534
|
BSON::BSON_CODER.serialize(doc, false, true).to_a
|
535
535
|
assert doc.has_key?("_id")
|
536
536
|
assert doc['embedded'][0].has_key?("_id")
|
537
537
|
|
538
|
-
doc['_id'] =
|
538
|
+
doc['_id'] = ObjectId.new
|
539
539
|
BSON::BSON_CODER.serialize(doc, false, true).to_a
|
540
540
|
assert doc.has_key?("_id")
|
541
541
|
end
|
@@ -1,4 +1,5 @@
|
|
1
|
-
|
1
|
+
# encoding: binary
|
2
|
+
require './test/test_helper'
|
2
3
|
|
3
4
|
class ByteBufferTest < Test::Unit::TestCase
|
4
5
|
include BSON
|
@@ -6,6 +7,13 @@ class ByteBufferTest < Test::Unit::TestCase
|
|
6
7
|
def setup
|
7
8
|
@buf = ByteBuffer.new
|
8
9
|
end
|
10
|
+
|
11
|
+
def test_initial_state
|
12
|
+
assert_equal 0, @buf.position
|
13
|
+
assert_equal [], @buf.to_a
|
14
|
+
assert_equal "", @buf.to_s
|
15
|
+
assert_equal 0, @buf.length
|
16
|
+
end
|
9
17
|
|
10
18
|
def test_nil_get_returns_one_byte
|
11
19
|
@buf.put_array([1, 2, 3, 4])
|
@@ -25,10 +33,6 @@ class ByteBufferTest < Test::Unit::TestCase
|
|
25
33
|
assert_equal [], @buf.get(0)
|
26
34
|
end
|
27
35
|
|
28
|
-
def test_empty
|
29
|
-
assert_equal 0, @buf.length
|
30
|
-
end
|
31
|
-
|
32
36
|
def test_length
|
33
37
|
@buf.put_int 3
|
34
38
|
assert_equal 4, @buf.length
|
@@ -54,7 +58,80 @@ class ByteBufferTest < Test::Unit::TestCase
|
|
54
58
|
@buf.rewind
|
55
59
|
assert_equal 41.2, @buf.get_double
|
56
60
|
end
|
57
|
-
|
61
|
+
|
62
|
+
if defined?(Encoding)
|
63
|
+
def test_serialize_cstr_converts_encoding_to_utf8
|
64
|
+
theta = "hello \xC8".force_encoding("ISO-8859-7")
|
65
|
+
ByteBuffer.serialize_cstr(@buf, theta)
|
66
|
+
assert_equal "hello \xCE\x98\0", @buf.to_s
|
67
|
+
assert_equal Encoding.find('binary'), @buf.to_s.encoding
|
68
|
+
end
|
69
|
+
|
70
|
+
def test_serialize_cstr_validates_data_as_utf8
|
71
|
+
assert_raises(Encoding::UndefinedConversionError) do
|
72
|
+
ByteBuffer.serialize_cstr(@buf, "hello \xFF")
|
73
|
+
end
|
74
|
+
end
|
75
|
+
else
|
76
|
+
def test_serialize_cstr_forces_encoding_to_utf8
|
77
|
+
# Unicode snowman (\u2603)
|
78
|
+
ByteBuffer.serialize_cstr(@buf, "hello \342\230\203")
|
79
|
+
assert_equal "hello \342\230\203\0", @buf.to_s
|
80
|
+
end
|
81
|
+
|
82
|
+
def test_serialize_cstr_validates_data_as_utf8
|
83
|
+
assert_raises(BSON::InvalidStringEncoding) do
|
84
|
+
ByteBuffer.serialize_cstr(@buf, "hello \xFF")
|
85
|
+
end
|
86
|
+
end
|
87
|
+
end
|
88
|
+
|
89
|
+
def test_put_negative_byte
|
90
|
+
@buf.put(-1)
|
91
|
+
@buf.rewind
|
92
|
+
assert_equal 255, @buf.get
|
93
|
+
assert_equal "\xFF", @buf.to_s
|
94
|
+
end
|
95
|
+
|
96
|
+
def test_put_with_offset
|
97
|
+
@buf.put(1)
|
98
|
+
@buf.put(2, 0)
|
99
|
+
@buf.put(3, 3)
|
100
|
+
assert_equal "\x02\x00\x00\x03", @buf.to_s
|
101
|
+
end
|
102
|
+
|
103
|
+
def test_put_array_with_offset
|
104
|
+
@buf.put(1)
|
105
|
+
@buf.put_array([2, 3], 0)
|
106
|
+
@buf.put_array([4, 5], 4)
|
107
|
+
assert_equal "\x02\x03\x00\x00\x04\x05", @buf.to_s
|
108
|
+
end
|
109
|
+
|
110
|
+
def test_put_int_with_offset
|
111
|
+
@buf.put(1)
|
112
|
+
@buf.put_int(2, 0)
|
113
|
+
@buf.put_int(3, 5)
|
114
|
+
assert_equal "\x02\x00\x00\x00\x00\x03\x00\x00\x00", @buf.to_s
|
115
|
+
end
|
116
|
+
|
117
|
+
def test_put_long_with_offset
|
118
|
+
@buf.put(1)
|
119
|
+
@buf.put_long(2, 0)
|
120
|
+
@buf.put_long(3, 9)
|
121
|
+
assert_equal(
|
122
|
+
"\x02\x00\x00\x00\x00\x00\x00\x00" +
|
123
|
+
"\x00" +
|
124
|
+
"\x03\x00\x00\x00\x00\x00\x00\x00",
|
125
|
+
@buf.to_s)
|
126
|
+
end
|
127
|
+
|
128
|
+
def test_put_binary
|
129
|
+
@buf.put(1)
|
130
|
+
@buf.put_binary("\x02\x03", 0)
|
131
|
+
@buf.put_binary("\x04\x05", 4)
|
132
|
+
assert_equal "\x02\x03\x00\x00\x04\x05", @buf.to_s
|
133
|
+
end
|
134
|
+
|
58
135
|
def test_rewrite
|
59
136
|
@buf.put_int(0)
|
60
137
|
@buf.rewind
|
@@ -78,5 +155,36 @@ class ByteBufferTest < Test::Unit::TestCase
|
|
78
155
|
@buf.append!(new_buf)
|
79
156
|
assert_equal [4, 0, 0, 0, 5, 0, 0, 0], @buf.to_a
|
80
157
|
end
|
158
|
+
|
159
|
+
def test_array_as_initial_input
|
160
|
+
@buf = ByteBuffer.new([5, 0, 0, 0])
|
161
|
+
assert_equal 4, @buf.size
|
162
|
+
assert_equal "\x05\x00\x00\x00", @buf.to_s
|
163
|
+
assert_equal [5, 0, 0, 0], @buf.to_a
|
164
|
+
@buf.put_int(32)
|
165
|
+
@buf.rewind
|
166
|
+
assert_equal 5, @buf.get_int
|
167
|
+
assert_equal 32, @buf.get_int
|
168
|
+
end
|
169
|
+
|
170
|
+
def test_binary_string_as_initial_input
|
171
|
+
str = "abcd"
|
172
|
+
str.force_encoding('binary') if str.respond_to?(:force_encoding)
|
173
|
+
@buf = ByteBuffer.new(str)
|
174
|
+
assert_equal "abcd", @buf.to_s
|
175
|
+
assert_equal [97, 98, 99, 100], @buf.to_a
|
176
|
+
@buf.put_int(0)
|
177
|
+
assert_equal [97, 98, 99, 100, 0, 0, 0, 0], @buf.to_a
|
178
|
+
end
|
179
|
+
|
180
|
+
def test_more
|
181
|
+
assert !@buf.more?
|
182
|
+
@buf.put_int(5)
|
183
|
+
assert !@buf.more?
|
184
|
+
@buf.rewind
|
185
|
+
assert @buf.more?
|
186
|
+
@buf.get_int
|
187
|
+
assert !@buf.more?
|
188
|
+
end
|
81
189
|
|
82
190
|
end
|
@@ -34,7 +34,7 @@ LARGE = {
|
|
34
34
|
|
35
35
|
class BSONTest < Test::Unit::TestCase
|
36
36
|
include BSON
|
37
|
-
|
37
|
+
|
38
38
|
def setup
|
39
39
|
@encoder = BSON::BSON_CODER
|
40
40
|
con = Mongo::Connection.new
|
@@ -106,14 +106,14 @@ class BSONTest < Test::Unit::TestCase
|
|
106
106
|
# assert @@test.find_one({:a => 3})['processed']
|
107
107
|
# end
|
108
108
|
#
|
109
|
-
def test_invalid_string
|
110
|
-
require 'iconv'
|
111
|
-
string = Iconv.conv('iso-8859-1', 'utf-8', 'aé')
|
112
|
-
doc = {'doc' => string}
|
113
|
-
bson = @encoder.serialize(doc)
|
114
|
-
assert_equal doc, @encoder.deserialize(bson)
|
115
|
-
end
|
116
|
-
|
109
|
+
# def test_invalid_string
|
110
|
+
# require 'iconv'
|
111
|
+
# string = Iconv.conv('iso-8859-1', 'utf-8', 'aé')
|
112
|
+
# doc = {'doc' => string}
|
113
|
+
# bson = @encoder.serialize(doc)
|
114
|
+
# assert_equal doc, @encoder.deserialize(bson)
|
115
|
+
# end
|
116
|
+
#
|
117
117
|
# def test_null
|
118
118
|
# #doc = {"\x00" => "foo"}
|
119
119
|
# #@encoder.serialize(doc)
|
@@ -152,9 +152,11 @@ class BSONTest < Test::Unit::TestCase
|
|
152
152
|
# assert_equal new_doc, @coder.deserialize(bson)
|
153
153
|
# end
|
154
154
|
#
|
155
|
-
|
156
|
-
|
157
|
-
|
155
|
+
|
156
|
+
def test_string
|
157
|
+
assert_doc_pass({'doc' => 'hello, world'})
|
158
|
+
end
|
159
|
+
|
158
160
|
##
|
159
161
|
# require 'iconv'
|
160
162
|
# def test_invalid_string
|
@@ -1,4 +1,4 @@
|
|
1
|
-
require 'test/test_helper'
|
1
|
+
require './test/test_helper'
|
2
2
|
require 'rubygems'
|
3
3
|
require 'json'
|
4
4
|
|
@@ -8,7 +8,7 @@ class JSONTest < Test::Unit::TestCase
|
|
8
8
|
include BSON
|
9
9
|
|
10
10
|
def test_object_id_as_json
|
11
|
-
id =
|
11
|
+
id = ObjectId.new
|
12
12
|
obj = {'_id' => id}
|
13
13
|
assert_equal "{\"_id\":{\"$oid\": \"#{id.to_s}\"}}", obj.to_json
|
14
14
|
end
|
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: bson
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 5
|
5
5
|
prerelease: false
|
6
6
|
segments:
|
7
7
|
- 1
|
8
8
|
- 0
|
9
|
-
-
|
10
|
-
version: 1.0.
|
9
|
+
- 9
|
10
|
+
version: 1.0.9
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Jim Menard
|
@@ -17,7 +17,7 @@ autorequire:
|
|
17
17
|
bindir: bin
|
18
18
|
cert_chain: []
|
19
19
|
|
20
|
-
date: 2010-
|
20
|
+
date: 2010-09-20 00:00:00 -04:00
|
21
21
|
default_executable:
|
22
22
|
dependencies: []
|
23
23
|
|
@@ -56,7 +56,6 @@ files:
|
|
56
56
|
- test/mongo_bson/jruby_encode_test.rb
|
57
57
|
- test/mongo_bson/json_test.rb
|
58
58
|
- test/mongo_bson/object_id_test.rb
|
59
|
-
- test/mongo_bson/objectid_test.rb
|
60
59
|
- test/mongo_bson/ordered_hash_test.rb
|
61
60
|
has_rdoc: true
|
62
61
|
homepage: http://www.mongodb.org
|
@@ -101,5 +100,4 @@ test_files:
|
|
101
100
|
- test/mongo_bson/jruby_encode_test.rb
|
102
101
|
- test/mongo_bson/json_test.rb
|
103
102
|
- test/mongo_bson/object_id_test.rb
|
104
|
-
- test/mongo_bson/objectid_test.rb
|
105
103
|
- test/mongo_bson/ordered_hash_test.rb
|
@@ -1,132 +0,0 @@
|
|
1
|
-
require 'test/test_helper'
|
2
|
-
require 'rubygems'
|
3
|
-
require 'json'
|
4
|
-
|
5
|
-
class ObjectIDTest < Test::Unit::TestCase
|
6
|
-
|
7
|
-
include Mongo
|
8
|
-
include BSON
|
9
|
-
|
10
|
-
def setup
|
11
|
-
@o = ObjectID.new
|
12
|
-
end
|
13
|
-
|
14
|
-
def test_hashcode
|
15
|
-
assert_equal @o.instance_variable_get(:@data).hash, @o.hash
|
16
|
-
end
|
17
|
-
|
18
|
-
def test_array_uniq_for_equilavent_ids
|
19
|
-
a = ObjectID.new('123')
|
20
|
-
b = ObjectID.new('123')
|
21
|
-
assert_equal a, b
|
22
|
-
assert_equal 1, [a, b].uniq.size
|
23
|
-
end
|
24
|
-
|
25
|
-
def test_create_pk_method
|
26
|
-
doc = {:name => 'Mongo'}
|
27
|
-
doc = ObjectID.create_pk(doc)
|
28
|
-
assert doc[:_id]
|
29
|
-
|
30
|
-
doc = {:name => 'Mongo', :_id => '12345'}
|
31
|
-
doc = ObjectID.create_pk(doc)
|
32
|
-
assert_equal '12345', doc[:_id]
|
33
|
-
end
|
34
|
-
|
35
|
-
def test_different
|
36
|
-
a = ObjectID.new
|
37
|
-
b = ObjectID.new
|
38
|
-
assert_not_equal a.to_a, b.to_a
|
39
|
-
assert_not_equal a, b
|
40
|
-
end
|
41
|
-
|
42
|
-
def test_eql?
|
43
|
-
o2 = ObjectID.new(@o.to_a)
|
44
|
-
assert_equal @o, o2
|
45
|
-
end
|
46
|
-
|
47
|
-
def test_to_s
|
48
|
-
s = @o.to_s
|
49
|
-
assert_equal 24, s.length
|
50
|
-
s =~ /^([0-9a-f]+)$/
|
51
|
-
assert_equal 24, $1.length
|
52
|
-
end
|
53
|
-
|
54
|
-
def test_method
|
55
|
-
assert_equal ObjectID.from_string(@o.to_s), BSON::ObjectID(@o.to_s)
|
56
|
-
end
|
57
|
-
|
58
|
-
def test_inspect
|
59
|
-
assert_equal "BSON::ObjectID('#{@o.to_s}')", @o.inspect
|
60
|
-
end
|
61
|
-
|
62
|
-
def test_save_and_restore
|
63
|
-
host = ENV['MONGO_RUBY_DRIVER_HOST'] || 'localhost'
|
64
|
-
port = ENV['MONGO_RUBY_DRIVER_PORT'] || Connection::DEFAULT_PORT
|
65
|
-
db = Connection.new(host, port).db(MONGO_TEST_DB)
|
66
|
-
coll = db.collection('test')
|
67
|
-
|
68
|
-
coll.remove
|
69
|
-
coll << {'a' => 1, '_id' => @o}
|
70
|
-
|
71
|
-
row = coll.find().collect.first
|
72
|
-
assert_equal 1, row['a']
|
73
|
-
assert_equal @o, row['_id']
|
74
|
-
end
|
75
|
-
|
76
|
-
def test_from_string
|
77
|
-
hex_str = @o.to_s
|
78
|
-
o2 = ObjectID.from_string(hex_str)
|
79
|
-
assert_equal hex_str, o2.to_s
|
80
|
-
assert_equal @o, o2
|
81
|
-
assert_equal @o.to_s, o2.to_s
|
82
|
-
end
|
83
|
-
|
84
|
-
def test_illegal_from_string
|
85
|
-
assert_raise InvalidObjectID do
|
86
|
-
ObjectID.from_string("")
|
87
|
-
end
|
88
|
-
end
|
89
|
-
|
90
|
-
def test_legal
|
91
|
-
assert !ObjectID.legal?(nil)
|
92
|
-
assert !ObjectID.legal?("fred")
|
93
|
-
assert !ObjectID.legal?("0000")
|
94
|
-
assert !ObjectID.legal?('000102030405060708090A0')
|
95
|
-
assert ObjectID.legal?('000102030405060708090A0B')
|
96
|
-
assert ObjectID.legal?('abcdefABCDEF123456789012')
|
97
|
-
assert !ObjectID.legal?('abcdefABCDEF12345678901x')
|
98
|
-
end
|
99
|
-
|
100
|
-
def test_from_string_leading_zeroes
|
101
|
-
hex_str = '000000000000000000000000'
|
102
|
-
o = ObjectID.from_string(hex_str)
|
103
|
-
assert_equal hex_str, o.to_s
|
104
|
-
end
|
105
|
-
|
106
|
-
def test_byte_order
|
107
|
-
hex_str = '000102030405060708090A0B'
|
108
|
-
o = ObjectID.from_string(hex_str)
|
109
|
-
assert_equal [0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b], o.to_a
|
110
|
-
end
|
111
|
-
|
112
|
-
def test_generation_time
|
113
|
-
time = Time.now
|
114
|
-
id = ObjectID.new
|
115
|
-
generated_time = id.generation_time
|
116
|
-
|
117
|
-
assert_in_delta time.to_i, generated_time.to_i, 2
|
118
|
-
assert_equal "UTC", generated_time.zone
|
119
|
-
end
|
120
|
-
|
121
|
-
def test_from_time
|
122
|
-
time = Time.now.utc
|
123
|
-
id = ObjectID.from_time(time)
|
124
|
-
|
125
|
-
assert_equal time.to_i, id.generation_time.to_i
|
126
|
-
end
|
127
|
-
|
128
|
-
def test_json
|
129
|
-
id = ObjectID.new
|
130
|
-
assert_equal "{\"$oid\": \"#{id}\"}", id.to_json
|
131
|
-
end
|
132
|
-
end
|