Skip to content

armgit / sure

Public Imported from GitHub
0

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.rb
index 284bc727e9..94bf09df2e 100644
--- a/app/models/import.rb
+++ b/app/models/import.rb
@@ -336,7 +336,9 @@ class Import < ApplicationRecord
def revert
Import.transaction do
accounts.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_all
revert_derived_state!
end
diff --git a/test/models/qif_import_test.rb b/test/models/qif_import_test.rb
index 82765c0d51..4aed26c3ad 100644
--- a/test/models/qif_import_test.rb
+++ b/test/models/qif_import_test.rb
@@ -852,6 +852,23 @@ class QifImportTest < ActiveSupport::TestCase
assert 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" do
qif = <<~QIF
!Type:Bank
diff --git a/test/models/transaction_import_test.rb b/test/models/transaction_import_test.rb
index 6987eb18bc..f652ca7186 100644
--- a/test/models/transaction_import_test.rb
+++ b/test/models/transaction_import_test.rb
@@ -369,6 +369,38 @@ class TransactionImportTest < ActiveSupport::TestCase
assert_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" do
account = accounts(:depository)