From 3cb1e4644b6703ee0064233bae9ad940d85063ac Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dani=C3=ABl=20van=20de=20Burgt?= Date: Tue, 10 May 2011 22:57:22 -0500 Subject: [PATCH] added failing test: reading chunks from an empty (zero-length) grid-stored file --- test/data/empty_data | 0 test/grid_test.rb | 38 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 38 insertions(+) create mode 100644 test/data/empty_data diff --git a/test/data/empty_data b/test/data/empty_data new file mode 100644 index 0000000..e69de29 diff --git a/test/grid_test.rb b/test/grid_test.rb index f4b972c..d1962fa 100644 --- a/test/grid_test.rb +++ b/test/grid_test.rb @@ -13,6 +13,7 @@ def read_and_write_stream(filename, read_length, opts={}) read_data = "" while(chunk = file.read(read_length)) read_data << chunk + break if chunk.empty? end assert_equal data.length, read_data.length end @@ -195,6 +196,39 @@ class GridTest < Test::Unit::TestCase end end + context "Grid streaming an empty file: " do + setup do + @grid = Grid.new(@db, 'test-fs') + filename = 'empty_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 "be equal in length" do + @io.rewind + assert_equal @io.read.length, @file.read.length + 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 @grid = Grid.new(@db, 'test-fs') @@ -204,6 +238,10 @@ class GridTest < Test::Unit::TestCase read_and_write_stream('small_data.txt', 1, :chunk_size => 2) end + should "put and get an empty io object" do + read_and_write_stream('empty_data', 1) + end + should "put and get a small io object" do read_and_write_stream('small_data.txt', 1) end