diff --git a/CHANGELOG.md b/CHANGELOG.md index 373f1e4..9e0aaaa 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,8 @@ ## [Unreleased] -* no unreleased changes + +### Fixed +* Fix bug in VCF metadata +* Fix bug where table columns are mutated by a regexp column ## 11.3.0/ 2025-02-11 ### Fixed diff --git a/lib/ndr_import/table.rb b/lib/ndr_import/table.rb index 27711d3..8f6274d 100644 --- a/lib/ndr_import/table.rb +++ b/lib/ndr_import/table.rb @@ -34,6 +34,8 @@ def initialize(options = {}) end @row_index = 0 + # Keep a copy of the original column mappings for use later if columns are mutated + @original_columns = @columns.deep_dup end def match(filename, tablename) @@ -51,12 +53,18 @@ def transform(lines, &block) @header_valid = false @header_best_guess = nil @notifier.try(:started) - last_col = last_column_to_transform skip_footer_lines(lines, footer_lines).each do |line| line.is_a?(Array) ? process_line(line[0..last_col], &block) : process_line(line, &block) end + # @columns may have been mutated where column is a regular expression. + # We want to restore `@columns` back to its original state, so the column regexp + # will work on the next file + @columns = @original_columns + # Also ensure that @masked_mappings are recalculated + @masked_mappings = nil + @notifier.try(:finished) end diff --git a/lib/ndr_import/vcf/table.rb b/lib/ndr_import/vcf/table.rb index 4359bf7..ef8c6ba 100644 --- a/lib/ndr_import/vcf/table.rb +++ b/lib/ndr_import/vcf/table.rb @@ -5,10 +5,14 @@ module Vcf # Syntatic sugar to ensure `header_lines` and `footer_lines` are 1 and 0 respectively. # All other Table logic is inherited from `NdrImport::Table` class Table < ::NdrImport::Table + VCF_OPTIONS = %w[vcf_file_metadata].freeze + def self.all_valid_options - super - %w[delimiter header_lines footer_lines] + %w[vcf_file_metadata] + super - %w[delimiter header_lines footer_lines] + VCF_OPTIONS end + attr_reader(*VCF_OPTIONS) + def header_lines 1 end diff --git a/test/resources/regex_column_names.zip b/test/resources/regex_column_names.zip new file mode 100644 index 0000000..c654615 Binary files /dev/null and b/test/resources/regex_column_names.zip differ diff --git a/test/universal_importer_helper_test.rb b/test/universal_importer_helper_test.rb index c1e7fcf..1449582 100644 --- a/test/universal_importer_helper_test.rb +++ b/test/universal_importer_helper_test.rb @@ -222,6 +222,30 @@ def setup assert_equal 4, table_enums.first.last.count end + test 'mutated columns get reset' do + table_mappings = [ + NdrImport::Table.new(filename_pattern: /\.csv\z/i, + canonical_name: 'a_table', + header_lines: 1, + footer_lines: 0, + klass: 'SomeTestClass', + columns: [{ 'column' => 'one' }, + { 'column' => 'two' }, + { 'column' => /\A[AB]\d{3}\z/i }]) + ] + source_file = @permanent_test_files.join('regex_column_names.zip') + @test_importer.stubs(:get_table_mapping).returns(table_mappings.first) + mapped_rows = [] + @test_importer.extract(source_file) { |table, rows| mapped_rows << table.transform(rows).to_a } + + expected_mapped_data = [ + [['SomeTestClass', { rawtext: { 'one' => '2', 'two' => '2', 'b456' => '2' } }, 1]], + [['SomeTestClass', { rawtext: { 'one' => '1', 'two' => '1', 'a123' => '1' } }, 1]] + ] + + assert_equal expected_mapped_data, mapped_rows + end + test 'get_notifier' do class TestImporterWithoutNotifier include NdrImport::UniversalImporterHelper