bson 1.0.4 → 1.0.5

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 CHANGED
@@ -58,23 +58,48 @@ namespace :test do
58
58
  t.verbose = true
59
59
  end
60
60
 
61
- Rake::TestTask.new(:pair_count) do |t|
62
- t.test_files = FileList['test/replica/count_test.rb']
61
+ Rake::TestTask.new(:replica_pair_count) do |t|
62
+ t.test_files = FileList['test/replica_pairs/count_test.rb']
63
63
  t.verbose = true
64
64
  end
65
65
 
66
- Rake::TestTask.new(:pair_insert) do |t|
67
- t.test_files = FileList['test/replica/insert_test.rb']
66
+ Rake::TestTask.new(:replica_pair_insert) do |t|
67
+ t.test_files = FileList['test/replica_pairs/insert_test.rb']
68
68
  t.verbose = true
69
69
  end
70
70
 
71
- Rake::TestTask.new(:pooled_pair_insert) do |t|
72
- t.test_files = FileList['test/replica/pooled_insert_test.rb']
71
+ Rake::TestTask.new(:pooled_replica_pair_insert) do |t|
72
+ t.test_files = FileList['test/replica_pairs/pooled_insert_test.rb']
73
73
  t.verbose = true
74
74
  end
75
75
 
76
- Rake::TestTask.new(:pair_query) do |t|
77
- t.test_files = FileList['test/replica/query_test.rb']
76
+ Rake::TestTask.new(:replica_pair_query) do |t|
77
+ t.test_files = FileList['test/replica_pairs/query_test.rb']
78
+ t.verbose = true
79
+ end
80
+
81
+ Rake::TestTask.new(:replica_set_count) do |t|
82
+ t.test_files = FileList['test/replica_sets/count_test.rb']
83
+ t.verbose = true
84
+ end
85
+
86
+ Rake::TestTask.new(:replica_set_insert) do |t|
87
+ t.test_files = FileList['test/replica_sets/insert_test.rb']
88
+ t.verbose = true
89
+ end
90
+
91
+ Rake::TestTask.new(:pooled_replica_set_insert) do |t|
92
+ t.test_files = FileList['test/replica_sets/pooled_insert_test.rb']
93
+ t.verbose = true
94
+ end
95
+
96
+ Rake::TestTask.new(:replica_set_query) do |t|
97
+ t.test_files = FileList['test/replica_sets/query_test.rb']
98
+ t.verbose = true
99
+ end
100
+
101
+ Rake::TestTask.new(:replica_set_ack) do |t|
102
+ t.test_files = FileList['test/replica_sets/replication_ack_test.rb']
78
103
  t.verbose = true
79
104
  end
80
105
 
@@ -1,4 +1,4 @@
1
- require "lib/bson"
1
+ require "./lib/bson"
2
2
 
3
3
  Gem::Specification.new do |s|
4
4
  s.name = 'bson'
@@ -2,10 +2,10 @@
2
2
 
3
3
  $:.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
4
4
 
5
- MINIMUM_BSON_EXT_VERSION = "1.0.4"
5
+ MINIMUM_BSON_EXT_VERSION = "1.0.5"
6
6
 
7
7
  module BSON
8
- VERSION = "1.0.4"
8
+ VERSION = "1.0.5"
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
@@ -60,6 +60,7 @@ require 'bson/types/binary'
60
60
  require 'bson/types/code'
61
61
  require 'bson/types/dbref'
62
62
  require 'bson/types/objectid'
63
+ require 'bson/types/object_id'
63
64
  require 'bson/types/min_max_keys'
64
65
 
65
66
  require 'base64'
@@ -89,6 +89,7 @@ module BSON
89
89
  end
90
90
 
91
91
  def serialize(obj, check_keys=false, move_id=false)
92
+ raise(InvalidDocument, "BSON.serialize takes a Hash but got a #{obj.class}") unless obj.is_a?(Hash)
92
93
  raise "Document is null" unless obj
93
94
 
94
95
  @buf.rewind
@@ -360,7 +361,7 @@ module BSON
360
361
  end
361
362
 
362
363
  def deserialize_oid_data(buf)
363
- ObjectID.new(buf.get(12))
364
+ ObjectId.new(buf.get(12))
364
365
  end
365
366
 
366
367
  def deserialize_dbref_data(buf)
@@ -562,6 +563,8 @@ module BSON
562
563
  REGEX
563
564
  when ObjectID
564
565
  OID
566
+ when ObjectId
567
+ OID
565
568
  when DBRef
566
569
  REF
567
570
  when true, false
@@ -32,6 +32,9 @@ module BSON
32
32
  # Raised when attempting to initialize an invalid ObjectID.
33
33
  class InvalidObjectID < BSONError; end
34
34
 
35
+ # Raised when attempting to initialize an invalid ObjectID.
36
+ class InvalidObjectId < BSONError; end
37
+
35
38
  # Raised when trying to insert a document that exceeds the 4MB limit or
36
39
  # when the document contains objects that can't be serialized as BSON.
37
40
  class InvalidDocument < BSONError; end
@@ -129,6 +129,12 @@ module BSON
129
129
  }
130
130
  end
131
131
 
132
+ def reject(&block)
133
+ clone = self.clone
134
+ return clone unless block_given?
135
+ clone.delete_if &block
136
+ end
137
+
132
138
  def clear
133
139
  super
134
140
  @ordered_set.clear
@@ -26,9 +26,10 @@ module BSON
26
26
  # Use this class when storing binary data in documents.
27
27
  class Binary < ByteBuffer
28
28
 
29
- SUBTYPE_BYTES = 0x02
30
- SUBTYPE_UUID = 0x03
31
- SUBTYPE_MD5 = 0x05
29
+ SUBTYPE_SIMPLE = 0x00
30
+ SUBTYPE_BYTES = 0x02
31
+ SUBTYPE_UUID = 0x03
32
+ SUBTYPE_MD5 = 0x05
32
33
  SUBTYPE_USER_DEFINED = 0x80
33
34
 
34
35
  # One of the SUBTYPE_* constants. Default is SUBTYPE_BYTES.
@@ -0,0 +1,184 @@
1
+ # encoding: UTF-8
2
+
3
+ # --
4
+ # Copyright (C) 2008-2010 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
+ require 'thread'
20
+ require 'socket'
21
+ require 'digest/md5'
22
+
23
+ module BSON
24
+
25
+ def BSON::ObjectId(s)
26
+ ObjectId.from_string(s)
27
+ end
28
+
29
+ # Generates MongoDB object ids.
30
+ #
31
+ # @core objectids
32
+ class ObjectId
33
+ @@lock = Mutex.new
34
+ @@index = 0
35
+
36
+ # Create a new object id. If no parameter is given, an id corresponding
37
+ # to the ObjectId BSON data type will be created. This is a 12-byte value
38
+ # consisting of a 4-byte timestamp, a 3-byte machine id, a 2-byte process id,
39
+ # and a 3-byte counter.
40
+ #
41
+ # @param [Array] data should be an array of bytes. If you want
42
+ # to generate a standard MongoDB object id, leave this argument blank.
43
+ def initialize(data=nil)
44
+ @data = data || generate
45
+ end
46
+
47
+ # Determine if the supplied string is legal. Legal strings will
48
+ # consist of 24 hexadecimal characters.
49
+ #
50
+ # @param [String] str
51
+ #
52
+ # @return [Boolean]
53
+ def self.legal?(str)
54
+ len = 24
55
+ str =~ /([0-9a-f]+)/i
56
+ match = $1
57
+ str && str.length == len && match == str
58
+ end
59
+
60
+ # Create an object id from the given time. This is useful for doing range
61
+ # queries; it works because MongoDB's object ids begin
62
+ # with a timestamp.
63
+ #
64
+ # @param [Time] time a utc time to encode as an object id.
65
+ #
66
+ # @return [Mongo::ObjectId]
67
+ #
68
+ # @example Return all document created before Jan 1, 2010.
69
+ # time = Time.utc(2010, 1, 1)
70
+ # time_id = ObjectId.from_time(time)
71
+ # collection.find({'_id' => {'$lt' => time_id}})
72
+ def self.from_time(time)
73
+ self.new([time.to_i,0,0].pack("NNN").unpack("C12"))
74
+ end
75
+
76
+ # Adds a primary key to the given document if needed.
77
+ #
78
+ # @param [Hash] doc a document requiring an _id.
79
+ #
80
+ # @return [Mongo::ObjectId, Object] returns a newly-created or
81
+ # current _id for the given document.
82
+ def self.create_pk(doc)
83
+ doc.has_key?(:_id) || doc.has_key?('_id') ? doc : doc.merge!(:_id => self.new)
84
+ end
85
+
86
+ # Check equality of this object id with another.
87
+ #
88
+ # @param [Mongo::ObjectId] object_id
89
+ def eql?(object_id)
90
+ @data == object_id.instance_variable_get("@data")
91
+ end
92
+ alias_method :==, :eql?
93
+
94
+ # Get a unique hashcode for this object.
95
+ # This is required since we've defined an #eql? method.
96
+ #
97
+ # @return [Integer]
98
+ def hash
99
+ @data.hash
100
+ end
101
+
102
+ # Get an array representation of the object id.
103
+ #
104
+ # @return [Array]
105
+ def to_a
106
+ @data.dup
107
+ end
108
+
109
+ # Given a string representation of an ObjectId, return a new ObjectId
110
+ # with that value.
111
+ #
112
+ # @param [String] str
113
+ #
114
+ # @return [Mongo::ObjectId]
115
+ def self.from_string(str)
116
+ raise InvalidObjectId, "illegal ObjectId format" unless legal?(str)
117
+ data = []
118
+ 12.times do |i|
119
+ data[i] = str[i * 2, 2].to_i(16)
120
+ end
121
+ self.new(data)
122
+ end
123
+
124
+ # Get a string representation of this object id.
125
+ #
126
+ # @return [String]
127
+ def to_s
128
+ str = ' ' * 24
129
+ 12.times do |i|
130
+ str[i * 2, 2] = '%02x' % @data[i]
131
+ end
132
+ str
133
+ end
134
+
135
+ def inspect
136
+ "BSON::ObjectId('#{to_s}')"
137
+ end
138
+
139
+ # Convert to MongoDB extended JSON format. Since JSON includes type information,
140
+ # but lacks an ObjectId type, this JSON format encodes the type using an $oid key.
141
+ #
142
+ # @return [String] the object id represented as MongoDB extended JSON.
143
+ def to_json(*a)
144
+ "{\"$oid\": \"#{to_s}\"}"
145
+ end
146
+
147
+ # Return the UTC time at which this ObjectId was generated. This may
148
+ # be used in lieu of a created_at timestamp since this information
149
+ # is always encoded in the object id.
150
+ #
151
+ # @return [Time] the time at which this object was created.
152
+ def generation_time
153
+ Time.at(@data.pack("C4").unpack("N")[0]).utc
154
+ end
155
+
156
+ private
157
+
158
+ # This gets overwritten by the C extension if it loads.
159
+ def generate
160
+ oid = ''
161
+
162
+ # 4 bytes current time
163
+ time = Time.new.to_i
164
+ oid += [time].pack("N")
165
+
166
+ # 3 bytes machine
167
+ oid += Digest::MD5.digest(Socket.gethostname)[0, 3]
168
+
169
+ # 2 bytes pid
170
+ oid += [Process.pid % 0xFFFF].pack("n")
171
+
172
+ # 3 bytes inc
173
+ oid += [get_inc].pack("N")[1, 3]
174
+
175
+ oid.unpack("C12")
176
+ end
177
+
178
+ def get_inc
179
+ @@lock.synchronize do
180
+ @@index = (@@index + 1) % 0xFFFFFF
181
+ end
182
+ end
183
+ end
184
+ end
@@ -33,6 +33,8 @@ module BSON
33
33
  @@lock = Mutex.new
34
34
  @@index = 0
35
35
 
36
+ # @deprecated
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,
@@ -41,6 +43,7 @@ module BSON
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
45
  def initialize(data=nil)
46
+ warn "BSON::ObjectID is deprecated. Please use BSON::ObjectId instead."
44
47
  @data = data || generate
45
48
  end
46
49
 
@@ -155,26 +158,24 @@ module BSON
155
158
 
156
159
  private
157
160
 
158
- # We need to define this method only if CBson isn't loaded.
159
- unless defined? CBson
160
- def generate
161
- oid = ''
161
+ # This gets overwritten by the C extension if it loads.
162
+ def generate
163
+ oid = ''
162
164
 
163
- # 4 bytes current time
164
- time = Time.new.to_i
165
- oid += [time].pack("N")
165
+ # 4 bytes current time
166
+ time = Time.new.to_i
167
+ oid += [time].pack("N")
166
168
 
167
- # 3 bytes machine
168
- oid += Digest::MD5.digest(Socket.gethostname)[0, 3]
169
+ # 3 bytes machine
170
+ oid += Digest::MD5.digest(Socket.gethostname)[0, 3]
169
171
 
170
- # 2 bytes pid
171
- oid += [Process.pid % 0xFFFF].pack("n")
172
+ # 2 bytes pid
173
+ oid += [Process.pid % 0xFFFF].pack("n")
172
174
 
173
- # 3 bytes inc
174
- oid += [get_inc].pack("N")[1, 3]
175
+ # 3 bytes inc
176
+ oid += [get_inc].pack("N")[1, 3]
175
177
 
176
- oid.unpack("C12")
177
- end
178
+ oid.unpack("C12")
178
179
  end
179
180
 
180
181
  def get_inc
@@ -22,6 +22,20 @@ class BSONTest < Test::Unit::TestCase
22
22
 
23
23
  include BSON
24
24
 
25
+ def test_require_hash
26
+ assert_raise_error InvalidDocument, "takes a Hash" do
27
+ BSON.serialize('foo')
28
+ end
29
+
30
+ assert_raise_error InvalidDocument, "takes a Hash" do
31
+ BSON.serialize(Object.new)
32
+ end
33
+
34
+ assert_raise_error InvalidDocument, "takes a Hash" do
35
+ BSON.serialize(Set.new)
36
+ end
37
+ end
38
+
25
39
  def test_read_bson_io_document
26
40
  doc = {'doc' => 'hello, world'}
27
41
  bson = BSON.serialize(doc)
@@ -309,6 +323,18 @@ class BSONTest < Test::Unit::TestCase
309
323
  assert_equal Binary::SUBTYPE_USER_DEFINED, bin2.subtype
310
324
  end
311
325
 
326
+ def test_binary_binary_subtype_0
327
+ bin = Binary.new([1, 2, 3, 4, 5], Binary::SUBTYPE_SIMPLE)
328
+
329
+ doc = {'bin' => bin}
330
+ bson = BSON::BSON_CODER.serialize(doc)
331
+ doc2 = BSON::BSON_CODER.deserialize(bson)
332
+ bin2 = doc2['bin']
333
+ assert_kind_of Binary, bin2
334
+ assert_equal [1, 2, 3, 4, 5], bin2.to_a
335
+ assert_equal Binary::SUBTYPE_SIMPLE, bin2.subtype
336
+ end
337
+
312
338
  def test_binary_byte_buffer
313
339
  bb = Binary.new
314
340
  5.times { |i| bb.put(i + 1) }
@@ -98,22 +98,6 @@ class BSONTest < Test::Unit::TestCase
98
98
  assert_doc_pass({'foo' => :bar})
99
99
  end
100
100
 
101
- # def test_binary
102
- # doc = {'foo' => BSON::Binary.new("A".unpack("C*"))}
103
- # #doc = assert_doc_pass({'foo' => BSON::Binary.new("A".unpack("C*"))})
104
- # puts "First"
105
- # p doc.to_a
106
- # bson = @coder.serialize(doc)
107
- # de = @coder.deserialize(bson)
108
- # puts "Second"
109
- # p de.to_a
110
- # puts "subtype"
111
- # p de['foo'].subtype
112
- # puts "data"
113
- # p de['foo'].data
114
- # assert_equal doc, de
115
- # end
116
-
117
101
  def test_valid_utf8_string
118
102
  doc = {'doc' => 'aé'}
119
103
  bson = bson = BSON::BSON_CODER.serialize(doc)
@@ -157,6 +141,7 @@ class BSONTest < Test::Unit::TestCase
157
141
  doc["c"] = 3
158
142
  doc["d"] = 4
159
143
  bson = BSON::BSON_CODER.serialize(doc)
144
+ p bson
160
145
  assert_equal doc, BSON::BSON_CODER.deserialize(bson)
161
146
  end
162
147
 
@@ -37,9 +37,79 @@ class BSONTest < Test::Unit::TestCase
37
37
 
38
38
  def setup
39
39
  @encoder = BSON::BSON_CODER
40
+ con = Mongo::Connection.new
41
+ db = con['mongo-ruby-test']
42
+ @@test = db['col']
43
+ @@test.remove
40
44
  end
41
45
 
42
46
  def assert_doc_pass(doc)
47
+ bson = @encoder.serialize(doc)
48
+ puts "#{doc.inspect} serializes to #{bson.inspect}"
49
+ p bson.to_a
50
+
51
+ assert_equal doc, @encoder.deserialize(bson)
52
+ end
53
+
54
+ def test_code
55
+ @reduce_function = "1"#function (obj, prev) { prev.count += inc_value; }"
56
+ doc = {"a" => Code.new(@reduce_function, {"a" => 1})}
57
+ assert_doc_pass(doc)
58
+ end
59
+
60
+ def test_float
61
+ doc = {'a' => 1}
62
+ assert_doc_pass(doc)
63
+ end
64
+
65
+ def test_group_cmd
66
+ initial = {"c" => 0}
67
+ reduce = "function (obj, prev) { prev.c += inc_value; }"
68
+ group_command = {
69
+ "group" => {
70
+ "ns" => 'col',
71
+ "$reduce" => reduce,
72
+ "cond" => {},
73
+ "initial" => initial
74
+ }
75
+ }
76
+
77
+ assert_doc_pass(group_command)
78
+ end
79
+
80
+ context "Grouping" do
81
+ setup do
82
+ @@test.remove
83
+ @@test.save("a" => 1)
84
+ @@test.save("b" => 1)
85
+ @initial = {"count" => 0}
86
+ @reduce_function = "function (obj, prev) { prev.count += inc_value; }"
87
+ end
88
+
89
+ should "group results using eval form" do
90
+ p @@test.group([], {}, @initial, Code.new(@reduce_function, {"inc_value" => 1}))
91
+ p @@test.group([], {}, @initial, Code.new(@reduce_function, {"inc_value" => 2}))
92
+ assert_equal 2, @@test.group([], {}, @initial, Code.new(@reduce_function, {"inc_value" => 1}))[0]["count"]
93
+ assert_equal 4, @@test.group([], {}, @initial, Code.new(@reduce_function, {"inc_value" => 2}))[0]["count"]
94
+ end
95
+ end
96
+
97
+ # def test_find_and_modify
98
+ # @@test << { :a => 1, :processed => false }
99
+ # @@test << { :a => 2, :processed => false }
100
+ # @@test << { :a => 3, :processed => false }
101
+ # p @@test.find.to_a
102
+ #
103
+ # @@test.find_and_modify(:query => {}, :sort => [['a', -1]], :update => {"$set" => {"processed" => true}})
104
+ #
105
+ # p @@test.find.to_a
106
+ # assert @@test.find_one({:a => 3})['processed']
107
+ # end
108
+ #
109
+ def test_invalid_string
110
+ require 'iconv'
111
+ string = Iconv.conv('iso-8859-1', 'utf-8', 'aé')
112
+ doc = {'doc' => string}
43
113
  bson = @encoder.serialize(doc)
44
114
  assert_equal doc, @encoder.deserialize(bson)
45
115
  end
@@ -179,15 +249,40 @@ class BSONTest < Test::Unit::TestCase
179
249
  # bson = BSON::BSON_CODER.serialize(doc)
180
250
  # assert_equal doc, BSON::BSON_CODER.deserialize(bson)
181
251
  # end
252
+ #
253
+ # def test_obj
254
+ # doc = {'doc' => {'age' => 42, 'name' => 'Spongebob', 'shoe_size' => 9.5}}
255
+ # bson = BSON::BSON_CODER.serialize(doc)
256
+ # assert_equal doc, BSON::BSON_CODER.deserialize(bson)
257
+ #
258
+ # doc = {"a" => 10, "b" => 20}# {'doc' => {'age' => 42, 'name' => 'Spongebob', 'shoe_size' => 9.5}}
259
+ # bson = BSON::BSON_CODER.serialize(doc)
260
+ # assert_equal doc, BSON::BSON_CODER.deserialize(bson)
261
+ # end
262
+ #
263
+ # def test_oh
264
+ # oh = BSON::OrderedHash.new
265
+ # oh["z"] = 10
266
+ # oh["a"] = 1
267
+ # bson = BSON::BSON_CODER.serialize(oh)
268
+ # p bson
269
+ # assert_equal oh, BSON::BSON_CODER.deserialize(bson)
270
+ # end
182
271
  #
183
272
  def test_ordered_hash
184
- doc = BSON::OrderedHash.new
185
- doc["b"] = 1
186
- doc["a"] = 2
187
- doc["c"] = 3
188
- doc["d"] = 4
189
- bson = BSON::BSON_CODER.serialize(doc)
190
- assert_equal doc, BSON::BSON_CODER.deserialize(bson)
273
+ # doc = BSON::OrderedHash.new
274
+ # doc["b"] = 1
275
+ # doc["a"] = 2
276
+ # doc["c"] = 3
277
+ # doc["d"] = 4
278
+ # bson1 = BSON::BSON_CODER.serialize(doc)
279
+ # bson2 = BSON::BSON_RUBY.serialize({"b" => 1, "a" => 2, "c" => 3, "d" => 4})
280
+ # bson3 = BSON::BSON_RUBY.serialize({"b" => '1', "a" => '2', "c" => '3', "d" => '4'})
281
+ # p bson1
282
+ # p bson2
283
+ # p bson3
284
+ # p BSON::BSON_CODER.deserialize(bson3)
285
+ # assert_equal doc, BSON::BSON_RUBY.deserialize(bson3)
191
286
  end
192
287
  #
193
288
  # def test_object
@@ -0,0 +1,132 @@
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
@@ -179,4 +179,12 @@ class OrderedHashTest < Test::Unit::TestCase
179
179
  assert !@oh.keys.include?('z')
180
180
  end
181
181
 
182
+ def test_reject
183
+ new = @oh.reject { |k, v| k == 'foo' }
184
+ assert new.keys == @oh.keys
185
+
186
+ new = @oh.reject { |k, v| k == 'z' }
187
+ assert !new.keys.include?('z')
188
+ end
189
+
182
190
  end
metadata CHANGED
@@ -1,12 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: bson
3
3
  version: !ruby/object:Gem::Version
4
+ hash: 29
4
5
  prerelease: false
5
6
  segments:
6
7
  - 1
7
8
  - 0
8
- - 4
9
- version: 1.0.4
9
+ - 5
10
+ version: 1.0.5
10
11
  platform: ruby
11
12
  authors:
12
13
  - Jim Menard
@@ -16,7 +17,7 @@ autorequire:
16
17
  bindir: bin
17
18
  cert_chain: []
18
19
 
19
- date: 2010-07-13 00:00:00 -04:00
20
+ date: 2010-08-27 00:00:00 -04:00
20
21
  default_executable:
21
22
  dependencies: []
22
23
 
@@ -43,9 +44,20 @@ files:
43
44
  - lib/bson/types/code.rb
44
45
  - lib/bson/types/dbref.rb
45
46
  - lib/bson/types/min_max_keys.rb
47
+ - lib/bson/types/object_id.rb
46
48
  - lib/bson/types/objectid.rb
47
49
  - bin/b2json
48
50
  - bin/j2bson
51
+ - test/mongo_bson/basic_test.rb
52
+ - test/mongo_bson/binary_test.rb
53
+ - test/mongo_bson/bson_test.rb
54
+ - test/mongo_bson/byte_buffer_test.rb
55
+ - test/mongo_bson/jruby_bson_test.rb
56
+ - test/mongo_bson/jruby_encode_test.rb
57
+ - test/mongo_bson/json_test.rb
58
+ - test/mongo_bson/object_id_test.rb
59
+ - test/mongo_bson/objectid_test.rb
60
+ - test/mongo_bson/ordered_hash_test.rb
49
61
  has_rdoc: true
50
62
  homepage: http://www.mongodb.org
51
63
  licenses: []
@@ -56,23 +68,27 @@ rdoc_options: []
56
68
  require_paths:
57
69
  - lib
58
70
  required_ruby_version: !ruby/object:Gem::Requirement
71
+ none: false
59
72
  requirements:
60
73
  - - ">="
61
74
  - !ruby/object:Gem::Version
75
+ hash: 3
62
76
  segments:
63
77
  - 0
64
78
  version: "0"
65
79
  required_rubygems_version: !ruby/object:Gem::Requirement
80
+ none: false
66
81
  requirements:
67
82
  - - ">="
68
83
  - !ruby/object:Gem::Version
84
+ hash: 3
69
85
  segments:
70
86
  - 0
71
87
  version: "0"
72
88
  requirements: []
73
89
 
74
90
  rubyforge_project:
75
- rubygems_version: 1.3.6
91
+ rubygems_version: 1.3.7
76
92
  signing_key:
77
93
  specification_version: 3
78
94
  summary: Ruby implementation of BSON
@@ -84,5 +100,6 @@ test_files:
84
100
  - test/mongo_bson/jruby_bson_test.rb
85
101
  - test/mongo_bson/jruby_encode_test.rb
86
102
  - test/mongo_bson/json_test.rb
103
+ - test/mongo_bson/object_id_test.rb
87
104
  - test/mongo_bson/objectid_test.rb
88
105
  - test/mongo_bson/ordered_hash_test.rb