2009-12-31 18:57:01 +00:00
|
|
|
require 'test/test_helper'
|
2010-05-18 16:22:19 +00:00
|
|
|
require 'rubygems'
|
|
|
|
require 'json'
|
2008-12-09 17:35:03 +00:00
|
|
|
|
|
|
|
class ObjectIDTest < Test::Unit::TestCase
|
|
|
|
|
2009-08-20 14:50:48 +00:00
|
|
|
include Mongo
|
2010-04-05 14:39:55 +00:00
|
|
|
include BSON
|
2008-12-09 17:35:03 +00:00
|
|
|
|
|
|
|
def setup
|
2009-11-11 15:47:42 +00:00
|
|
|
@o = ObjectID.new
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_hashcode
|
|
|
|
assert_equal @o.instance_variable_get(:@data).hash, @o.hash
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_array_uniq_for_equilavent_ids
|
|
|
|
a = ObjectID.new('123')
|
|
|
|
b = ObjectID.new('123')
|
|
|
|
assert_equal a, b
|
|
|
|
assert_equal 1, [a, b].uniq.size
|
2008-12-09 17:35:03 +00:00
|
|
|
end
|
|
|
|
|
2009-10-19 21:14:41 +00:00
|
|
|
def test_create_pk_method
|
|
|
|
doc = {:name => 'Mongo'}
|
|
|
|
doc = ObjectID.create_pk(doc)
|
|
|
|
assert doc[:_id]
|
2010-01-22 19:24:17 +00:00
|
|
|
|
2009-10-19 21:14:41 +00:00
|
|
|
doc = {:name => 'Mongo', :_id => '12345'}
|
|
|
|
doc = ObjectID.create_pk(doc)
|
|
|
|
assert_equal '12345', doc[:_id]
|
|
|
|
end
|
|
|
|
|
2008-12-09 17:35:03 +00:00
|
|
|
def test_different
|
2009-08-25 14:30:14 +00:00
|
|
|
a = ObjectID.new
|
|
|
|
b = ObjectID.new
|
|
|
|
assert_not_equal a.to_a, b.to_a
|
|
|
|
assert_not_equal a, b
|
2008-12-09 17:35:03 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_eql?
|
|
|
|
o2 = ObjectID.new(@o.to_a)
|
2009-08-25 14:30:14 +00:00
|
|
|
assert_equal @o, o2
|
2008-12-09 17:35:03 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_to_s
|
|
|
|
s = @o.to_s
|
|
|
|
assert_equal 24, s.length
|
|
|
|
s =~ /^([0-9a-f]+)$/
|
|
|
|
assert_equal 24, $1.length
|
|
|
|
end
|
|
|
|
|
2010-05-18 16:22:19 +00:00
|
|
|
def test_method
|
|
|
|
assert_equal ObjectID.from_string(@o.to_s), BSON::ObjectID(@o.to_s)
|
|
|
|
end
|
|
|
|
|
2010-02-23 21:22:48 +00:00
|
|
|
def test_inspect
|
2010-05-18 16:22:19 +00:00
|
|
|
assert_equal "BSON::ObjectID('#{@o.to_s}')", @o.inspect
|
2010-02-23 21:22:48 +00:00
|
|
|
end
|
|
|
|
|
2008-12-09 18:48:38 +00:00
|
|
|
def test_save_and_restore
|
|
|
|
host = ENV['MONGO_RUBY_DRIVER_HOST'] || 'localhost'
|
2009-08-20 22:48:09 +00:00
|
|
|
port = ENV['MONGO_RUBY_DRIVER_PORT'] || Connection::DEFAULT_PORT
|
2010-04-05 19:48:35 +00:00
|
|
|
db = Connection.new(host, port).db(MONGO_TEST_DB)
|
2008-12-09 18:48:38 +00:00
|
|
|
coll = db.collection('test')
|
|
|
|
|
2009-10-20 15:31:07 +00:00
|
|
|
coll.remove
|
2008-12-09 18:48:38 +00:00
|
|
|
coll << {'a' => 1, '_id' => @o}
|
|
|
|
|
|
|
|
row = coll.find().collect.first
|
|
|
|
assert_equal 1, row['a']
|
|
|
|
assert_equal @o, row['_id']
|
|
|
|
end
|
|
|
|
|
2009-01-09 18:34:30 +00:00
|
|
|
def test_from_string
|
|
|
|
hex_str = @o.to_s
|
|
|
|
o2 = ObjectID.from_string(hex_str)
|
|
|
|
assert_equal hex_str, o2.to_s
|
|
|
|
assert_equal @o, o2
|
|
|
|
assert_equal @o.to_s, o2.to_s
|
|
|
|
end
|
|
|
|
|
2009-12-08 22:13:54 +00:00
|
|
|
def test_illegal_from_string
|
|
|
|
assert_raise InvalidObjectID do
|
|
|
|
ObjectID.from_string("")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2009-02-09 16:38:24 +00:00
|
|
|
def test_legal
|
|
|
|
assert !ObjectID.legal?(nil)
|
|
|
|
assert !ObjectID.legal?("fred")
|
|
|
|
assert !ObjectID.legal?("0000")
|
|
|
|
assert !ObjectID.legal?('000102030405060708090A0')
|
|
|
|
assert ObjectID.legal?('000102030405060708090A0B')
|
|
|
|
assert ObjectID.legal?('abcdefABCDEF123456789012')
|
|
|
|
assert !ObjectID.legal?('abcdefABCDEF12345678901x')
|
2009-01-15 16:25:23 +00:00
|
|
|
end
|
|
|
|
|
2009-01-12 14:59:46 +00:00
|
|
|
def test_from_string_leading_zeroes
|
2009-08-25 18:56:02 +00:00
|
|
|
hex_str = '000000000000000000000000'
|
2009-01-12 14:59:46 +00:00
|
|
|
o = ObjectID.from_string(hex_str)
|
|
|
|
assert_equal hex_str, o.to_s
|
|
|
|
end
|
|
|
|
|
2009-01-12 15:14:48 +00:00
|
|
|
def test_byte_order
|
|
|
|
hex_str = '000102030405060708090A0B'
|
|
|
|
o = ObjectID.from_string(hex_str)
|
2009-08-25 18:56:02 +00:00
|
|
|
assert_equal [0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b], o.to_a
|
|
|
|
end
|
|
|
|
|
2009-12-03 22:45:53 +00:00
|
|
|
def test_generation_time
|
|
|
|
time = Time.now
|
|
|
|
id = ObjectID.new
|
2010-01-22 19:24:17 +00:00
|
|
|
generated_time = id.generation_time
|
2009-12-03 22:45:53 +00:00
|
|
|
|
2010-01-22 19:24:17 +00:00
|
|
|
assert_in_delta time.to_i, generated_time.to_i, 2
|
|
|
|
assert_equal "UTC", generated_time.zone
|
2009-12-03 22:45:53 +00:00
|
|
|
end
|
2010-01-22 19:24:17 +00:00
|
|
|
|
|
|
|
def test_from_time
|
|
|
|
time = Time.now.utc
|
|
|
|
id = ObjectID.from_time(time)
|
|
|
|
|
|
|
|
assert_equal time.to_i, id.generation_time.to_i
|
|
|
|
end
|
|
|
|
|
2009-12-05 15:54:36 +00:00
|
|
|
def test_json
|
|
|
|
id = ObjectID.new
|
2010-01-22 19:56:00 +00:00
|
|
|
assert_equal "{\"$oid\": \"#{id}\"}", id.to_json
|
2009-12-05 15:54:36 +00:00
|
|
|
end
|
2009-01-06 15:47:29 +00:00
|
|
|
end
|