The personal finance app for everyone (by everyone)
Ruby
QIF (and others): Revert imports whose transactions were split (#5676)
Sailing747ddc20e91bb7
diff --git a/app/models/import.rb b/app/models/import.rbindex 284bc727e9..94bf09df2e 100644--- a/app/models/import.rb+++ b/app/models/import.rb@@ -336,7 +336,9 @@ class Import < ApplicationRecorddef revertImport.transaction doaccounts.destroy_all- entries.destroy_all+ # Split parts carry the import too (Entry#split! copies it), but may+ # only be destroyed through their parent, which takes them along.+ entries.where(parent_entry_id: nil).destroy_allrevert_derived_state!enddiff --git a/test/models/qif_import_test.rb b/test/models/qif_import_test.rbindex 82765c0d51..4aed26c3ad 100644--- a/test/models/qif_import_test.rb+++ b/test/models/qif_import_test.rb@@ -852,6 +852,23 @@ class QifImportTest < ActiveSupport::TestCaseassert supplies.import_locked?end+ test "revert removes split transactions imported into an existing account" do+ @import.update!(raw_file_str: QIF_WITH_SPLITS)+ @import.generate_rows_from_csv+ @import.sync_mappings++ assert_difference "Entry.count", 4 do+ @import.import!+ end++ assert_difference "Entry.count", -4 do+ @import.revert+ end++ assert @import.reload.pending?, @import.error+ assert_empty @import.entries+ end+test "import! creates split children from QIF split lines with detected non-default date format" doqif = <<~QIF!Type:Bankdiff --git a/test/models/transaction_import_test.rb b/test/models/transaction_import_test.rbindex 6987eb18bc..f652ca7186 100644--- a/test/models/transaction_import_test.rb+++ b/test/models/transaction_import_test.rb@@ -369,6 +369,38 @@ class TransactionImportTest < ActiveSupport::TestCaseassert_not account.entries.exists?(name: "Bakery")end+ test "revert removes an imported transaction that was split afterwards" do+ account = accounts(:depository)++ @import.update!(+ account: account,+ raw_file_str: "date,name,amount\n01/01/2024,Supermarket,100\n01/02/2024,Bakery,20\n",+ date_col_label: "date",+ amount_col_label: "amount",+ name_col_label: "name",+ date_format: "%m/%d/%Y",+ amount_type_strategy: "signed_amount",+ signage_convention: "inflows_negative"+ )+ @import.generate_rows_from_csv+ @import.reload+ @import.publish+ assert @import.reload.complete?++ supermarket = account.entries.find_by!(name: "Supermarket")+ supermarket.split!([+ { name: "Groceries", amount: supermarket.amount - 30 },+ { name: "Household", amount: 30 }+ ])++ assert_difference -> { Entry.count } => -4 do+ @import.revert+ end++ assert @import.reload.pending?, @import.error+ assert_not account.entries.exists?(name: %w[Supermarket Groceries Household Bakery])+ end+test "creates new transaction when name differs even if date and amount match" doaccount = accounts(:depository)