diff --git a/README b/README index 8c3b706..f275b3a 100644 --- a/README +++ b/README @@ -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. diff --git a/tests/test_db_api.rb b/tests/test_db_api.rb index 57bec50..a2c4b14 100644 --- a/tests/test_db_api.rb +++ b/tests/test_db_api.rb @@ -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 @@ -41,6 +41,81 @@ class DBAPITest < Test::Unit::TestCase assert_equal 4, docs.length 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 @@ -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