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')
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

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
# 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"

View File

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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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
@ -324,4 +318,31 @@ class TestCollection < Test::Unit::TestCase
# 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

View File

@ -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')

View File

@ -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|

View File

@ -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

View File

@ -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()

View File

@ -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

View File

@ -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

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')
coll = db.collection('test')
coll.clear
coll.remove
coll << {'a' => 1, '_id' => @o}
row = coll.find().collect.first

View File

@ -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)