2009-01-29 16:23:50 +00:00
|
|
|
$LOAD_PATH[0,0] = File.join(File.dirname(__FILE__), '..', 'lib')
|
|
|
|
require 'test/unit'
|
|
|
|
require 'mongo'
|
|
|
|
require 'mongo/gridfs'
|
|
|
|
|
|
|
|
class GridStoreTest < Test::Unit::TestCase
|
|
|
|
|
|
|
|
include XGen::Mongo::Driver
|
|
|
|
include XGen::Mongo::GridFS
|
|
|
|
|
2009-02-05 15:10:41 +00:00
|
|
|
@@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')
|
2009-01-29 16:23:50 +00:00
|
|
|
|
2009-02-05 15:10:41 +00:00
|
|
|
def setup
|
|
|
|
@@chunks.clear
|
|
|
|
@@files.clear
|
|
|
|
GridStore.open(@@db, 'foobar', 'w') { |f| f.write("hello, world!") }
|
2009-01-29 16:23:50 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def teardown
|
2009-02-05 15:10:41 +00:00
|
|
|
@@chunks.clear
|
|
|
|
@@files.clear
|
2009-01-29 16:23:50 +00:00
|
|
|
end
|
|
|
|
|
2009-01-30 21:44:29 +00:00
|
|
|
def test_exist
|
2009-02-05 15:10:41 +00:00
|
|
|
assert GridStore.exist?(@@db, 'foobar')
|
|
|
|
assert !GridStore.exist?(@@db, 'does_not_exist')
|
|
|
|
assert !GridStore.exist?(@@db, 'foobar', 'another_root')
|
2009-01-30 21:44:29 +00:00
|
|
|
end
|
|
|
|
|
2009-01-29 16:23:50 +00:00
|
|
|
def test_small_write
|
2009-02-05 15:10:41 +00:00
|
|
|
rows = @@files.find({'filename' => 'foobar'}).to_a
|
2009-01-29 16:23:50 +00:00
|
|
|
assert_not_nil rows
|
|
|
|
assert_equal 1, rows.length
|
|
|
|
row = rows[0]
|
|
|
|
assert_not_nil row
|
|
|
|
|
2009-01-30 21:44:29 +00:00
|
|
|
file_id = row['_id']
|
|
|
|
assert_kind_of ObjectID, file_id
|
2009-02-05 15:10:41 +00:00
|
|
|
rows = @@chunks.find({'files_id' => file_id}).to_a
|
2009-01-30 21:44:29 +00:00
|
|
|
assert_not_nil rows
|
|
|
|
assert_equal 1, rows.length
|
2009-01-29 16:23:50 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_small_file
|
2009-02-05 15:10:41 +00:00
|
|
|
rows = @@files.find({'filename' => 'foobar'}).to_a
|
2009-01-29 16:23:50 +00:00
|
|
|
assert_not_nil rows
|
|
|
|
assert_equal 1, rows.length
|
|
|
|
row = rows[0]
|
|
|
|
assert_not_nil row
|
2009-02-05 15:10:41 +00:00
|
|
|
assert_equal "hello, world!", GridStore.read(@@db, 'foobar')
|
2009-01-29 16:23:50 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_overwrite
|
2009-02-05 15:10:41 +00:00
|
|
|
GridStore.open(@@db, 'foobar', 'w') { |f| f.write("overwrite") }
|
|
|
|
assert_equal "overwrite", GridStore.read(@@db, 'foobar')
|
2009-01-29 16:23:50 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_read_length
|
2009-02-05 15:10:41 +00:00
|
|
|
assert_equal "hello", GridStore.read(@@db, 'foobar', 5)
|
2009-01-29 16:23:50 +00:00
|
|
|
end
|
|
|
|
|
2009-01-30 21:44:29 +00:00
|
|
|
# Also tests seek
|
|
|
|
def test_read_with_offset
|
2009-02-05 15:10:41 +00:00
|
|
|
assert_equal "world", GridStore.read(@@db, 'foobar', 5, 7)
|
|
|
|
assert_equal "world!", GridStore.read(@@db, 'foobar', nil, 7)
|
2009-01-30 21:44:29 +00:00
|
|
|
end
|
2009-01-29 16:23:50 +00:00
|
|
|
|
2009-02-02 21:45:36 +00:00
|
|
|
def test_seek
|
2009-02-05 15:10:41 +00:00
|
|
|
GridStore.open(@@db, 'foobar', 'r') { |f|
|
2009-02-02 21:45:36 +00:00
|
|
|
f.seek(0)
|
|
|
|
assert_equal 'h', f.getc.chr
|
|
|
|
f.seek(7)
|
|
|
|
assert_equal 'w', f.getc.chr
|
|
|
|
f.seek(4)
|
|
|
|
assert_equal 'o', f.getc.chr
|
|
|
|
|
|
|
|
f.seek(-1, IO::SEEK_END)
|
|
|
|
assert_equal '!', f.getc.chr
|
|
|
|
f.seek(-6, IO::SEEK_END)
|
|
|
|
assert_equal 'w', f.getc.chr
|
|
|
|
|
|
|
|
f.seek(0)
|
|
|
|
f.seek(7, IO::SEEK_CUR)
|
|
|
|
assert_equal 'w', f.getc.chr
|
|
|
|
f.seek(-1, IO::SEEK_CUR)
|
|
|
|
assert_equal 'w', f.getc.chr
|
|
|
|
f.seek(-4, IO::SEEK_CUR)
|
|
|
|
assert_equal 'o', f.getc.chr
|
|
|
|
f.seek(3, IO::SEEK_CUR)
|
|
|
|
assert_equal 'o', f.getc.chr
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2009-01-29 16:23:50 +00:00
|
|
|
def test_multi_chunk
|
2009-02-05 15:10:41 +00:00
|
|
|
@@chunks.clear
|
|
|
|
@@files.clear
|
2009-01-29 16:23:50 +00:00
|
|
|
|
|
|
|
size = 512
|
2009-02-05 15:10:41 +00:00
|
|
|
GridStore.open(@@db, 'biggie', 'w') { |f|
|
2009-01-29 16:23:50 +00:00
|
|
|
f.chunk_size = size
|
|
|
|
f.write('x' * size)
|
|
|
|
f.write('y' * size)
|
|
|
|
f.write('z' * size)
|
|
|
|
}
|
|
|
|
|
2009-02-05 15:10:41 +00:00
|
|
|
assert_equal 3, @@chunks.count
|
|
|
|
assert_equal ('x' * size) + ('y' * size) + ('z' * size), GridStore.read(@@db, 'biggie')
|
2009-01-29 16:23:50 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_puts_and_readlines
|
2009-02-05 15:10:41 +00:00
|
|
|
GridStore.open(@@db, 'multiline', 'w') { |f|
|
2009-01-29 16:23:50 +00:00
|
|
|
f.puts "line one"
|
|
|
|
f.puts "line two\n"
|
|
|
|
f.puts "line three"
|
|
|
|
}
|
|
|
|
|
2009-02-05 15:10:41 +00:00
|
|
|
lines = GridStore.readlines(@@db, 'multiline')
|
2009-01-29 16:23:50 +00:00
|
|
|
assert_equal ["line one\n", "line two\n", "line three\n"], lines
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_unlink
|
2009-02-05 15:10:41 +00:00
|
|
|
assert_equal 1, @@files.count
|
|
|
|
assert_equal 1, @@chunks.count
|
|
|
|
GridStore.unlink(@@db, 'foobar')
|
|
|
|
assert_equal 0, @@files.count
|
|
|
|
assert_equal 0, @@chunks.count
|
2009-01-29 16:23:50 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_append
|
2009-02-05 15:10:41 +00:00
|
|
|
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')
|
2009-01-29 16:23:50 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_rewind_and_truncate_on_write
|
2009-02-05 15:10:41 +00:00
|
|
|
GridStore.open(@@db, 'foobar', 'w') { |f|
|
2009-01-29 16:23:50 +00:00
|
|
|
f.write("some text is inserted here")
|
|
|
|
f.rewind
|
|
|
|
f.write("abc")
|
|
|
|
}
|
2009-02-05 15:10:41 +00:00
|
|
|
assert_equal "abc", GridStore.read(@@db, 'foobar')
|
2009-01-29 16:23:50 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_tell
|
2009-02-05 15:10:41 +00:00
|
|
|
GridStore.open(@@db, 'foobar', 'r') { |f|
|
2009-01-29 16:23:50 +00:00
|
|
|
f.read(5)
|
|
|
|
assert_equal 5, f.tell
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_empty_block_ok
|
2009-02-05 15:10:41 +00:00
|
|
|
GridStore.open(@@db, 'empty', 'w')
|
2009-01-29 16:23:50 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_save_empty_file
|
2009-02-05 15:10:41 +00:00
|
|
|
@@chunks.clear
|
|
|
|
@@files.clear
|
|
|
|
GridStore.open(@@db, 'empty', 'w') {} # re-write with zero bytes
|
|
|
|
assert_equal 1, @@files.count
|
|
|
|
assert_equal 0, @@chunks.count
|
2009-01-29 16:23:50 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_empty_file_eof
|
2009-02-05 15:10:41 +00:00
|
|
|
GridStore.open(@@db, 'empty', 'w')
|
|
|
|
GridStore.open(@@db, 'empty', 'r') { |f|
|
2009-01-29 16:23:50 +00:00
|
|
|
assert f.eof?
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_cannot_change_chunk_size_on_read
|
|
|
|
begin
|
2009-02-05 15:10:41 +00:00
|
|
|
GridStore.open(@@db, 'foobar', 'r') { |f| f.chunk_size = 42 }
|
2009-01-29 16:23:50 +00:00
|
|
|
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
|
2009-02-05 15:10:41 +00:00
|
|
|
GridStore.open(@@db, 'foobar', 'w') { |f|
|
2009-01-29 16:23:50 +00:00
|
|
|
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
|
2009-02-05 15:10:41 +00:00
|
|
|
GridStore.open(@@db, 'new-file', 'w') { |f|
|
2009-01-29 16:23:50 +00:00
|
|
|
f.chunk_size = 42
|
|
|
|
f.write("foo")
|
|
|
|
}
|
2009-02-05 15:10:41 +00:00
|
|
|
GridStore.open(@@db, 'new-file', 'r') { |f|
|
2009-01-29 16:23:50 +00:00
|
|
|
assert f.chunk_size == 42
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2009-01-30 19:54:58 +00:00
|
|
|
def test_chunk_size_in_option
|
2009-02-05 15:10:41 +00:00
|
|
|
GridStore.open(@@db, 'new-file', 'w', :chunk_size => 42) { |f| f.write("foo") }
|
|
|
|
GridStore.open(@@db, 'new-file', 'r') { |f|
|
2009-01-30 19:54:58 +00:00
|
|
|
assert f.chunk_size == 42
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2009-02-18 19:11:22 +00:00
|
|
|
def test_md5
|
|
|
|
GridStore.open(@@db, 'new-file', 'w') { |f| f.write("hello world\n")}
|
|
|
|
GridStore.open(@@db, 'new-file', 'r') { |f|
|
|
|
|
assert f.md5 == '6f5902ac237024bdd0c176cb93063dc4'
|
|
|
|
begin
|
|
|
|
f.md5 = 'cant do this'
|
|
|
|
fail "should have seen error"
|
|
|
|
rescue => ex
|
|
|
|
true
|
|
|
|
end
|
|
|
|
}
|
|
|
|
GridStore.open(@@db, 'new-file', 'w') {}
|
|
|
|
GridStore.open(@@db, 'new-file', 'r') { |f|
|
|
|
|
assert f.md5 == 'd41d8cd98f00b204e9800998ecf8427e'
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2009-01-30 19:54:58 +00:00
|
|
|
def test_upload_date
|
|
|
|
now = Time.now
|
|
|
|
orig_file_upload_date = nil
|
2009-02-05 15:10:41 +00:00
|
|
|
GridStore.open(@@db, 'foobar', 'r') { |f| orig_file_upload_date = f.upload_date }
|
2009-01-30 19:54:58 +00:00
|
|
|
assert_not_nil orig_file_upload_date
|
|
|
|
assert (orig_file_upload_date - now) < 5 # even a really slow system < 5 secs
|
|
|
|
|
|
|
|
sleep(2)
|
2009-02-05 15:10:41 +00:00
|
|
|
GridStore.open(@@db, 'foobar', 'w') { |f| f.write "new data" }
|
2009-01-30 19:54:58 +00:00
|
|
|
file_upload_date = nil
|
2009-02-05 15:10:41 +00:00
|
|
|
GridStore.open(@@db, 'foobar', 'r') { |f| file_upload_date = f.upload_date }
|
2009-01-30 19:54:58 +00:00
|
|
|
assert_equal orig_file_upload_date, file_upload_date
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_content_type
|
|
|
|
ct = nil
|
2009-02-05 15:10:41 +00:00
|
|
|
GridStore.open(@@db, 'foobar', 'r') { |f| ct = f.content_type }
|
2009-01-30 19:54:58 +00:00
|
|
|
assert_equal GridStore::DEFAULT_CONTENT_TYPE, ct
|
|
|
|
|
2009-02-05 15:10:41 +00:00
|
|
|
GridStore.open(@@db, 'foobar', 'w+') { |f| f.content_type = 'text/html' }
|
2009-01-30 19:54:58 +00:00
|
|
|
ct2 = nil
|
2009-02-05 15:10:41 +00:00
|
|
|
GridStore.open(@@db, 'foobar', 'r') { |f| ct2 = f.content_type }
|
2009-01-30 19:54:58 +00:00
|
|
|
assert_equal 'text/html', ct2
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_content_type_option
|
2009-02-05 15:10:41 +00:00
|
|
|
GridStore.open(@@db, 'new-file', 'w', :content_type => 'image/jpg') { |f| f.write('foo') }
|
2009-01-30 19:54:58 +00:00
|
|
|
ct = nil
|
2009-02-05 15:10:41 +00:00
|
|
|
GridStore.open(@@db, 'new-file', 'r') { |f| ct = f.content_type }
|
2009-01-30 19:54:58 +00:00
|
|
|
assert_equal 'image/jpg', ct
|
|
|
|
end
|
|
|
|
|
2009-01-30 21:44:29 +00:00
|
|
|
def test_unknown_mode
|
2009-02-05 15:10:41 +00:00
|
|
|
GridStore.open(@@db, 'foobar', 'x')
|
2009-01-30 21:44:29 +00:00
|
|
|
fail 'should have seen "illegal mode" error raised'
|
|
|
|
rescue => ex
|
|
|
|
assert_equal "error: illegal mode x", ex.to_s
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_metadata
|
2009-02-05 15:10:41 +00:00
|
|
|
GridStore.open(@@db, 'foobar', 'r') { |f| assert_nil f.metadata }
|
|
|
|
GridStore.open(@@db, 'foobar', 'w+') { |f| f.metadata = {'a' => 1} }
|
|
|
|
GridStore.open(@@db, 'foobar', 'r') { |f| assert_equal({'a' => 1}, f.metadata) }
|
2009-01-30 21:44:29 +00:00
|
|
|
end
|
|
|
|
|
2009-01-29 16:23:50 +00:00
|
|
|
end
|