More tests and improvements thanks to aemadrid.
This commit is contained in:
parent
df2b10c17e
commit
cfee63194c
7
README
7
README
|
@ -52,6 +52,13 @@ type
|
|||
|
||||
= 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.
|
||||
|
||||
* More code comments. More text in this file.
|
||||
|
|
|
@ -33,7 +33,8 @@ module XGen
|
|||
|
||||
def insert(*objects)
|
||||
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
|
||||
alias_method :<<, :insert
|
||||
|
||||
|
|
|
@ -20,6 +20,8 @@ module XGen
|
|||
|
||||
class ObjectID
|
||||
|
||||
include Comparable
|
||||
|
||||
@@uuid_generator = UUID.new
|
||||
|
||||
# String UUID
|
||||
|
@ -35,6 +37,12 @@ module XGen
|
|||
def to_s
|
||||
@uuid
|
||||
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
|
||||
|
|
|
@ -6,7 +6,9 @@ require 'test/unit'
|
|||
class DBAPITest < Test::Unit::TestCase
|
||||
|
||||
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.clear
|
||||
@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 regex, rows[0]['b']
|
||||
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
|
||||
|
|
|
@ -6,7 +6,9 @@ require 'test/unit'
|
|||
class DBConnectionTest < Test::Unit::TestCase
|
||||
|
||||
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.clear
|
||||
end
|
||||
|
|
Loading…
Reference in New Issue