Sha256: 62343e6eb36797ce46c188dbfde7d9cde229cc0d319a10fa84c0bd69806ea743

Contents?: true

Size: 1.8 KB

Versions: 2

Compression:

Stored size: 1.8 KB

Contents

require_relative "test_helper"

class RecordReaderTest < Minitest::Test
  describe IOStreams::Record::Reader do
    let :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 :csv_rows do
      CSV.read(file_name)
    end

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

    describe "#each" do
      it "csv file" do
        records = []
        IOStreams::Record::Reader.file(file_name, cleanse_header: false) do |io|
          io.each { |row| records << row }
        end
        assert_equal expected, records
      end

      it "json file" do
        records = []
        IOStreams::Record::Reader.file(json_file_name, cleanse_header: false, format: :json) do |input|
          input.each { |row| records << row }
        end
        assert_equal expected, records
      end

      it "stream" do
        rows = []
        IOStreams::Line::Reader.file(file_name) do |file|
          IOStreams::Record::Reader.stream(file, cleanse_header: false) do |io|
            io.each { |row| rows << row }
          end
        end
        assert_equal expected, rows
      end

      it "with no block returns enumerator" do
        records = IOStreams::Record::Reader.file(file_name, cleanse_header: false) do |io|
          io.each.first(100)
        end
        assert_equal expected, records
      end
    end

    describe "#collect" do
      it "json file" do
        records = IOStreams::Record::Reader.file(json_file_name, format: :json) do |input|
          input.collect { |record| record["state"] }
        end
        assert_equal expected.collect { |record| record["state"] }, records
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
iostreams-1.10.1 test/record_reader_test.rb
iostreams-1.10.0 test/record_reader_test.rb