Create one DB object per test suite, not per test.
This commit is contained in:
parent
d5f9f024ed
commit
8c1b72b2b9
@ -7,24 +7,21 @@ class AdminTest < Test::Unit::TestCase
|
|||||||
|
|
||||||
include XGen::Mongo::Driver
|
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
|
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.
|
# Insert some data to make sure the database itself exists.
|
||||||
@coll = @db.collection('test')
|
@@coll.clear
|
||||||
@coll.clear
|
@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
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def teardown
|
def teardown
|
||||||
if @db.connected?
|
@admin.profiling_level = :off
|
||||||
@admin.profiling_level = :off
|
@@coll.clear if @@coll
|
||||||
@coll.clear if @coll
|
|
||||||
@db.close
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_default_profiling_level
|
def test_default_profiling_level
|
||||||
@ -41,7 +38,7 @@ class AdminTest < Test::Unit::TestCase
|
|||||||
def test_profiling_info
|
def test_profiling_info
|
||||||
# Perform at least one query while profiling so we have something to see.
|
# Perform at least one query while profiling so we have something to see.
|
||||||
@admin.profiling_level = :all
|
@admin.profiling_level = :all
|
||||||
@coll.find()
|
@@coll.find()
|
||||||
@admin.profiling_level = :off
|
@admin.profiling_level = :off
|
||||||
|
|
||||||
info = @admin.profiling_info
|
info = @admin.profiling_info
|
||||||
@ -54,7 +51,7 @@ class AdminTest < Test::Unit::TestCase
|
|||||||
end
|
end
|
||||||
|
|
||||||
def test_validate_collection
|
def test_validate_collection
|
||||||
doc = @admin.validate_collection(@coll.name)
|
doc = @admin.validate_collection(@@coll.name)
|
||||||
assert_not_nil doc
|
assert_not_nil doc
|
||||||
result = doc['result']
|
result = doc['result']
|
||||||
assert_not_nil result
|
assert_not_nil result
|
||||||
|
@ -8,26 +8,22 @@ class ChunkTest < Test::Unit::TestCase
|
|||||||
include XGen::Mongo::Driver
|
include XGen::Mongo::Driver
|
||||||
include XGen::Mongo::GridFS
|
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
|
def setup
|
||||||
@host = ENV['MONGO_RUBY_DRIVER_HOST'] || 'localhost'
|
@@chunks.clear
|
||||||
@port = ENV['MONGO_RUBY_DRIVER_PORT'] || Mongo::DEFAULT_PORT
|
@@files.clear
|
||||||
@db = Mongo.new(@host, @port).db('ruby-mongo-utils-test')
|
|
||||||
|
|
||||||
@files = @db.collection('gridfs.files')
|
@f = GridStore.new(@@db, 'foobar', 'w')
|
||||||
@chunks = @db.collection('gridfs.chunks')
|
|
||||||
@chunks.clear
|
|
||||||
@files.clear
|
|
||||||
|
|
||||||
@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
|
||||||
if @db && @db.connected?
|
@@chunks.clear
|
||||||
@chunks.clear
|
@@files.clear
|
||||||
@files.clear
|
|
||||||
@db.close
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_pos
|
def test_pos
|
||||||
|
@ -7,25 +7,22 @@ class CursorTest < Test::Unit::TestCase
|
|||||||
|
|
||||||
include XGen::Mongo::Driver
|
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
|
def setup
|
||||||
host = ENV['MONGO_RUBY_DRIVER_HOST'] || 'localhost'
|
@@coll.clear
|
||||||
port = ENV['MONGO_RUBY_DRIVER_PORT'] || Mongo::DEFAULT_PORT
|
@@coll.insert('a' => 1) # collection not created until it's used
|
||||||
@db = Mongo.new(host, port).db('ruby-mongo-test')
|
@@coll_full_name = 'ruby-mongo-test.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'
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def teardown
|
def teardown
|
||||||
if @db.connected?
|
@@coll.clear
|
||||||
@coll.clear if @coll
|
|
||||||
@db.close
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_explain
|
def test_explain
|
||||||
cursor = @coll.find('a' => 1)
|
cursor = @@coll.find('a' => 1)
|
||||||
explaination = cursor.explain
|
explaination = cursor.explain
|
||||||
assert_not_nil explaination['cursor']
|
assert_not_nil explaination['cursor']
|
||||||
assert_kind_of Numeric, explaination['n']
|
assert_kind_of Numeric, explaination['n']
|
||||||
@ -35,7 +32,7 @@ class CursorTest < Test::Unit::TestCase
|
|||||||
|
|
||||||
def test_close_no_query_sent
|
def test_close_no_query_sent
|
||||||
begin
|
begin
|
||||||
cursor = @coll.find('a' => 1)
|
cursor = @@coll.find('a' => 1)
|
||||||
cursor.close
|
cursor.close
|
||||||
assert cursor.closed?
|
assert cursor.closed?
|
||||||
rescue => ex
|
rescue => ex
|
||||||
@ -45,7 +42,7 @@ class CursorTest < Test::Unit::TestCase
|
|||||||
|
|
||||||
def test_close_after_query_sent
|
def test_close_after_query_sent
|
||||||
begin
|
begin
|
||||||
cursor = @coll.find('a' => 1)
|
cursor = @@coll.find('a' => 1)
|
||||||
cursor.next_object
|
cursor.next_object
|
||||||
cursor.close
|
cursor.close
|
||||||
assert cursor.closed?
|
assert cursor.closed?
|
||||||
|
@ -15,50 +15,53 @@ class DBTest < Test::Unit::TestCase
|
|||||||
|
|
||||||
include XGen::Mongo::Driver
|
include XGen::Mongo::Driver
|
||||||
|
|
||||||
def setup
|
@@host = ENV['MONGO_RUBY_DRIVER_HOST'] || 'localhost'
|
||||||
@host = ENV['MONGO_RUBY_DRIVER_HOST'] || 'localhost'
|
@@port = ENV['MONGO_RUBY_DRIVER_PORT'] || Mongo::DEFAULT_PORT
|
||||||
@port = ENV['MONGO_RUBY_DRIVER_PORT'] || Mongo::DEFAULT_PORT
|
@@db = Mongo.new(@@host, @@port).db('ruby-mongo-test')
|
||||||
@db = Mongo.new(@host, @port).db('ruby-mongo-test')
|
@@users = @@db.collection('system.users')
|
||||||
|
|
||||||
|
def setup
|
||||||
@spongebob = 'spongebob'
|
@spongebob = 'spongebob'
|
||||||
@spongebob_password = 'squarepants'
|
@spongebob_password = 'squarepants'
|
||||||
@users = @db.collection('system.users')
|
@@users.clear
|
||||||
@users.clear
|
@@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
|
||||||
if @db && @db.connected?
|
@@users.clear if @@users
|
||||||
@users.clear if @users
|
|
||||||
@db.close
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_close
|
def test_close
|
||||||
@db.close
|
@@db.close
|
||||||
assert !@db.connected?
|
assert !@@db.connected?
|
||||||
begin
|
begin
|
||||||
@db.collection('test').insert('a' => 1)
|
@@db.collection('test').insert('a' => 1)
|
||||||
fail "expected 'NilClass' exception"
|
fail "expected 'NilClass' exception"
|
||||||
rescue => ex
|
rescue => ex
|
||||||
assert_match /NilClass/, ex.to_s
|
assert_match /NilClass/, ex.to_s
|
||||||
|
ensure
|
||||||
|
@@db = Mongo.new(@@host, @@port).db('ruby-mongo-test')
|
||||||
|
@@users = @@db.collection('system.users')
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_full_coll_name
|
def test_full_coll_name
|
||||||
coll = @db.collection('test')
|
coll = @@db.collection('test')
|
||||||
assert_equal 'ruby-mongo-test.test', @db.full_coll_name(coll.name)
|
assert_equal 'ruby-mongo-test.test', @@db.full_coll_name(coll.name)
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_pair
|
def test_pair
|
||||||
@db.close
|
@@db.close
|
||||||
@users = nil
|
@@users = nil
|
||||||
@db = Mongo.new({:left => "this-should-fail", :right => [@host, @port]}).db('ruby-mongo-test')
|
@@db = Mongo.new({:left => "this-should-fail", :right => [@@host, @@port]}).db('ruby-mongo-test')
|
||||||
assert @db.connected?
|
assert @@db.connected?
|
||||||
|
ensure
|
||||||
|
@@db = Mongo.new(@@host, @@port) unless @@db.connected?
|
||||||
|
@@users = @@db.collection('system.users')
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_pk_factory
|
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 = db.collection('test')
|
||||||
coll.clear
|
coll.clear
|
||||||
|
|
||||||
@ -80,28 +83,31 @@ class DBTest < Test::Unit::TestCase
|
|||||||
end
|
end
|
||||||
|
|
||||||
def test_pk_factory_reset
|
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
|
begin
|
||||||
@db.pk_factory = Object.new
|
db.pk_factory = Object.new
|
||||||
fail "error: expected exception"
|
fail "error: expected exception"
|
||||||
rescue => ex
|
rescue => ex
|
||||||
assert_match /can not change PK factory/, ex.to_s
|
assert_match /can not change PK factory/, ex.to_s
|
||||||
|
ensure
|
||||||
|
db.close
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_authenticate
|
def test_authenticate
|
||||||
assert !@db.authenticate('nobody', 'nopassword')
|
assert !@@db.authenticate('nobody', 'nopassword')
|
||||||
assert !@db.authenticate(@spongebob, 'squareliederhosen')
|
assert !@@db.authenticate(@spongebob, 'squareliederhosen')
|
||||||
assert @db.authenticate(@spongebob, @spongebob_password)
|
assert @@db.authenticate(@spongebob, @spongebob_password)
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_logout
|
def test_logout
|
||||||
@db.logout # only testing that we don't throw exception
|
@@db.logout # only testing that we don't throw exception
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_auto_connect
|
def test_auto_connect
|
||||||
@db.close
|
@@db.close
|
||||||
db = Mongo.new(@host, @port, :auto_reconnect => true).db('ruby-mongo-test')
|
db = Mongo.new(@@host, @@port, :auto_reconnect => true).db('ruby-mongo-test')
|
||||||
assert db.connected?
|
assert db.connected?
|
||||||
assert db.auto_reconnect?
|
assert db.auto_reconnect?
|
||||||
db.close
|
db.close
|
||||||
@ -109,22 +115,25 @@ class DBTest < Test::Unit::TestCase
|
|||||||
assert db.auto_reconnect?
|
assert db.auto_reconnect?
|
||||||
db.collection('test').insert('a' => 1)
|
db.collection('test').insert('a' => 1)
|
||||||
assert db.connected?
|
assert db.connected?
|
||||||
|
ensure
|
||||||
|
@@db = Mongo.new(@@host, @@port).db('ruby-mongo-test')
|
||||||
|
@@users = @@db.collection('system.users')
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_error
|
def test_error
|
||||||
doc = @db.send(:db_command, :forceerror => 1)
|
doc = @@db.send(:db_command, :forceerror => 1)
|
||||||
assert @db.error?
|
assert @@db.error?
|
||||||
err = @db.error
|
err = @@db.error
|
||||||
assert_match /forced error/, err
|
assert_match /forced error/, err
|
||||||
|
|
||||||
# ask again
|
# ask again
|
||||||
assert @db.error?
|
assert @@db.error?
|
||||||
err2 = @db.error
|
err2 = @@db.error
|
||||||
assert_equal err, err2
|
assert_equal err, err2
|
||||||
end
|
end
|
||||||
|
|
||||||
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').clear
|
||||||
end
|
end
|
||||||
|
@ -7,51 +7,48 @@ class DBAPITest < Test::Unit::TestCase
|
|||||||
|
|
||||||
include XGen::Mongo::Driver
|
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
|
def setup
|
||||||
host = ENV['MONGO_RUBY_DRIVER_HOST'] || 'localhost'
|
@@coll.clear
|
||||||
port = ENV['MONGO_RUBY_DRIVER_PORT'] || Mongo::DEFAULT_PORT
|
@r1 = @@coll.insert('a' => 1) # collection not created until it's used
|
||||||
@db = Mongo.new(host, port).db('ruby-mongo-test')
|
@@coll_full_name = 'ruby-mongo-test.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'
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def teardown
|
def teardown
|
||||||
if @db.connected?
|
@@coll.clear
|
||||||
@coll.clear unless @coll == nil
|
|
||||||
@db.close
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_clear
|
def test_clear
|
||||||
assert_equal 1, @coll.count
|
assert_equal 1, @@coll.count
|
||||||
@coll.clear
|
@@coll.clear
|
||||||
assert_equal 0, @coll.count
|
assert_equal 0, @@coll.count
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_insert
|
def test_insert
|
||||||
@coll.insert('a' => 2)
|
@@coll.insert('a' => 2)
|
||||||
@coll.insert('b' => 3)
|
@@coll.insert('b' => 3)
|
||||||
|
|
||||||
assert_equal 3, @coll.count
|
assert_equal 3, @@coll.count
|
||||||
docs = @coll.find().to_a
|
docs = @@coll.find().to_a
|
||||||
assert_equal 3, docs.length
|
assert_equal 3, docs.length
|
||||||
assert docs.detect { |row| row['a'] == 1 }
|
assert docs.detect { |row| row['a'] == 1 }
|
||||||
assert docs.detect { |row| row['a'] == 2 }
|
assert docs.detect { |row| row['a'] == 2 }
|
||||||
assert docs.detect { |row| row['b'] == 3 }
|
assert docs.detect { |row| row['b'] == 3 }
|
||||||
|
|
||||||
@coll << {'b' => 4}
|
@@coll << {'b' => 4}
|
||||||
docs = @coll.find().to_a
|
docs = @@coll.find().to_a
|
||||||
assert_equal 4, docs.length
|
assert_equal 4, docs.length
|
||||||
assert docs.detect { |row| row['b'] == 4 }
|
assert docs.detect { |row| row['b'] == 4 }
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_insert_multiple
|
def test_insert_multiple
|
||||||
@coll.insert({'a' => 2}, {'b' => 3})
|
@@coll.insert({'a' => 2}, {'b' => 3})
|
||||||
|
|
||||||
assert_equal 3, @coll.count
|
assert_equal 3, @@coll.count
|
||||||
docs = @coll.find().to_a
|
docs = @@coll.find().to_a
|
||||||
assert_equal 3, docs.length
|
assert_equal 3, docs.length
|
||||||
assert docs.detect { |row| row['a'] == 1 }
|
assert docs.detect { |row| row['a'] == 1 }
|
||||||
assert docs.detect { |row| row['a'] == 2 }
|
assert docs.detect { |row| row['a'] == 2 }
|
||||||
@ -59,15 +56,15 @@ class DBAPITest < Test::Unit::TestCase
|
|||||||
end
|
end
|
||||||
|
|
||||||
def test_find_simple
|
def test_find_simple
|
||||||
@r2 = @coll.insert('a' => 2)
|
@r2 = @@coll.insert('a' => 2)
|
||||||
@r3 = @coll.insert('b' => 3)
|
@r3 = @@coll.insert('b' => 3)
|
||||||
# Check sizes
|
# Check sizes
|
||||||
docs = @coll.find().to_a
|
docs = @@coll.find().to_a
|
||||||
assert_equal 3, docs.size
|
assert_equal 3, docs.size
|
||||||
assert_equal 3, @coll.count
|
assert_equal 3, @@coll.count
|
||||||
|
|
||||||
# Find by other value
|
# 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
|
assert_equal 1, docs.size
|
||||||
doc = docs.first
|
doc = docs.first
|
||||||
# Can't compare _id values because at insert, an _id was added to @r1 by
|
# 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
|
end
|
||||||
|
|
||||||
def test_find_advanced
|
def test_find_advanced
|
||||||
@coll.insert('a' => 2)
|
@@coll.insert('a' => 2)
|
||||||
@coll.insert('b' => 3)
|
@@coll.insert('b' => 3)
|
||||||
|
|
||||||
# Find by advanced query (less than)
|
# 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_equal 2, docs.size
|
||||||
assert docs.detect { |row| row['a'] == 1 }
|
assert docs.detect { |row| row['a'] == 1 }
|
||||||
assert docs.detect { |row| row['a'] == 2 }
|
assert docs.detect { |row| row['a'] == 2 }
|
||||||
|
|
||||||
# Find by advanced query (greater than)
|
# 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_equal 1, docs.size
|
||||||
assert docs.detect { |row| row['a'] == 2 }
|
assert docs.detect { |row| row['a'] == 2 }
|
||||||
|
|
||||||
# Find by advanced query (less than or equal to)
|
# 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_equal 1, docs.size
|
||||||
assert docs.detect { |row| row['a'] == 1 }
|
assert docs.detect { |row| row['a'] == 1 }
|
||||||
|
|
||||||
# Find by advanced query (greater than or equal to)
|
# 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_equal 2, docs.size
|
||||||
assert docs.detect { |row| row['a'] == 1 }
|
assert docs.detect { |row| row['a'] == 1 }
|
||||||
assert docs.detect { |row| row['a'] == 2 }
|
assert docs.detect { |row| row['a'] == 2 }
|
||||||
|
|
||||||
# Find by advanced query (between)
|
# 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_equal 1, docs.size
|
||||||
assert docs.detect { |row| row['a'] == 2 }
|
assert docs.detect { |row| row['a'] == 2 }
|
||||||
|
|
||||||
# Find by advanced query (in clause)
|
# 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_equal 2, docs.size
|
||||||
assert docs.detect { |row| row['a'] == 1 }
|
assert docs.detect { |row| row['a'] == 1 }
|
||||||
assert docs.detect { |row| row['a'] == 2 }
|
assert docs.detect { |row| row['a'] == 2 }
|
||||||
|
|
||||||
# Find by advanced query (regexp)
|
# 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_equal 2, docs.size
|
||||||
assert docs.detect { |row| row['a'] == 1 }
|
assert docs.detect { |row| row['a'] == 1 }
|
||||||
assert docs.detect { |row| row['a'] == 2 }
|
assert docs.detect { |row| row['a'] == 2 }
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_find_sorting
|
def test_find_sorting
|
||||||
@coll.clear
|
@@coll.clear
|
||||||
@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)
|
||||||
@coll.insert('a' => 4, 'b' => 1)
|
@@coll.insert('a' => 4, 'b' => 1)
|
||||||
|
|
||||||
# Sorting (ascending)
|
# 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 4, docs.size
|
||||||
assert_equal 1, docs[0]['a']
|
assert_equal 1, docs[0]['a']
|
||||||
assert_equal 2, docs[1]['a']
|
assert_equal 2, docs[1]['a']
|
||||||
@ -137,7 +134,7 @@ class DBAPITest < Test::Unit::TestCase
|
|||||||
assert_equal 4, docs[3]['a']
|
assert_equal 4, docs[3]['a']
|
||||||
|
|
||||||
# Sorting (descending)
|
# 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.size
|
||||||
assert_equal 4, docs[0]['a']
|
assert_equal 4, docs[0]['a']
|
||||||
assert_equal 3, docs[1]['a']
|
assert_equal 3, docs[1]['a']
|
||||||
@ -145,7 +142,7 @@ class DBAPITest < Test::Unit::TestCase
|
|||||||
assert_equal 1, docs[3]['a']
|
assert_equal 1, docs[3]['a']
|
||||||
|
|
||||||
# Sorting using array of names; assumes ascending order.
|
# 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 4, docs.size
|
||||||
assert_equal 1, docs[0]['a']
|
assert_equal 1, docs[0]['a']
|
||||||
assert_equal 2, docs[1]['a']
|
assert_equal 2, docs[1]['a']
|
||||||
@ -153,14 +150,14 @@ class DBAPITest < Test::Unit::TestCase
|
|||||||
assert_equal 4, docs[3]['a']
|
assert_equal 4, docs[3]['a']
|
||||||
|
|
||||||
# Sorting using single name; assumes ascending order.
|
# 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 4, docs.size
|
||||||
assert_equal 1, docs[0]['a']
|
assert_equal 1, docs[0]['a']
|
||||||
assert_equal 2, docs[1]['a']
|
assert_equal 2, docs[1]['a']
|
||||||
assert_equal 3, docs[2]['a']
|
assert_equal 3, docs[2]['a']
|
||||||
assert_equal 4, docs[3]['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 4, docs.size
|
||||||
assert_equal 2, docs[0]['a']
|
assert_equal 2, docs[0]['a']
|
||||||
assert_equal 4, docs[1]['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
|
# Sorting using empty array; no order guarantee (Mongo bug #898) but
|
||||||
# should not blow up.
|
# 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
|
assert_equal 4, docs.size
|
||||||
|
|
||||||
# Sorting using array of hashes; no order guarantee (Mongo bug #898) but
|
# Sorting using array of hashes; no order guarantee (Mongo bug #898) but
|
||||||
# should not blow up.
|
# 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
|
assert_equal 4, docs.size
|
||||||
|
|
||||||
# Sorting using ordered hash. You can use an unordered one, but then the
|
# 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.
|
# order of the keys won't be guaranteed thus your sort won't make sense.
|
||||||
oh = OrderedHash.new
|
oh = OrderedHash.new
|
||||||
oh['a'] = -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 4, docs.size
|
||||||
assert_equal 4, docs[0]['a']
|
assert_equal 4, docs[0]['a']
|
||||||
assert_equal 3, docs[1]['a']
|
assert_equal 3, docs[1]['a']
|
||||||
@ -192,7 +189,7 @@ class DBAPITest < Test::Unit::TestCase
|
|||||||
# oh = OrderedHash.new
|
# oh = OrderedHash.new
|
||||||
# oh['b'] = -1
|
# oh['b'] = -1
|
||||||
# oh['a'] = 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 4, docs.size
|
||||||
# assert_equal 1, docs[0]['a']
|
# assert_equal 1, docs[0]['a']
|
||||||
# assert_equal 3, docs[1]['a']
|
# assert_equal 3, docs[1]['a']
|
||||||
@ -201,72 +198,73 @@ class DBAPITest < Test::Unit::TestCase
|
|||||||
end
|
end
|
||||||
|
|
||||||
def test_find_limits
|
def test_find_limits
|
||||||
@coll.insert('b' => 2)
|
@@coll.insert('b' => 2)
|
||||||
@coll.insert('c' => 3)
|
@@coll.insert('c' => 3)
|
||||||
@coll.insert('d' => 4)
|
@@coll.insert('d' => 4)
|
||||||
|
|
||||||
docs = @coll.find({}, :limit => 1).to_a
|
docs = @@coll.find({}, :limit => 1).to_a
|
||||||
assert_equal 1, docs.size
|
assert_equal 1, docs.size
|
||||||
docs = @coll.find({}, :limit => 2).to_a
|
docs = @@coll.find({}, :limit => 2).to_a
|
||||||
assert_equal 2, docs.size
|
assert_equal 2, docs.size
|
||||||
docs = @coll.find({}, :limit => 3).to_a
|
docs = @@coll.find({}, :limit => 3).to_a
|
||||||
assert_equal 3, docs.size
|
assert_equal 3, docs.size
|
||||||
docs = @coll.find({}, :limit => 4).to_a
|
docs = @@coll.find({}, :limit => 4).to_a
|
||||||
assert_equal 4, docs.size
|
assert_equal 4, docs.size
|
||||||
docs = @coll.find({}).to_a
|
docs = @@coll.find({}).to_a
|
||||||
assert_equal 4, docs.size
|
assert_equal 4, docs.size
|
||||||
docs = @coll.find({}, :limit => 99).to_a
|
docs = @@coll.find({}, :limit => 99).to_a
|
||||||
assert_equal 4, docs.size
|
assert_equal 4, docs.size
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_drop_collection
|
def test_drop_collection
|
||||||
assert @db.drop_collection(@coll.name), "drop of collection #{@coll.name} failed"
|
assert @@db.drop_collection(@@coll.name), "drop of collection #{@@coll.name} failed"
|
||||||
assert !@db.collection_names.include?(@coll_full_name)
|
assert !@@db.collection_names.include?(@@coll_full_name)
|
||||||
@coll = nil
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_collection_names
|
def test_collection_names
|
||||||
names = @db.collection_names
|
names = @@db.collection_names
|
||||||
assert names.length >= 1
|
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
|
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.length >= 2
|
||||||
assert names.include?(@coll_full_name)
|
assert names.include?(@@coll_full_name)
|
||||||
assert names.include?('ruby-mongo-test.test2')
|
assert names.include?('ruby-mongo-test.test2')
|
||||||
ensure
|
ensure
|
||||||
@db.drop_collection('test2')
|
@@db.drop_collection('test2')
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_collections_info
|
def test_collections_info
|
||||||
cursor = @db.collections_info
|
cursor = @@db.collections_info
|
||||||
rows = cursor.to_a
|
rows = cursor.to_a
|
||||||
assert rows.length >= 1
|
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
|
assert_not_nil row
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_collection_options
|
def test_collection_options
|
||||||
@db.drop_collection('foobar')
|
@@db.drop_collection('foobar')
|
||||||
@db.strict = true
|
@@db.strict = true
|
||||||
|
|
||||||
begin
|
begin
|
||||||
coll = @db.create_collection('foobar', :capped => true, :size => 1024)
|
coll = @@db.create_collection('foobar', :capped => true, :size => 1024)
|
||||||
options = coll.options()
|
options = coll.options()
|
||||||
assert_equal 'foobar', options['create']
|
assert_equal 'foobar', options['create']
|
||||||
assert_equal true, options['capped']
|
assert_equal true, options['capped']
|
||||||
assert_equal 1024, options['size']
|
assert_equal 1024, options['size']
|
||||||
rescue => ex
|
rescue => ex
|
||||||
@db.drop_collection('foobar')
|
@@db.drop_collection('foobar')
|
||||||
fail "did not expect exception \"#{ex}\""
|
fail "did not expect exception \"#{ex}\""
|
||||||
|
ensure
|
||||||
|
@@db.strict = false
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_index_information
|
def test_index_information
|
||||||
@db.create_index(@coll.name, 'index_name', ['a'])
|
@@db.create_index(@@coll.name, 'index_name', ['a'])
|
||||||
list = @db.index_information(@coll.name)
|
list = @@db.index_information(@@coll.name)
|
||||||
assert_equal 1, list.length
|
assert_equal 1, list.length
|
||||||
|
|
||||||
info = list[0]
|
info = list[0]
|
||||||
@ -275,16 +273,16 @@ class DBAPITest < Test::Unit::TestCase
|
|||||||
end
|
end
|
||||||
|
|
||||||
def test_array
|
def test_array
|
||||||
@coll << {'b' => [1, 2, 3]}
|
@@coll << {'b' => [1, 2, 3]}
|
||||||
rows = @coll.find({}, {:fields => ['b']}).to_a
|
rows = @@coll.find({}, {:fields => ['b']}).to_a
|
||||||
assert_equal 1, rows.length
|
assert_equal 1, rows.length
|
||||||
assert_equal [1, 2, 3], rows[0]['b']
|
assert_equal [1, 2, 3], rows[0]['b']
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_regex
|
def test_regex
|
||||||
regex = /foobar/i
|
regex = /foobar/i
|
||||||
@coll << {'b' => regex}
|
@@coll << {'b' => regex}
|
||||||
rows = @coll.find({}, {:fields => ['b']}).to_a
|
rows = @@coll.find({}, {:fields => ['b']}).to_a
|
||||||
assert_equal 1, rows.length
|
assert_equal 1, rows.length
|
||||||
assert_equal regex, rows[0]['b']
|
assert_equal regex, rows[0]['b']
|
||||||
end
|
end
|
||||||
@ -293,37 +291,39 @@ class DBAPITest < Test::Unit::TestCase
|
|||||||
# Note: can't use Time.new because that will include fractional seconds,
|
# Note: can't use Time.new because that will include fractional seconds,
|
||||||
# which Mongo does not store.
|
# which Mongo does not store.
|
||||||
t = Time.at(1234567890)
|
t = Time.at(1234567890)
|
||||||
@coll << {'_id' => t}
|
@@coll << {'_id' => t}
|
||||||
rows = @coll.find({'_id' => t}).to_a
|
rows = @@coll.find({'_id' => t}).to_a
|
||||||
assert_equal 1, rows.length
|
assert_equal 1, rows.length
|
||||||
assert_equal t, rows[0]['_id']
|
assert_equal t, rows[0]['_id']
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_strict
|
def test_strict
|
||||||
assert !@db.strict?
|
assert !@@db.strict?
|
||||||
@db.strict = true
|
@@db.strict = true
|
||||||
assert @db.strict?
|
assert @@db.strict?
|
||||||
|
ensure
|
||||||
|
@@db.strict = false
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_strict_access_collection
|
def test_strict_access_collection
|
||||||
@db.strict = true
|
@@db.strict = true
|
||||||
begin
|
begin
|
||||||
@db.collection('does-not-exist')
|
@@db.collection('does-not-exist')
|
||||||
fail "expected exception"
|
fail "expected exception"
|
||||||
rescue => ex
|
rescue => ex
|
||||||
assert_equal "Collection does-not-exist doesn't exist. Currently in strict mode.", ex.to_s
|
assert_equal "Collection does-not-exist doesn't exist. Currently in strict mode.", ex.to_s
|
||||||
ensure
|
ensure
|
||||||
@db.strict = false
|
@@db.strict = false
|
||||||
@db.drop_collection('does-not-exist')
|
@@db.drop_collection('does-not-exist')
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_strict_create_collection
|
def test_strict_create_collection
|
||||||
@db.drop_collection('foobar')
|
@@db.drop_collection('foobar')
|
||||||
@db.strict = true
|
@@db.strict = true
|
||||||
|
|
||||||
begin
|
begin
|
||||||
@db.create_collection('foobar')
|
@@db.create_collection('foobar')
|
||||||
assert true
|
assert true
|
||||||
rescue => ex
|
rescue => ex
|
||||||
fail "did not expect exception \"#{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.
|
# Now the collection exists. This time we should see an exception.
|
||||||
begin
|
begin
|
||||||
@db.create_collection('foobar')
|
@@db.create_collection('foobar')
|
||||||
fail "expected exception"
|
fail "expected exception"
|
||||||
rescue => ex
|
rescue => ex
|
||||||
assert_equal "Collection foobar already exists. Currently in strict mode.", ex.to_s
|
assert_equal "Collection foobar already exists. Currently in strict mode.", ex.to_s
|
||||||
ensure
|
ensure
|
||||||
@db.strict = false
|
@@db.strict = false
|
||||||
@db.drop_collection('foobar')
|
@@db.drop_collection('foobar')
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_to_a
|
def test_to_a
|
||||||
cursor = @coll.find()
|
cursor = @@coll.find()
|
||||||
rows = cursor.to_a
|
rows = cursor.to_a
|
||||||
|
|
||||||
# Make sure we get back exactly the same array the next time we ask
|
# Make sure we get back exactly the same array the next time we ask
|
||||||
@ -357,7 +357,7 @@ class DBAPITest < Test::Unit::TestCase
|
|||||||
end
|
end
|
||||||
|
|
||||||
def test_to_a_after_each
|
def test_to_a_after_each
|
||||||
cursor = @coll.find
|
cursor = @@coll.find
|
||||||
cursor.each { |row| row }
|
cursor.each { |row| row }
|
||||||
begin
|
begin
|
||||||
cursor.to_a
|
cursor.to_a
|
||||||
@ -368,40 +368,40 @@ class DBAPITest < Test::Unit::TestCase
|
|||||||
end
|
end
|
||||||
|
|
||||||
def test_ismaster
|
def test_ismaster
|
||||||
assert @db.master?
|
assert @@db.master?
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_master
|
def test_master
|
||||||
assert_equal "#{@db.host}:#{@db.port}", @db.master
|
assert_equal "#{@@db.host}:#{@@db.port}", @@db.master
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_hint
|
def test_hint
|
||||||
@coll.create_index('test_a_index', 'a')
|
@@coll.create_index('test_a_index', 'a')
|
||||||
begin
|
begin
|
||||||
assert_nil @coll.hint
|
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']).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_equal 1, @@coll.find({'a' => 1}, :hint => {'a' => 1}).to_a.size
|
||||||
|
|
||||||
@coll.hint = 'a'
|
@@coll.hint = 'a'
|
||||||
assert_equal({'a' => 1}, @coll.hint)
|
assert_equal({'a' => 1}, @@coll.hint)
|
||||||
assert_equal 1, @coll.find('a' => 1).to_a.size
|
assert_equal 1, @@coll.find('a' => 1).to_a.size
|
||||||
|
|
||||||
@coll.hint = ['a']
|
@@coll.hint = ['a']
|
||||||
assert_equal({'a' => 1}, @coll.hint)
|
assert_equal({'a' => 1}, @@coll.hint)
|
||||||
assert_equal 1, @coll.find('a' => 1).to_a.size
|
assert_equal 1, @@coll.find('a' => 1).to_a.size
|
||||||
|
|
||||||
@coll.hint = {'a' => 1}
|
@@coll.hint = {'a' => 1}
|
||||||
assert_equal({'a' => 1}, @coll.hint)
|
assert_equal({'a' => 1}, @@coll.hint)
|
||||||
assert_equal 1, @coll.find('a' => 1).to_a.size
|
assert_equal 1, @@coll.find('a' => 1).to_a.size
|
||||||
|
|
||||||
@coll.hint = nil
|
@@coll.hint = nil
|
||||||
assert_nil @coll.hint
|
assert_nil @@coll.hint
|
||||||
assert_equal 1, @coll.find('a' => 1).to_a.size
|
assert_equal 1, @@coll.find('a' => 1).to_a.size
|
||||||
rescue => ex
|
rescue => ex
|
||||||
fail ex.to_s
|
fail ex.to_s
|
||||||
ensure
|
ensure
|
||||||
@coll.drop_index('test_a_index')
|
@@coll.drop_index('test_a_index')
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -410,11 +410,11 @@ class DBAPITest < Test::Unit::TestCase
|
|||||||
|
|
||||||
# def test_insert_undefined
|
# def test_insert_undefined
|
||||||
# doc = {'undef' => Undefined.new}
|
# doc = {'undef' => Undefined.new}
|
||||||
# @coll.clear
|
# @@coll.clear
|
||||||
# @coll.insert(doc)
|
# @@coll.insert(doc)
|
||||||
# p @db.error # DEBUG
|
# p @@db.error # DEBUG
|
||||||
# assert_equal 1, @coll.count
|
# assert_equal 1, @@coll.count
|
||||||
# row = @coll.find().next_object
|
# row = @@coll.find().next_object
|
||||||
# assert_not_nil row
|
# assert_not_nil row
|
||||||
# end
|
# end
|
||||||
|
|
||||||
|
@ -8,35 +8,30 @@ class GridStoreTest < Test::Unit::TestCase
|
|||||||
include XGen::Mongo::Driver
|
include XGen::Mongo::Driver
|
||||||
include XGen::Mongo::GridFS
|
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
|
def setup
|
||||||
@host = ENV['MONGO_RUBY_DRIVER_HOST'] || 'localhost'
|
@@chunks.clear
|
||||||
@port = ENV['MONGO_RUBY_DRIVER_PORT'] || Mongo::DEFAULT_PORT
|
@@files.clear
|
||||||
@db = Mongo.new(@host, @port).db('ruby-mongo-utils-test')
|
GridStore.open(@@db, 'foobar', 'w') { |f| f.write("hello, world!") }
|
||||||
|
|
||||||
@files = @db.collection('gridfs.files')
|
|
||||||
@chunks = @db.collection('gridfs.chunks')
|
|
||||||
@chunks.clear
|
|
||||||
@files.clear
|
|
||||||
|
|
||||||
GridStore.open(@db, 'foobar', 'w') { |f| f.write("hello, world!") }
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def teardown
|
def teardown
|
||||||
if @db && @db.connected?
|
@@chunks.clear
|
||||||
@chunks.clear
|
@@files.clear
|
||||||
@files.clear
|
|
||||||
@db.close
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_exist
|
def test_exist
|
||||||
assert GridStore.exist?(@db, 'foobar')
|
assert GridStore.exist?(@@db, 'foobar')
|
||||||
assert !GridStore.exist?(@db, 'does_not_exist')
|
assert !GridStore.exist?(@@db, 'does_not_exist')
|
||||||
assert !GridStore.exist?(@db, 'foobar', 'another_root')
|
assert !GridStore.exist?(@@db, 'foobar', 'another_root')
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_small_write
|
def test_small_write
|
||||||
rows = @files.find({'filename' => 'foobar'}).to_a
|
rows = @@files.find({'filename' => 'foobar'}).to_a
|
||||||
assert_not_nil rows
|
assert_not_nil rows
|
||||||
assert_equal 1, rows.length
|
assert_equal 1, rows.length
|
||||||
row = rows[0]
|
row = rows[0]
|
||||||
@ -44,37 +39,37 @@ class GridStoreTest < Test::Unit::TestCase
|
|||||||
|
|
||||||
file_id = row['_id']
|
file_id = row['_id']
|
||||||
assert_kind_of ObjectID, file_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_not_nil rows
|
||||||
assert_equal 1, rows.length
|
assert_equal 1, rows.length
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_small_file
|
def test_small_file
|
||||||
rows = @files.find({'filename' => 'foobar'}).to_a
|
rows = @@files.find({'filename' => 'foobar'}).to_a
|
||||||
assert_not_nil rows
|
assert_not_nil rows
|
||||||
assert_equal 1, rows.length
|
assert_equal 1, rows.length
|
||||||
row = rows[0]
|
row = rows[0]
|
||||||
assert_not_nil row
|
assert_not_nil row
|
||||||
assert_equal "hello, world!", GridStore.read(@db, 'foobar')
|
assert_equal "hello, world!", GridStore.read(@@db, 'foobar')
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_overwrite
|
def test_overwrite
|
||||||
GridStore.open(@db, 'foobar', 'w') { |f| f.write("overwrite") }
|
GridStore.open(@@db, 'foobar', 'w') { |f| f.write("overwrite") }
|
||||||
assert_equal "overwrite", GridStore.read(@db, 'foobar')
|
assert_equal "overwrite", GridStore.read(@@db, 'foobar')
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_read_length
|
def test_read_length
|
||||||
assert_equal "hello", GridStore.read(@db, 'foobar', 5)
|
assert_equal "hello", GridStore.read(@@db, 'foobar', 5)
|
||||||
end
|
end
|
||||||
|
|
||||||
# Also tests seek
|
# Also tests seek
|
||||||
def test_read_with_offset
|
def test_read_with_offset
|
||||||
assert_equal "world", GridStore.read(@db, 'foobar', 5, 7)
|
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', nil, 7)
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_seek
|
def test_seek
|
||||||
GridStore.open(@db, 'foobar', 'r') { |f|
|
GridStore.open(@@db, 'foobar', 'r') { |f|
|
||||||
f.seek(0)
|
f.seek(0)
|
||||||
assert_equal 'h', f.getc.chr
|
assert_equal 'h', f.getc.chr
|
||||||
f.seek(7)
|
f.seek(7)
|
||||||
@ -100,84 +95,84 @@ class GridStoreTest < Test::Unit::TestCase
|
|||||||
end
|
end
|
||||||
|
|
||||||
def test_multi_chunk
|
def test_multi_chunk
|
||||||
@chunks.clear
|
@@chunks.clear
|
||||||
@files.clear
|
@@files.clear
|
||||||
|
|
||||||
size = 512
|
size = 512
|
||||||
GridStore.open(@db, 'biggie', 'w') { |f|
|
GridStore.open(@@db, 'biggie', 'w') { |f|
|
||||||
f.chunk_size = size
|
f.chunk_size = size
|
||||||
f.write('x' * size)
|
f.write('x' * size)
|
||||||
f.write('y' * size)
|
f.write('y' * size)
|
||||||
f.write('z' * size)
|
f.write('z' * size)
|
||||||
}
|
}
|
||||||
|
|
||||||
assert_equal 3, @chunks.count
|
assert_equal 3, @@chunks.count
|
||||||
assert_equal ('x' * size) + ('y' * size) + ('z' * size), GridStore.read(@db, 'biggie')
|
assert_equal ('x' * size) + ('y' * size) + ('z' * size), GridStore.read(@@db, 'biggie')
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_puts_and_readlines
|
def test_puts_and_readlines
|
||||||
GridStore.open(@db, 'multiline', 'w') { |f|
|
GridStore.open(@@db, 'multiline', 'w') { |f|
|
||||||
f.puts "line one"
|
f.puts "line one"
|
||||||
f.puts "line two\n"
|
f.puts "line two\n"
|
||||||
f.puts "line three"
|
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
|
assert_equal ["line one\n", "line two\n", "line three\n"], lines
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_unlink
|
def test_unlink
|
||||||
assert_equal 1, @files.count
|
assert_equal 1, @@files.count
|
||||||
assert_equal 1, @chunks.count
|
assert_equal 1, @@chunks.count
|
||||||
GridStore.unlink(@db, 'foobar')
|
GridStore.unlink(@@db, 'foobar')
|
||||||
assert_equal 0, @files.count
|
assert_equal 0, @@files.count
|
||||||
assert_equal 0, @chunks.count
|
assert_equal 0, @@chunks.count
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_append
|
def test_append
|
||||||
GridStore.open(@db, 'foobar', 'w+') { |f| f.write(" how are you?") }
|
GridStore.open(@@db, 'foobar', 'w+') { |f| f.write(" how are you?") }
|
||||||
assert_equal 1, @chunks.count
|
assert_equal 1, @@chunks.count
|
||||||
assert_equal "hello, world! how are you?", GridStore.read(@db, 'foobar')
|
assert_equal "hello, world! how are you?", GridStore.read(@@db, 'foobar')
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_rewind_and_truncate_on_write
|
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.write("some text is inserted here")
|
||||||
f.rewind
|
f.rewind
|
||||||
f.write("abc")
|
f.write("abc")
|
||||||
}
|
}
|
||||||
assert_equal "abc", GridStore.read(@db, 'foobar')
|
assert_equal "abc", GridStore.read(@@db, 'foobar')
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_tell
|
def test_tell
|
||||||
GridStore.open(@db, 'foobar', 'r') { |f|
|
GridStore.open(@@db, 'foobar', 'r') { |f|
|
||||||
f.read(5)
|
f.read(5)
|
||||||
assert_equal 5, f.tell
|
assert_equal 5, f.tell
|
||||||
}
|
}
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_empty_block_ok
|
def test_empty_block_ok
|
||||||
GridStore.open(@db, 'empty', 'w')
|
GridStore.open(@@db, 'empty', 'w')
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_save_empty_file
|
def test_save_empty_file
|
||||||
@chunks.clear
|
@@chunks.clear
|
||||||
@files.clear
|
@@files.clear
|
||||||
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
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_empty_file_eof
|
def test_empty_file_eof
|
||||||
GridStore.open(@db, 'empty', 'w')
|
GridStore.open(@@db, 'empty', 'w')
|
||||||
GridStore.open(@db, 'empty', 'r') { |f|
|
GridStore.open(@@db, 'empty', 'r') { |f|
|
||||||
assert f.eof?
|
assert f.eof?
|
||||||
}
|
}
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_cannot_change_chunk_size_on_read
|
def test_cannot_change_chunk_size_on_read
|
||||||
begin
|
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"
|
fail "should have seen error"
|
||||||
rescue => ex
|
rescue => ex
|
||||||
assert_match /error: can only change chunk size/, ex.to_s
|
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
|
def test_cannot_change_chunk_size_after_data_written
|
||||||
begin
|
begin
|
||||||
GridStore.open(@db, 'foobar', 'w') { |f|
|
GridStore.open(@@db, 'foobar', 'w') { |f|
|
||||||
f.write("some text")
|
f.write("some text")
|
||||||
f.chunk_size = 42
|
f.chunk_size = 42
|
||||||
}
|
}
|
||||||
@ -197,18 +192,18 @@ class GridStoreTest < Test::Unit::TestCase
|
|||||||
end
|
end
|
||||||
|
|
||||||
def test_change_chunk_size
|
def test_change_chunk_size
|
||||||
GridStore.open(@db, 'new-file', 'w') { |f|
|
GridStore.open(@@db, 'new-file', 'w') { |f|
|
||||||
f.chunk_size = 42
|
f.chunk_size = 42
|
||||||
f.write("foo")
|
f.write("foo")
|
||||||
}
|
}
|
||||||
GridStore.open(@db, 'new-file', 'r') { |f|
|
GridStore.open(@@db, 'new-file', 'r') { |f|
|
||||||
assert f.chunk_size == 42
|
assert f.chunk_size == 42
|
||||||
}
|
}
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_chunk_size_in_option
|
def test_chunk_size_in_option
|
||||||
GridStore.open(@db, 'new-file', 'w', :chunk_size => 42) { |f| f.write("foo") }
|
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', 'r') { |f|
|
||||||
assert f.chunk_size == 42
|
assert f.chunk_size == 42
|
||||||
}
|
}
|
||||||
end
|
end
|
||||||
@ -216,46 +211,46 @@ class GridStoreTest < Test::Unit::TestCase
|
|||||||
def test_upload_date
|
def test_upload_date
|
||||||
now = Time.now
|
now = Time.now
|
||||||
orig_file_upload_date = nil
|
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_not_nil orig_file_upload_date
|
||||||
assert (orig_file_upload_date - now) < 5 # even a really slow system < 5 secs
|
assert (orig_file_upload_date - now) < 5 # even a really slow system < 5 secs
|
||||||
|
|
||||||
sleep(2)
|
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
|
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
|
assert_equal orig_file_upload_date, file_upload_date
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_content_type
|
def test_content_type
|
||||||
ct = nil
|
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
|
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
|
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
|
assert_equal 'text/html', ct2
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_content_type_option
|
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
|
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
|
assert_equal 'image/jpg', ct
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_unknown_mode
|
def test_unknown_mode
|
||||||
GridStore.open(@db, 'foobar', 'x')
|
GridStore.open(@@db, 'foobar', 'x')
|
||||||
fail 'should have seen "illegal mode" error raised'
|
fail 'should have seen "illegal mode" error raised'
|
||||||
rescue => ex
|
rescue => ex
|
||||||
assert_equal "error: illegal mode x", ex.to_s
|
assert_equal "error: illegal mode x", ex.to_s
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_metadata
|
def test_metadata
|
||||||
GridStore.open(@db, 'foobar', 'r') { |f| assert_nil 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', '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_equal({'a' => 1}, f.metadata) }
|
||||||
end
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
|
Loading…
Reference in New Issue
Block a user