From 68bb1d2060c134c6ece91c50834663125ef464bc Mon Sep 17 00:00:00 2001 From: Kyle Banker Date: Mon, 2 Nov 2009 15:50:16 -0500 Subject: [PATCH] minor: Initial collection unit tests. Logging message update. --- lib/mongo/cursor.rb | 2 +- test/test_connection.rb | 2 +- test/unit/collection_test.rb | 36 ++++++++++++++++++++++++++++++++++++ 3 files changed, 38 insertions(+), 2 deletions(-) create mode 100644 test/unit/collection_test.rb diff --git a/lib/mongo/cursor.rb b/lib/mongo/cursor.rb index 359945a..508ccbc 100644 --- a/lib/mongo/cursor.rb +++ b/lib/mongo/cursor.rb @@ -395,7 +395,7 @@ module Mongo end def query_log_message - "db.#{@admin ? 'admin' : @db.name}.#{@collection.name}.find(#{@selector.inspect}, #{@fields ? @fields.inspect : '{}'})" + + "#{@admin ? 'admin' : @db.name}.#{@collection.name}.find(#{@selector.inspect}, #{@fields ? @fields.inspect : '{}'})" + "#{@skip != 0 ? ('.skip(' + @skip.to_s + ')') : ''}#{@limit != 0 ? ('.limit(' + @limit.to_s + ')') : ''}" end diff --git a/test/test_connection.rb b/test/test_connection.rb index a047ee0..03dfb7c 100644 --- a/test/test_connection.rb +++ b/test/test_connection.rb @@ -72,7 +72,7 @@ class TestConnection < Test::Unit::TestCase logger.level = Logger::DEBUG db = Connection.new(@host, @port, :logger => logger).db('ruby-mongo-test') - assert output.string.include?("2004") + assert output.string.include?("$cmd.find") end def test_connection_logger diff --git a/test/unit/collection_test.rb b/test/unit/collection_test.rb new file mode 100644 index 0000000..cb1b792 --- /dev/null +++ b/test/unit/collection_test.rb @@ -0,0 +1,36 @@ +require 'test/test_helper' + +class CollectionTest < Test::Unit::TestCase + + class MockDB < DB + def connect_to_master + true + end + end + + context "Basic operations: " do + setup do + @logger = mock() + end + + should "send update message" do + @db = MockDB.new("testing", ['localhost', 27017], :logger => @logger) + @coll = @db.collection('books') + @db.expects(:send_message_with_operation).with do |op, msg, log| + op == 2001 && log.include?("db.books.update") + end + @coll.update({}, {:title => 'Moby Dick'}) + end + + should "send insert message" do + @db = MockDB.new("testing", ['localhost', 27017], :logger => @logger) + @coll = @db.collection('books') + @db.expects(:send_message_with_operation).with do |op, msg, log| + op == 2002 && log.include?("db.books.insert") + end + @coll.insert({:title => 'Moby Dick'}) + end + end +end + +