require_relative '../test_helper' class FixedStorageTest < PipelineTest LAYOUT = DataPipeline::Layout.new do record 'KOPF', [['SATYP', 'AN', 4, true], ['BELNR', 'AN', 6, true], ['DATUM', 'DAT', 8]] record 'POS', [['SATYP', 'AN', 4, true], ['TEXT', 'AN', 10], ['BETRAG', 'N', '7.2']] end ROWS = [{ 'satyp' => 'KOPF', 'belnr' => 'R1', 'datum' => Date.new(2026, 10, 9) }, { 'satyp' => 'POS', 'text' => 'Müller', 'betrag' => 12.5 }, { 'satyp' => 'POS', 'betrag' => -3 }].freeze def test_write_and_read_back with_tmpdir do |dir| DataPipeline::FixedStorage.new(file: "#{dir}/out.txt", layout: LAYOUT).write(ROWS) assert_equal "KOPFR1 20261009\nPOS Müller 00012.50\nPOS -0003.00\n", File.read("#{dir}/out.txt") assert_equal [{ 'satyp' => 'KOPF', 'belnr' => 'R1', 'datum' => '20261009' }, { 'satyp' => 'POS', 'text' => 'Müller', 'betrag' => '00012.50' }, { 'satyp' => 'POS', 'text' => nil, 'betrag' => '-0003.00' }], DataPipeline::FixedStorage.new(file: "#{dir}/out.txt", layout: LAYOUT).read end end def test_encoding_line_end_and_streaming_read with_tmpdir do |dir| DataPipeline::FixedStorage.new(file: "#{dir}/out.txt", layout: LAYOUT, encoding: 'ISO-8859-1', line_end: "\r\n").write(ROWS.first(2)) assert_equal "KOPFR1 20261009\r\nPOS M\xFCller 00012.50\r\n".b, File.binread("#{dir}/out.txt") rows = [] DataPipeline::FixedStorage.new(file: "#{dir}/out.txt", layout: LAYOUT, encoding: 'ISO-8859-1').read { |row| rows << row } assert_equal 'Müller', rows.last['text'] end end def test_pipeline_with_container_and_dry_run with_tmpdir do |dir| rows = ROWS.map(&:dup) out = capture_stdout do DataPipeline.new do logger PipelineHelper::QUIET dry_run read MemoryStorage.new(rows:) write container(:fixed, file: "#{dir}/dry.txt", layout: LAYOUT) end.run end refute File.exist?("#{dir}/dry.txt") assert_includes out, 'DRY RUN' DataPipeline.new do logger PipelineHelper::QUIET read MemoryStorage.new(rows:) write container(:fixed, file: "#{dir}/out.txt", layout: LAYOUT) end.run assert_equal 3, File.readlines("#{dir}/out.txt").size end end def test_options_and_lengths_required assert_raises(ArgumentError) { DataPipeline::FixedStorage.new(layout: LAYOUT) } assert_raises(ArgumentError) { DataPipeline::FixedStorage.new(file: 'x') } layout = DataPipeline::Layout.new { record 'A', [['SATYP', 'AN']] } assert_raises(DataPipeline::LayoutError) { DataPipeline::FixedStorage.new(file: 'x', layout:) } end end