Create one DB object per test suite, not per test.

This commit is contained in:
Jim Menard 2009-02-05 10:10:41 -05:00
parent d5f9f024ed
commit 8c1b72b2b9
6 changed files with 267 additions and 273 deletions

View File

@ -7,24 +7,21 @@ class AdminTest < Test::Unit::TestCase
include XGen::Mongo::Driver
@@db = Mongo.new(ENV['MONGO_RUBY_DRIVER_HOST'] || 'localhost',
ENV['MONGO_RUBY_DRIVER_PORT'] || Mongo::DEFAULT_PORT).db('ruby-mongo-test')
@@coll = @@db.collection('test')
def setup
host = ENV['MONGO_RUBY_DRIVER_HOST'] || 'localhost'
port = ENV['MONGO_RUBY_DRIVER_PORT'] || Mongo::DEFAULT_PORT
@db = Mongo.new(host, port).db('ruby-mongo-test')
# Insert some data to make sure the database itself exists.
@coll = @db.collection('test')
@coll.clear
@r1 = @coll.insert('a' => 1) # collection not created until it's used
@coll_full_name = 'ruby-mongo-test.test'
@admin = @db.admin
@@coll.clear
@r1 = @@coll.insert('a' => 1) # collection not created until it's used
@@coll_full_name = 'ruby-mongo-test.test'
@admin = @@db.admin
end
def teardown
if @db.connected?
@admin.profiling_level = :off
@coll.clear if @coll
@db.close
end
@admin.profiling_level = :off
@@coll.clear if @@coll
end
def test_default_profiling_level
@ -41,7 +38,7 @@ class AdminTest < Test::Unit::TestCase
def test_profiling_info
# Perform at least one query while profiling so we have something to see.
@admin.profiling_level = :all
@coll.find()
@@coll.find()
@admin.profiling_level = :off
info = @admin.profiling_info
@ -54,7 +51,7 @@ class AdminTest < Test::Unit::TestCase
end
def test_validate_collection
doc = @admin.validate_collection(@coll.name)
doc = @admin.validate_collection(@@coll.name)
assert_not_nil doc
result = doc['result']
assert_not_nil result

View File

@ -8,26 +8,22 @@ class ChunkTest < Test::Unit::TestCase
include XGen::Mongo::Driver
include XGen::Mongo::GridFS
@@db = Mongo.new(ENV['MONGO_RUBY_DRIVER_HOST'] || 'localhost',
ENV['MONGO_RUBY_DRIVER_PORT'] || Mongo::DEFAULT_PORT).db('ruby-mongo-utils-test')
@@files = @@db.collection('gridfs.files')
@@chunks = @@db.collection('gridfs.chunks')
def setup
@host = ENV['MONGO_RUBY_DRIVER_HOST'] || 'localhost'
@port = ENV['MONGO_RUBY_DRIVER_PORT'] || Mongo::DEFAULT_PORT
@db = Mongo.new(@host, @port).db('ruby-mongo-utils-test')
@@chunks.clear
@@files.clear
@files = @db.collection('gridfs.files')
@chunks = @db.collection('gridfs.chunks')
@chunks.clear
@files.clear
@f = GridStore.new(@db, 'foobar', 'w')
@f = GridStore.new(@@db, 'foobar', 'w')
@c = @f.instance_variable_get('@curr_chunk')
end
def teardown
if @db && @db.connected?
@chunks.clear
@files.clear
@db.close
end
@@chunks.clear
@@files.clear
end
def test_pos

View File

@ -7,25 +7,22 @@ class CursorTest < Test::Unit::TestCase
include XGen::Mongo::Driver
@@db = Mongo.new(ENV['MONGO_RUBY_DRIVER_HOST'] || 'localhost',
ENV['MONGO_RUBY_DRIVER_PORT'] || Mongo::DEFAULT_PORT).db('ruby-mongo-test')
@@coll = @@db.collection('test')
def setup
host = ENV['MONGO_RUBY_DRIVER_HOST'] || 'localhost'
port = ENV['MONGO_RUBY_DRIVER_PORT'] || Mongo::DEFAULT_PORT
@db = Mongo.new(host, port).db('ruby-mongo-test')
@coll = @db.collection('test')
@coll.clear
@r1 = @coll.insert('a' => 1) # collection not created until it's used
@coll_full_name = 'ruby-mongo-test.test'
@@coll.clear
@@coll.insert('a' => 1) # collection not created until it's used
@@coll_full_name = 'ruby-mongo-test.test'
end
def teardown
if @db.connected?
@coll.clear if @coll
@db.close
end
@@coll.clear
end
def test_explain
cursor = @coll.find('a' => 1)
cursor = @@coll.find('a' => 1)
explaination = cursor.explain
assert_not_nil explaination['cursor']
assert_kind_of Numeric, explaination['n']
@ -35,7 +32,7 @@ class CursorTest < Test::Unit::TestCase
def test_close_no_query_sent
begin
cursor = @coll.find('a' => 1)
cursor = @@coll.find('a' => 1)
cursor.close
assert cursor.closed?
rescue => ex
@ -45,7 +42,7 @@ class CursorTest < Test::Unit::TestCase
def test_close_after_query_sent
begin
cursor = @coll.find('a' => 1)
cursor = @@coll.find('a' => 1)
cursor.next_object
cursor.close
assert cursor.closed?

View File

@ -15,50 +15,53 @@ class DBTest < Test::Unit::TestCase
include XGen::Mongo::Driver
def setup
@host = ENV['MONGO_RUBY_DRIVER_HOST'] || 'localhost'
@port = ENV['MONGO_RUBY_DRIVER_PORT'] || Mongo::DEFAULT_PORT
@db = Mongo.new(@host, @port).db('ruby-mongo-test')
@@host = ENV['MONGO_RUBY_DRIVER_HOST'] || 'localhost'
@@port = ENV['MONGO_RUBY_DRIVER_PORT'] || Mongo::DEFAULT_PORT
@@db = Mongo.new(@@host, @@port).db('ruby-mongo-test')
@@users = @@db.collection('system.users')
def setup
@spongebob = 'spongebob'
@spongebob_password = 'squarepants'
@users = @db.collection('system.users')
@users.clear
@users.insert(:user => @spongebob, :pwd => @db.send(:hash_password, @spongebob, @spongebob_password))
@@users.clear
@@users.insert(:user => @spongebob, :pwd => @@db.send(:hash_password, @spongebob, @spongebob_password))
end
def teardown
if @db && @db.connected?
@users.clear if @users
@db.close
end
@@users.clear if @@users
end
def test_close
@db.close
assert !@db.connected?
@@db.close
assert !@@db.connected?
begin
@db.collection('test').insert('a' => 1)
@@db.collection('test').insert('a' => 1)
fail "expected 'NilClass' exception"
rescue => ex
assert_match /NilClass/, ex.to_s
ensure
@@db = Mongo.new(@@host, @@port).db('ruby-mongo-test')
@@users = @@db.collection('system.users')
end
end
def test_full_coll_name
coll = @db.collection('test')
assert_equal 'ruby-mongo-test.test', @db.full_coll_name(coll.name)
coll = @@db.collection('test')
assert_equal 'ruby-mongo-test.test', @@db.full_coll_name(coll.name)
end
def test_pair
@db.close
@users = nil
@db = Mongo.new({:left => "this-should-fail", :right => [@host, @port]}).db('ruby-mongo-test')
assert @db.connected?
@@db.close
@@users = nil
@@db = Mongo.new({:left => "this-should-fail", :right => [@@host, @@port]}).db('ruby-mongo-test')
assert @@db.connected?
ensure
@@db = Mongo.new(@@host, @@port) unless @@db.connected?
@@users = @@db.collection('system.users')
end
def test_pk_factory
db = Mongo.new(@host, @port).db('ruby-mongo-test', :pk => TestPKFactory.new)
db = Mongo.new(@@host, @@port).db('ruby-mongo-test', :pk => TestPKFactory.new)
coll = db.collection('test')
coll.clear
@ -80,28 +83,31 @@ class DBTest < Test::Unit::TestCase
end
def test_pk_factory_reset
@db.pk_factory = Object.new # first time
db = Mongo.new(@@host, @@port).db('ruby-mongo-test')
db.pk_factory = Object.new # first time
begin
@db.pk_factory = Object.new
db.pk_factory = Object.new
fail "error: expected exception"
rescue => ex
assert_match /can not change PK factory/, ex.to_s
ensure
db.close
end
end
def test_authenticate
assert !@db.authenticate('nobody', 'nopassword')
assert !@db.authenticate(@spongebob, 'squareliederhosen')
assert @db.authenticate(@spongebob, @spongebob_password)
assert !@@db.authenticate('nobody', 'nopassword')
assert !@@db.authenticate(@spongebob, 'squareliederhosen')
assert @@db.authenticate(@spongebob, @spongebob_password)
end
def test_logout
@db.logout # only testing that we don't throw exception
@@db.logout # only testing that we don't throw exception
end
def test_auto_connect
@db.close
db = Mongo.new(@host, @port, :auto_reconnect => true).db('ruby-mongo-test')
@@db.close
db = Mongo.new(@@host, @@port, :auto_reconnect => true).db('ruby-mongo-test')
assert db.connected?
assert db.auto_reconnect?
db.close
@ -109,22 +115,25 @@ class DBTest < Test::Unit::TestCase
assert db.auto_reconnect?
db.collection('test').insert('a' => 1)
assert db.connected?
ensure
@@db = Mongo.new(@@host, @@port).db('ruby-mongo-test')
@@users = @@db.collection('system.users')
end
def test_error
doc = @db.send(:db_command, :forceerror => 1)
assert @db.error?
err = @db.error
doc = @@db.send(:db_command, :forceerror => 1)
assert @@db.error?
err = @@db.error
assert_match /forced error/, err
# ask again
assert @db.error?
err2 = @db.error
assert @@db.error?
err2 = @@db.error
assert_equal err, err2
end
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
db.collection('users').clear
end

View File

@ -7,51 +7,48 @@ class DBAPITest < Test::Unit::TestCase
include XGen::Mongo::Driver
@@db = Mongo.new(ENV['MONGO_RUBY_DRIVER_HOST'] || 'localhost',
ENV['MONGO_RUBY_DRIVER_PORT'] || Mongo::DEFAULT_PORT).db('ruby-mongo-test')
@@coll = @@db.collection('test')
def setup
host = ENV['MONGO_RUBY_DRIVER_HOST'] || 'localhost'
port = ENV['MONGO_RUBY_DRIVER_PORT'] || Mongo::DEFAULT_PORT
@db = Mongo.new(host, port).db('ruby-mongo-test')
@coll = @db.collection('test')
@coll.clear
@r1 = @coll.insert('a' => 1) # collection not created until it's used
@coll_full_name = 'ruby-mongo-test.test'
@@coll.clear
@r1 = @@coll.insert('a' => 1) # collection not created until it's used
@@coll_full_name = 'ruby-mongo-test.test'
end
def teardown
if @db.connected?
@coll.clear unless @coll == nil
@db.close
end
@@coll.clear
end
def test_clear
assert_equal 1, @coll.count
@coll.clear
assert_equal 0, @coll.count
assert_equal 1, @@coll.count
@@coll.clear
assert_equal 0, @@coll.count
end
def test_insert
@coll.insert('a' => 2)
@coll.insert('b' => 3)
@@coll.insert('a' => 2)
@@coll.insert('b' => 3)
assert_equal 3, @coll.count
docs = @coll.find().to_a
assert_equal 3, @@coll.count
docs = @@coll.find().to_a
assert_equal 3, docs.length
assert docs.detect { |row| row['a'] == 1 }
assert docs.detect { |row| row['a'] == 2 }
assert docs.detect { |row| row['b'] == 3 }
@coll << {'b' => 4}
docs = @coll.find().to_a
@@coll << {'b' => 4}
docs = @@coll.find().to_a
assert_equal 4, docs.length
assert docs.detect { |row| row['b'] == 4 }
end
def test_insert_multiple
@coll.insert({'a' => 2}, {'b' => 3})
@@coll.insert({'a' => 2}, {'b' => 3})
assert_equal 3, @coll.count
docs = @coll.find().to_a
assert_equal 3, @@coll.count
docs = @@coll.find().to_a
assert_equal 3, docs.length
assert docs.detect { |row| row['a'] == 1 }
assert docs.detect { |row| row['a'] == 2 }
@ -59,15 +56,15 @@ class DBAPITest < Test::Unit::TestCase
end
def test_find_simple
@r2 = @coll.insert('a' => 2)
@r3 = @coll.insert('b' => 3)
@r2 = @@coll.insert('a' => 2)
@r3 = @@coll.insert('b' => 3)
# Check sizes
docs = @coll.find().to_a
docs = @@coll.find().to_a
assert_equal 3, docs.size
assert_equal 3, @coll.count
assert_equal 3, @@coll.count
# Find by other value
docs = @coll.find('a' => @r1['a']).to_a
docs = @@coll.find('a' => @r1['a']).to_a
assert_equal 1, docs.size
doc = docs.first
# Can't compare _id values because at insert, an _id was added to @r1 by
@ -78,58 +75,58 @@ class DBAPITest < Test::Unit::TestCase
end
def test_find_advanced
@coll.insert('a' => 2)
@coll.insert('b' => 3)
@@coll.insert('a' => 2)
@@coll.insert('b' => 3)
# Find by advanced query (less than)
docs = @coll.find('a' => { '$lt' => 10 }).to_a
docs = @@coll.find('a' => { '$lt' => 10 }).to_a
assert_equal 2, docs.size
assert docs.detect { |row| row['a'] == 1 }
assert docs.detect { |row| row['a'] == 2 }
# Find by advanced query (greater than)
docs = @coll.find('a' => { '$gt' => 1 }).to_a
docs = @@coll.find('a' => { '$gt' => 1 }).to_a
assert_equal 1, docs.size
assert docs.detect { |row| row['a'] == 2 }
# Find by advanced query (less than or equal to)
docs = @coll.find('a' => { '$lte' => 1 }).to_a
docs = @@coll.find('a' => { '$lte' => 1 }).to_a
assert_equal 1, docs.size
assert docs.detect { |row| row['a'] == 1 }
# Find by advanced query (greater than or equal to)
docs = @coll.find('a' => { '$gte' => 1 }).to_a
docs = @@coll.find('a' => { '$gte' => 1 }).to_a
assert_equal 2, docs.size
assert docs.detect { |row| row['a'] == 1 }
assert docs.detect { |row| row['a'] == 2 }
# Find by advanced query (between)
docs = @coll.find('a' => { '$gt' => 1, '$lt' => 3 }).to_a
docs = @@coll.find('a' => { '$gt' => 1, '$lt' => 3 }).to_a
assert_equal 1, docs.size
assert docs.detect { |row| row['a'] == 2 }
# Find by advanced query (in clause)
docs = @coll.find('a' => {'$in' => [1,2]}).to_a
docs = @@coll.find('a' => {'$in' => [1,2]}).to_a
assert_equal 2, docs.size
assert docs.detect { |row| row['a'] == 1 }
assert docs.detect { |row| row['a'] == 2 }
# Find by advanced query (regexp)
docs = @coll.find('a' => /[1|2]/).to_a
docs = @@coll.find('a' => /[1|2]/).to_a
assert_equal 2, docs.size
assert docs.detect { |row| row['a'] == 1 }
assert docs.detect { |row| row['a'] == 2 }
end
def test_find_sorting
@coll.clear
@coll.insert('a' => 1, 'b' => 2)
@coll.insert('a' => 2, 'b' => 1)
@coll.insert('a' => 3, 'b' => 2)
@coll.insert('a' => 4, 'b' => 1)
@@coll.clear
@@coll.insert('a' => 1, 'b' => 2)
@@coll.insert('a' => 2, 'b' => 1)
@@coll.insert('a' => 3, 'b' => 2)
@@coll.insert('a' => 4, 'b' => 1)
# Sorting (ascending)
docs = @coll.find({'a' => { '$lt' => 10 }}, :sort => {'a' => 1}).to_a
docs = @@coll.find({'a' => { '$lt' => 10 }}, :sort => {'a' => 1}).to_a
assert_equal 4, docs.size
assert_equal 1, docs[0]['a']
assert_equal 2, docs[1]['a']
@ -137,7 +134,7 @@ class DBAPITest < Test::Unit::TestCase
assert_equal 4, docs[3]['a']
# Sorting (descending)
docs = @coll.find({'a' => { '$lt' => 10 }}, :sort => {'a' => -1}).to_a
docs = @@coll.find({'a' => { '$lt' => 10 }}, :sort => {'a' => -1}).to_a
assert_equal 4, docs.size
assert_equal 4, docs[0]['a']
assert_equal 3, docs[1]['a']
@ -145,7 +142,7 @@ class DBAPITest < Test::Unit::TestCase
assert_equal 1, docs[3]['a']
# Sorting using array of names; assumes ascending order.
docs = @coll.find({'a' => { '$lt' => 10 }}, :sort => ['a']).to_a
docs = @@coll.find({'a' => { '$lt' => 10 }}, :sort => ['a']).to_a
assert_equal 4, docs.size
assert_equal 1, docs[0]['a']
assert_equal 2, docs[1]['a']
@ -153,14 +150,14 @@ class DBAPITest < Test::Unit::TestCase
assert_equal 4, docs[3]['a']
# Sorting using single name; assumes ascending order.
docs = @coll.find({'a' => { '$lt' => 10 }}, :sort => 'a').to_a
docs = @@coll.find({'a' => { '$lt' => 10 }}, :sort => 'a').to_a
assert_equal 4, docs.size
assert_equal 1, docs[0]['a']
assert_equal 2, docs[1]['a']
assert_equal 3, docs[2]['a']
assert_equal 4, docs[3]['a']
docs = @coll.find({'a' => { '$lt' => 10 }}, :sort => ['b', 'a']).to_a
docs = @@coll.find({'a' => { '$lt' => 10 }}, :sort => ['b', 'a']).to_a
assert_equal 4, docs.size
assert_equal 2, docs[0]['a']
assert_equal 4, docs[1]['a']
@ -169,19 +166,19 @@ class DBAPITest < Test::Unit::TestCase
# Sorting using empty array; no order guarantee (Mongo bug #898) but
# should not blow up.
docs = @coll.find({'a' => { '$lt' => 10 }}, :sort => []).to_a
docs = @@coll.find({'a' => { '$lt' => 10 }}, :sort => []).to_a
assert_equal 4, docs.size
# Sorting using array of hashes; no order guarantee (Mongo bug #898) but
# should not blow up.
docs = @coll.find({'a' => { '$lt' => 10 }}, :sort => [{'b' => 1}, {'a' => -1}]).to_a
docs = @@coll.find({'a' => { '$lt' => 10 }}, :sort => [{'b' => 1}, {'a' => -1}]).to_a
assert_equal 4, docs.size
# Sorting using ordered hash. You can use an unordered one, but then the
# order of the keys won't be guaranteed thus your sort won't make sense.
oh = OrderedHash.new
oh['a'] = -1
docs = @coll.find({'a' => { '$lt' => 10 }}, :sort => oh).to_a
docs = @@coll.find({'a' => { '$lt' => 10 }}, :sort => oh).to_a
assert_equal 4, docs.size
assert_equal 4, docs[0]['a']
assert_equal 3, docs[1]['a']
@ -192,7 +189,7 @@ class DBAPITest < Test::Unit::TestCase
# oh = OrderedHash.new
# oh['b'] = -1
# oh['a'] = 1
# docs = @coll.find({'a' => { '$lt' => 10 }}, :sort => oh).to_a
# docs = @@coll.find({'a' => { '$lt' => 10 }}, :sort => oh).to_a
# assert_equal 4, docs.size
# assert_equal 1, docs[0]['a']
# assert_equal 3, docs[1]['a']
@ -201,72 +198,73 @@ class DBAPITest < Test::Unit::TestCase
end
def test_find_limits
@coll.insert('b' => 2)
@coll.insert('c' => 3)
@coll.insert('d' => 4)
@@coll.insert('b' => 2)
@@coll.insert('c' => 3)
@@coll.insert('d' => 4)
docs = @coll.find({}, :limit => 1).to_a
docs = @@coll.find({}, :limit => 1).to_a
assert_equal 1, docs.size
docs = @coll.find({}, :limit => 2).to_a
docs = @@coll.find({}, :limit => 2).to_a
assert_equal 2, docs.size
docs = @coll.find({}, :limit => 3).to_a
docs = @@coll.find({}, :limit => 3).to_a
assert_equal 3, docs.size
docs = @coll.find({}, :limit => 4).to_a
docs = @@coll.find({}, :limit => 4).to_a
assert_equal 4, docs.size
docs = @coll.find({}).to_a
docs = @@coll.find({}).to_a
assert_equal 4, docs.size
docs = @coll.find({}, :limit => 99).to_a
docs = @@coll.find({}, :limit => 99).to_a
assert_equal 4, docs.size
end
def test_drop_collection
assert @db.drop_collection(@coll.name), "drop of collection #{@coll.name} failed"
assert !@db.collection_names.include?(@coll_full_name)
@coll = nil
assert @@db.drop_collection(@@coll.name), "drop of collection #{@@coll.name} failed"
assert !@@db.collection_names.include?(@@coll_full_name)
end
def test_collection_names
names = @db.collection_names
names = @@db.collection_names
assert names.length >= 1
assert names.include?(@coll_full_name)
assert names.include?(@@coll_full_name)
coll2 = @db.collection('test2')
coll2 = @@db.collection('test2')
coll2.insert('a' => 1) # collection not created until it's used
names = @db.collection_names
names = @@db.collection_names
assert names.length >= 2
assert names.include?(@coll_full_name)
assert names.include?(@@coll_full_name)
assert names.include?('ruby-mongo-test.test2')
ensure
@db.drop_collection('test2')
@@db.drop_collection('test2')
end
def test_collections_info
cursor = @db.collections_info
cursor = @@db.collections_info
rows = cursor.to_a
assert rows.length >= 1
row = rows.detect { |r| r['name'] == @coll_full_name }
row = rows.detect { |r| r['name'] == @@coll_full_name }
assert_not_nil row
end
def test_collection_options
@db.drop_collection('foobar')
@db.strict = true
@@db.drop_collection('foobar')
@@db.strict = true
begin
coll = @db.create_collection('foobar', :capped => true, :size => 1024)
coll = @@db.create_collection('foobar', :capped => true, :size => 1024)
options = coll.options()
assert_equal 'foobar', options['create']
assert_equal true, options['capped']
assert_equal 1024, options['size']
rescue => ex
@db.drop_collection('foobar')
@@db.drop_collection('foobar')
fail "did not expect exception \"#{ex}\""
ensure
@@db.strict = false
end
end
def test_index_information
@db.create_index(@coll.name, 'index_name', ['a'])
list = @db.index_information(@coll.name)
@@db.create_index(@@coll.name, 'index_name', ['a'])
list = @@db.index_information(@@coll.name)
assert_equal 1, list.length
info = list[0]
@ -275,16 +273,16 @@ class DBAPITest < Test::Unit::TestCase
end
def test_array
@coll << {'b' => [1, 2, 3]}
rows = @coll.find({}, {:fields => ['b']}).to_a
@@coll << {'b' => [1, 2, 3]}
rows = @@coll.find({}, {:fields => ['b']}).to_a
assert_equal 1, rows.length
assert_equal [1, 2, 3], rows[0]['b']
end
def test_regex
regex = /foobar/i
@coll << {'b' => regex}
rows = @coll.find({}, {:fields => ['b']}).to_a
@@coll << {'b' => regex}
rows = @@coll.find({}, {:fields => ['b']}).to_a
assert_equal 1, rows.length
assert_equal regex, rows[0]['b']
end
@ -293,37 +291,39 @@ class DBAPITest < Test::Unit::TestCase
# Note: can't use Time.new because that will include fractional seconds,
# which Mongo does not store.
t = Time.at(1234567890)
@coll << {'_id' => t}
rows = @coll.find({'_id' => t}).to_a
@@coll << {'_id' => t}
rows = @@coll.find({'_id' => t}).to_a
assert_equal 1, rows.length
assert_equal t, rows[0]['_id']
end
def test_strict
assert !@db.strict?
@db.strict = true
assert @db.strict?
assert !@@db.strict?
@@db.strict = true
assert @@db.strict?
ensure
@@db.strict = false
end
def test_strict_access_collection
@db.strict = true
@@db.strict = true
begin
@db.collection('does-not-exist')
@@db.collection('does-not-exist')
fail "expected exception"
rescue => ex
assert_equal "Collection does-not-exist doesn't exist. Currently in strict mode.", ex.to_s
ensure
@db.strict = false
@db.drop_collection('does-not-exist')
@@db.strict = false
@@db.drop_collection('does-not-exist')
end
end
def test_strict_create_collection
@db.drop_collection('foobar')
@db.strict = true
@@db.drop_collection('foobar')
@@db.strict = true
begin
@db.create_collection('foobar')
@@db.create_collection('foobar')
assert true
rescue => ex
fail "did not expect exception \"#{ex}\""
@ -331,18 +331,18 @@ class DBAPITest < Test::Unit::TestCase
# Now the collection exists. This time we should see an exception.
begin
@db.create_collection('foobar')
@@db.create_collection('foobar')
fail "expected exception"
rescue => ex
assert_equal "Collection foobar already exists. Currently in strict mode.", ex.to_s
ensure
@db.strict = false
@db.drop_collection('foobar')
@@db.strict = false
@@db.drop_collection('foobar')
end
end
def test_to_a
cursor = @coll.find()
cursor = @@coll.find()
rows = cursor.to_a
# Make sure we get back exactly the same array the next time we ask
@ -357,7 +357,7 @@ class DBAPITest < Test::Unit::TestCase
end
def test_to_a_after_each
cursor = @coll.find
cursor = @@coll.find
cursor.each { |row| row }
begin
cursor.to_a
@ -368,40 +368,40 @@ class DBAPITest < Test::Unit::TestCase
end
def test_ismaster
assert @db.master?
assert @@db.master?
end
def test_master
assert_equal "#{@db.host}:#{@db.port}", @db.master
assert_equal "#{@@db.host}:#{@@db.port}", @@db.master
end
def test_hint
@coll.create_index('test_a_index', 'a')
@@coll.create_index('test_a_index', 'a')
begin
assert_nil @coll.hint
assert_equal 1, @coll.find({'a' => 1}, :hint => 'a').to_a.size
assert_equal 1, @coll.find({'a' => 1}, :hint => ['a']).to_a.size
assert_equal 1, @coll.find({'a' => 1}, :hint => {'a' => 1}).to_a.size
assert_nil @@coll.hint
assert_equal 1, @@coll.find({'a' => 1}, :hint => 'a').to_a.size
assert_equal 1, @@coll.find({'a' => 1}, :hint => ['a']).to_a.size
assert_equal 1, @@coll.find({'a' => 1}, :hint => {'a' => 1}).to_a.size
@coll.hint = 'a'
assert_equal({'a' => 1}, @coll.hint)
assert_equal 1, @coll.find('a' => 1).to_a.size
@@coll.hint = 'a'
assert_equal({'a' => 1}, @@coll.hint)
assert_equal 1, @@coll.find('a' => 1).to_a.size
@coll.hint = ['a']
assert_equal({'a' => 1}, @coll.hint)
assert_equal 1, @coll.find('a' => 1).to_a.size
@@coll.hint = ['a']
assert_equal({'a' => 1}, @@coll.hint)
assert_equal 1, @@coll.find('a' => 1).to_a.size
@coll.hint = {'a' => 1}
assert_equal({'a' => 1}, @coll.hint)
assert_equal 1, @coll.find('a' => 1).to_a.size
@@coll.hint = {'a' => 1}
assert_equal({'a' => 1}, @@coll.hint)
assert_equal 1, @@coll.find('a' => 1).to_a.size
@coll.hint = nil
assert_nil @coll.hint
assert_equal 1, @coll.find('a' => 1).to_a.size
@@coll.hint = nil
assert_nil @@coll.hint
assert_equal 1, @@coll.find('a' => 1).to_a.size
rescue => ex
fail ex.to_s
ensure
@coll.drop_index('test_a_index')
@@coll.drop_index('test_a_index')
end
end
@ -410,11 +410,11 @@ class DBAPITest < Test::Unit::TestCase
# def test_insert_undefined
# doc = {'undef' => Undefined.new}
# @coll.clear
# @coll.insert(doc)
# p @db.error # DEBUG
# assert_equal 1, @coll.count
# row = @coll.find().next_object
# @@coll.clear
# @@coll.insert(doc)
# p @@db.error # DEBUG
# assert_equal 1, @@coll.count
# row = @@coll.find().next_object
# assert_not_nil row
# end

View File

@ -8,35 +8,30 @@ class GridStoreTest < Test::Unit::TestCase
include XGen::Mongo::Driver
include XGen::Mongo::GridFS
@@db = Mongo.new(ENV['MONGO_RUBY_DRIVER_HOST'] || 'localhost',
ENV['MONGO_RUBY_DRIVER_PORT'] || Mongo::DEFAULT_PORT).db('ruby-mongo-test')
@@files = @@db.collection('gridfs.files')
@@chunks = @@db.collection('gridfs.chunks')
def setup
@host = ENV['MONGO_RUBY_DRIVER_HOST'] || 'localhost'
@port = ENV['MONGO_RUBY_DRIVER_PORT'] || Mongo::DEFAULT_PORT
@db = Mongo.new(@host, @port).db('ruby-mongo-utils-test')
@files = @db.collection('gridfs.files')
@chunks = @db.collection('gridfs.chunks')
@chunks.clear
@files.clear
GridStore.open(@db, 'foobar', 'w') { |f| f.write("hello, world!") }
@@chunks.clear
@@files.clear
GridStore.open(@@db, 'foobar', 'w') { |f| f.write("hello, world!") }
end
def teardown
if @db && @db.connected?
@chunks.clear
@files.clear
@db.close
end
@@chunks.clear
@@files.clear
end
def test_exist
assert GridStore.exist?(@db, 'foobar')
assert !GridStore.exist?(@db, 'does_not_exist')
assert !GridStore.exist?(@db, 'foobar', 'another_root')
assert GridStore.exist?(@@db, 'foobar')
assert !GridStore.exist?(@@db, 'does_not_exist')
assert !GridStore.exist?(@@db, 'foobar', 'another_root')
end
def test_small_write
rows = @files.find({'filename' => 'foobar'}).to_a
rows = @@files.find({'filename' => 'foobar'}).to_a
assert_not_nil rows
assert_equal 1, rows.length
row = rows[0]
@ -44,37 +39,37 @@ class GridStoreTest < Test::Unit::TestCase
file_id = row['_id']
assert_kind_of ObjectID, file_id
rows = @chunks.find({'files_id' => file_id}).to_a
rows = @@chunks.find({'files_id' => file_id}).to_a
assert_not_nil rows
assert_equal 1, rows.length
end
def test_small_file
rows = @files.find({'filename' => 'foobar'}).to_a
rows = @@files.find({'filename' => 'foobar'}).to_a
assert_not_nil rows
assert_equal 1, rows.length
row = rows[0]
assert_not_nil row
assert_equal "hello, world!", GridStore.read(@db, 'foobar')
assert_equal "hello, world!", GridStore.read(@@db, 'foobar')
end
def test_overwrite
GridStore.open(@db, 'foobar', 'w') { |f| f.write("overwrite") }
assert_equal "overwrite", GridStore.read(@db, 'foobar')
GridStore.open(@@db, 'foobar', 'w') { |f| f.write("overwrite") }
assert_equal "overwrite", GridStore.read(@@db, 'foobar')
end
def test_read_length
assert_equal "hello", GridStore.read(@db, 'foobar', 5)
assert_equal "hello", GridStore.read(@@db, 'foobar', 5)
end
# Also tests seek
def test_read_with_offset
assert_equal "world", GridStore.read(@db, 'foobar', 5, 7)
assert_equal "world!", GridStore.read(@db, 'foobar', nil, 7)
assert_equal "world", GridStore.read(@@db, 'foobar', 5, 7)
assert_equal "world!", GridStore.read(@@db, 'foobar', nil, 7)
end
def test_seek
GridStore.open(@db, 'foobar', 'r') { |f|
GridStore.open(@@db, 'foobar', 'r') { |f|
f.seek(0)
assert_equal 'h', f.getc.chr
f.seek(7)
@ -100,84 +95,84 @@ class GridStoreTest < Test::Unit::TestCase
end
def test_multi_chunk
@chunks.clear
@files.clear
@@chunks.clear
@@files.clear
size = 512
GridStore.open(@db, 'biggie', 'w') { |f|
GridStore.open(@@db, 'biggie', 'w') { |f|
f.chunk_size = size
f.write('x' * size)
f.write('y' * size)
f.write('z' * size)
}
assert_equal 3, @chunks.count
assert_equal ('x' * size) + ('y' * size) + ('z' * size), GridStore.read(@db, 'biggie')
assert_equal 3, @@chunks.count
assert_equal ('x' * size) + ('y' * size) + ('z' * size), GridStore.read(@@db, 'biggie')
end
def test_puts_and_readlines
GridStore.open(@db, 'multiline', 'w') { |f|
GridStore.open(@@db, 'multiline', 'w') { |f|
f.puts "line one"
f.puts "line two\n"
f.puts "line three"
}
lines = GridStore.readlines(@db, 'multiline')
lines = GridStore.readlines(@@db, 'multiline')
assert_equal ["line one\n", "line two\n", "line three\n"], lines
end
def test_unlink
assert_equal 1, @files.count
assert_equal 1, @chunks.count
GridStore.unlink(@db, 'foobar')
assert_equal 0, @files.count
assert_equal 0, @chunks.count
assert_equal 1, @@files.count
assert_equal 1, @@chunks.count
GridStore.unlink(@@db, 'foobar')
assert_equal 0, @@files.count
assert_equal 0, @@chunks.count
end
def test_append
GridStore.open(@db, 'foobar', 'w+') { |f| f.write(" how are you?") }
assert_equal 1, @chunks.count
assert_equal "hello, world! how are you?", GridStore.read(@db, 'foobar')
GridStore.open(@@db, 'foobar', 'w+') { |f| f.write(" how are you?") }
assert_equal 1, @@chunks.count
assert_equal "hello, world! how are you?", GridStore.read(@@db, 'foobar')
end
def test_rewind_and_truncate_on_write
GridStore.open(@db, 'foobar', 'w') { |f|
GridStore.open(@@db, 'foobar', 'w') { |f|
f.write("some text is inserted here")
f.rewind
f.write("abc")
}
assert_equal "abc", GridStore.read(@db, 'foobar')
assert_equal "abc", GridStore.read(@@db, 'foobar')
end
def test_tell
GridStore.open(@db, 'foobar', 'r') { |f|
GridStore.open(@@db, 'foobar', 'r') { |f|
f.read(5)
assert_equal 5, f.tell
}
end
def test_empty_block_ok
GridStore.open(@db, 'empty', 'w')
GridStore.open(@@db, 'empty', 'w')
end
def test_save_empty_file
@chunks.clear
@files.clear
GridStore.open(@db, 'empty', 'w') {} # re-write with zero bytes
assert_equal 1, @files.count
assert_equal 0, @chunks.count
@@chunks.clear
@@files.clear
GridStore.open(@@db, 'empty', 'w') {} # re-write with zero bytes
assert_equal 1, @@files.count
assert_equal 0, @@chunks.count
end
def test_empty_file_eof
GridStore.open(@db, 'empty', 'w')
GridStore.open(@db, 'empty', 'r') { |f|
GridStore.open(@@db, 'empty', 'w')
GridStore.open(@@db, 'empty', 'r') { |f|
assert f.eof?
}
end
def test_cannot_change_chunk_size_on_read
begin
GridStore.open(@db, 'foobar', 'r') { |f| f.chunk_size = 42 }
GridStore.open(@@db, 'foobar', 'r') { |f| f.chunk_size = 42 }
fail "should have seen error"
rescue => ex
assert_match /error: can only change chunk size/, ex.to_s
@ -186,7 +181,7 @@ class GridStoreTest < Test::Unit::TestCase
def test_cannot_change_chunk_size_after_data_written
begin
GridStore.open(@db, 'foobar', 'w') { |f|
GridStore.open(@@db, 'foobar', 'w') { |f|
f.write("some text")
f.chunk_size = 42
}
@ -197,18 +192,18 @@ class GridStoreTest < Test::Unit::TestCase
end
def test_change_chunk_size
GridStore.open(@db, 'new-file', 'w') { |f|
GridStore.open(@@db, 'new-file', 'w') { |f|
f.chunk_size = 42
f.write("foo")
}
GridStore.open(@db, 'new-file', 'r') { |f|
GridStore.open(@@db, 'new-file', 'r') { |f|
assert f.chunk_size == 42
}
end
def test_chunk_size_in_option
GridStore.open(@db, 'new-file', 'w', :chunk_size => 42) { |f| f.write("foo") }
GridStore.open(@db, 'new-file', 'r') { |f|
GridStore.open(@@db, 'new-file', 'w', :chunk_size => 42) { |f| f.write("foo") }
GridStore.open(@@db, 'new-file', 'r') { |f|
assert f.chunk_size == 42
}
end
@ -216,46 +211,46 @@ class GridStoreTest < Test::Unit::TestCase
def test_upload_date
now = Time.now
orig_file_upload_date = nil
GridStore.open(@db, 'foobar', 'r') { |f| orig_file_upload_date = f.upload_date }
GridStore.open(@@db, 'foobar', 'r') { |f| orig_file_upload_date = f.upload_date }
assert_not_nil orig_file_upload_date
assert (orig_file_upload_date - now) < 5 # even a really slow system < 5 secs
sleep(2)
GridStore.open(@db, 'foobar', 'w') { |f| f.write "new data" }
GridStore.open(@@db, 'foobar', 'w') { |f| f.write "new data" }
file_upload_date = nil
GridStore.open(@db, 'foobar', 'r') { |f| file_upload_date = f.upload_date }
GridStore.open(@@db, 'foobar', 'r') { |f| file_upload_date = f.upload_date }
assert_equal orig_file_upload_date, file_upload_date
end
def test_content_type
ct = nil
GridStore.open(@db, 'foobar', 'r') { |f| ct = f.content_type }
GridStore.open(@@db, 'foobar', 'r') { |f| ct = f.content_type }
assert_equal GridStore::DEFAULT_CONTENT_TYPE, ct
GridStore.open(@db, 'foobar', 'w+') { |f| f.content_type = 'text/html' }
GridStore.open(@@db, 'foobar', 'w+') { |f| f.content_type = 'text/html' }
ct2 = nil
GridStore.open(@db, 'foobar', 'r') { |f| ct2 = f.content_type }
GridStore.open(@@db, 'foobar', 'r') { |f| ct2 = f.content_type }
assert_equal 'text/html', ct2
end
def test_content_type_option
GridStore.open(@db, 'new-file', 'w', :content_type => 'image/jpg') { |f| f.write('foo') }
GridStore.open(@@db, 'new-file', 'w', :content_type => 'image/jpg') { |f| f.write('foo') }
ct = nil
GridStore.open(@db, 'new-file', 'r') { |f| ct = f.content_type }
GridStore.open(@@db, 'new-file', 'r') { |f| ct = f.content_type }
assert_equal 'image/jpg', ct
end
def test_unknown_mode
GridStore.open(@db, 'foobar', 'x')
GridStore.open(@@db, 'foobar', 'x')
fail 'should have seen "illegal mode" error raised'
rescue => ex
assert_equal "error: illegal mode x", ex.to_s
end
def test_metadata
GridStore.open(@db, 'foobar', 'r') { |f| assert_nil f.metadata }
GridStore.open(@db, 'foobar', 'w+') { |f| f.metadata = {'a' => 1} }
GridStore.open(@db, 'foobar', 'r') { |f| assert_equal({'a' => 1}, f.metadata) }
GridStore.open(@@db, 'foobar', 'r') { |f| assert_nil f.metadata }
GridStore.open(@@db, 'foobar', 'w+') { |f| f.metadata = {'a' => 1} }
GridStore.open(@@db, 'foobar', 'r') { |f| assert_equal({'a' => 1}, f.metadata) }
end
end