mirror of
https://github.com/we-promise/sure.git
synced 2026-04-19 12:04:08 +00:00
Initial split transaction support (#1230)
* Initial split transaction support * Add support to unsplit and edit split * Update show.html.erb * FIX address reviews * Improve UX * Update show.html.erb * Reviews * Update edit.html.erb * Add parent category to dialog * Update en.yml * Add UI indication to totals * FIX ui update * Add category select like rest of app --------- Signed-off-by: Juan José Mata <juanjo.mata@gmail.com> Co-authored-by: Juan José Mata <juanjo.mata@gmail.com>
This commit is contained in:
212
test/controllers/splits_controller_test.rb
Normal file
212
test/controllers/splits_controller_test.rb
Normal file
@@ -0,0 +1,212 @@
|
||||
require "test_helper"
|
||||
|
||||
class SplitsControllerTest < ActionDispatch::IntegrationTest
|
||||
include EntriesTestHelper
|
||||
|
||||
setup do
|
||||
sign_in @user = users(:family_admin)
|
||||
@entry = create_transaction(
|
||||
amount: 100,
|
||||
name: "Grocery Store",
|
||||
account: accounts(:depository)
|
||||
)
|
||||
end
|
||||
|
||||
test "new renders split editor" do
|
||||
get new_transaction_split_path(@entry)
|
||||
assert_response :success
|
||||
end
|
||||
|
||||
test "create with valid params splits transaction" do
|
||||
assert_difference "Entry.count", 2 do
|
||||
post transaction_split_path(@entry), params: {
|
||||
split: {
|
||||
splits: [
|
||||
{ name: "Groceries", amount: "-70", category_id: categories(:food_and_drink).id },
|
||||
{ name: "Household", amount: "-30", category_id: "" }
|
||||
]
|
||||
}
|
||||
}
|
||||
end
|
||||
|
||||
assert_redirected_to transactions_url
|
||||
assert_equal I18n.t("splits.create.success"), flash[:notice]
|
||||
assert @entry.reload.excluded?
|
||||
assert @entry.split_parent?
|
||||
end
|
||||
|
||||
test "create with mismatched amounts rejects" do
|
||||
assert_no_difference "Entry.count" do
|
||||
post transaction_split_path(@entry), params: {
|
||||
split: {
|
||||
splits: [
|
||||
{ name: "Part 1", amount: "-60", category_id: "" },
|
||||
{ name: "Part 2", amount: "-20", category_id: "" }
|
||||
]
|
||||
}
|
||||
}
|
||||
end
|
||||
|
||||
assert_redirected_to transactions_url
|
||||
assert flash[:alert].present?
|
||||
end
|
||||
|
||||
test "destroy unsplits transaction" do
|
||||
@entry.split!([
|
||||
{ name: "Part 1", amount: 50, category_id: nil },
|
||||
{ name: "Part 2", amount: 50, category_id: nil }
|
||||
])
|
||||
|
||||
assert_difference "Entry.count", -2 do
|
||||
delete transaction_split_path(@entry)
|
||||
end
|
||||
|
||||
assert_redirected_to transactions_url
|
||||
assert_equal I18n.t("splits.destroy.success"), flash[:notice]
|
||||
refute @entry.reload.excluded?
|
||||
end
|
||||
|
||||
test "create with income transaction applies correct sign" do
|
||||
income_entry = create_transaction(
|
||||
amount: -400,
|
||||
name: "Reimbursement",
|
||||
account: accounts(:depository)
|
||||
)
|
||||
|
||||
assert_difference "Entry.count", 2 do
|
||||
post transaction_split_path(income_entry), params: {
|
||||
split: {
|
||||
splits: [
|
||||
{ name: "Part 1", amount: "200", category_id: "" },
|
||||
{ name: "Part 2", amount: "200", category_id: "" }
|
||||
]
|
||||
}
|
||||
}
|
||||
end
|
||||
|
||||
assert income_entry.reload.excluded?
|
||||
children = income_entry.child_entries
|
||||
assert_equal(-200, children.first.amount.to_i)
|
||||
assert_equal(-200, children.last.amount.to_i)
|
||||
end
|
||||
|
||||
test "create with mixed sign amounts on expense" do
|
||||
assert_difference "Entry.count", 2 do
|
||||
post transaction_split_path(@entry), params: {
|
||||
split: {
|
||||
splits: [
|
||||
{ name: "Main expense", amount: "-130", category_id: "" },
|
||||
{ name: "Refund", amount: "30", category_id: "" }
|
||||
]
|
||||
}
|
||||
}
|
||||
end
|
||||
|
||||
assert @entry.reload.excluded?
|
||||
children = @entry.child_entries.order(:amount)
|
||||
assert_equal(-30, children.first.amount.to_i)
|
||||
assert_equal 130, children.last.amount.to_i
|
||||
end
|
||||
|
||||
test "only family members can access splits" do
|
||||
other_family_entry = create_transaction(
|
||||
amount: 100,
|
||||
name: "Other",
|
||||
account: accounts(:depository)
|
||||
)
|
||||
|
||||
# This should work since both belong to same family
|
||||
get new_transaction_split_path(other_family_entry)
|
||||
assert_response :success
|
||||
end
|
||||
|
||||
# Edit action tests
|
||||
test "edit renders with existing children pre-filled" do
|
||||
@entry.split!([
|
||||
{ name: "Part 1", amount: 60, category_id: nil },
|
||||
{ name: "Part 2", amount: 40, category_id: nil }
|
||||
])
|
||||
|
||||
get edit_transaction_split_path(@entry)
|
||||
assert_response :success
|
||||
end
|
||||
|
||||
test "edit on a child redirects to parent edit" do
|
||||
@entry.split!([
|
||||
{ name: "Part 1", amount: 60, category_id: nil },
|
||||
{ name: "Part 2", amount: 40, category_id: nil }
|
||||
])
|
||||
child = @entry.child_entries.first
|
||||
|
||||
get edit_transaction_split_path(child)
|
||||
assert_response :success
|
||||
end
|
||||
|
||||
test "edit on a non-split entry redirects with alert" do
|
||||
get edit_transaction_split_path(@entry)
|
||||
assert_redirected_to transactions_url
|
||||
assert_equal I18n.t("splits.edit.not_split"), flash[:alert]
|
||||
end
|
||||
|
||||
# Update action tests
|
||||
test "update modifies split entries" do
|
||||
@entry.split!([
|
||||
{ name: "Part 1", amount: 60, category_id: nil },
|
||||
{ name: "Part 2", amount: 40, category_id: nil }
|
||||
])
|
||||
|
||||
patch transaction_split_path(@entry), params: {
|
||||
split: {
|
||||
splits: [
|
||||
{ name: "Food", amount: "-50", category_id: categories(:food_and_drink).id },
|
||||
{ name: "Transport", amount: "-30", category_id: "" },
|
||||
{ name: "Other", amount: "-20", category_id: "" }
|
||||
]
|
||||
}
|
||||
}
|
||||
|
||||
assert_redirected_to transactions_url
|
||||
assert_equal I18n.t("splits.update.success"), flash[:notice]
|
||||
@entry.reload
|
||||
assert @entry.split_parent?
|
||||
assert_equal 3, @entry.child_entries.count
|
||||
end
|
||||
|
||||
test "update with mismatched amounts rejects" do
|
||||
@entry.split!([
|
||||
{ name: "Part 1", amount: 60, category_id: nil },
|
||||
{ name: "Part 2", amount: 40, category_id: nil }
|
||||
])
|
||||
|
||||
patch transaction_split_path(@entry), params: {
|
||||
split: {
|
||||
splits: [
|
||||
{ name: "Part 1", amount: "-70", category_id: "" },
|
||||
{ name: "Part 2", amount: "-20", category_id: "" }
|
||||
]
|
||||
}
|
||||
}
|
||||
|
||||
assert_redirected_to transactions_url
|
||||
assert flash[:alert].present?
|
||||
# Original splits should remain intact
|
||||
assert_equal 2, @entry.reload.child_entries.count
|
||||
end
|
||||
|
||||
# Destroy from child tests
|
||||
test "destroy from child resolves to parent and unsplits" do
|
||||
@entry.split!([
|
||||
{ name: "Part 1", amount: 60, category_id: nil },
|
||||
{ name: "Part 2", amount: 40, category_id: nil }
|
||||
])
|
||||
child = @entry.child_entries.first
|
||||
|
||||
assert_difference "Entry.count", -2 do
|
||||
delete transaction_split_path(child)
|
||||
end
|
||||
|
||||
assert_redirected_to transactions_url
|
||||
assert_equal I18n.t("splits.destroy.success"), flash[:notice]
|
||||
refute @entry.reload.excluded?
|
||||
end
|
||||
end
|
||||
177
test/models/entry_split_test.rb
Normal file
177
test/models/entry_split_test.rb
Normal file
@@ -0,0 +1,177 @@
|
||||
require "test_helper"
|
||||
|
||||
class EntrySplitTest < ActiveSupport::TestCase
|
||||
include EntriesTestHelper
|
||||
|
||||
setup do
|
||||
@entry = create_transaction(
|
||||
amount: 100,
|
||||
name: "Grocery Store",
|
||||
account: accounts(:depository),
|
||||
category: categories(:food_and_drink)
|
||||
)
|
||||
end
|
||||
|
||||
test "split! creates child entries with correct amounts and marks parent excluded" do
|
||||
splits = [
|
||||
{ name: "Groceries", amount: 70, category_id: categories(:food_and_drink).id },
|
||||
{ name: "Household", amount: 30, category_id: nil }
|
||||
]
|
||||
|
||||
children = @entry.split!(splits)
|
||||
|
||||
assert_equal 2, children.size
|
||||
assert_equal 70, children.first.amount
|
||||
assert_equal 30, children.last.amount
|
||||
assert @entry.reload.excluded?
|
||||
assert @entry.split_parent?
|
||||
end
|
||||
|
||||
test "split! rejects when amounts don't sum to parent" do
|
||||
splits = [
|
||||
{ name: "Part 1", amount: 60, category_id: nil },
|
||||
{ name: "Part 2", amount: 30, category_id: nil }
|
||||
]
|
||||
|
||||
assert_raises(ActiveRecord::RecordInvalid) do
|
||||
@entry.split!(splits)
|
||||
end
|
||||
end
|
||||
|
||||
test "split! allows mixed positive and negative amounts that sum to parent" do
|
||||
splits = [
|
||||
{ name: "Main expense", amount: 130, category_id: nil },
|
||||
{ name: "Refund", amount: -30, category_id: nil }
|
||||
]
|
||||
|
||||
children = @entry.split!(splits)
|
||||
|
||||
assert_equal 2, children.size
|
||||
assert_equal 130, children.first.amount
|
||||
assert_equal(-30, children.last.amount)
|
||||
end
|
||||
|
||||
test "cannot split transfers" do
|
||||
transfer = create_transfer(
|
||||
from_account: accounts(:depository),
|
||||
to_account: accounts(:credit_card),
|
||||
amount: 100
|
||||
)
|
||||
outflow_transaction = transfer.outflow_transaction
|
||||
|
||||
refute outflow_transaction.splittable?
|
||||
end
|
||||
|
||||
test "cannot split already-split parent" do
|
||||
@entry.split!([
|
||||
{ name: "Part 1", amount: 50, category_id: nil },
|
||||
{ name: "Part 2", amount: 50, category_id: nil }
|
||||
])
|
||||
|
||||
refute @entry.entryable.splittable?
|
||||
end
|
||||
|
||||
test "cannot split child entry" do
|
||||
children = @entry.split!([
|
||||
{ name: "Part 1", amount: 50, category_id: nil },
|
||||
{ name: "Part 2", amount: 50, category_id: nil }
|
||||
])
|
||||
|
||||
refute children.first.entryable.splittable?
|
||||
end
|
||||
|
||||
test "unsplit! removes children and restores parent" do
|
||||
@entry.split!([
|
||||
{ name: "Part 1", amount: 50, category_id: nil },
|
||||
{ name: "Part 2", amount: 50, category_id: nil }
|
||||
])
|
||||
|
||||
assert @entry.reload.excluded?
|
||||
assert_equal 2, @entry.child_entries.count
|
||||
|
||||
@entry.unsplit!
|
||||
|
||||
refute @entry.reload.excluded?
|
||||
assert_equal 0, @entry.child_entries.count
|
||||
end
|
||||
|
||||
test "parent deletion cascades to children" do
|
||||
@entry.split!([
|
||||
{ name: "Part 1", amount: 50, category_id: nil },
|
||||
{ name: "Part 2", amount: 50, category_id: nil }
|
||||
])
|
||||
|
||||
child_ids = @entry.child_entries.pluck(:id)
|
||||
|
||||
@entry.destroy!
|
||||
|
||||
assert_empty Entry.where(id: child_ids)
|
||||
end
|
||||
|
||||
test "individual child deletion is blocked" do
|
||||
children = @entry.split!([
|
||||
{ name: "Part 1", amount: 50, category_id: nil },
|
||||
{ name: "Part 2", amount: 50, category_id: nil }
|
||||
])
|
||||
|
||||
refute children.first.destroy
|
||||
assert children.first.persisted?
|
||||
end
|
||||
|
||||
test "split parent cannot be un-excluded" do
|
||||
@entry.split!([
|
||||
{ name: "Part 1", amount: 50, category_id: nil },
|
||||
{ name: "Part 2", amount: 50, category_id: nil }
|
||||
])
|
||||
|
||||
@entry.reload
|
||||
@entry.excluded = false
|
||||
refute @entry.valid?
|
||||
assert_includes @entry.errors[:excluded], "cannot be toggled off for a split transaction"
|
||||
end
|
||||
|
||||
test "excluding_split_parents scope excludes parents with children" do
|
||||
@entry.split!([
|
||||
{ name: "Part 1", amount: 50, category_id: nil },
|
||||
{ name: "Part 2", amount: 50, category_id: nil }
|
||||
])
|
||||
|
||||
scope = Entry.excluding_split_parents.where(account: accounts(:depository))
|
||||
refute_includes scope.pluck(:id), @entry.id
|
||||
assert_includes scope.pluck(:id), @entry.child_entries.first.id
|
||||
end
|
||||
|
||||
test "children inherit parent's account, date, and currency" do
|
||||
children = @entry.split!([
|
||||
{ name: "Part 1", amount: 50, category_id: nil },
|
||||
{ name: "Part 2", amount: 50, category_id: nil }
|
||||
])
|
||||
|
||||
children.each do |child|
|
||||
assert_equal @entry.account_id, child.account_id
|
||||
assert_equal @entry.date, child.date
|
||||
assert_equal @entry.currency, child.currency
|
||||
end
|
||||
end
|
||||
|
||||
test "split_parent? returns true when entry has children" do
|
||||
refute @entry.split_parent?
|
||||
|
||||
@entry.split!([
|
||||
{ name: "Part 1", amount: 50, category_id: nil },
|
||||
{ name: "Part 2", amount: 50, category_id: nil }
|
||||
])
|
||||
|
||||
assert @entry.split_parent?
|
||||
end
|
||||
|
||||
test "split_child? returns true for child entries" do
|
||||
children = @entry.split!([
|
||||
{ name: "Part 1", amount: 50, category_id: nil },
|
||||
{ name: "Part 2", amount: 50, category_id: nil }
|
||||
])
|
||||
|
||||
assert children.first.split_child?
|
||||
refute @entry.split_child?
|
||||
end
|
||||
end
|
||||
Reference in New Issue
Block a user