diff --git a/README.rdoc b/README.rdoc index 9b9c230..5277845 100644 --- a/README.rdoc +++ b/README.rdoc @@ -292,12 +292,10 @@ It's also possible to test replica pairs with connection pooling: $ rake test:pooled_pair_insert -===Shoulda and Mocha +===Mocha -All tests now require shoulda and mocha. You can install these gems as -follows: +Running the test suite requires mocha. You can install it as follows: - $ gem install shoulda $ gem install mocha The tests assume that the Mongo database is running on the default port. You @@ -305,23 +303,6 @@ can override the default host (localhost) and port (Connection::DEFAULT_PORT) by using the environment variables MONGO_RUBY_DRIVER_HOST and MONGO_RUBY_DRIVER_PORT. -The project mongo-qa (http://github.com/mongodb/mongo-qa) contains many more -Mongo driver tests that are language independent. To run thoses tests as part -of the "rake test" task, download the code "next to" this directory. So, after -installing the mongo-qa code you would have these two directories next to each -other: - - $ ls - mongo-qa - mongo-ruby-driver - $ rake test - -The tests run just fine if the mongo-qa directory is not there. - -Additionally, the script bin/validate is used by the mongo-qa project's -validator script. - - = Documentation This documentation is available online at http://api.mongodb.org/ruby. You can @@ -342,7 +323,7 @@ See CREDITS. = License - Copyright 2008-2009 10gen Inc. + Copyright 2008-2010 10gen Inc. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. @@ -355,4 +336,3 @@ See CREDITS. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. - diff --git a/test/binary_test.rb b/test/binary_test.rb index b4f6327..87c6769 100644 --- a/test/binary_test.rb +++ b/test/binary_test.rb @@ -1,17 +1,13 @@ # encoding:utf-8 - require 'test/test_helper' -class BinaryTest < Test::Unit::TestCase +context "Inspecting" do + setup do + @data = ("THIS IS BINARY " * 50).unpack("c*") + end - context "Inspecting" do - setup do - @data = ("THIS IS BINARY " * 50).unpack("c*") - end - - should "not display actual data" do - binary = Mongo::Binary.new(@data) - assert_equal "", binary.inspect - end + should "not display actual data" do + binary = Mongo::Binary.new(@data) + assert_equal "", binary.inspect end end diff --git a/test/grid_file_system_test.rb b/test/grid_file_system_test.rb index 110cb91..a3832bc 100644 --- a/test/grid_file_system_test.rb +++ b/test/grid_file_system_test.rb @@ -1,15 +1,15 @@ require 'test/test_helper' +include Mongo -class GridTest < Test::Unit::TestCase - - def setup +context "GridFileSystem:" do + setup do @db ||= Connection.new(ENV['MONGO_RUBY_DRIVER_HOST'] || 'localhost', ENV['MONGO_RUBY_DRIVER_PORT'] || Connection::DEFAULT_PORT).db('ruby-mongo-test') @files = @db.collection('fs.files') @chunks = @db.collection('fs.chunks') end - def teardown + teardown do @files.remove @chunks.remove end diff --git a/test/grid_io_test.rb b/test/grid_io_test.rb index 47f6dec..955758a 100644 --- a/test/grid_io_test.rb +++ b/test/grid_io_test.rb @@ -1,16 +1,15 @@ require 'test/test_helper' +include Mongo -class GridIOTest < Test::Unit::TestCase - include GridFS - - def setup +context "" do + setup do @db ||= Connection.new(ENV['MONGO_RUBY_DRIVER_HOST'] || 'localhost', ENV['MONGO_RUBY_DRIVER_PORT'] || Connection::DEFAULT_PORT).db('ruby-mongo-test') @files = @db.collection('fs.files') @chunks = @db.collection('fs.chunks') end - def teardown + teardown do @files.remove @chunks.remove end @@ -30,6 +29,5 @@ class GridIOTest < Test::Unit::TestCase file = GridIO.new(@files, @chunks, @filename, @mode, false, :chunk_size => 1000) assert_equal 1000, file.chunk_size end - end end diff --git a/test/grid_test.rb b/test/grid_test.rb index 82a40ad..296c5e7 100644 --- a/test/grid_test.rb +++ b/test/grid_test.rb @@ -1,15 +1,15 @@ require 'test/test_helper' +include Mongo -class GridTest < Test::Unit::TestCase - - def setup +context "Tests:" do + setup do @db ||= Connection.new(ENV['MONGO_RUBY_DRIVER_HOST'] || 'localhost', ENV['MONGO_RUBY_DRIVER_PORT'] || Connection::DEFAULT_PORT).db('ruby-mongo-test') @files = @db.collection('test-fs.files') @chunks = @db.collection('test-fs.chunks') end - def teardown + teardown do @files.remove @chunks.remove end @@ -44,24 +44,24 @@ class GridTest < Test::Unit::TestCase end end - def read_and_write_stream(filename, read_length, opts={}) - io = File.open(File.join(File.dirname(__FILE__), 'data', filename), 'r') - id = @grid.put(io, filename + read_length.to_s, opts) - file = @grid.get(id) - io.rewind - data = io.read - if data.respond_to?(:force_encoding) - data.force_encoding(:binary) - end - read_data = "" - while(chunk = file.read(read_length)) - read_data << chunk - end - assert_equal data.length, read_data.length - end - context "Streaming: " do setup do + def read_and_write_stream(filename, read_length, opts={}) + io = File.open(File.join(File.dirname(__FILE__), 'data', filename), 'r') + id = @grid.put(io, filename + read_length.to_s, opts) + file = @grid.get(id) + io.rewind + data = io.read + if data.respond_to?(:force_encoding) + data.force_encoding(:binary) + end + read_data = "" + while(chunk = file.read(read_length)) + read_data << chunk + end + assert_equal data.length, read_data.length + end + @grid = Grid.new(@db, 'test-fs') end diff --git a/test/test_helper.rb b/test/test_helper.rb index d6fe269..3e33a6e 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -5,14 +5,12 @@ require 'test/unit' begin require 'rubygems' - require 'shoulda' require 'mocha' rescue LoadError puts < @logger, :connect => false) + @db = @conn['testing'] + @coll = @db.collection('books') + @conn.expects(:send_message).with do |op, msg, log| + op == 2001 && log.include?("db.books.update") end + @coll.update({}, {:title => 'Moby Dick'}) + end - should "send update message" do - @conn = Connection.new('localhost', 27017, :logger => @logger, :connect => false) - @db = @conn['testing'] - @coll = @db.collection('books') - @conn.expects(:send_message).with do |op, msg, log| - op == 2001 && log.include?("db.books.update") - end - @coll.update({}, {:title => 'Moby Dick'}) + should "send insert message" do + @conn = Connection.new('localhost', 27017, :logger => @logger, :connect => false) + @db = @conn['testing'] + @coll = @db.collection('books') + @conn.expects(:send_message).with do |op, msg, log| + op == 2002 && log.include?("db.books.insert") end + @coll.insert({:title => 'Moby Dick'}) + end - should "send insert message" do - @conn = Connection.new('localhost', 27017, :logger => @logger, :connect => false) - @db = @conn['testing'] - @coll = @db.collection('books') - @conn.expects(:send_message).with do |op, msg, log| - op == 2002 && log.include?("db.books.insert") - end - @coll.insert({:title => 'Moby Dick'}) + should "not log binary data" do + @conn = Connection.new('localhost', 27017, :logger => @logger, :connect => false) + @db = @conn['testing'] + @coll = @db.collection('books') + data = Mongo::Binary.new(("BINARY " * 1000).unpack("c*")) + @conn.expects(:send_message).with do |op, msg, log| + op == 2002 && log.include?("Mongo::Binary") end + @coll.insert({:data => data}) + end - should "not log binary data" do - @conn = Connection.new('localhost', 27017, :logger => @logger, :connect => false) - @db = @conn['testing'] - @coll = @db.collection('books') - data = Mongo::Binary.new(("BINARY " * 1000).unpack("c*")) - @conn.expects(:send_message).with do |op, msg, log| - op == 2002 && log.include?("Mongo::Binary") - end - @coll.insert({:data => data}) + should "send safe update message" do + @conn = Connection.new('localhost', 27017, :logger => @logger, :connect => false) + @db = @conn['testing'] + @coll = @db.collection('books') + @conn.expects(:send_message_with_safe_check).with do |op, msg, db_name, log| + op == 2001 && log.include?("db.books.update") end + @coll.update({}, {:title => 'Moby Dick'}, :safe => true) + end - should "send safe update message" do - @conn = Connection.new('localhost', 27017, :logger => @logger, :connect => false) - @db = @conn['testing'] - @coll = @db.collection('books') - @conn.expects(:send_message_with_safe_check).with do |op, msg, db_name, log| - op == 2001 && log.include?("db.books.update") - end - @coll.update({}, {:title => 'Moby Dick'}, :safe => true) - end - - should "send safe insert message" do - @conn = Connection.new('localhost', 27017, :logger => @logger, :connect => false) - @db = @conn['testing'] - @coll = @db.collection('books') - @conn.expects(:send_message_with_safe_check).with do |op, msg, db_name, log| - op == 2001 && log.include?("db.books.update") - end - @coll.update({}, {:title => 'Moby Dick'}, :safe => true) + should "send safe insert message" do + @conn = Connection.new('localhost', 27017, :logger => @logger, :connect => false) + @db = @conn['testing'] + @coll = @db.collection('books') + @conn.expects(:send_message_with_safe_check).with do |op, msg, db_name, log| + op == 2001 && log.include?("db.books.update") end + @coll.update({}, {:title => 'Moby Dick'}, :safe => true) end end - - diff --git a/test/unit/connection_test.rb b/test/unit/connection_test.rb index af3b81e..a718b11 100644 --- a/test/unit/connection_test.rb +++ b/test/unit/connection_test.rb @@ -1,133 +1,115 @@ require 'test/test_helper' +include Mongo -class ConnectionTest < Test::Unit::TestCase - - def new_mock_socket - socket = Object.new - socket.stubs(:setsockopt).with(Socket::IPPROTO_TCP, Socket::TCP_NODELAY, 1) - socket - end - - def new_mock_db - db = Object.new - end - - context "Initialization: " do - - context "given a single node" do - setup do - TCPSocket.stubs(:new).returns(new_mock_socket) - @conn = Connection.new('localhost', 27017, :connect => false) - - admin_db = new_mock_db - admin_db.expects(:command).returns({'ok' => 1, 'ismaster' => 1}) - @conn.expects(:[]).with('admin').returns(admin_db) - @conn.connect_to_master - end - - should "set localhost and port to master" do - assert_equal 'localhost', @conn.host - assert_equal 27017, @conn.port - end - - should "set connection pool to 1" do - assert_equal 1, @conn.size - end - - should "default slave_ok to false" do - assert !@conn.slave_ok? - end +context "Initialization: " do + setup do + def new_mock_socket + socket = Object.new + socket.stubs(:setsockopt).with(Socket::IPPROTO_TCP, Socket::TCP_NODELAY, 1) + socket end - context "initializing a paired connection" do - should "require left and right nodes" do - assert_raise MongoArgumentError do - Connection.paired(['localhost', 27018], :connect => false) - end - - assert_raise MongoArgumentError do - Connection.paired(['localhost', 27018], :connect => false) - end - end - - should "store both nodes" do - @conn = Connection.paired([['localhost', 27017], ['localhost', 27018]], :connect => false) - - assert_equal ['localhost', 27017], @conn.nodes[0] - assert_equal ['localhost', 27018], @conn.nodes[1] - end - end - - context "initializing with a mongodb uri" do - should "parse a simple uri" do - @conn = Connection.from_uri("mongodb://localhost", :connect => false) - assert_equal ['localhost', 27017], @conn.nodes[0] - end - - should "parse a uri specifying multiple nodes" do - @conn = Connection.from_uri("mongodb://localhost:27017,mydb.com:27018", :connect => false) - assert_equal ['localhost', 27017], @conn.nodes[0] - assert_equal ['mydb.com', 27018], @conn.nodes[1] - end - - should "parse a uri specifying multiple nodes with auth" do - @conn = Connection.from_uri("mongodb://kyle:s3cr3t@localhost:27017/app,mickey:m0u5e@mydb.com:27018/dsny", :connect => false) - assert_equal ['localhost', 27017], @conn.nodes[0] - assert_equal ['mydb.com', 27018], @conn.nodes[1] - assert_equal ['kyle', 's3cr3t', 'app'], @conn.auths[0] - assert_equal ['mickey', 'm0u5e', 'dsny'], @conn.auths[1] - end - - should "attempt to connect" do - TCPSocket.stubs(:new).returns(new_mock_socket) - @conn = Connection.from_uri("mongodb://localhost", :connect => false) - - admin_db = new_mock_db - admin_db.expects(:command).returns({'ok' => 1, 'ismaster' => 1}) - @conn.expects(:[]).with('admin').returns(admin_db) - @conn.connect_to_master - end - - should "raise an error on invalid uris" do - assert_raise MongoArgumentError do - Connection.from_uri("mongo://localhost", :connect => false) - end - - assert_raise MongoArgumentError do - Connection.from_uri("mongodb://localhost:abc", :connect => false) - end - - assert_raise MongoArgumentError do - Connection.from_uri("mongodb://localhost:27017, my.db.com:27018, ", :connect => false) - end - end - - should "require all of username, password, and database if any one is specified" do - assert_raise MongoArgumentError do - Connection.from_uri("mongodb://localhost/db", :connect => false) - end - - assert_raise MongoArgumentError do - Connection.from_uri("mongodb://kyle:password@localhost", :connect => false) - end - end + def new_mock_db + db = Object.new end end - context "with a nonstandard port" do - setup do + context "given a single node" do + setup do TCPSocket.stubs(:new).returns(new_mock_socket) - @conn = Connection.new('255.255.255.255', 2500, :connect => false) + @conn = Connection.new('localhost', 27017, :connect => false) + admin_db = new_mock_db admin_db.expects(:command).returns({'ok' => 1, 'ismaster' => 1}) @conn.expects(:[]).with('admin').returns(admin_db) @conn.connect_to_master end - should "set localhost and port correctly" do - assert_equal '255.255.255.255', @conn.host - assert_equal 2500, @conn.port + should "set localhost and port to master" do + assert_equal 'localhost', @conn.host + assert_equal 27017, @conn.port + end + + should "set connection pool to 1" do + assert_equal 1, @conn.size + end + + should "default slave_ok to false" do + assert !@conn.slave_ok? + end + end + + context "initializing a paired connection" do + should "require left and right nodes" do + assert_raise MongoArgumentError do + Connection.paired(['localhost', 27018], :connect => false) + end + + assert_raise MongoArgumentError do + Connection.paired(['localhost', 27018], :connect => false) + end + end + + should "store both nodes" do + @conn = Connection.paired([['localhost', 27017], ['localhost', 27018]], :connect => false) + + assert_equal ['localhost', 27017], @conn.nodes[0] + assert_equal ['localhost', 27018], @conn.nodes[1] + end + end + + context "initializing with a mongodb uri" do + should "parse a simple uri" do + @conn = Connection.from_uri("mongodb://localhost", :connect => false) + assert_equal ['localhost', 27017], @conn.nodes[0] + end + + should "parse a uri specifying multiple nodes" do + @conn = Connection.from_uri("mongodb://localhost:27017,mydb.com:27018", :connect => false) + assert_equal ['localhost', 27017], @conn.nodes[0] + assert_equal ['mydb.com', 27018], @conn.nodes[1] + end + + should "parse a uri specifying multiple nodes with auth" do + @conn = Connection.from_uri("mongodb://kyle:s3cr3t@localhost:27017/app,mickey:m0u5e@mydb.com:27018/dsny", :connect => false) + assert_equal ['localhost', 27017], @conn.nodes[0] + assert_equal ['mydb.com', 27018], @conn.nodes[1] + assert_equal ['kyle', 's3cr3t', 'app'], @conn.auths[0] + assert_equal ['mickey', 'm0u5e', 'dsny'], @conn.auths[1] + end + + should "attempt to connect" do + TCPSocket.stubs(:new).returns(new_mock_socket) + @conn = Connection.from_uri("mongodb://localhost", :connect => false) + + admin_db = new_mock_db + admin_db.expects(:command).returns({'ok' => 1, 'ismaster' => 1}) + @conn.expects(:[]).with('admin').returns(admin_db) + @conn.connect_to_master + end + + should "raise an error on invalid uris" do + assert_raise MongoArgumentError do + Connection.from_uri("mongo://localhost", :connect => false) + end + + assert_raise MongoArgumentError do + Connection.from_uri("mongodb://localhost:abc", :connect => false) + end + + assert_raise MongoArgumentError do + Connection.from_uri("mongodb://localhost:27017, my.db.com:27018, ", :connect => false) + end + end + + should "require all of username, password, and database if any one is specified" do + assert_raise MongoArgumentError do + Connection.from_uri("mongodb://localhost/db", :connect => false) + end + + assert_raise MongoArgumentError do + Connection.from_uri("mongodb://kyle:password@localhost", :connect => false) + end end end end - diff --git a/test/unit/cursor_test.rb b/test/unit/cursor_test.rb index 09ab883..28cacc5 100644 --- a/test/unit/cursor_test.rb +++ b/test/unit/cursor_test.rb @@ -1,94 +1,91 @@ require 'test/test_helper' -class TestCursor < Test::Unit::TestCase - - context "Cursor options" do - setup do - @connection = stub(:class => Connection) - @db = stub(:name => "testing", :slave_ok? => false, :connection => @connection) - @collection = stub(:db => @db, :name => "items") - @cursor = Cursor.new(@collection) - end - - should "set admin to false" do - assert_equal false, @cursor.admin - - @cursor = Cursor.new(@collection, :admin => true) - assert_equal true, @cursor.admin - end - - should "set selector" do - assert @cursor.selector == {} - - @cursor = Cursor.new(@collection, :selector => {:name => "Jones"}) - assert @cursor.selector == {:name => "Jones"} - end - - should "set fields" do - assert_nil @cursor.fields - - @cursor = Cursor.new(@collection, :fields => [:name, :date]) - assert @cursor.fields == {:name => 1, :date => 1} - end - - should "set limit" do - assert_equal 0, @cursor.limit - - @cursor = Cursor.new(@collection, :limit => 10) - assert_equal 10, @cursor.limit - end - - - should "set skip" do - assert_equal 0, @cursor.skip - - @cursor = Cursor.new(@collection, :skip => 5) - assert_equal 5, @cursor.skip - end - - should "set sort order" do - assert_nil @cursor.order - - @cursor = Cursor.new(@collection, :order => "last_name") - assert_equal "last_name", @cursor.order - end - - should "set hint" do - assert_nil @cursor.hint - - @cursor = Cursor.new(@collection, :hint => "name") - assert_equal "name", @cursor.hint - end - - should "cache full collection name" do - assert_equal "testing.items", @cursor.full_collection_name - end +context "Cursor options" do + setup do + @connection = stub(:class => Connection) + @db = stub(:name => "testing", :slave_ok? => false, :connection => @connection) + @collection = stub(:db => @db, :name => "items") + @cursor = Cursor.new(@collection) end - context "Query fields" do - setup do - @connection = stub(:class => Collection) - @db = stub(:slave_ok? => true, :name => "testing", :connection => @connection) - @collection = stub(:db => @db, :name => "items") - end + should "set admin to false" do + assert_equal false, @cursor.admin - should "when an array should return a hash with each key" do - @cursor = Cursor.new(@collection, :fields => [:name, :age]) - result = @cursor.fields - assert_equal result.keys.sort{|a,b| a.to_s <=> b.to_s}, [:age, :name].sort{|a,b| a.to_s <=> b.to_s} - assert result.values.all? {|v| v == 1} - end + @cursor = Cursor.new(@collection, :admin => true) + assert_equal true, @cursor.admin + end - should "when a string, return a hash with just the key" do - @cursor = Cursor.new(@collection, :fields => "name") - result = @cursor.fields - assert_equal result.keys.sort, ["name"] - assert result.values.all? {|v| v == 1} - end + should "set selector" do + assert @cursor.selector == {} - should "return nil when neither hash nor string nor symbol" do - @cursor = Cursor.new(@collection, :fields => 1234567) - assert_nil @cursor.fields - end + @cursor = Cursor.new(@collection, :selector => {:name => "Jones"}) + assert @cursor.selector == {:name => "Jones"} + end + + should "set fields" do + assert_nil @cursor.fields + + @cursor = Cursor.new(@collection, :fields => [:name, :date]) + assert @cursor.fields == {:name => 1, :date => 1} + end + + should "set limit" do + assert_equal 0, @cursor.limit + + @cursor = Cursor.new(@collection, :limit => 10) + assert_equal 10, @cursor.limit + end + + + should "set skip" do + assert_equal 0, @cursor.skip + + @cursor = Cursor.new(@collection, :skip => 5) + assert_equal 5, @cursor.skip + end + + should "set sort order" do + assert_nil @cursor.order + + @cursor = Cursor.new(@collection, :order => "last_name") + assert_equal "last_name", @cursor.order + end + + should "set hint" do + assert_nil @cursor.hint + + @cursor = Cursor.new(@collection, :hint => "name") + assert_equal "name", @cursor.hint + end + + should "cache full collection name" do + assert_equal "testing.items", @cursor.full_collection_name + end +end + +context "Query fields" do + setup do + @connection = stub(:class => Collection) + @db = stub(:slave_ok? => true, :name => "testing", :connection => @connection) + @collection = stub(:db => @db, :name => "items") + end + + should "when an array should return a hash with each key" do + @cursor = Cursor.new(@collection, :fields => [:name, :age]) + result = @cursor.fields + assert_equal result.keys.sort{|a,b| a.to_s <=> b.to_s}, [:age, :name].sort{|a,b| a.to_s <=> b.to_s} + assert result.values.all? {|v| v == 1} + end + + should "when a string, return a hash with just the key" do + @cursor = Cursor.new(@collection, :fields => "name") + result = @cursor.fields + assert_equal result.keys.sort, ["name"] + assert result.values.all? {|v| v == 1} + end + + should "return nil when neither hash nor string nor symbol" do + @cursor = Cursor.new(@collection, :fields => 1234567) + assert_nil @cursor.fields end end diff --git a/test/unit/db_test.rb b/test/unit/db_test.rb index 05e12a6..097e3cc 100644 --- a/test/unit/db_test.rb +++ b/test/unit/db_test.rb @@ -1,14 +1,15 @@ require 'test/test_helper' -class DBTest < Test::Unit::TestCase - - def insert_message(db, documents) - documents = [documents] unless documents.is_a?(Array) - message = ByteBuffer.new - message.put_int(0) - BSON.serialize_cstr(message, "#{db.name}.test") - documents.each { |doc| message.put_array(BSON.new.serialize(doc, true).to_a) } - message = db.add_message_headers(Mongo::Constants::OP_INSERT, message) +context "DBTest: " do + setup do + def insert_message(db, documents) + documents = [documents] unless documents.is_a?(Array) + message = ByteBuffer.new + message.put_int(0) + BSON.serialize_cstr(message, "#{db.name}.test") + documents.each { |doc| message.put_array(BSON.new.serialize(doc, true).to_a) } + message = db.add_message_headers(Mongo::Constants::OP_INSERT, message) + end end context "DB commands" do