mongo-ruby-driver/test/grid_test.rb

85 lines
2.3 KiB
Ruby
Raw Normal View History

2010-02-12 23:03:07 +00:00
require 'test/test_helper'
2010-02-20 00:17:38 +00:00
include Mongo
2010-02-12 23:03:07 +00:00
2010-02-20 00:17:38 +00:00
context "Tests:" do
setup do
2010-02-12 23:03:07 +00:00
@db ||= Connection.new(ENV['MONGO_RUBY_DRIVER_HOST'] || 'localhost',
ENV['MONGO_RUBY_DRIVER_PORT'] || Connection::DEFAULT_PORT).db('ruby-mongo-test')
2010-02-19 21:20:46 +00:00
@files = @db.collection('test-fs.files')
@chunks = @db.collection('test-fs.chunks')
2010-02-12 23:03:07 +00:00
end
2010-02-20 00:17:38 +00:00
teardown do
2010-02-12 23:03:07 +00:00
@files.remove
@chunks.remove
end
2010-02-18 21:31:25 +00:00
context "A basic grid-stored file" do
2010-02-12 23:03:07 +00:00
setup do
2010-02-18 21:31:25 +00:00
@data = "GRIDDATA" * 50000
2010-02-19 21:20:46 +00:00
@grid = Grid.new(@db, 'test-fs')
2010-02-18 21:31:25 +00:00
@id = @grid.put(@data, 'sample', :metadata => {'app' => 'photos'})
2010-02-12 23:03:07 +00:00
end
2010-02-18 21:31:25 +00:00
should "retrieve the stored data" do
data = @grid.get(@id).data
assert_equal @data, data
2010-02-12 23:03:07 +00:00
end
2010-02-18 21:31:25 +00:00
should "store the filename" do
file = @grid.get(@id)
assert_equal 'sample', file.filename
2010-02-12 23:03:07 +00:00
end
2010-02-18 21:31:25 +00:00
should "store any relevant metadata" do
file = @grid.get(@id)
assert_equal 'photos', file.metadata['app']
2010-02-12 23:03:07 +00:00
end
2010-02-18 21:31:25 +00:00
should "delete the file and any chunks" do
@grid.delete(@id)
assert_raise GridError do
@grid.get(@id)
2010-02-12 23:03:07 +00:00
end
end
end
2010-02-19 21:20:46 +00:00
context "Streaming: " do
setup do
2010-02-20 00:17:38 +00:00
def read_and_write_stream(filename, read_length, opts={})
io = File.open(File.join(File.dirname(__FILE__), 'data', filename), 'r')
id = @grid.put(io, filename + read_length.to_s, opts)
file = @grid.get(id)
io.rewind
data = io.read
if data.respond_to?(:force_encoding)
data.force_encoding(:binary)
end
read_data = ""
while(chunk = file.read(read_length))
read_data << chunk
end
assert_equal data.length, read_data.length
end
2010-02-19 21:20:46 +00:00
@grid = Grid.new(@db, 'test-fs')
end
should "put and get a small io object with a small chunk size" do
read_and_write_stream('small_data.txt', 1, :chunk_size => 2)
end
should "put and get a small io object" do
read_and_write_stream('small_data.txt', 1)
end
should "put and get a large io object when reading smaller than the chunk size" do
read_and_write_stream('sample_file.pdf', 256 * 1024)
end
should "put and get a large io object when reading larger than the chunk size" do
read_and_write_stream('sample_file.pdf', 300 * 1024)
end
end
2010-02-12 23:03:07 +00:00
end