More tests and improvements thanks to aemadrid.

This commit is contained in:
Jim Menard 2008-12-08 16:41:52 -05:00
parent df2b10c17e
commit cfee63194c
5 changed files with 60 additions and 3 deletions

7
README
View File

@ -52,6 +52,13 @@ type
= To Do = To Do
* Implement Babble's algorithm for ObjectID. Implement Comparable.
* Change find(selector={}, fields=nil, options={}) to find(selector={},
options={})
* ObjectID equality.
* Capped collection support. * Capped collection support.
* More code comments. More text in this file. * More code comments. More text in this file.

View File

@ -33,7 +33,8 @@ module XGen
def insert(*objects) def insert(*objects)
objects = objects.first if objects.size == 1 && objects.first.is_a?(Array) objects = objects.first if objects.size == 1 && objects.first.is_a?(Array)
@db.insert_into_db(@name, objects) res = @db.insert_into_db(@name, objects)
res.size > 1 ? res : res.first
end end
alias_method :<<, :insert alias_method :<<, :insert

View File

@ -20,6 +20,8 @@ module XGen
class ObjectID class ObjectID
include Comparable
@@uuid_generator = UUID.new @@uuid_generator = UUID.new
# String UUID # String UUID
@ -35,6 +37,12 @@ module XGen
def to_s def to_s
@uuid @uuid
end end
# This will make more sense when we implement the Babble algorithm for
# generating object ids.
def <=>(other)
to_s <=> other.to_s
end
end end
end end
end end

View File

@ -6,7 +6,9 @@ require 'test/unit'
class DBAPITest < Test::Unit::TestCase class DBAPITest < Test::Unit::TestCase
def setup def setup
@db = XGen::Mongo::Driver::Mongo.new.db('ruby-mongo-test') host = ENV['MONGO_RUBY_DRIVER_HOST'] || 'localhost'
port = ENV['MONGO_RUBY_DRIVER_PORT'] || XGen::Mongo::Driver::Mongo::DEFAULT_PORT
@db = XGen::Mongo::Driver::Mongo.new(host, port).db('ruby-mongo-test')
@coll = @db.collection('test') @coll = @db.collection('test')
@coll.clear @coll.clear
@coll.insert('a' => 1) # collection not created until it's used @coll.insert('a' => 1) # collection not created until it's used
@ -109,4 +111,41 @@ class DBAPITest < Test::Unit::TestCase
assert_equal 1, rows.length assert_equal 1, rows.length
assert_equal regex, rows[0]['b'] assert_equal regex, rows[0]['b']
end end
def test_find
@coll.insert('a' => 2)
# Find by advanced query (less than)
docs = @coll.find('a' => { '$lt' => 10 }).map
assert_equal 2, docs.size
assert docs.detect { |row| row['a'] == 1 }
assert docs.detect { |row| row['a'] == 2 }
# Find by advanced query (greater than)
docs = @coll.find('a' => { '$gt' => 1 }).map
assert_equal 1, docs.size
assert docs.detect { |row| row['a'] == 2 }
# Find by advanced query (less than or equal to)
docs = @coll.find('a' => { '$lte' => 1 }).map
assert_equal 1, docs.size
assert docs.detect { |row| row['a'] == 1 }
# Find by advanced query (greater than or equal to)
docs = @coll.find('a' => { '$gte' => 1 }).map
assert_equal 2, docs.size
assert docs.detect { |row| row['a'] == 1 }
assert docs.detect { |row| row['a'] == 2 }
# Find by advanced query (between)
docs = @coll.find('a' => { '$gt' => 1, '$lt' => 3 }).map
assert_equal 1, docs.size
assert docs.detect { |row| row['a'] == 2 }
# Find by advanced query (in clause)
docs = @coll.find('a' => {'$in' => [1,2]}).map
assert_equal 2, docs.size
assert docs.detect { |row| row['a'] == 1 }
assert docs.detect { |row| row['a'] == 2 }
end
end end

View File

@ -6,7 +6,9 @@ require 'test/unit'
class DBConnectionTest < Test::Unit::TestCase class DBConnectionTest < Test::Unit::TestCase
def test_no_exceptions def test_no_exceptions
db = XGen::Mongo::Driver::Mongo.new.db('ruby-mongo-demo') host = ENV['MONGO_RUBY_DRIVER_HOST'] || 'localhost'
port = ENV['MONGO_RUBY_DRIVER_PORT'] || XGen::Mongo::Driver::Mongo::DEFAULT_PORT
db = XGen::Mongo::Driver::Mongo.new(host, port).db('ruby-mongo-demo')
coll = db.collection('test') coll = db.collection('test')
coll.clear coll.clear
end end