diff --git a/README.rdoc b/README.rdoc index c1fc69c..6a04929 100644 --- a/README.rdoc +++ b/README.rdoc @@ -12,7 +12,7 @@ many more. db = Connection.new('localhost').db('sample-db') coll = db.collection('test') - coll.clear + coll.remove 3.times { |i| coll.insert({'a' => i+1}) } puts "There are #{coll.count()} records. Here they are:" coll.find().each { |doc| puts doc.inspect } @@ -82,7 +82,7 @@ Here is some simple example code: db = Connection.new.db('my-db-name') things = db.collection('things') - things.clear + things.remove things.insert('a' => 42) things.insert('a' => 99, 'b' => Time.now) puts things.count # => 2 @@ -258,6 +258,12 @@ If you have the source code, you can run the tests. $ rake test +The tests now require shoulda and mocha. You can install these gems as +follows: + + $ gem install shoulda + $ gem install mocha + The tests assume that the Mongo database is running on the default port. You can override the default host (localhost) and port (Connection::DEFAULT_PORT) by using the environment variables MONGO_RUBY_DRIVER_HOST and diff --git a/Rakefile b/Rakefile index f4851be..8a59e49 100644 --- a/Rakefile +++ b/Rakefile @@ -15,8 +15,10 @@ gem_command = "gem" gem_command = "gem1.9" if $0.match(/1\.9$/) # use gem1.9 if we used rake1.9 # NOTE: some of the tests assume Mongo is running -Rake::TestTask.new do |t| +desc "Test the MongoDB Ruby driver." +Rake::TestTask.new(:test) do |t| t.test_files = FileList['test/test*.rb'] + t.verbose = true end desc "Generate documentation" diff --git a/lib/mongo.rb b/lib/mongo.rb index f6c2d90..2ab18df 100644 --- a/lib/mongo.rb +++ b/lib/mongo.rb @@ -15,5 +15,5 @@ module Mongo ASCENDING = 1 DESCENDING = -1 - VERSION = "0.15.1" + VERSION = "1.15.1" end diff --git a/lib/mongo/collection.rb b/lib/mongo/collection.rb index 06dc0b0..6b5492f 100644 --- a/lib/mongo/collection.rb +++ b/lib/mongo/collection.rb @@ -214,10 +214,15 @@ module Mongo end alias_method :<<, :insert - # Remove the records that match +selector+. - # def remove(selector={}) - # @db.remove_from_db(@name, selector) - # end + # Remove all records from this collection. + # If +selector+ is specified, only matching documents will be removed. + # + # Remove all records from the collection: + # @collection.remove + # @collection.remove({}) + # + # Remove only records that have expired: + # @collection.remove({:expire => {'$lte' => Time.now}}) def remove(selector={}) message = ByteBuffer.new message.put_int(0) @@ -228,7 +233,9 @@ module Mongo end # Remove all records. + # DEPRECATED: please use Collection#remove instead. def clear + warn "Collection#clear is deprecated. Please use Collection#remove instead." remove({}) end diff --git a/test/test_admin.rb b/test/test_admin.rb index 4897716..07e8a9e 100644 --- a/test/test_admin.rb +++ b/test/test_admin.rb @@ -13,7 +13,7 @@ class AdminTest < Test::Unit::TestCase def setup # Insert some data to make sure the database itself exists. - @@coll.clear + @@coll.remove @r1 = @@coll.insert('a' => 1) # collection not created until it's used @@coll_full_name = 'ruby-mongo-test.test' @admin = @@db.admin @@ -21,7 +21,7 @@ class AdminTest < Test::Unit::TestCase def teardown @admin.profiling_level = :off - @@coll.clear if @@coll + @@coll.remove if @@coll @@db.error end diff --git a/test/test_chunk.rb b/test/test_chunk.rb index ff90e2b..7239c77 100644 --- a/test/test_chunk.rb +++ b/test/test_chunk.rb @@ -14,16 +14,16 @@ class ChunkTest < Test::Unit::TestCase @@chunks = @@db.collection('gridfs.chunks') def setup - @@chunks.clear - @@files.clear + @@chunks.remove + @@files.remove @f = GridStore.new(@@db, 'foobar', 'w') @c = @f.instance_variable_get('@curr_chunk') end def teardown - @@chunks.clear - @@files.clear + @@chunks.remove + @@files.remove @@db.error end diff --git a/test/test_collection.rb b/test/test_collection.rb index 65806b4..2f624b8 100644 --- a/test/test_collection.rb +++ b/test/test_collection.rb @@ -14,14 +14,8 @@ # limitations under the License. # ++ -$LOAD_PATH[0,0] = File.join(File.dirname(__FILE__), '..', 'lib') -require 'mongo' -require 'test/unit' - -# NOTE: assumes Mongo is running +require 'test/test_helper' class TestCollection < Test::Unit::TestCase - include Mongo - @@db = Connection.new(ENV['MONGO_RUBY_DRIVER_HOST'] || 'localhost', ENV['MONGO_RUBY_DRIVER_PORT'] || Connection::DEFAULT_PORT).db('ruby-mongo-test') @@test = @@db.collection("test") @@ -59,7 +53,7 @@ class TestCollection < Test::Unit::TestCase assert_equal @@db["test"]["foo"].name(), @@db.collection("test.foo").name() assert_equal @@db["test"]["foo"].name(), @@db["test.foo"].name() - @@db["test"]["foo"].clear + @@db["test"]["foo"].remove @@db["test"]["foo"].insert("x" => 5) assert_equal 5, @@db.collection("test.foo").find_one()["x"] end @@ -323,5 +317,32 @@ class TestCollection < Test::Unit::TestCase # assert_equal 1, @@test.group([], {}, {"count" => 0}, # Code.new(reduce_function, # {"inc_value" => 0.5}), true)[0]["count"] + end + + context "A collection with two records" do + setup do + @collection = @@db.collection('test-collection') + @collection.insert({:name => "Jones"}) + @collection.insert({:name => "Smith"}) + end + + should "have two records" do + assert_equal 2, @collection.size + end + + should "remove the two records" do + @collection.remove() + assert_equal 0, @collection.size + end + + should "remove all records if an empty document is specified" do + @collection.remove({}) + assert_equal 0, @collection.find.count + end + + should "remove only matching records" do + @collection.remove({:name => "Jones"}) + assert_equal 1, @collection.size + end end end diff --git a/test/test_connection.rb b/test/test_connection.rb index aab17d7..790bd58 100644 --- a/test/test_connection.rb +++ b/test/test_connection.rb @@ -81,7 +81,7 @@ class TestConnection < Test::Unit::TestCase def test_drop_database db = @mongo.db('ruby-mongo-will-be-deleted') coll = db.collection('temp') - coll.clear + coll.remove coll.insert(:name => 'temp') assert_equal 1, coll.count() assert @mongo.database_names.include?('ruby-mongo-will-be-deleted') diff --git a/test/test_cursor.rb b/test/test_cursor.rb index e7f093b..083644a 100644 --- a/test/test_cursor.rb +++ b/test/test_cursor.rb @@ -12,13 +12,13 @@ class CursorTest < Test::Unit::TestCase @@coll = @@db.collection('test') def setup - @@coll.clear + @@coll.remove @@coll.insert('a' => 1) # collection not created until it's used @@coll_full_name = 'ruby-mongo-test.test' end def teardown - @@coll.clear + @@coll.remove @@db.error end @@ -32,7 +32,7 @@ class CursorTest < Test::Unit::TestCase end def test_count - @@coll.clear + @@coll.remove assert_equal 0, @@coll.find().count() @@ -59,7 +59,7 @@ class CursorTest < Test::Unit::TestCase end def test_sort - @@coll.clear + @@coll.remove 5.times{|x| @@coll.insert({"a" => x}) } assert_kind_of Cursor, @@coll.find().sort(:a, 1) @@ -89,7 +89,7 @@ class CursorTest < Test::Unit::TestCase end def test_limit - @@coll.clear + @@coll.remove 10.times do |i| @@coll.save("x" => i) @@ -119,7 +119,7 @@ class CursorTest < Test::Unit::TestCase end def test_skip - @@coll.clear + @@coll.remove 10.times do |i| @@coll.save("x" => i) @@ -153,7 +153,7 @@ class CursorTest < Test::Unit::TestCase end def test_limit_skip_chaining - @@coll.clear + @@coll.remove 10.times do |i| @@coll.save("x" => i) end @@ -202,7 +202,7 @@ class CursorTest < Test::Unit::TestCase def test_refill_via_get_more_alt_coll coll = @@db.collection('test-alt-coll') - coll.clear + coll.remove coll.insert('a' => 1) # collection not created until it's used assert_equal 1, coll.count @@ -318,7 +318,7 @@ class CursorTest < Test::Unit::TestCase end def test_count_with_fields - @@coll.clear + @@coll.remove @@coll.save("x" => 1) @@coll.find({}, :fields => ["a"]).each do |doc| diff --git a/test/test_db.rb b/test/test_db.rb index a5de5e6..ad9082a 100644 --- a/test/test_db.rb +++ b/test/test_db.rb @@ -25,12 +25,12 @@ class DBTest < Test::Unit::TestCase def setup @spongebob = 'spongebob' @spongebob_password = 'squarepants' - @@users.clear + @@users.remove @@users.insert(:user => @spongebob, :pwd => @@db.send(:hash_password, @spongebob, @spongebob_password)) end def teardown - @@users.clear if @@users + @@users.remove if @@users @@db.error end @@ -101,7 +101,7 @@ class DBTest < Test::Unit::TestCase def test_pk_factory db = Connection.new(@@host, @@port).db('ruby-mongo-test', :pk => TestPKFactory.new) coll = db.collection('test') - coll.clear + coll.remove insert_id = coll.insert('name' => 'Fred', 'age' => 42) # new id gets added to returned object @@ -118,7 +118,7 @@ class DBTest < Test::Unit::TestCase assert_equal oid, db_oid assert_equal data, row - coll.clear + coll.remove end def test_pk_factory_reset @@ -190,7 +190,7 @@ class DBTest < Test::Unit::TestCase end def test_last_status - @@db['test'].clear + @@db['test'].remove @@db['test'].save("i" => 1) @@db['test'].update({"i" => 1}, {"$set" => {"i" => 2}}) @@ -203,7 +203,7 @@ class DBTest < Test::Unit::TestCase def test_text_port_number db = DB.new('ruby-mongo-test', [[@@host, @@port.to_s]]) # If there is no error, all is well - db.collection('users').clear + db.collection('users').remove end end diff --git a/test/test_db_api.rb b/test/test_db_api.rb index c34aeba..e4817aa 100644 --- a/test/test_db_api.rb +++ b/test/test_db_api.rb @@ -11,20 +11,20 @@ class DBAPITest < Test::Unit::TestCase @@coll = @@db.collection('test') def setup - @@coll.clear + @@coll.remove @r1 = {'a' => 1} @@coll.insert(@r1) # collection not created until it's used @@coll_full_name = 'ruby-mongo-test.test' end def teardown - @@coll.clear + @@coll.remove @@db.error end def test_clear assert_equal 1, @@coll.count - @@coll.clear + @@coll.remove assert_equal 0, @@coll.count end @@ -142,7 +142,7 @@ class DBAPITest < Test::Unit::TestCase end def test_find_sorting - @@coll.clear + @@coll.remove @@coll.insert('a' => 1, 'b' => 2) @@coll.insert('a' => 2, 'b' => 1) @@coll.insert('a' => 3, 'b' => 2) @@ -233,7 +233,7 @@ class DBAPITest < Test::Unit::TestCase end def test_find_one_no_records - @@coll.clear + @@coll.remove x = @@coll.find_one('a' => 1) assert_nil x end @@ -580,7 +580,7 @@ class DBAPITest < Test::Unit::TestCase end def test_deref - @@coll.clear + @@coll.remove assert_equal nil, @@db.dereference(DBRef.new("test", ObjectID.new)) @@coll.insert({"x" => "hello"}) @@ -592,13 +592,13 @@ class DBAPITest < Test::Unit::TestCase @@coll.insert(obj) assert_equal obj, @@db.dereference(DBRef.new("test", 4)) - @@coll.clear + @@coll.remove @@coll.insert({"x" => "hello"}) assert_equal nil, @@db.dereference(DBRef.new("test", nil)) end def test_save - @@coll.clear + @@coll.remove a = {"hello" => "world"} @@ -622,13 +622,13 @@ class DBAPITest < Test::Unit::TestCase end def test_save_long - @@coll.clear + @@coll.remove @@coll.insert("x" => 9223372036854775807) assert_equal 9223372036854775807, @@coll.find_one()["x"] end def test_find_by_oid - @@coll.clear + @@coll.remove @@coll.save("hello" => "mike") id = @@coll.save("hello" => "world") @@ -644,7 +644,7 @@ class DBAPITest < Test::Unit::TestCase end def test_save_with_object_that_has_id_but_does_not_actually_exist_in_collection - @@coll.clear + @@coll.remove a = {'_id' => '1', 'hello' => 'world'} @@coll.save(a) @@ -658,7 +658,7 @@ class DBAPITest < Test::Unit::TestCase end def test_invalid_key_names - @@coll.clear + @@coll.remove @@coll.insert({"hello" => "world"}) @@coll.insert({"hello" => {"hello" => "world"}}) @@ -779,7 +779,7 @@ class DBAPITest < Test::Unit::TestCase assert_equal "UTF-8", utf8.encoding.name assert_equal "ISO-8859-1", iso8859.encoding.name - @@coll.clear + @@coll.remove @@coll.save("ascii" => ascii, "utf8" => utf8, "iso8859" => iso8859) doc = @@coll.find_one() diff --git a/test/test_db_connection.rb b/test/test_db_connection.rb index 992fb26..432c477 100644 --- a/test/test_db_connection.rb +++ b/test/test_db_connection.rb @@ -12,7 +12,7 @@ class DBConnectionTest < Test::Unit::TestCase port = ENV['MONGO_RUBY_DRIVER_PORT'] || Connection::DEFAULT_PORT db = Connection.new(host, port).db('ruby-mongo-demo') coll = db.collection('test') - coll.clear + coll.remove db.error end end diff --git a/test/test_grid_store.rb b/test/test_grid_store.rb index a838b88..594b771 100644 --- a/test/test_grid_store.rb +++ b/test/test_grid_store.rb @@ -14,14 +14,14 @@ class GridStoreTest < Test::Unit::TestCase @@chunks = @@db.collection('fs.chunks') def setup - @@chunks.clear - @@files.clear + @@chunks.remove + @@files.remove GridStore.open(@@db, 'foobar', 'w') { |f| f.write("hello, world!") } end def teardown - @@chunks.clear - @@files.clear + @@chunks.remove + @@files.remove @@db.error end @@ -106,8 +106,8 @@ class GridStoreTest < Test::Unit::TestCase end def test_multi_chunk - @@chunks.clear - @@files.clear + @@chunks.remove + @@files.remove size = 512 GridStore.open(@@db, 'biggie', 'w') { |f| @@ -167,8 +167,8 @@ class GridStoreTest < Test::Unit::TestCase end def test_save_empty_file - @@chunks.clear - @@files.clear + @@chunks.remove + @@files.remove GridStore.open(@@db, 'empty', 'w') {} # re-write with zero bytes assert_equal 1, @@files.count assert_equal 0, @@chunks.count diff --git a/test/test_helper.rb b/test/test_helper.rb new file mode 100644 index 0000000..bfd1eef --- /dev/null +++ b/test/test_helper.rb @@ -0,0 +1,25 @@ +$LOAD_PATH[0,0] = File.join(File.dirname(__FILE__), '..', 'lib') +require 'rubygems' +require 'test/unit' + +begin + require 'shoulda' + require 'mocha' + rescue LoadError + puts < 1, '_id' => @o} row = coll.find().collect.first diff --git a/test/test_threading.rb b/test/test_threading.rb index aa072e3..8d11b11 100644 --- a/test/test_threading.rb +++ b/test/test_threading.rb @@ -12,7 +12,7 @@ class TestThreading < Test::Unit::TestCase @@coll = @@db.collection('thread-test-collection') def test_threading - @@coll.clear + @@coll.remove 1000.times do |i| @@coll.insert("x" => i)