Skip to content

Commit

Permalink
Fix #59 #67 : Add warning message when input is corrupted
Browse files Browse the repository at this point in the history
  • Loading branch information
felginep committed Aug 24, 2021
1 parent 3a24935 commit d42175f
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 0 deletions.
8 changes: 8 additions & 0 deletions lib/ad_localize/mappers/locale_wording_to_hash.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions test/fixtures/exports_reference_json_bad_input/en.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"en":{"aaa":{"bbb":"first"},"bbb":{"ccc":"second"},"zzz":"first"}}
6 changes: 6 additions & 0 deletions test/fixtures/reference_json_bad_input.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
,key,en,
,aaa.bbb,first,
,aaa.bbb.ccc,second,
,bbb.ccc.ddd,first,
,bbb.ccc,second,
,zzz,first
20 changes: 20 additions & 0 deletions test/interactors/execute_export_request_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down

0 comments on commit d42175f

Please sign in to comment.