From 8eb80ee6f0c9433a26fe722bde604b7a9b72fc76 Mon Sep 17 00:00:00 2001 From: Tilo Sloboda Date: Sun, 10 Dec 2023 17:07:20 -0800 Subject: [PATCH] test coverage --- lib/smarter_csv/headers.rb | 2 +- spec/smarter_csv/basic_spec.rb | 20 ++++++++++++++++++++ spec/smarter_csv/key_mapping_spec.rb | 26 ++++++++++++++++++++++++++ 3 files changed, 47 insertions(+), 1 deletion(-) diff --git a/lib/smarter_csv/headers.rb b/lib/smarter_csv/headers.rb index c0d533a5..d659697e 100644 --- a/lib/smarter_csv/headers.rb +++ b/lib/smarter_csv/headers.rb @@ -91,7 +91,7 @@ def disambiguate_headers(headers, options) def remap_headers(headers, options) key_mapping = options[:key_mapping] if key_mapping.empty? || !key_mapping.is_a?(Hash) || key_mapping.keys.empty? - raise(SmarterCSV::IncorrectOption, "Error: incorrect format for key_mapping! Expecting hash with from -> to mappings") + raise(SmarterCSV::IncorrectOption, "ERROR: incorrect format for key_mapping! Expecting hash with from -> to mappings") end key_mapping = options[:key_mapping] diff --git a/spec/smarter_csv/basic_spec.rb b/spec/smarter_csv/basic_spec.rb index bb40bb0c..d10f65b7 100644 --- a/spec/smarter_csv/basic_spec.rb +++ b/spec/smarter_csv/basic_spec.rb @@ -68,6 +68,26 @@ end.to raise_exception(SmarterCSV::HeaderSizeMismatch) end end + + context 'with empty user_provided_headers' do + let(:options) { super().merge({user_provided_headers: []}) } + + it 'raises an exception if the user_provided_headers is empty' do + expect do + SmarterCSV.process("#{fixture_path}/basic.csv", options) + end.to raise_exception(SmarterCSV::IncorrectOption, /ERROR: incorrect format for user_provided_headers! Expecting array with headers/) + end + end + + context 'with incorrect user_provided_headers' do + let(:options) { super().merge({user_provided_headers: {}}) } + + it 'raises an exception if the user_provided_headers is of incorrect type' do + expect do + SmarterCSV.process("#{fixture_path}/basic.csv", options) + end.to raise_exception(SmarterCSV::IncorrectOption, /ERROR: incorrect format for user_provided_headers! Expecting array with headers/) + end + end end end end diff --git a/spec/smarter_csv/key_mapping_spec.rb b/spec/smarter_csv/key_mapping_spec.rb index c3c7cbfc..39e61b5f 100644 --- a/spec/smarter_csv/key_mapping_spec.rb +++ b/spec/smarter_csv/key_mapping_spec.rb @@ -18,6 +18,32 @@ end end + context 'with empty key_mapping' do + let(:options) { {key_mapping: {}} } + + it 'raises an exception if the key_mapping is empty' do + expect do + SmarterCSV.process("#{fixture_path}/basic.csv", options) + end.to raise_exception( + SmarterCSV::IncorrectOption, + /ERROR: incorrect format for key_mapping! Expecting hash with from -> to mappings/ + ) + end + end + + context 'with incorrect key_mapping' do + let(:options) { {key_mapping: []} } + + it 'raises an exception if the key_mapping is of incorrect type' do + expect do + SmarterCSV.process("#{fixture_path}/basic.csv", options) + end.to raise_exception( + SmarterCSV::IncorrectOption, + /ERROR: incorrect format for key_mapping! Expecting hash with from -> to mappings/ + ) + end + end + it 'remove_values_matching' do options = {remove_zero_values: true, key_mapping: {first_name: :vorname, last_name: :nachname} } data = SmarterCSV.process("#{fixture_path}/basic.csv", options)