From d42175fac92c84a4b56f1f46db8155962c262684 Mon Sep 17 00:00:00 2001 From: Pierre Felgines Date: Tue, 24 Aug 2021 14:29:30 +0200 Subject: [PATCH] Fix #59 #67 : Add warning message when input is corrupted --- .../mappers/locale_wording_to_hash.rb | 8 ++++++++ .../exports_reference_json_bad_input/en.json | 1 + test/fixtures/reference_json_bad_input.csv | 6 ++++++ .../execute_export_request_test.rb | 20 +++++++++++++++++++ 4 files changed, 35 insertions(+) create mode 100644 test/fixtures/exports_reference_json_bad_input/en.json create mode 100644 test/fixtures/reference_json_bad_input.csv diff --git a/lib/ad_localize/mappers/locale_wording_to_hash.rb b/lib/ad_localize/mappers/locale_wording_to_hash.rb index 827d4d6..98a7a0d 100644 --- a/lib/ad_localize/mappers/locale_wording_to_hash.rb +++ b/lib/ad_localize/mappers/locale_wording_to_hash.rb @@ -10,6 +10,14 @@ def map(locale_wording:) hash[translation.key.label] = {} unless hash.key? translation.key.label hash[translation.key.label][translation.key.plural_key] = translation.value else + unless hash.is_a?(Hash) + LOGGER.warn "Corrupted input. Trying to insert a value for key '#{translation.key.label}' but a value already exists for '#{inner_keys[0..index-1].join(".")}'. Skipping." + break # skip this corrupted key + end + previous_value = hash[inner_key.to_s] + if !previous_value.blank? && previous_value.is_a?(Hash) + LOGGER.warn "Corrupted input. Trying to insert a value for key '#{translation.key.label}' but values already exist for keys '#{translation.key.label}.*'. Previous values will be lost." + end hash[inner_key.to_s] = translation.value end else diff --git a/test/fixtures/exports_reference_json_bad_input/en.json b/test/fixtures/exports_reference_json_bad_input/en.json new file mode 100644 index 0000000..a08e555 --- /dev/null +++ b/test/fixtures/exports_reference_json_bad_input/en.json @@ -0,0 +1 @@ +{"en":{"aaa":{"bbb":"first"},"bbb":{"ccc":"second"},"zzz":"first"}} \ No newline at end of file diff --git a/test/fixtures/reference_json_bad_input.csv b/test/fixtures/reference_json_bad_input.csv new file mode 100644 index 0000000..738bbb1 --- /dev/null +++ b/test/fixtures/reference_json_bad_input.csv @@ -0,0 +1,6 @@ +,key,en, +,aaa.bbb,first, +,aaa.bbb.ccc,second, +,bbb.ccc.ddd,first, +,bbb.ccc,second, +,zzz,first diff --git a/test/interactors/execute_export_request_test.rb b/test/interactors/execute_export_request_test.rb index 5d529f5..c15313e 100644 --- a/test/interactors/execute_export_request_test.rb +++ b/test/interactors/execute_export_request_test.rb @@ -13,6 +13,26 @@ def teardown FileUtils.rm_rf('exports') end + test 'it should export json file with bad inputs' do + # Given + csv_file = "test/fixtures/reference_json_bad_input.csv" + reference_dir = "test/fixtures/exports_reference_json_bad_input" + assert(File.exist?(csv_file), "File does not exists #{csv_file}") + assert(File.exist?(reference_dir), "File does not exists #{reference_dir}") + + # When + export_request = Requests::ExportRequest.new(csv_paths: [csv_file], platforms: 'json') + ExecuteExportRequest.new.call(export_request: export_request) + + # Then + reference_file = "#{reference_dir}/en.json" + generated_file = "exports/en.json" + assert(File.exist?(reference_file), "File does not exists #{reference_file}") + assert(File.exist?(generated_file), "File does not exists #{generated_file}") + diff = Diffy::Diff.new(reference_file, generated_file, :source => 'files') + assert_empty(diff.to_s, "File #{generated_file} do not match reference. Diff: \n\n#{diff}\n") + end + test 'it should export correct platforms files' do # Given csv_file = "test/fixtures/reference.csv"