require 'test_helper' require 'stream_wrapper' include StreamWrapper class StreamWrapper::GzipMultiFileTest < Test::Unit::TestCase context "A GzipMultiFile with one file" do setup { @sw = GzipMultiFile.new fixture_file("log1.log") } should "read from that file with #gets" do assert_equal("Log 1 line 1\n", @sw.gets) end should "return nil from #gets after the last line" do @sw.gets assert_equal(nil, @sw.gets) end end context "A GzipMultiFile with data to read" do setup { @sw = GzipMultiFile.new fixture_file("log1.log") } should "not be #eof?" do assert ! @sw.eof? end end context "A GzipMultiFile with no data left to read" do setup do @sw = GzipMultiFile.new fixture_file("log1.log") @sw.gets end should "be #eof?" do assert @sw.eof? end end context "A GzipMultiFile with two files" do setup do @sw = GzipMultiFile.new [fixture_file("log1.log"), fixture_file("log2.log")] end should "read the line from file 1 first" do assert_equal("Log 1 line 1\n", @sw.gets) end should "read the lines from file 2 after file 1" do @sw.gets assert_equal("Log 2 line 1\n", @sw.gets) assert_equal("Log 2 line 2\n", @sw.gets) assert_equal(nil, @sw.gets) end should "return nil when file 2 is empty" do 3.times { @sw.gets } assert_equal(nil, @sw.gets) end end context "A GzipMultiFile without files" do setup do @stdin = $stdin $stdin = StringIO.new "Line 1\nLine 2\n" @sw = GzipMultiFile.new end teardown do $stdin = @stdin end should "read from STDIN" do assert_equal("Line 1\n", @sw.gets) assert_equal("Line 2\n", @sw.gets) assert_equal(nil, @sw.gets) end end context "A GzipMultiFile with one gzipped file" do setup do @sw = GzipMultiFile.new fixture_file("log1.log.gz") end should "read the unzipped data with #gets" do assert_equal("Log 1 line 1\n", @sw.gets) end should "return nil from #gets after the last line" do @sw.gets assert_equal(nil, @sw.gets) end end context "A GzipMultiFile with two gzipped files" do setup do @sw = GzipMultiFile.new [fixture_file("log1.log.gz"), fixture_file("log2.log.gz")] end should "read the line from file 1 first" do assert_equal("Log 1 line 1\n", @sw.gets) end should "read the lines from file 2 after file 1" do @sw.gets assert_equal("Log 2 line 1\n", @sw.gets) assert_equal("Log 2 line 2\n", @sw.gets) assert_equal(nil, @sw.gets) end should "return nil when file 2 is empty" do 3.times { @sw.gets } assert_equal(nil, @sw.gets) end end context "If one of the files is empty, the GzipMultiFile" do setup do @sw = GzipMultiFile.new [fixture_file("log0.log"), fixture_file("log2.log")] end should "ignore that file without a glitch" do assert_equal("Log 2 line 1\n", @sw.gets) end end context "#shell_command" do should "cat regular files" do command = GzipMultiFile.new.send :shell_command, "file.log" assert_match /^cat file.log/, command end should "unzip zipped files" do command = GzipMultiFile.new.send :shell_command, "file.gz" assert_match /^gunzip -c file.gz/, command end should "add filter for shop" do command = GzipMultiFile.new("", {:shop_id => "www.24.se"}).send :shell_command, "file.gz" assert_match /grep -F sid=www\.24\.se/, command end should "add filter for account" do command = GzipMultiFile.new("", {:account_id => "jetshop"}).send :shell_command, "file.gz" assert_match /grep -F aid=jetshop/, command end should "add filter for type" do command = GzipMultiFile.new("", {:observation_type => "view_item"}).send :shell_command, "file.gz" assert_match /grep -F o=view_item/, command end end end