mongo-ruby-driver/tests/test_grid_store.rb
2009-01-29 11:23:50 -05:00

178 lines
4.3 KiB
Ruby

$LOAD_PATH[0,0] = File.join(File.dirname(__FILE__), '..', 'lib')
require 'test/unit'
require 'rubygems'
require 'mongo'
require 'mongo/gridfs'
class GridStoreTest < Test::Unit::TestCase
include XGen::Mongo::Driver
include XGen::Mongo::GridFS
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('_files')
@chunks = @db.collection('_chunks')
@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
end
def test_small_write
rows = @files.find({'filename' => 'foobar'}).to_a
assert_not_nil rows
assert_equal 1, rows.length
assert_equal 1, rows.length
row = rows[0]
assert_not_nil row
assert_kind_of DBRef, row['next']
first_chunk_id = row['next'].object_id
first_chunk = @chunks.find({'_id' => first_chunk_id}).next_object
end
def test_small_file
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')
end
def test_overwrite
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)
end
# TODO seek not yet implemented
# def test_read_with_offset
# assert_equal "world", GridStore.read(@db, 'foobar', 5, 7)
# assert_equal "world!", GridStore.read(@db, 'foobar', nil, 7)
# end
def test_multi_chunk
@chunks.clear
@files.clear
size = 512
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')
end
def test_puts_and_readlines
GridStore.open(@db, 'multiline', 'w') { |f|
f.puts "line one"
f.puts "line two\n"
f.puts "line three"
}
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
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')
end
def test_rewind_and_truncate_on_write
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')
end
def test_tell
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')
end
def test_save_empty_file
@chunks.clear
@files.clear
GridStore.open(@db, 'empty', 'w')
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|
assert f.eof?
}
end
def test_cannot_change_chunk_size_on_read
begin
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
end
end
def test_cannot_change_chunk_size_after_data_written
begin
GridStore.open(@db, 'foobar', 'w') { |f|
f.write("some text")
f.chunk_size = 42
}
fail "should have seen error"
rescue => ex
assert_match /error: can only change chunk size/, ex.to_s
end
end
def test_change_chunk_size
GridStore.open(@db, 'new-file', 'w') { |f|
f.chunk_size = 42
f.write("foo")
}
GridStore.open(@db, 'new-file', 'r') { |f|
assert f.chunk_size == 42
}
end
end