Sha256: 37ab71ff3973b36eee246114de1eb7b04ffca5ddb628bf0e10acfe3919980dc1

Contents?: true

Size: 1.58 KB

Versions: 18

Compression:

Stored size: 1.58 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
    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

18 entries across 18 versions & 1 rubygems

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