diff --git a/lib/mongo/gridfs/grid.rb b/lib/mongo/gridfs/grid.rb index 14c240c..05a3158 100644 --- a/lib/mongo/gridfs/grid.rb +++ b/lib/mongo/gridfs/grid.rb @@ -45,7 +45,7 @@ module Mongo end # Store a file in the file store. This method is designed only for writing new files; - # if you need to update a given file, first delete it using #Grid#delete. + # if you need to update a given file, first delete it using Grid#delete. # # Note that arbitary metadata attributes can be saved to the file by passing # them in as options. diff --git a/lib/mongo/gridfs/grid_io.rb b/lib/mongo/gridfs/grid_io.rb index 2d9ca36..982e238 100644 --- a/lib/mongo/gridfs/grid_io.rb +++ b/lib/mongo/gridfs/grid_io.rb @@ -191,11 +191,14 @@ module Mongo end id end - + # Read a chunk of the data from the file and yield it to the given - # block. It will read from the current file position. + # block. # - # @param [Block] A block called with each chunk + # Note that this method reads from the current file position. + # + # @yield Yields on chunk per iteration as defined by this file's + # chunk size. # # @return [Mongo::GridIO] self def each diff --git a/test/data/data.tar.gz b/test/data/data.tar.gz deleted file mode 100644 index 8f0f560..0000000 Binary files a/test/data/data.tar.gz and /dev/null differ diff --git a/test/data/sample_data b/test/data/sample_data new file mode 100644 index 0000000..9e0f96a Binary files /dev/null and b/test/data/sample_data differ diff --git a/test/grid_io_test.rb b/test/grid_io_test.rb index 3a8789a..4226159 100644 --- a/test/grid_io_test.rb +++ b/test/grid_io_test.rb @@ -62,7 +62,6 @@ class GridIOTest < Test::Unit::TestCase end context "Content types" do - if defined?(MIME) should "determine common content types from the extension" do file = GridIO.new(@files, @chunks, 'sample.pdf', 'w') diff --git a/test/grid_test.rb b/test/grid_test.rb index 6ec0e05..184f23b 100644 --- a/test/grid_test.rb +++ b/test/grid_test.rb @@ -18,7 +18,8 @@ class GridTest < Test::Unit::TestCase setup do @data = "GRIDDATA" * 50000 @grid = Grid.new(@db, 'test-fs') - @id = @grid.put(@data, :filename => 'sample', :metadata => {'app' => 'photos'}) + @id = @grid.put(@data, :filename => 'sample', + :metadata => {'app' => 'photos'}) end should "check existence" do @@ -120,7 +121,8 @@ class GridTest < Test::Unit::TestCase context "Storing data with a length of zero" do setup do @grid = Grid.new(@db, 'test-fs') - @id = @grid.put('', :filename => 'sample', :metadata => {'app' => 'photos'}) + @id = @grid.put('', :filename => 'sample', + :metadata => {'app' => 'photos'}) end should "return the zero length" do @@ -129,6 +131,34 @@ class GridTest < Test::Unit::TestCase end end + context "Grid streaming: " do + setup do + @grid = Grid.new(@db, 'test-fs') + filename = 'sample_data' + @io = File.open(File.join(File.dirname(__FILE__), 'data', filename), 'r') + id = @grid.put(@io, :filename => filename) + @file = @grid.get(id) + @io.rewind + @data = @io.read + if @data.respond_to?(:force_encoding) + @data.force_encoding("binary") + end + end + + should "read the file" do + read_data = "" + @file.each do |chunk| + read_data << chunk + end + assert_equal @data.length, read_data.length + end + + should "read the file if no block is given" do + read_data = @file.each + assert_equal @data.length, read_data.length + end + end + context "Streaming: " do || {} setup do def read_and_write_stream(filename, read_length, opts={}) @@ -158,12 +188,12 @@ class GridTest < Test::Unit::TestCase 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) + should "put and get a large io object if reading less than the chunk size" do + read_and_write_stream('sample_data', 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) + should "put and get a large io object if reading more than the chunk size" do + read_and_write_stream('sample_data', 300 * 1024) end end end