Deprecated Collection#clear in favor of Collection#remove. Introduced shoulda and mocha for upcoming tests.

This commit is contained in:
Kyle Banker 2009-10-20 11:31:07 -04:00
parent 2d7bae4e0a
commit e40d9cec3c
16 changed files with 123 additions and 62 deletions

View File

@ -12,7 +12,7 @@ many more.
db = Connection.new('localhost').db('sample-db') db = Connection.new('localhost').db('sample-db')
coll = db.collection('test') coll = db.collection('test')
coll.clear coll.remove
3.times { |i| coll.insert({'a' => i+1}) } 3.times { |i| coll.insert({'a' => i+1}) }
puts "There are #{coll.count()} records. Here they are:" puts "There are #{coll.count()} records. Here they are:"
coll.find().each { |doc| puts doc.inspect } coll.find().each { |doc| puts doc.inspect }
@ -82,7 +82,7 @@ Here is some simple example code:
db = Connection.new.db('my-db-name') db = Connection.new.db('my-db-name')
things = db.collection('things') things = db.collection('things')
things.clear things.remove
things.insert('a' => 42) things.insert('a' => 42)
things.insert('a' => 99, 'b' => Time.now) things.insert('a' => 99, 'b' => Time.now)
puts things.count # => 2 puts things.count # => 2
@ -258,6 +258,12 @@ If you have the source code, you can run the tests.
$ rake test $ 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 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 can override the default host (localhost) and port (Connection::DEFAULT_PORT) by
using the environment variables MONGO_RUBY_DRIVER_HOST and using the environment variables MONGO_RUBY_DRIVER_HOST and

View File

@ -15,8 +15,10 @@ gem_command = "gem"
gem_command = "gem1.9" if $0.match(/1\.9$/) # use gem1.9 if we used rake1.9 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 # 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.test_files = FileList['test/test*.rb']
t.verbose = true
end end
desc "Generate documentation" desc "Generate documentation"

View File

@ -15,5 +15,5 @@ module Mongo
ASCENDING = 1 ASCENDING = 1
DESCENDING = -1 DESCENDING = -1
VERSION = "0.15.1" VERSION = "1.15.1"
end end

View File

@ -214,10 +214,15 @@ module Mongo
end end
alias_method :<<, :insert alias_method :<<, :insert
# Remove the records that match +selector+. # Remove all records from this collection.
# def remove(selector={}) # If +selector+ is specified, only matching documents will be removed.
# @db.remove_from_db(@name, selector) #
# end # 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={}) def remove(selector={})
message = ByteBuffer.new message = ByteBuffer.new
message.put_int(0) message.put_int(0)
@ -228,7 +233,9 @@ module Mongo
end end
# Remove all records. # Remove all records.
# DEPRECATED: please use Collection#remove instead.
def clear def clear
warn "Collection#clear is deprecated. Please use Collection#remove instead."
remove({}) remove({})
end end

View File

@ -13,7 +13,7 @@ class AdminTest < Test::Unit::TestCase
def setup def setup
# Insert some data to make sure the database itself exists. # 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 @r1 = @@coll.insert('a' => 1) # collection not created until it's used
@@coll_full_name = 'ruby-mongo-test.test' @@coll_full_name = 'ruby-mongo-test.test'
@admin = @@db.admin @admin = @@db.admin
@ -21,7 +21,7 @@ class AdminTest < Test::Unit::TestCase
def teardown def teardown
@admin.profiling_level = :off @admin.profiling_level = :off
@@coll.clear if @@coll @@coll.remove if @@coll
@@db.error @@db.error
end end

View File

@ -14,16 +14,16 @@ class ChunkTest < Test::Unit::TestCase
@@chunks = @@db.collection('gridfs.chunks') @@chunks = @@db.collection('gridfs.chunks')
def setup def setup
@@chunks.clear @@chunks.remove
@@files.clear @@files.remove
@f = GridStore.new(@@db, 'foobar', 'w') @f = GridStore.new(@@db, 'foobar', 'w')
@c = @f.instance_variable_get('@curr_chunk') @c = @f.instance_variable_get('@curr_chunk')
end end
def teardown def teardown
@@chunks.clear @@chunks.remove
@@files.clear @@files.remove
@@db.error @@db.error
end end

View File

@ -14,14 +14,8 @@
# limitations under the License. # limitations under the License.
# ++ # ++
$LOAD_PATH[0,0] = File.join(File.dirname(__FILE__), '..', 'lib') require 'test/test_helper'
require 'mongo'
require 'test/unit'
# NOTE: assumes Mongo is running
class TestCollection < Test::Unit::TestCase class TestCollection < Test::Unit::TestCase
include Mongo
@@db = Connection.new(ENV['MONGO_RUBY_DRIVER_HOST'] || 'localhost', @@db = Connection.new(ENV['MONGO_RUBY_DRIVER_HOST'] || 'localhost',
ENV['MONGO_RUBY_DRIVER_PORT'] || Connection::DEFAULT_PORT).db('ruby-mongo-test') ENV['MONGO_RUBY_DRIVER_PORT'] || Connection::DEFAULT_PORT).db('ruby-mongo-test')
@@test = @@db.collection("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.collection("test.foo").name()
assert_equal @@db["test"]["foo"].name(), @@db["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) @@db["test"]["foo"].insert("x" => 5)
assert_equal 5, @@db.collection("test.foo").find_one()["x"] assert_equal 5, @@db.collection("test.foo").find_one()["x"]
end end
@ -324,4 +318,31 @@ class TestCollection < Test::Unit::TestCase
# Code.new(reduce_function, # Code.new(reduce_function,
# {"inc_value" => 0.5}), true)[0]["count"] # {"inc_value" => 0.5}), true)[0]["count"]
end 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 end

View File

@ -81,7 +81,7 @@ class TestConnection < Test::Unit::TestCase
def test_drop_database def test_drop_database
db = @mongo.db('ruby-mongo-will-be-deleted') db = @mongo.db('ruby-mongo-will-be-deleted')
coll = db.collection('temp') coll = db.collection('temp')
coll.clear coll.remove
coll.insert(:name => 'temp') coll.insert(:name => 'temp')
assert_equal 1, coll.count() assert_equal 1, coll.count()
assert @mongo.database_names.include?('ruby-mongo-will-be-deleted') assert @mongo.database_names.include?('ruby-mongo-will-be-deleted')

View File

@ -12,13 +12,13 @@ class CursorTest < Test::Unit::TestCase
@@coll = @@db.collection('test') @@coll = @@db.collection('test')
def setup def setup
@@coll.clear @@coll.remove
@@coll.insert('a' => 1) # collection not created until it's used @@coll.insert('a' => 1) # collection not created until it's used
@@coll_full_name = 'ruby-mongo-test.test' @@coll_full_name = 'ruby-mongo-test.test'
end end
def teardown def teardown
@@coll.clear @@coll.remove
@@db.error @@db.error
end end
@ -32,7 +32,7 @@ class CursorTest < Test::Unit::TestCase
end end
def test_count def test_count
@@coll.clear @@coll.remove
assert_equal 0, @@coll.find().count() assert_equal 0, @@coll.find().count()
@ -59,7 +59,7 @@ class CursorTest < Test::Unit::TestCase
end end
def test_sort def test_sort
@@coll.clear @@coll.remove
5.times{|x| @@coll.insert({"a" => x}) } 5.times{|x| @@coll.insert({"a" => x}) }
assert_kind_of Cursor, @@coll.find().sort(:a, 1) assert_kind_of Cursor, @@coll.find().sort(:a, 1)
@ -89,7 +89,7 @@ class CursorTest < Test::Unit::TestCase
end end
def test_limit def test_limit
@@coll.clear @@coll.remove
10.times do |i| 10.times do |i|
@@coll.save("x" => i) @@coll.save("x" => i)
@ -119,7 +119,7 @@ class CursorTest < Test::Unit::TestCase
end end
def test_skip def test_skip
@@coll.clear @@coll.remove
10.times do |i| 10.times do |i|
@@coll.save("x" => i) @@coll.save("x" => i)
@ -153,7 +153,7 @@ class CursorTest < Test::Unit::TestCase
end end
def test_limit_skip_chaining def test_limit_skip_chaining
@@coll.clear @@coll.remove
10.times do |i| 10.times do |i|
@@coll.save("x" => i) @@coll.save("x" => i)
end end
@ -202,7 +202,7 @@ class CursorTest < Test::Unit::TestCase
def test_refill_via_get_more_alt_coll def test_refill_via_get_more_alt_coll
coll = @@db.collection('test-alt-coll') coll = @@db.collection('test-alt-coll')
coll.clear coll.remove
coll.insert('a' => 1) # collection not created until it's used coll.insert('a' => 1) # collection not created until it's used
assert_equal 1, coll.count assert_equal 1, coll.count
@ -318,7 +318,7 @@ class CursorTest < Test::Unit::TestCase
end end
def test_count_with_fields def test_count_with_fields
@@coll.clear @@coll.remove
@@coll.save("x" => 1) @@coll.save("x" => 1)
@@coll.find({}, :fields => ["a"]).each do |doc| @@coll.find({}, :fields => ["a"]).each do |doc|

View File

@ -25,12 +25,12 @@ class DBTest < Test::Unit::TestCase
def setup def setup
@spongebob = 'spongebob' @spongebob = 'spongebob'
@spongebob_password = 'squarepants' @spongebob_password = 'squarepants'
@@users.clear @@users.remove
@@users.insert(:user => @spongebob, :pwd => @@db.send(:hash_password, @spongebob, @spongebob_password)) @@users.insert(:user => @spongebob, :pwd => @@db.send(:hash_password, @spongebob, @spongebob_password))
end end
def teardown def teardown
@@users.clear if @@users @@users.remove if @@users
@@db.error @@db.error
end end
@ -101,7 +101,7 @@ class DBTest < Test::Unit::TestCase
def test_pk_factory def test_pk_factory
db = Connection.new(@@host, @@port).db('ruby-mongo-test', :pk => TestPKFactory.new) db = Connection.new(@@host, @@port).db('ruby-mongo-test', :pk => TestPKFactory.new)
coll = db.collection('test') coll = db.collection('test')
coll.clear coll.remove
insert_id = coll.insert('name' => 'Fred', 'age' => 42) insert_id = coll.insert('name' => 'Fred', 'age' => 42)
# new id gets added to returned object # new id gets added to returned object
@ -118,7 +118,7 @@ class DBTest < Test::Unit::TestCase
assert_equal oid, db_oid assert_equal oid, db_oid
assert_equal data, row assert_equal data, row
coll.clear coll.remove
end end
def test_pk_factory_reset def test_pk_factory_reset
@ -190,7 +190,7 @@ class DBTest < Test::Unit::TestCase
end end
def test_last_status def test_last_status
@@db['test'].clear @@db['test'].remove
@@db['test'].save("i" => 1) @@db['test'].save("i" => 1)
@@db['test'].update({"i" => 1}, {"$set" => {"i" => 2}}) @@db['test'].update({"i" => 1}, {"$set" => {"i" => 2}})
@ -203,7 +203,7 @@ class DBTest < Test::Unit::TestCase
def test_text_port_number def test_text_port_number
db = DB.new('ruby-mongo-test', [[@@host, @@port.to_s]]) db = DB.new('ruby-mongo-test', [[@@host, @@port.to_s]])
# If there is no error, all is well # If there is no error, all is well
db.collection('users').clear db.collection('users').remove
end end
end end

View File

@ -11,20 +11,20 @@ class DBAPITest < Test::Unit::TestCase
@@coll = @@db.collection('test') @@coll = @@db.collection('test')
def setup def setup
@@coll.clear @@coll.remove
@r1 = {'a' => 1} @r1 = {'a' => 1}
@@coll.insert(@r1) # collection not created until it's used @@coll.insert(@r1) # collection not created until it's used
@@coll_full_name = 'ruby-mongo-test.test' @@coll_full_name = 'ruby-mongo-test.test'
end end
def teardown def teardown
@@coll.clear @@coll.remove
@@db.error @@db.error
end end
def test_clear def test_clear
assert_equal 1, @@coll.count assert_equal 1, @@coll.count
@@coll.clear @@coll.remove
assert_equal 0, @@coll.count assert_equal 0, @@coll.count
end end
@ -142,7 +142,7 @@ class DBAPITest < Test::Unit::TestCase
end end
def test_find_sorting def test_find_sorting
@@coll.clear @@coll.remove
@@coll.insert('a' => 1, 'b' => 2) @@coll.insert('a' => 1, 'b' => 2)
@@coll.insert('a' => 2, 'b' => 1) @@coll.insert('a' => 2, 'b' => 1)
@@coll.insert('a' => 3, 'b' => 2) @@coll.insert('a' => 3, 'b' => 2)
@ -233,7 +233,7 @@ class DBAPITest < Test::Unit::TestCase
end end
def test_find_one_no_records def test_find_one_no_records
@@coll.clear @@coll.remove
x = @@coll.find_one('a' => 1) x = @@coll.find_one('a' => 1)
assert_nil x assert_nil x
end end
@ -580,7 +580,7 @@ class DBAPITest < Test::Unit::TestCase
end end
def test_deref def test_deref
@@coll.clear @@coll.remove
assert_equal nil, @@db.dereference(DBRef.new("test", ObjectID.new)) assert_equal nil, @@db.dereference(DBRef.new("test", ObjectID.new))
@@coll.insert({"x" => "hello"}) @@coll.insert({"x" => "hello"})
@ -592,13 +592,13 @@ class DBAPITest < Test::Unit::TestCase
@@coll.insert(obj) @@coll.insert(obj)
assert_equal obj, @@db.dereference(DBRef.new("test", 4)) assert_equal obj, @@db.dereference(DBRef.new("test", 4))
@@coll.clear @@coll.remove
@@coll.insert({"x" => "hello"}) @@coll.insert({"x" => "hello"})
assert_equal nil, @@db.dereference(DBRef.new("test", nil)) assert_equal nil, @@db.dereference(DBRef.new("test", nil))
end end
def test_save def test_save
@@coll.clear @@coll.remove
a = {"hello" => "world"} a = {"hello" => "world"}
@ -622,13 +622,13 @@ class DBAPITest < Test::Unit::TestCase
end end
def test_save_long def test_save_long
@@coll.clear @@coll.remove
@@coll.insert("x" => 9223372036854775807) @@coll.insert("x" => 9223372036854775807)
assert_equal 9223372036854775807, @@coll.find_one()["x"] assert_equal 9223372036854775807, @@coll.find_one()["x"]
end end
def test_find_by_oid def test_find_by_oid
@@coll.clear @@coll.remove
@@coll.save("hello" => "mike") @@coll.save("hello" => "mike")
id = @@coll.save("hello" => "world") id = @@coll.save("hello" => "world")
@ -644,7 +644,7 @@ class DBAPITest < Test::Unit::TestCase
end end
def test_save_with_object_that_has_id_but_does_not_actually_exist_in_collection def test_save_with_object_that_has_id_but_does_not_actually_exist_in_collection
@@coll.clear @@coll.remove
a = {'_id' => '1', 'hello' => 'world'} a = {'_id' => '1', 'hello' => 'world'}
@@coll.save(a) @@coll.save(a)
@ -658,7 +658,7 @@ class DBAPITest < Test::Unit::TestCase
end end
def test_invalid_key_names def test_invalid_key_names
@@coll.clear @@coll.remove
@@coll.insert({"hello" => "world"}) @@coll.insert({"hello" => "world"})
@@coll.insert({"hello" => {"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 "UTF-8", utf8.encoding.name
assert_equal "ISO-8859-1", iso8859.encoding.name assert_equal "ISO-8859-1", iso8859.encoding.name
@@coll.clear @@coll.remove
@@coll.save("ascii" => ascii, "utf8" => utf8, "iso8859" => iso8859) @@coll.save("ascii" => ascii, "utf8" => utf8, "iso8859" => iso8859)
doc = @@coll.find_one() doc = @@coll.find_one()

View File

@ -12,7 +12,7 @@ class DBConnectionTest < Test::Unit::TestCase
port = ENV['MONGO_RUBY_DRIVER_PORT'] || Connection::DEFAULT_PORT port = ENV['MONGO_RUBY_DRIVER_PORT'] || Connection::DEFAULT_PORT
db = Connection.new(host, port).db('ruby-mongo-demo') db = Connection.new(host, port).db('ruby-mongo-demo')
coll = db.collection('test') coll = db.collection('test')
coll.clear coll.remove
db.error db.error
end end
end end

View File

@ -14,14 +14,14 @@ class GridStoreTest < Test::Unit::TestCase
@@chunks = @@db.collection('fs.chunks') @@chunks = @@db.collection('fs.chunks')
def setup def setup
@@chunks.clear @@chunks.remove
@@files.clear @@files.remove
GridStore.open(@@db, 'foobar', 'w') { |f| f.write("hello, world!") } GridStore.open(@@db, 'foobar', 'w') { |f| f.write("hello, world!") }
end end
def teardown def teardown
@@chunks.clear @@chunks.remove
@@files.clear @@files.remove
@@db.error @@db.error
end end
@ -106,8 +106,8 @@ class GridStoreTest < Test::Unit::TestCase
end end
def test_multi_chunk def test_multi_chunk
@@chunks.clear @@chunks.remove
@@files.clear @@files.remove
size = 512 size = 512
GridStore.open(@@db, 'biggie', 'w') { |f| GridStore.open(@@db, 'biggie', 'w') { |f|
@ -167,8 +167,8 @@ class GridStoreTest < Test::Unit::TestCase
end end
def test_save_empty_file def test_save_empty_file
@@chunks.clear @@chunks.remove
@@files.clear @@files.remove
GridStore.open(@@db, 'empty', 'w') {} # re-write with zero bytes GridStore.open(@@db, 'empty', 'w') {} # re-write with zero bytes
assert_equal 1, @@files.count assert_equal 1, @@files.count
assert_equal 0, @@chunks.count assert_equal 0, @@chunks.count

25
test/test_helper.rb Normal file
View File

@ -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 <<MSG
This test suite now requires shoulda and mocha.
You can install these gems as follows:
gem install shoulda
gem install mocha
MSG
exit
end
require 'mongo'
# NOTE: most tests assume that MongoDB is running.
class Test::Unit::TestCase
include Mongo
end

View File

@ -54,7 +54,7 @@ class ObjectIDTest < Test::Unit::TestCase
db = Connection.new(host, port).db('ruby-mongo-test') db = Connection.new(host, port).db('ruby-mongo-test')
coll = db.collection('test') coll = db.collection('test')
coll.clear coll.remove
coll << {'a' => 1, '_id' => @o} coll << {'a' => 1, '_id' => @o}
row = coll.find().collect.first row = coll.find().collect.first

View File

@ -12,7 +12,7 @@ class TestThreading < Test::Unit::TestCase
@@coll = @@db.collection('thread-test-collection') @@coll = @@db.collection('thread-test-collection')
def test_threading def test_threading
@@coll.clear @@coll.remove
1000.times do |i| 1000.times do |i|
@@coll.insert("x" => i) @@coll.insert("x" => i)