Sha256: 043484601ccd000194f00b25a8f0f142d3a4a72c6d7685b6e58fdc691ae3629d

Contents?: true

Size: 1.59 KB

Versions: 19

Compression:

Stored size: 1.59 KB

Contents

require_relative "test_helper"
require "csv"

class RecordWriterTest < Minitest::Test
  describe IOStreams::Record::Writer do
    let :csv_file_name do
      File.join(File.dirname(__FILE__), "files", "test.csv")
    end

    let :json_file_name do
      File.join(File.dirname(__FILE__), "files", "test.json")
    end

    let :raw_csv_data do
      File.read(csv_file_name)
    end

    let :raw_json_data do
      File.read(json_file_name)
    end

    let :csv_rows do
      CSV.read(csv_file_name)
    end

    let :inputs do
      rows   = csv_rows.dup
      header = rows.shift
      rows.collect { |row| header.zip(row).to_h }
    end

    let :temp_file do
      Tempfile.new("iostreams")
    end

    let :file_name do
      temp_file.path
    end

    after do
      temp_file.delete
    end

    describe "#<<" do
      it "file" do
        IOStreams::Record::Writer.file(file_name) do |io|
          inputs.each { |hash| io << hash }
        end
        result = File.read(file_name)
        assert_equal raw_csv_data, result
      end

      it "json file" do
        IOStreams::Record::Writer.file(file_name, file_name: "abc.json") do |io|
          inputs.each { |hash| io << hash }
        end
        result = File.read(file_name)
        assert_equal raw_json_data, result
      end

      it "stream" do
        io_string = StringIO.new
        IOStreams::Line::Writer.stream(io_string) do |io|
          IOStreams::Record::Writer.stream(io) do |stream|
            inputs.each { |row| stream << row }
          end
        end
        assert_equal raw_csv_data, io_string.string
      end
    end
  end
end

Version data entries

19 entries across 19 versions & 1 rubygems

Version Path
iostreams-1.10.2 test/record_writer_test.rb
iostreams-1.10.1 test/record_writer_test.rb
iostreams-1.10.0 test/record_writer_test.rb
iostreams-1.9.0 test/record_writer_test.rb
iostreams-1.8.0 test/record_writer_test.rb
iostreams-1.7.0 test/record_writer_test.rb
iostreams-1.6.2 test/record_writer_test.rb
iostreams-1.6.1 test/record_writer_test.rb
iostreams-1.6.0 test/record_writer_test.rb
iostreams-1.5.1 test/record_writer_test.rb
iostreams-1.5.0 test/record_writer_test.rb
iostreams-1.4.0 test/record_writer_test.rb
iostreams-1.3.3 test/record_writer_test.rb
iostreams-1.3.2 test/record_writer_test.rb
iostreams-1.3.1 test/record_writer_test.rb
iostreams-1.3.0 test/record_writer_test.rb
iostreams-1.2.1 test/record_writer_test.rb
iostreams-1.2.0 test/record_writer_test.rb
iostreams-1.1.1 test/record_writer_test.rb