bson 1.2.4-jruby → 1.3.0-jruby

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.

Binary file
@@ -18,10 +18,10 @@
18
18
 
19
19
  $:.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
20
20
 
21
- MINIMUM_BSON_EXT_VERSION = "1.2.4"
21
+ MINIMUM_BSON_EXT_VERSION = "1.3.0"
22
22
 
23
23
  module BSON
24
- VERSION = "1.2.4"
24
+ VERSION = "1.3.0"
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'
@@ -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] = [deserialize_number_int_data(@buf),
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
@@ -231,13 +231,17 @@ 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
- def to_a
236
- @str.unpack("C*")
239
+ def to_a(format="C*")
240
+ @str.unpack(format)
237
241
  end
238
242
 
239
- def unpack(args)
240
- to_a
243
+ def unpack(format="C*")
244
+ to_a(format)
241
245
  end
242
246
 
243
247
  def to_s
@@ -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
@@ -62,11 +62,11 @@ module BSON
62
62
  end
63
63
 
64
64
  def keys
65
- @ordered_keys || []
65
+ @ordered_keys# || []
66
66
  end
67
67
 
68
68
  def []=(key, value)
69
- @ordered_keys ||= []
69
+ #@ordered_keys ||= []
70
70
  unless has_key?(key)
71
71
  @ordered_keys << key
72
72
  end
@@ -74,14 +74,14 @@ module BSON
74
74
  end
75
75
 
76
76
  def each
77
- @ordered_keys ||= []
77
+ #@ordered_keys ||= []
78
78
  @ordered_keys.each { |k| yield k, self[k] }
79
79
  self
80
80
  end
81
81
  alias :each_pair :each
82
82
 
83
83
  def to_a
84
- @ordered_keys ||= []
84
+ #@ordered_keys ||= []
85
85
  @ordered_keys.map { |k| [k, self[k]] }
86
86
  end
87
87
 
@@ -96,13 +96,22 @@ module BSON
96
96
  end
97
97
 
98
98
  def merge!(other)
99
- @ordered_keys ||= []
99
+ #@ordered_keys ||= []
100
100
  @ordered_keys += other.keys # unordered if not an BSON::OrderedHash
101
101
  @ordered_keys.uniq!
102
102
  super(other)
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 = '{'
@@ -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=SUBTYPE_BYTES)
46
+ def initialize(data=[], subtype=SUBTYPE_SIMPLE)
47
47
  super(data)
48
48
  @subtype = subtype
49
49
  end
@@ -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
- def initialize(data=nil)
44
- @data = data || generate
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
- self.new([time.to_i,0,0].pack("NNN").unpack("C12"))
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 [Mongo::ObjectId] object_id
101
+ # @param [BSON::ObjectId] object_id
88
102
  def eql?(object_id)
89
- @data == object_id.instance_variable_get("@data")
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
- time = Time.new.to_i
174
- oid += [time].pack("N")
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
@@ -1,10 +1,14 @@
1
1
  # encoding:utf-8
2
2
  require './test/test_helper'
3
- require 'complex'
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
- bson = BSON::BSON_CODER.serialize({'str' => 'aé'.encode('iso-8859-1')})
120
- result = BSON::BSON_CODER.deserialize(bson)['str']
121
- assert_equal 'aé', result
122
- assert_equal 'UTF-8', result.encoding.name
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
- bson = BSON::BSON_CODER.serialize({'aé'.encode('iso-8859-1') => 'hello'})
127
- assert_equal 'hello', BSON::BSON_CODER.deserialize(bson)['aé']
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
- def test_double
165
- doc = {'doc' => 41.25}
166
- assert_doc_pass(doc)
167
- end
176
+ def test_double
177
+ doc = {'doc' => 41.25}
178
+ assert_doc_pass(doc)
179
+ end
168
180
 
169
- def test_int
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 /UTC Time/, e.message
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::SUBTYPE_BYTES, bin2.subtype
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::SUBTYPE_BYTES, bin2.subtype
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
- assert_equal val, @encoder.deserialize([0x13, 0x00, 0x00, 0x00,
397
- 0x11, 0x74, 0x65, 0x73,
398
- 0x74, 0x00, 0x04, 0x00,
399
- 0x00, 0x00, 0x14, 0x00,
400
- 0x00, 0x00, 0x00])
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 /Cannot serialize/, e.message
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
- assert_raise_error BSON::InvalidKeyName, "$hello" do
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
- assert_raise_error BSON::InvalidKeyName, ".hello" do
625
+ assert_raise BSON::InvalidKeyName do
585
626
  @encoder.serialize({".hello" => "world"}, true)
586
627
  end
587
628
 
@@ -21,6 +21,13 @@ class ByteBufferTest < Test::Unit::TestCase
21
21
  assert_equal 1, @buf.get
22
22
  end
23
23
 
24
+ def test_unpack
25
+ @buf.put_array([17, 2, 3, 4])
26
+ assert_equal [17, 2, 3, 4], @buf.to_a
27
+ assert_equal ["11020304"], @buf.unpack("H*")
28
+ assert_equal ["11020304"], @buf.to_a("H*")
29
+ end
30
+
24
31
  def test_one_get_returns_array_length_one
25
32
  @buf.put_array([1, 2, 3, 4])
26
33
  @buf.rewind
@@ -186,5 +193,16 @@ class ByteBufferTest < Test::Unit::TestCase
186
193
  @buf.get_int
187
194
  assert !@buf.more?
188
195
  end
196
+
197
+ def test_equality
198
+ @buf = ByteBuffer.new("foo")
199
+ assert_equal @buf, @buf
200
+ assert_equal ByteBuffer.new(""), ByteBuffer.new("")
201
+ assert_equal ByteBuffer.new("123"), ByteBuffer.new("123")
202
+ assert_not_equal ByteBuffer.new("123"), ByteBuffer.new("1234")
203
+ assert_equal @buf, "foo"
204
+ assert_not_equal @buf, 123
205
+ assert_not_equal @buf, nil
206
+ end
189
207
 
190
208
  end
@@ -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)
@@ -194,4 +194,11 @@ class OrderedHashTest < Test::Unit::TestCase
194
194
  copy[:foo] = 1
195
195
  assert copy.keys != @oh.keys
196
196
  end
197
+
198
+ def test_dup
199
+ oh2 = @oh.dup
200
+ oh2['f'] = 9
201
+ assert_nil @oh['f']
202
+ assert_equal ['c', 'a', 'z'], @oh.keys
203
+ end
197
204
  end
@@ -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,12 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: bson
3
3
  version: !ruby/object:Gem::Version
4
- hash: 23
5
4
  prerelease: false
6
5
  segments:
7
6
  - 1
8
- - 2
9
- - 4
10
- version: 1.2.4
7
+ - 3
8
+ - 0
9
+ version: 1.3.0
11
10
  platform: jruby
12
11
  authors:
13
12
  - Jim Menard
@@ -17,7 +16,7 @@ autorequire:
17
16
  bindir: bin
18
17
  cert_chain: []
19
18
 
20
- date: 2011-02-23 00:00:00 -05:00
19
+ date: 2011-04-01 00:00:00 -04:00
21
20
  default_executable:
22
21
  dependencies: []
23
22
 
@@ -33,30 +32,31 @@ extra_rdoc_files: []
33
32
  files:
34
33
  - LICENSE.txt
35
34
  - lib/bson.rb
36
- - lib/bson/bson_c.rb
37
- - lib/bson/bson_java.rb
38
35
  - lib/bson/bson_ruby.rb
39
- - lib/bson/byte_buffer.rb
36
+ - lib/bson/bson_c.rb
40
37
  - lib/bson/exceptions.rb
41
38
  - lib/bson/ordered_hash.rb
42
- - lib/bson/types/binary.rb
43
- - lib/bson/types/code.rb
44
39
  - lib/bson/types/dbref.rb
45
- - lib/bson/types/min_max_keys.rb
40
+ - lib/bson/types/code.rb
46
41
  - lib/bson/types/object_id.rb
42
+ - lib/bson/types/binary.rb
43
+ - lib/bson/types/timestamp.rb
44
+ - lib/bson/types/min_max_keys.rb
45
+ - lib/bson/byte_buffer.rb
46
+ - lib/bson/bson_java.rb
47
47
  - bin/b2json
48
48
  - bin/j2bson
49
49
  - ext/java/jar/bson-2.2.jar
50
50
  - ext/java/jar/jbson.jar
51
51
  - ext/java/jar/mongo-2.4.jar
52
- - test/bson/binary_test.rb
53
- - test/bson/bson_string_test.rb
54
- - test/bson/bson_test.rb
55
52
  - test/bson/byte_buffer_test.rb
56
- - test/bson/hash_with_indifferent_access_test.rb
57
- - test/bson/json_test.rb
53
+ - test/bson/binary_test.rb
58
54
  - test/bson/object_id_test.rb
55
+ - test/bson/json_test.rb
56
+ - test/bson/timestamp_test.rb
57
+ - test/bson/bson_test.rb
59
58
  - test/bson/ordered_hash_test.rb
59
+ - test/bson/hash_with_indifferent_access_test.rb
60
60
  has_rdoc: true
61
61
  homepage: http://www.mongodb.org
62
62
  licenses: []
@@ -71,7 +71,6 @@ required_ruby_version: !ruby/object:Gem::Requirement
71
71
  requirements:
72
72
  - - ">="
73
73
  - !ruby/object:Gem::Version
74
- hash: 3
75
74
  segments:
76
75
  - 0
77
76
  version: "0"
@@ -80,7 +79,6 @@ required_rubygems_version: !ruby/object:Gem::Requirement
80
79
  requirements:
81
80
  - - ">="
82
81
  - !ruby/object:Gem::Version
83
- hash: 3
84
82
  segments:
85
83
  - 0
86
84
  version: "0"
@@ -92,11 +90,11 @@ signing_key:
92
90
  specification_version: 3
93
91
  summary: Ruby implementation of BSON
94
92
  test_files:
95
- - test/bson/binary_test.rb
96
- - test/bson/bson_string_test.rb
97
- - test/bson/bson_test.rb
98
93
  - test/bson/byte_buffer_test.rb
99
- - test/bson/hash_with_indifferent_access_test.rb
100
- - test/bson/json_test.rb
94
+ - test/bson/binary_test.rb
101
95
  - test/bson/object_id_test.rb
96
+ - test/bson/json_test.rb
97
+ - test/bson/timestamp_test.rb
98
+ - test/bson/bson_test.rb
102
99
  - test/bson/ordered_hash_test.rb
100
+ - test/bson/hash_with_indifferent_access_test.rb
@@ -1,30 +0,0 @@
1
- # encoding:utf-8
2
- require './test/test_helper'
3
- require 'complex'
4
- require 'bigdecimal'
5
- require 'rational'
6
-
7
- class BSONTest < Test::Unit::TestCase
8
-
9
- include BSON
10
-
11
- def setup
12
- @encoder = BSON::BSON_CODER
13
- end
14
-
15
- def assert_doc_pass(doc, options={})
16
- bson = @encoder.serialize(doc)
17
- if options[:debug]
18
- puts "DEBUGGING DOC:"
19
- p bson.to_a
20
- puts "DESERIALIZES TO:"
21
- end
22
- assert_equal @encoder.serialize(doc).to_a, bson.to_a
23
- assert_equal doc, @encoder.deserialize(bson)
24
- end
25
-
26
- def test_string
27
- assert_doc_pass({:a => "hello"})
28
- end
29
-
30
- end