test/io_streams_test.rb in iostreams-1.2.0 vs test/io_streams_test.rb in iostreams-1.2.1
- old
+ new
@@ -1,10 +1,26 @@
require_relative "test_helper"
+require "json"
module IOStreams
class PathTest < Minitest::Test
describe IOStreams do
+ let :records do
+ [
+ {"name" => "Jack Jones", "login" => "jjones"},
+ {"name" => "Jill Smith", "login" => "jsmith"}
+ ]
+ end
+
+ let :expected_json do
+ records.collect(&:to_json).join("\n") + "\n"
+ end
+
+ let :json_file_name do
+ "/tmp/io_streams/abc.json"
+ end
+
describe ".root" do
it "return default path" do
path = ::File.expand_path(::File.join(__dir__, "../tmp/default"))
assert_equal path, IOStreams.root.to_s
end
@@ -57,9 +73,42 @@
it "s3" do
skip "TODO"
IOStreams.path("s3://a.xyz")
assert_equal :s3, path
+ end
+
+ it "hash writer detects json format from file name" do
+ path = IOStreams.path("/tmp/io_streams/abc.json")
+ path.writer(:hash) do |io|
+ records.each { |hash| io << hash }
+ end
+ actual = path.read
+ path.delete
+ assert_equal expected_json, actual
+ end
+
+ it "hash reader detects json format from file name" do
+ ::File.open(json_file_name, "wb") { |file| file.write(expected_json) }
+ rows = []
+ path = IOStreams.path("/tmp/io_streams/abc.json")
+ path.each(:hash) do |row|
+ rows << row
+ end
+ actual = rows.collect(&:to_json).join("\n") + "\n"
+ path.delete
+ assert_equal expected_json, actual
+ end
+
+ it "array writer detects json format from file name" do
+ path = IOStreams.path("/tmp/io_streams/abc.json")
+ path.writer(:array, columns: %w[name login]) do |io|
+ io << ["Jack Jones", "jjones"]
+ io << ["Jill Smith", "jsmith"]
+ end
+ actual = path.read
+ path.delete
+ assert_equal expected_json, actual
end
end
describe ".temp_file" do
it "returns value from block" do