mongo-ruby-driver/test/grid_io_test.rb

90 lines
2.8 KiB
Ruby
Raw Normal View History

2010-09-09 19:58:51 +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-22 20:49:04 +00:00
class GridIOTest < Test::Unit::TestCase
2010-02-12 23:03:07 +00:00
2010-02-22 20:49:04 +00:00
context "GridIO" do
2010-02-12 23:03:07 +00:00
setup do
2010-02-22 20:49:04 +00:00
@db ||= Connection.new(ENV['MONGO_RUBY_DRIVER_HOST'] || 'localhost',
2010-04-05 19:48:35 +00:00
ENV['MONGO_RUBY_DRIVER_PORT'] || Connection::DEFAULT_PORT).db(MONGO_TEST_DB)
2010-02-22 20:49:04 +00:00
@files = @db.collection('fs.files')
@chunks = @db.collection('fs.chunks')
2010-02-22 23:06:59 +00:00
@chunks.create_index([['files_id', Mongo::ASCENDING], ['n', Mongo::ASCENDING]])
2010-02-12 23:03:07 +00:00
end
2010-02-22 20:49:04 +00:00
teardown do
@files.remove
@chunks.remove
2010-02-12 23:03:07 +00:00
end
2010-02-22 20:49:04 +00:00
context "Options" do
setup do
@filename = 'test'
@mode = 'w'
end
should "set default 256k chunk size" do
file = GridIO.new(@files, @chunks, @filename, @mode)
assert_equal 256 * 1024, file.chunk_size
end
should "set chunk size" do
file = GridIO.new(@files, @chunks, @filename, @mode, :chunk_size => 1000)
assert_equal 1000, file.chunk_size
end
2010-02-12 23:03:07 +00:00
end
2010-02-22 23:06:59 +00:00
context "Grid MD5 check" do
should "run in safe mode" do
file = GridIO.new(@files, @chunks, 'smallfile', 'w', :safe => true)
file.write("DATA" * 100)
assert file.close
assert_equal file.server_md5, file.client_md5
end
should "validate with a large file" do
io = File.open(File.join(File.dirname(__FILE__), 'data', 'sample_file.pdf'), 'r')
file = GridIO.new(@files, @chunks, 'bigfile', 'w', :safe => true)
file.write(io)
assert file.close
assert_equal file.server_md5, file.client_md5
end
should "raise an exception when check fails" do
io = File.open(File.join(File.dirname(__FILE__), 'data', 'sample_file.pdf'), 'r')
2010-07-09 20:01:55 +00:00
@db.stubs(:command).returns({'md5' => '12345'})
file = GridIO.new(@files, @chunks, 'bigfile', 'w', :safe => true)
file.write(io)
assert_raise GridMD5Failure do
assert file.close
end
assert_not_equal file.server_md5, file.client_md5
end
2010-02-22 23:06:59 +00:00
end
2010-02-23 23:03:11 +00:00
context "Content types" do
2010-02-23 23:25:28 +00:00
if defined?(MIME)
should "determine common content types from the extension" do
file = GridIO.new(@files, @chunks, 'sample.pdf', 'w')
assert_equal 'application/pdf', file.content_type
file = GridIO.new(@files, @chunks, 'sample.txt', 'w')
assert_equal 'text/plain', file.content_type
end
2010-02-23 23:03:11 +00:00
end
should "default to binary/octet-stream when type is unknown" do
file = GridIO.new(@files, @chunks, 'sample.l33t', 'w')
assert_equal 'binary/octet-stream', file.content_type
end
should "use any provided content type by default" do
file = GridIO.new(@files, @chunks, 'sample.l33t', 'w', :content_type => 'image/jpg')
assert_equal 'image/jpg', file.content_type
end
end
2010-02-12 23:03:07 +00:00
end
2010-02-22 20:49:04 +00:00
2010-02-12 23:03:07 +00:00
end