2024-07-01 10:49:43 -04:00
|
|
|
require "test_helper"
|
|
|
|
|
2025-04-14 11:40:34 -04:00
|
|
|
class EntryTest < ActiveSupport::TestCase
|
|
|
|
include EntriesTestHelper
|
2024-07-10 11:22:59 -04:00
|
|
|
|
2024-07-01 10:49:43 -04:00
|
|
|
setup do
|
2025-04-14 11:40:34 -04:00
|
|
|
@entry = entries :transaction
|
2024-07-01 10:49:43 -04:00
|
|
|
end
|
|
|
|
|
2024-07-26 10:47:27 -04:00
|
|
|
test "entry cannot be older than 10 years ago" do
|
|
|
|
assert_raises ActiveRecord::RecordInvalid do
|
|
|
|
@entry.update! date: 50.years.ago.to_date
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2024-07-01 10:49:43 -04:00
|
|
|
test "valuations cannot have more than one entry per day" do
|
2025-04-14 11:40:34 -04:00
|
|
|
existing_valuation = entries :valuation
|
2024-07-10 11:22:59 -04:00
|
|
|
|
2025-04-14 11:40:34 -04:00
|
|
|
new_valuation = Entry.new \
|
|
|
|
entryable: Valuation.new,
|
2024-07-16 09:26:49 -04:00
|
|
|
account: existing_valuation.account,
|
2024-07-10 11:22:59 -04:00
|
|
|
date: existing_valuation.date, # invalid
|
|
|
|
currency: existing_valuation.currency,
|
|
|
|
amount: existing_valuation.amount
|
2024-07-01 10:49:43 -04:00
|
|
|
|
2024-07-10 11:22:59 -04:00
|
|
|
assert new_valuation.invalid?
|
2024-07-01 10:49:43 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
test "triggers sync with correct start date when transaction is set to prior date" do
|
|
|
|
prior_date = @entry.date - 1
|
|
|
|
@entry.update! date: prior_date
|
|
|
|
|
2025-05-15 10:19:56 -04:00
|
|
|
@entry.account.expects(:sync_later).with(window_start_date: prior_date)
|
2024-07-01 10:49:43 -04:00
|
|
|
@entry.sync_account_later
|
|
|
|
end
|
|
|
|
|
|
|
|
test "triggers sync with correct start date when transaction is set to future date" do
|
|
|
|
prior_date = @entry.date
|
|
|
|
@entry.update! date: @entry.date + 1
|
|
|
|
|
2025-05-15 10:19:56 -04:00
|
|
|
@entry.account.expects(:sync_later).with(window_start_date: prior_date)
|
2024-07-01 10:49:43 -04:00
|
|
|
@entry.sync_account_later
|
|
|
|
end
|
|
|
|
|
|
|
|
test "triggers sync with correct start date when transaction deleted" do
|
2024-12-10 17:41:20 -05:00
|
|
|
@entry.destroy!
|
2024-07-10 11:22:59 -04:00
|
|
|
|
2025-05-15 10:19:56 -04:00
|
|
|
@entry.account.expects(:sync_later).with(window_start_date: nil)
|
2024-12-10 17:41:20 -05:00
|
|
|
@entry.sync_account_later
|
2024-07-01 10:49:43 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
test "can search entries" do
|
2024-07-10 11:22:59 -04:00
|
|
|
family = families(:empty)
|
2024-07-17 14:18:12 -04:00
|
|
|
account = family.accounts.create! name: "Test", balance: 0, currency: "USD", accountable: Depository.new
|
2024-07-10 11:22:59 -04:00
|
|
|
category = family.categories.first
|
|
|
|
merchant = family.merchants.first
|
|
|
|
|
|
|
|
create_transaction(account: account, name: "a transaction")
|
|
|
|
create_transaction(account: account, name: "ignored")
|
|
|
|
create_transaction(account: account, name: "third transaction", category: category, merchant: merchant)
|
|
|
|
|
2024-07-01 10:49:43 -04:00
|
|
|
params = { search: "a" }
|
|
|
|
|
2024-07-10 11:22:59 -04:00
|
|
|
assert_equal 2, family.entries.search(params).size
|
2024-07-01 10:49:43 -04:00
|
|
|
|
2024-07-16 15:26:14 +02:00
|
|
|
params = { search: "%" }
|
|
|
|
assert_equal 0, family.entries.search(params).size
|
2024-07-01 10:49:43 -04:00
|
|
|
end
|
|
|
|
|
2025-07-03 09:33:07 -04:00
|
|
|
test "visible scope only returns entries from visible accounts" do
|
2025-02-05 11:52:44 -06:00
|
|
|
# Create transactions for all account types
|
2025-07-03 09:33:07 -04:00
|
|
|
visible_transaction = create_transaction(account: accounts(:depository), name: "Visible transaction")
|
|
|
|
invisible_transaction = create_transaction(account: accounts(:credit_card), name: "Invisible transaction")
|
2025-02-05 11:52:44 -06:00
|
|
|
|
|
|
|
# Update account statuses
|
2025-07-03 09:33:07 -04:00
|
|
|
accounts(:credit_card).disable!
|
2025-02-05 11:52:44 -06:00
|
|
|
|
|
|
|
# Test the scope
|
2025-07-03 09:33:07 -04:00
|
|
|
visible_entries = Entry.visible
|
2025-02-05 11:52:44 -06:00
|
|
|
|
|
|
|
# Should include entry from active account
|
2025-07-03 09:33:07 -04:00
|
|
|
assert_includes visible_entries, visible_transaction
|
2025-02-05 11:52:44 -06:00
|
|
|
|
2025-07-03 09:33:07 -04:00
|
|
|
# Should not include entry from disabled account
|
|
|
|
assert_not_includes visible_entries, invisible_transaction
|
2025-02-05 11:52:44 -06:00
|
|
|
end
|
2024-07-01 10:49:43 -04:00
|
|
|
end
|