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-10-13 21:09:23 +00:00
|
|
|
@db = standard_connection.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
|
|
|
|
2010-12-15 20:07:30 +00:00
|
|
|
context "Seeking" do
|
|
|
|
setup do
|
|
|
|
@filename = 'test'
|
|
|
|
@mode = 'w'
|
|
|
|
@data = "1" * 1024 * 1024
|
|
|
|
@file = GridIO.new(@files, @chunks, @filename, @mode)
|
|
|
|
@file.write(@data)
|
|
|
|
@file.close
|
|
|
|
end
|
|
|
|
|
|
|
|
should "read all data using read_length and then be able to seek" do
|
|
|
|
file = GridIO.new(@files, @chunks, nil, "r", :query => {:_id => @file.files_id})
|
|
|
|
assert_equal @data, file.read(1024 * 1024)
|
|
|
|
file.seek(0)
|
|
|
|
assert_equal @data, file.read
|
|
|
|
end
|
|
|
|
|
|
|
|
should "read all data using read_all and then be able to seek" do
|
|
|
|
file = GridIO.new(@files, @chunks, nil, "r", :query => {:_id => @file.files_id})
|
|
|
|
assert_equal @data, file.read
|
|
|
|
file.seek(0)
|
|
|
|
assert_equal @data, file.read
|
|
|
|
file.seek(1024 * 512)
|
|
|
|
assert_equal 524288, file.file_position
|
|
|
|
assert_equal @data.length / 2, file.read.length
|
|
|
|
assert_equal 1048576, file.file_position
|
|
|
|
assert_nil file.read
|
|
|
|
file.seek(1024 * 512)
|
|
|
|
assert_equal 524288, file.file_position
|
|
|
|
end
|
|
|
|
|
|
|
|
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
|
2010-04-07 18:39:05 +00:00
|
|
|
|
|
|
|
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'})
|
2010-04-07 18:39:05 +00:00
|
|
|
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
|