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 ChunkTest < Test::Unit::TestCase
|
|
|
|
|
2009-08-20 14:50:48 +00:00
|
|
|
include Mongo
|
|
|
|
include GridFS
|
2009-01-29 16:23:50 +00:00
|
|
|
|
2009-08-20 22:48:09 +00:00
|
|
|
@@db = Connection.new(ENV['MONGO_RUBY_DRIVER_HOST'] || 'localhost',
|
|
|
|
ENV['MONGO_RUBY_DRIVER_PORT'] || Connection::DEFAULT_PORT).db('ruby-mongo-utils-test')
|
2009-02-05 15:10:41 +00:00
|
|
|
@@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
|
2009-10-20 15:31:07 +00:00
|
|
|
@@chunks.remove
|
|
|
|
@@files.remove
|
2009-01-29 16:23:50 +00:00
|
|
|
|
2009-02-05 15:10:41 +00:00
|
|
|
@f = GridStore.new(@@db, 'foobar', 'w')
|
2009-01-30 21:44:29 +00:00
|
|
|
@c = @f.instance_variable_get('@curr_chunk')
|
2009-01-29 16:23:50 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def teardown
|
2009-10-20 15:31:07 +00:00
|
|
|
@@chunks.remove
|
|
|
|
@@files.remove
|
2009-03-13 21:09:19 +00:00
|
|
|
@@db.error
|
2009-01-29 16:23:50 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_pos
|
|
|
|
assert_equal 0, @c.pos
|
|
|
|
assert @c.eof? # since data is empty
|
|
|
|
|
|
|
|
b = ByteBuffer.new
|
|
|
|
3.times { |i| b.put(i) }
|
2009-01-30 21:44:29 +00:00
|
|
|
c = Chunk.new(@f, 'data' => b)
|
2009-01-29 16:23:50 +00:00
|
|
|
assert !c.eof?
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_getc
|
|
|
|
b = ByteBuffer.new
|
|
|
|
3.times { |i| b.put(i) }
|
2009-01-30 21:44:29 +00:00
|
|
|
c = Chunk.new(@f, 'data' => b)
|
2009-01-29 16:23:50 +00:00
|
|
|
|
|
|
|
assert !c.eof?
|
|
|
|
assert_equal 0, c.getc
|
|
|
|
assert !c.eof?
|
|
|
|
assert_equal 1, c.getc
|
|
|
|
assert !c.eof?
|
|
|
|
assert_equal 2, c.getc
|
|
|
|
assert c.eof?
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_putc
|
|
|
|
3.times { |i| @c.putc(i) }
|
|
|
|
@c.pos = 0
|
|
|
|
|
|
|
|
assert !@c.eof?
|
|
|
|
assert_equal 0, @c.getc
|
|
|
|
assert !@c.eof?
|
|
|
|
assert_equal 1, @c.getc
|
|
|
|
assert !@c.eof?
|
|
|
|
assert_equal 2, @c.getc
|
|
|
|
assert @c.eof?
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_truncate
|
|
|
|
10.times { |i| @c.putc(i) }
|
|
|
|
assert_equal 10, @c.size
|
|
|
|
@c.pos = 3
|
|
|
|
@c.truncate
|
|
|
|
assert_equal 3, @c.size
|
|
|
|
|
|
|
|
@c.pos = 0
|
|
|
|
assert !@c.eof?
|
|
|
|
assert_equal 0, @c.getc
|
|
|
|
assert !@c.eof?
|
|
|
|
assert_equal 1, @c.getc
|
|
|
|
assert !@c.eof?
|
|
|
|
assert_equal 2, @c.getc
|
|
|
|
assert @c.eof?
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|