From 77f166a5f8e452d6a51b7e72d4900e45c543eddc Mon Sep 17 00:00:00 2001 From: Jakub Kottnauer Date: Wed, 22 May 2024 14:12:56 +0200 Subject: [PATCH] Ignore empty categories while importing (#789) * Ignore empty categories while importing * Review fixes --- app/models/import.rb | 6 ++++-- app/views/transactions/categories/_badge.html.erb | 2 +- test/models/import/csv_test.rb | 2 +- test/models/import_test.rb | 15 ++++++++++++++- test/support/import_test_helper.rb | 6 +++--- 5 files changed, 23 insertions(+), 8 deletions(-) diff --git a/app/models/import.rb b/app/models/import.rb index 30076e57..af11d214 100644 --- a/app/models/import.rb +++ b/app/models/import.rb @@ -11,6 +11,8 @@ class Import < ApplicationRecord scope :ordered, -> { order(created_at: :desc) } + FALLBACK_TRANSACTION_NAME = "Imported transaction" + def publish_later ImportJob.perform_later(self) end @@ -110,9 +112,9 @@ class Import < ApplicationRecord transactions = [] csv.table.each do |row| - category = account.family.transaction_categories.find_or_initialize_by(name: row["category"]) + category = account.family.transaction_categories.find_or_initialize_by(name: row["category"]) if row["category"].present? txn = account.transactions.build \ - name: row["name"].presence || "Imported transaction", + name: row["name"].presence || FALLBACK_TRANSACTION_NAME, date: Date.iso8601(row["date"]), category: category, amount: BigDecimal(row["amount"]) * -1, # User inputs amounts with opposite signage of our internal representation diff --git a/app/views/transactions/categories/_badge.html.erb b/app/views/transactions/categories/_badge.html.erb index bc4a2918..c89c0a98 100644 --- a/app/views/transactions/categories/_badge.html.erb +++ b/app/views/transactions/categories/_badge.html.erb @@ -1,7 +1,7 @@ <%# locals: (category:) %> <% category ||= null_category %> - { Transaction::Category.count } => 2, -> { Transaction.count } => 2 do @loaded_import.publish end @@ -48,6 +48,19 @@ class ImportTest < ActiveSupport::TestCase assert @loaded_import.complete? end + test "publishes a valid import with missing data" do + @empty_import.update! raw_csv_str: valid_csv_with_missing_data + assert_difference -> { Transaction::Category.count } => 1, -> { Transaction.count } => 2 do + @empty_import.publish + end + + assert_not_nil Transaction.find_sole_by(name: Import::FALLBACK_TRANSACTION_NAME) + + @empty_import.reload + + assert @empty_import.complete? + end + test "failed publish results in error status" do @empty_import.update! raw_csv_str: valid_csv_with_invalid_values diff --git a/test/support/import_test_helper.rb b/test/support/import_test_helper.rb index 52ae44f0..85888570 100644 --- a/test/support/import_test_helper.rb +++ b/test/support/import_test_helper.rb @@ -14,11 +14,11 @@ module ImportTestHelper ROWS end - def valid_csv_with_extra_column + def valid_csv_with_missing_data <<-ROWS date,name,category,"optional id",amount - 2024-01-01,Starbucks drink,Food,1234,20 - 2024-01-02,Amazon stuff,Shopping,,200 + 2024-01-01,Drink,Food,1234,200 + 2024-01-02,,,,100 ROWS end