More tests

This commit is contained in:
Jim Menard 2008-12-08 17:20:31 -05:00
parent 125e84a6e9
commit 241361285c
2 changed files with 78 additions and 38 deletions

2
README
View File

@ -58,6 +58,8 @@ type
* Capped collection support.
* Implement order_by (right now, sort order is ignored)
* More code comments. More text in this file.
* Support more types: REF, SYMBOL, CODE_W_SCOPE, etc.

View File

@ -11,7 +11,7 @@ class DBAPITest < Test::Unit::TestCase
@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
@r1 = @coll.insert('a' => 1) # collection not created until it's used
@coll_full_name = 'ruby-mongo-test.test'
end
@ -42,6 +42,81 @@ class DBAPITest < Test::Unit::TestCase
assert docs.detect { |row| row['b'] == 4 }
end
def test_find_simple
@r2 = @coll.insert('a' => 2)
@r3 = @coll.insert('b' => 3)
# Check sizes
docs = @coll.find().map
assert_equal 3, docs.size
assert_equal 3, @coll.count
# Find by other value
docs = @coll.find('a' => @r1['a']).map
assert_equal 1, docs.size
doc = docs.first
assert_equal doc['_id'], @r1['_id']
assert_equal doc['a'], @r1['a']
end
def test_find_advanced
@coll.insert('a' => 2)
@coll.insert('b' => 3)
# 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 }
# Find by advanced query (regexp)
docs = @coll.find('a' => /[1|2]/).map
assert_equal 2, docs.size
assert docs.detect { |row| row['a'] == 1 }
assert docs.detect { |row| row['a'] == 2 }
end
def test_find_sorting
@coll.insert('a' => 2)
@coll.insert('b' => 3)
# Sorting (ascending)
docs = @coll.find({'a' => { '$lt' => 10 }}, :sort => { 'a' => 1 }).map
assert_equal 2, docs.size
assert_equal 1, docs.first['a']
# Sorting (descending)
docs = @coll.find({'a' => { '$lt' => 10 }}, :sort => { 'a' => -1 }).map
assert_equal 2, docs.size
assert_equal 2, docs.first['a']
end
def test_close
@db.close
assert @db.socket.closed?
@ -111,41 +186,4 @@ 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