bson 1.2.4 → 1.3.0.rc0
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 +3 -2
- data/lib/bson/bson_ruby.rb +24 -2
- data/lib/bson/byte_buffer.rb +4 -1
- data/lib/bson/ordered_hash.rb +9 -0
- data/lib/bson/types/binary.rb +1 -1
- data/lib/bson/types/object_id.rb +31 -20
- data/lib/bson/types/timestamp.rb +76 -0
- data/test/bson/bson_test.rb +68 -27
- data/test/bson/byte_buffer_test.rb +11 -0
- data/test/bson/object_id_test.rb +14 -1
- data/test/bson/ordered_hash_test.rb +7 -0
- data/test/bson/timestamp_test.rb +24 -0
- metadata +9 -17
data/lib/bson.rb
CHANGED
@@ -18,10 +18,10 @@
|
|
18
18
|
|
19
19
|
$:.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
|
20
20
|
|
21
|
-
MINIMUM_BSON_EXT_VERSION = "1.
|
21
|
+
MINIMUM_BSON_EXT_VERSION = "1.3.0.rc0"
|
22
22
|
|
23
23
|
module BSON
|
24
|
-
VERSION = "1.
|
24
|
+
VERSION = "1.3.0.rc0"
|
25
25
|
|
26
26
|
if defined? Mongo::DEFAULT_MAX_BSON_SIZE
|
27
27
|
DEFAULT_MAX_BSON_SIZE = Mongo::DEFAULT_MAX_BSON_SIZE
|
@@ -100,6 +100,7 @@ require 'bson/types/code'
|
|
100
100
|
require 'bson/types/dbref'
|
101
101
|
require 'bson/types/object_id'
|
102
102
|
require 'bson/types/min_max_keys'
|
103
|
+
require 'bson/types/timestamp'
|
103
104
|
|
104
105
|
require 'base64'
|
105
106
|
require 'bson/ordered_hash'
|
data/lib/bson/bson_ruby.rb
CHANGED
@@ -57,6 +57,11 @@ module BSON
|
|
57
57
|
BINARY_ENCODING = Encoding.find('binary')
|
58
58
|
|
59
59
|
def self.to_utf8_binary(str)
|
60
|
+
begin
|
61
|
+
str.unpack("U*")
|
62
|
+
rescue => ex
|
63
|
+
raise InvalidStringEncoding, "String not valid utf-8: #{str.inspect}"
|
64
|
+
end
|
60
65
|
str.encode(UTF8_ENCODING).force_encoding(BINARY_ENCODING)
|
61
66
|
end
|
62
67
|
else
|
@@ -187,6 +192,8 @@ module BSON
|
|
187
192
|
serialize_max_key_element(@buf, k)
|
188
193
|
when MINKEY
|
189
194
|
serialize_min_key_element(@buf, k)
|
195
|
+
when TIMESTAMP
|
196
|
+
serialize_timestamp_element(@buf, k, v)
|
190
197
|
else
|
191
198
|
raise "unhandled type #{type}"
|
192
199
|
end
|
@@ -256,8 +263,7 @@ module BSON
|
|
256
263
|
doc[key] = deserialize_code_w_scope_data(@buf)
|
257
264
|
when TIMESTAMP
|
258
265
|
key = deserialize_cstr(@buf)
|
259
|
-
doc[key] =
|
260
|
-
deserialize_number_int_data(@buf)]
|
266
|
+
doc[key] = deserialize_timestamp_data(@buf)
|
261
267
|
when MAXKEY
|
262
268
|
key = deserialize_cstr(@buf)
|
263
269
|
doc[key] = MaxKey.new
|
@@ -341,6 +347,12 @@ module BSON
|
|
341
347
|
Regexp.new(str, opts)
|
342
348
|
end
|
343
349
|
|
350
|
+
def deserialize_timestamp_data(buf)
|
351
|
+
increment = buf.get_int
|
352
|
+
seconds = buf.get_int
|
353
|
+
Timestamp.new(seconds, increment)
|
354
|
+
end
|
355
|
+
|
344
356
|
def encoded_str(str)
|
345
357
|
if RUBY_VERSION >= '1.9'
|
346
358
|
str.force_encoding("utf-8")
|
@@ -505,6 +517,14 @@ module BSON
|
|
505
517
|
self.class.serialize_key(buf, key)
|
506
518
|
end
|
507
519
|
|
520
|
+
def serialize_timestamp_element(buf, key, val)
|
521
|
+
buf.put(TIMESTAMP)
|
522
|
+
self.class.serialize_key(buf, key)
|
523
|
+
|
524
|
+
buf.put_int(val.increment)
|
525
|
+
buf.put_int(val.seconds)
|
526
|
+
end
|
527
|
+
|
508
528
|
def serialize_oid_element(buf, key, val)
|
509
529
|
buf.put(OID)
|
510
530
|
self.class.serialize_key(buf, key)
|
@@ -593,6 +613,8 @@ module BSON
|
|
593
613
|
MAXKEY
|
594
614
|
when MinKey
|
595
615
|
MINKEY
|
616
|
+
when Timestamp
|
617
|
+
TIMESTAMP
|
596
618
|
when Numeric
|
597
619
|
raise InvalidDocument, "Cannot serialize the Numeric type #{o.class} as BSON; only Fixum, Bignum, and Float are supported."
|
598
620
|
when Date, DateTime
|
data/lib/bson/byte_buffer.rb
CHANGED
@@ -231,6 +231,10 @@ module BSON
|
|
231
231
|
def more?
|
232
232
|
@cursor < @str.size
|
233
233
|
end
|
234
|
+
|
235
|
+
def ==(other)
|
236
|
+
other.respond_to?(:to_s) && @str == other.to_s
|
237
|
+
end
|
234
238
|
|
235
239
|
def to_a
|
236
240
|
@str.unpack("C*")
|
@@ -245,7 +249,6 @@ module BSON
|
|
245
249
|
end
|
246
250
|
|
247
251
|
def dump
|
248
|
-
i = 0
|
249
252
|
@str.each_byte do |c, i|
|
250
253
|
$stderr.puts "#{'%04d' % i}: #{'%02x' % c} #{'%03o' % c} #{'%s' % c.chr} #{'%3d' % c}"
|
251
254
|
i += 1
|
data/lib/bson/ordered_hash.rb
CHANGED
@@ -103,6 +103,15 @@ module BSON
|
|
103
103
|
end
|
104
104
|
|
105
105
|
alias :update :merge!
|
106
|
+
|
107
|
+
def dup
|
108
|
+
result = OrderedHash.new
|
109
|
+
@ordered_keys ||= []
|
110
|
+
@ordered_keys.each do |key|
|
111
|
+
result[key] = self[key]
|
112
|
+
end
|
113
|
+
result
|
114
|
+
end
|
106
115
|
|
107
116
|
def inspect
|
108
117
|
str = '{'
|
data/lib/bson/types/binary.rb
CHANGED
@@ -43,7 +43,7 @@ module BSON
|
|
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
|
-
def initialize(data=[], subtype=
|
46
|
+
def initialize(data=[], subtype=SUBTYPE_SIMPLE)
|
47
47
|
super(data)
|
48
48
|
@subtype = subtype
|
49
49
|
end
|
data/lib/bson/types/object_id.rb
CHANGED
@@ -33,6 +33,8 @@ module BSON
|
|
33
33
|
@@lock = Mutex.new
|
34
34
|
@@index = 0
|
35
35
|
|
36
|
+
attr_accessor :data
|
37
|
+
|
36
38
|
# Create a new object id. If no parameter is given, an id corresponding
|
37
39
|
# to the ObjectId BSON data type will be created. This is a 12-byte value
|
38
40
|
# consisting of a 4-byte timestamp, a 3-byte machine id, a 2-byte process id,
|
@@ -40,12 +42,15 @@ module BSON
|
|
40
42
|
#
|
41
43
|
# @param [Array] data should be an array of bytes. If you want
|
42
44
|
# to generate a standard MongoDB object id, leave this argument blank.
|
43
|
-
|
44
|
-
|
45
|
+
#
|
46
|
+
# @option opts :data (nil) An array of bytes to use as the object id.
|
47
|
+
# @option opts :time (nil) The value of this object ids timestamp. Note that
|
48
|
+
# the remaining bytes will consist of the standard machine id, pid, and counter. If
|
49
|
+
# you need a zeroed timestamp, used ObjectId.from_time.
|
50
|
+
def initialize(data=nil, time=nil)
|
51
|
+
@data = data || generate(time)
|
45
52
|
end
|
46
53
|
|
47
|
-
attr_accessor :data
|
48
|
-
|
49
54
|
# Determine if the supplied string is legal. Legal strings will
|
50
55
|
# consist of 24 hexadecimal characters.
|
51
56
|
#
|
@@ -62,14 +67,23 @@ module BSON
|
|
62
67
|
#
|
63
68
|
# @param [Time] time a utc time to encode as an object id.
|
64
69
|
#
|
70
|
+
# @option opts [:unique] (false) If false, the object id's bytes
|
71
|
+
# succeeding the timestamp will be zeroed; if true, they'll
|
72
|
+
# consist of the standard machine id, pid, and counter.
|
73
|
+
#
|
65
74
|
# @return [Mongo::ObjectId]
|
66
75
|
#
|
67
76
|
# @example Return all document created before Jan 1, 2010.
|
68
77
|
# time = Time.utc(2010, 1, 1)
|
69
78
|
# time_id = ObjectId.from_time(time)
|
70
79
|
# collection.find({'_id' => {'$lt' => time_id}})
|
71
|
-
def self.from_time(time)
|
72
|
-
|
80
|
+
def self.from_time(time, opts={})
|
81
|
+
unique = opts.fetch(:unique, false)
|
82
|
+
if unique
|
83
|
+
self.new(nil, time)
|
84
|
+
else
|
85
|
+
self.new([time.to_i,0,0].pack("NNN").unpack("C12"))
|
86
|
+
end
|
73
87
|
end
|
74
88
|
|
75
89
|
# Adds a primary key to the given document if needed.
|
@@ -84,9 +98,9 @@ module BSON
|
|
84
98
|
|
85
99
|
# Check equality of this object id with another.
|
86
100
|
#
|
87
|
-
# @param [
|
101
|
+
# @param [BSON::ObjectId] object_id
|
88
102
|
def eql?(object_id)
|
89
|
-
|
103
|
+
object_id.kind_of?(BSON::ObjectId) and self.data == object_id.data
|
90
104
|
end
|
91
105
|
alias_method :==, :eql?
|
92
106
|
|
@@ -105,13 +119,6 @@ module BSON
|
|
105
119
|
@data.dup
|
106
120
|
end
|
107
121
|
|
108
|
-
# Get the array representation without cloning.
|
109
|
-
#
|
110
|
-
# @return [Array]
|
111
|
-
def data
|
112
|
-
@data
|
113
|
-
end
|
114
|
-
|
115
122
|
# Given a string representation of an ObjectId, return a new ObjectId
|
116
123
|
# with that value.
|
117
124
|
#
|
@@ -145,10 +152,10 @@ module BSON
|
|
145
152
|
def to_json(*a)
|
146
153
|
"{\"$oid\": \"#{to_s}\"}"
|
147
154
|
end
|
148
|
-
|
155
|
+
|
149
156
|
# Create the JSON hash structure convert to MongoDB extended format. Rails 2.3.3
|
150
157
|
# introduced as_json to create the needed hash structure to encode objects into JSON.
|
151
|
-
#
|
158
|
+
#
|
152
159
|
# @return [Hash] the hash representation as MongoDB extended JSON
|
153
160
|
def as_json(options ={})
|
154
161
|
{"$oid" => to_s}
|
@@ -166,12 +173,16 @@ module BSON
|
|
166
173
|
private
|
167
174
|
|
168
175
|
# This gets overwritten by the C extension if it loads.
|
169
|
-
def generate
|
176
|
+
def generate(oid_time=nil)
|
170
177
|
oid = ''
|
171
178
|
|
172
179
|
# 4 bytes current time
|
173
|
-
|
174
|
-
|
180
|
+
if oid_time
|
181
|
+
t = oid_time.to_i
|
182
|
+
else
|
183
|
+
t = Time.new.to_i
|
184
|
+
end
|
185
|
+
oid += [t].pack("N")
|
175
186
|
|
176
187
|
# 3 bytes machine
|
177
188
|
oid += Digest::MD5.digest(Socket.gethostname)[0, 3]
|
@@ -0,0 +1,76 @@
|
|
1
|
+
# encoding: UTF-8
|
2
|
+
|
3
|
+
# --
|
4
|
+
# Copyright (C) 2008-2011 10gen Inc.
|
5
|
+
#
|
6
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
7
|
+
# you may not use this file except in compliance with the License.
|
8
|
+
# You may obtain a copy of the License at
|
9
|
+
#
|
10
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
11
|
+
#
|
12
|
+
# Unless required by applicable law or agreed to in writing, software
|
13
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
14
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
15
|
+
# See the License for the specific language governing permissions and
|
16
|
+
# limitations under the License.
|
17
|
+
# ++
|
18
|
+
|
19
|
+
module BSON
|
20
|
+
|
21
|
+
# A class for representing BSON Timestamps. The Timestamp type is used
|
22
|
+
# by MongoDB internally; thus, it should be used by application developers
|
23
|
+
# for diagnostic purposes only.
|
24
|
+
class Timestamp
|
25
|
+
include Enumerable
|
26
|
+
|
27
|
+
attr_reader :seconds, :increment
|
28
|
+
|
29
|
+
# Create a new BSON Timestamp.
|
30
|
+
#
|
31
|
+
# @param [Integer] seconds The number of seconds
|
32
|
+
# @param increment
|
33
|
+
def initialize(seconds, increment)
|
34
|
+
@seconds = seconds
|
35
|
+
@increment = increment
|
36
|
+
end
|
37
|
+
|
38
|
+
def to_s
|
39
|
+
"seconds: #{seconds}, increment: #{increment}"
|
40
|
+
end
|
41
|
+
|
42
|
+
def ==(other)
|
43
|
+
self.seconds == other.seconds &&
|
44
|
+
self.increment == other.increment
|
45
|
+
end
|
46
|
+
|
47
|
+
# This is for backward-compatibility. Timestamps in the Ruby
|
48
|
+
# driver used to deserialize as arrays. So we provide
|
49
|
+
# an equivalent interface.
|
50
|
+
#
|
51
|
+
# @deprecated
|
52
|
+
def [](index)
|
53
|
+
warn "Timestamps are no longer deserialized as arrays. If you're working " +
|
54
|
+
"with BSON Timestamp objects, see the BSON::Timestamp class. This usage will " +
|
55
|
+
"be deprecated in Ruby Driver v2.0."
|
56
|
+
if index == 0
|
57
|
+
self.increment
|
58
|
+
elsif index == 1
|
59
|
+
self.seconds
|
60
|
+
else
|
61
|
+
nil
|
62
|
+
end
|
63
|
+
end
|
64
|
+
|
65
|
+
# This method exists only for backward-compatibility.
|
66
|
+
#
|
67
|
+
# @deprecated
|
68
|
+
def each
|
69
|
+
i = 0
|
70
|
+
while i < 2
|
71
|
+
yield self[i]
|
72
|
+
i += 1
|
73
|
+
end
|
74
|
+
end
|
75
|
+
end
|
76
|
+
end
|
data/test/bson/bson_test.rb
CHANGED
@@ -1,10 +1,14 @@
|
|
1
1
|
# encoding:utf-8
|
2
2
|
require './test/test_helper'
|
3
|
-
|
3
|
+
|
4
|
+
if RUBY_VERSION < '1.9'
|
5
|
+
require 'complex'
|
6
|
+
require 'rational'
|
7
|
+
end
|
4
8
|
require 'bigdecimal'
|
5
|
-
require 'rational'
|
6
9
|
|
7
10
|
begin
|
11
|
+
require 'tzinfo'
|
8
12
|
require 'active_support/core_ext'
|
9
13
|
Time.zone = "Pacific Time (US & Canada)"
|
10
14
|
Zone = Time.zone.now
|
@@ -116,15 +120,23 @@ class BSONTest < Test::Unit::TestCase
|
|
116
120
|
end
|
117
121
|
else
|
118
122
|
def test_non_utf8_string
|
119
|
-
|
120
|
-
|
121
|
-
|
122
|
-
|
123
|
+
assert_raise BSON::InvalidStringEncoding do
|
124
|
+
BSON::BSON_CODER.serialize({'str' => 'aé'.encode('iso-8859-1')})
|
125
|
+
end
|
126
|
+
end
|
127
|
+
|
128
|
+
def test_invalid_utf8_string
|
129
|
+
str = "123\xD9"
|
130
|
+
assert !str.valid_encoding?
|
131
|
+
assert_raise BSON::InvalidStringEncoding do
|
132
|
+
BSON::BSON_CODER.serialize({'str' => str})
|
133
|
+
end
|
123
134
|
end
|
124
135
|
|
125
136
|
def test_non_utf8_key
|
126
|
-
|
127
|
-
|
137
|
+
assert_raise BSON::InvalidStringEncoding do
|
138
|
+
BSON::BSON_CODER.serialize({'aé'.encode('iso-8859-1') => 'hello'})
|
139
|
+
end
|
128
140
|
end
|
129
141
|
|
130
142
|
# Based on a test from sqlite3-ruby
|
@@ -134,14 +146,14 @@ class BSONTest < Test::Unit::TestCase
|
|
134
146
|
str = "壁に耳あり、障子に目あり"
|
135
147
|
bson = BSON::BSON_CODER.serialize("x" => str)
|
136
148
|
|
137
|
-
Encoding.default_internal = 'EUC-JP'
|
149
|
+
silently { Encoding.default_internal = 'EUC-JP' }
|
138
150
|
out = BSON::BSON_CODER.deserialize(bson)["x"]
|
139
151
|
|
140
152
|
assert_equal Encoding.default_internal, out.encoding
|
141
153
|
assert_equal str.encode('EUC-JP'), out
|
142
154
|
assert_equal str, out.encode(str.encoding)
|
143
155
|
ensure
|
144
|
-
Encoding.default_internal = before_enc
|
156
|
+
silently { Encoding.default_internal = before_enc }
|
145
157
|
end
|
146
158
|
end
|
147
159
|
|
@@ -161,12 +173,12 @@ class BSONTest < Test::Unit::TestCase
|
|
161
173
|
assert_doc_pass(doc)
|
162
174
|
end
|
163
175
|
|
164
|
-
|
165
|
-
|
166
|
-
|
167
|
-
|
176
|
+
def test_double
|
177
|
+
doc = {'doc' => 41.25}
|
178
|
+
assert_doc_pass(doc)
|
179
|
+
end
|
168
180
|
|
169
|
-
|
181
|
+
def test_int
|
170
182
|
doc = {'doc' => 42}
|
171
183
|
assert_doc_pass(doc)
|
172
184
|
|
@@ -246,8 +258,14 @@ class BSONTest < Test::Unit::TestCase
|
|
246
258
|
assert_in_delta doc['date'], doc2['date'], 0.001
|
247
259
|
end
|
248
260
|
|
261
|
+
def test_date_in_array
|
262
|
+
doc = {'date' => [Time.now.utc]}
|
263
|
+
bson = @encoder.serialize(doc)
|
264
|
+
doc2 = @encoder.deserialize(bson)
|
265
|
+
end
|
266
|
+
|
249
267
|
def test_date_returns_as_utc
|
250
|
-
doc = {'date' => Time.now}
|
268
|
+
doc = {'date' => Time.now.utc}
|
251
269
|
bson = @encoder.serialize(doc)
|
252
270
|
doc2 = @encoder.deserialize(bson)
|
253
271
|
assert doc2['date'].utc?
|
@@ -277,7 +295,7 @@ class BSONTest < Test::Unit::TestCase
|
|
277
295
|
ensure
|
278
296
|
if !invalid_date.is_a? Time
|
279
297
|
assert_equal InvalidDocument, e.class
|
280
|
-
assert_match
|
298
|
+
assert_match(/UTC Time/, e.message)
|
281
299
|
end
|
282
300
|
end
|
283
301
|
end
|
@@ -311,6 +329,20 @@ class BSONTest < Test::Unit::TestCase
|
|
311
329
|
bin = Binary.new
|
312
330
|
'binstring'.each_byte { |b| bin.put(b) }
|
313
331
|
|
332
|
+
doc = {'bin' => bin}
|
333
|
+
bson = @encoder.serialize(doc)
|
334
|
+
doc2 = @encoder.deserialize(bson)
|
335
|
+
bin2 = doc2['bin']
|
336
|
+
assert_kind_of Binary, bin2
|
337
|
+
assert_equal 'binstring', bin2.to_s
|
338
|
+
assert_equal Binary::SUBTYPE_SIMPLE, bin2.subtype
|
339
|
+
end
|
340
|
+
|
341
|
+
def test_binary_with_deprecated_subtype
|
342
|
+
bin = Binary.new
|
343
|
+
'binstring'.each_byte { |b| bin.put(b) }
|
344
|
+
bin.subtype = Binary::SUBTYPE_BYTES
|
345
|
+
|
314
346
|
doc = {'bin' => bin}
|
315
347
|
bson = @encoder.serialize(doc)
|
316
348
|
doc2 = @encoder.deserialize(bson)
|
@@ -328,7 +360,7 @@ class BSONTest < Test::Unit::TestCase
|
|
328
360
|
bin2 = doc2['bin']
|
329
361
|
assert_kind_of Binary, bin2
|
330
362
|
assert_equal 'somebinarystring', bin2.to_s
|
331
|
-
assert_equal Binary::
|
363
|
+
assert_equal Binary::SUBTYPE_SIMPLE, bin2.subtype
|
332
364
|
end
|
333
365
|
|
334
366
|
def test_binary_type
|
@@ -368,7 +400,7 @@ class BSONTest < Test::Unit::TestCase
|
|
368
400
|
bin2 = doc2['bin']
|
369
401
|
assert_kind_of Binary, bin2
|
370
402
|
assert_equal [1, 2, 3, 4, 5], bin2.to_a
|
371
|
-
assert_equal Binary::
|
403
|
+
assert_equal Binary::SUBTYPE_SIMPLE, bin2.subtype
|
372
404
|
end
|
373
405
|
|
374
406
|
def test_put_id_first
|
@@ -393,15 +425,24 @@ class BSONTest < Test::Unit::TestCase
|
|
393
425
|
if !(RUBY_PLATFORM =~ /java/)
|
394
426
|
def test_timestamp
|
395
427
|
val = {"test" => [4, 20]}
|
396
|
-
|
397
|
-
|
398
|
-
|
399
|
-
|
400
|
-
|
428
|
+
result = @encoder.deserialize([0x13, 0x00, 0x00, 0x00,
|
429
|
+
0x11, 0x74, 0x65, 0x73,
|
430
|
+
0x74, 0x00, 0x04, 0x00,
|
431
|
+
0x00, 0x00, 0x14, 0x00,
|
432
|
+
0x00, 0x00, 0x00])
|
401
433
|
|
434
|
+
assert_equal 4, result["test"][0]
|
435
|
+
assert_equal 20, result["test"][1]
|
402
436
|
end
|
403
437
|
end
|
404
438
|
|
439
|
+
def test_timestamp_type
|
440
|
+
ts = Timestamp.new(5000, 100)
|
441
|
+
doc = {:ts => ts}
|
442
|
+
bson = @encoder.serialize(doc)
|
443
|
+
assert_equal ts, @encoder.deserialize(bson)["ts"]
|
444
|
+
end
|
445
|
+
|
405
446
|
def test_overflow
|
406
447
|
doc = {"x" => 2**75}
|
407
448
|
assert_raise RangeError do
|
@@ -439,7 +480,7 @@ class BSONTest < Test::Unit::TestCase
|
|
439
480
|
rescue => e
|
440
481
|
ensure
|
441
482
|
assert_equal InvalidDocument, e.class
|
442
|
-
assert_match
|
483
|
+
assert_match(/Cannot serialize/, e.message)
|
443
484
|
end
|
444
485
|
end
|
445
486
|
end
|
@@ -573,7 +614,7 @@ class BSONTest < Test::Unit::TestCase
|
|
573
614
|
@encoder.serialize({"he\0llo" => "world"}, true)
|
574
615
|
end
|
575
616
|
|
576
|
-
|
617
|
+
assert_raise BSON::InvalidKeyName do
|
577
618
|
@encoder.serialize({"$hello" => "world"}, true)
|
578
619
|
end
|
579
620
|
|
@@ -581,7 +622,7 @@ class BSONTest < Test::Unit::TestCase
|
|
581
622
|
@encoder.serialize({"hello" => {"$hello" => "world"}}, true)
|
582
623
|
end
|
583
624
|
|
584
|
-
|
625
|
+
assert_raise BSON::InvalidKeyName do
|
585
626
|
@encoder.serialize({".hello" => "world"}, true)
|
586
627
|
end
|
587
628
|
|
@@ -186,5 +186,16 @@ class ByteBufferTest < Test::Unit::TestCase
|
|
186
186
|
@buf.get_int
|
187
187
|
assert !@buf.more?
|
188
188
|
end
|
189
|
+
|
190
|
+
def test_equality
|
191
|
+
@buf = ByteBuffer.new("foo")
|
192
|
+
assert_equal @buf, @buf
|
193
|
+
assert_equal ByteBuffer.new(""), ByteBuffer.new("")
|
194
|
+
assert_equal ByteBuffer.new("123"), ByteBuffer.new("123")
|
195
|
+
assert_not_equal ByteBuffer.new("123"), ByteBuffer.new("1234")
|
196
|
+
assert_equal @buf, "foo"
|
197
|
+
assert_not_equal @buf, 123
|
198
|
+
assert_not_equal @buf, nil
|
199
|
+
end
|
189
200
|
|
190
201
|
end
|
data/test/bson/object_id_test.rb
CHANGED
@@ -126,14 +126,27 @@ class ObjectIdTest < Test::Unit::TestCase
|
|
126
126
|
time = Time.now.utc
|
127
127
|
id = ObjectId.from_time(time)
|
128
128
|
|
129
|
+
assert id.to_a[4, 8].all? {|byte| byte == 0 }
|
129
130
|
assert_equal time.to_i, id.generation_time.to_i
|
130
131
|
end
|
131
132
|
|
133
|
+
def test_from_time_unique
|
134
|
+
time = Time.now.utc
|
135
|
+
id = ObjectId.from_time(time, :unique => true)
|
136
|
+
|
137
|
+
mac_id = Digest::MD5.digest(Socket.gethostname)[0, 3].unpack("C3")
|
138
|
+
assert_equal id.to_a[4, 3], mac_id
|
139
|
+
assert_equal time.to_i, id.generation_time.to_i
|
140
|
+
|
141
|
+
id2 = ObjectId.new(nil, time)
|
142
|
+
assert_equal time.to_i, id2.generation_time.to_i
|
143
|
+
end
|
144
|
+
|
132
145
|
def test_json
|
133
146
|
id = ObjectId.new
|
134
147
|
assert_equal "{\"$oid\": \"#{id}\"}", id.to_json
|
135
148
|
end
|
136
|
-
|
149
|
+
|
137
150
|
def test_as_json
|
138
151
|
id = ObjectId.new
|
139
152
|
assert_equal({"$oid" => id.to_s}, id.as_json)
|
@@ -0,0 +1,24 @@
|
|
1
|
+
require './test/test_helper'
|
2
|
+
|
3
|
+
class TiumestampTest < Test::Unit::TestCase
|
4
|
+
include Mongo
|
5
|
+
|
6
|
+
def test_timestamp_equality
|
7
|
+
t1 = Timestamp.new(5000, 200)
|
8
|
+
t2 = Timestamp.new(5000, 200)
|
9
|
+
assert_equal t2, t1
|
10
|
+
end
|
11
|
+
|
12
|
+
def test_implements_array_for_backward_compatibility
|
13
|
+
ts = Timestamp.new(5000, 200)
|
14
|
+
assert_equal 200, ts[0]
|
15
|
+
assert_equal 5000, ts[1]
|
16
|
+
|
17
|
+
array = ts.map {|t| t }
|
18
|
+
assert_equal 2, array.length
|
19
|
+
|
20
|
+
assert_equal 200, array[0]
|
21
|
+
assert_equal 5000, array[1]
|
22
|
+
end
|
23
|
+
|
24
|
+
end
|
metadata
CHANGED
@@ -1,13 +1,8 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: bson
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
|
5
|
-
|
6
|
-
segments:
|
7
|
-
- 1
|
8
|
-
- 2
|
9
|
-
- 4
|
10
|
-
version: 1.2.4
|
4
|
+
prerelease: 6
|
5
|
+
version: 1.3.0.rc0
|
11
6
|
platform: ruby
|
12
7
|
authors:
|
13
8
|
- Jim Menard
|
@@ -17,7 +12,7 @@ autorequire:
|
|
17
12
|
bindir: bin
|
18
13
|
cert_chain: []
|
19
14
|
|
20
|
-
date: 2011-
|
15
|
+
date: 2011-03-29 00:00:00 -04:00
|
21
16
|
default_executable:
|
22
17
|
dependencies: []
|
23
18
|
|
@@ -44,6 +39,7 @@ files:
|
|
44
39
|
- lib/bson/types/dbref.rb
|
45
40
|
- lib/bson/types/min_max_keys.rb
|
46
41
|
- lib/bson/types/object_id.rb
|
42
|
+
- lib/bson/types/timestamp.rb
|
47
43
|
- bin/b2json
|
48
44
|
- bin/j2bson
|
49
45
|
- test/bson/binary_test.rb
|
@@ -54,6 +50,7 @@ files:
|
|
54
50
|
- test/bson/json_test.rb
|
55
51
|
- test/bson/object_id_test.rb
|
56
52
|
- test/bson/ordered_hash_test.rb
|
53
|
+
- test/bson/timestamp_test.rb
|
57
54
|
has_rdoc: true
|
58
55
|
homepage: http://www.mongodb.org
|
59
56
|
licenses: []
|
@@ -68,23 +65,17 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
68
65
|
requirements:
|
69
66
|
- - ">="
|
70
67
|
- !ruby/object:Gem::Version
|
71
|
-
hash: 3
|
72
|
-
segments:
|
73
|
-
- 0
|
74
68
|
version: "0"
|
75
69
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
76
70
|
none: false
|
77
71
|
requirements:
|
78
|
-
- - "
|
72
|
+
- - ">"
|
79
73
|
- !ruby/object:Gem::Version
|
80
|
-
|
81
|
-
segments:
|
82
|
-
- 0
|
83
|
-
version: "0"
|
74
|
+
version: 1.3.1
|
84
75
|
requirements: []
|
85
76
|
|
86
77
|
rubyforge_project:
|
87
|
-
rubygems_version: 1.
|
78
|
+
rubygems_version: 1.5.2
|
88
79
|
signing_key:
|
89
80
|
specification_version: 3
|
90
81
|
summary: Ruby implementation of BSON
|
@@ -97,3 +88,4 @@ test_files:
|
|
97
88
|
- test/bson/json_test.rb
|
98
89
|
- test/bson/object_id_test.rb
|
99
90
|
- test/bson/ordered_hash_test.rb
|
91
|
+
- test/bson/timestamp_test.rb
|