From b200b712843252dcef00676710387a5dc0492705 Mon Sep 17 00:00:00 2001 From: Zach Gollwitzer Date: Wed, 17 Jul 2024 14:18:12 -0400 Subject: [PATCH] Add currency validation to account, update demo data generator (#996) * Add currency validation to account, update demo data generator * Fix tests --- app/models/account.rb | 2 +- app/models/demo/generator.rb | 16 ++++++-- test/controllers/accounts_controller_test.rb | 2 + .../transactions_controller_test.rb | 4 +- test/models/account/entry_test.rb | 8 ++-- test/models/family_test.rb | 39 +++++++++++-------- 6 files changed, 44 insertions(+), 27 deletions(-) diff --git a/app/models/account.rb b/app/models/account.rb index 98a9f843..7d999835 100644 --- a/app/models/account.rb +++ b/app/models/account.rb @@ -4,7 +4,7 @@ class Account < ApplicationRecord broadcasts_refreshes - validates :family, presence: true + validates :name, :balance, :currency, presence: true belongs_to :family belongs_to :institution, optional: true diff --git a/app/models/demo/generator.rb b/app/models/demo/generator.rb index 351483c6..b897064b 100644 --- a/app/models/demo/generator.rb +++ b/app/models/demo/generator.rb @@ -100,6 +100,7 @@ class Demo::Generator accountable: CreditCard.new, name: "Chase Credit Card", balance: 2300, + currency: "USD", institution: family.institutions.find_or_create_by(name: "Chase") 50.times do @@ -126,6 +127,7 @@ class Demo::Generator accountable: Depository.new, name: "Chase Checking", balance: 15000, + currency: "USD", institution: family.institutions.find_or_create_by(name: "Chase") 10.times do @@ -149,6 +151,7 @@ class Demo::Generator accountable: Depository.new, name: "Demo Savings", balance: 40000, + currency: "USD", subtype: "savings", institution: family.institutions.find_or_create_by(name: "Chase") @@ -195,6 +198,7 @@ class Demo::Generator accountable: Investment.new, name: "Robinhood", balance: 100000, + currency: "USD", institution: family.institutions.find_or_create_by(name: "Robinhood") aapl = Security.find_by(symbol: "AAPL") @@ -228,7 +232,8 @@ class Demo::Generator house = family.accounts.create! \ accountable: Property.new, name: "123 Maybe Way", - balance: 560000 + balance: 560000, + currency: "USD" create_valuation!(house, 3.years.ago.to_date, 520000) create_valuation!(house, 2.years.ago.to_date, 540000) @@ -237,19 +242,22 @@ class Demo::Generator family.accounts.create! \ accountable: Loan.new, name: "Mortgage", - balance: 495000 + balance: 495000, + currency: "USD" end def create_car_and_loan! family.accounts.create! \ accountable: Vehicle.new, name: "Honda Accord", - balance: 18000 + balance: 18000, + currency: "USD" family.accounts.create! \ accountable: Loan.new, name: "Car Loan", - balance: 8000 + balance: 8000, + currency: "USD" end def create_transaction!(attributes = {}) diff --git a/test/controllers/accounts_controller_test.rb b/test/controllers/accounts_controller_test.rb index 9f58c36a..dfed6d36 100644 --- a/test/controllers/accounts_controller_test.rb +++ b/test/controllers/accounts_controller_test.rb @@ -84,6 +84,7 @@ class AccountsControllerTest < ActionDispatch::IntegrationTest assert_difference [ "Account.count", "Account::Valuation.count", "Account::Entry.count" ], 1 do post accounts_path, params: { account: { + name: "Test", accountable_type: "Depository", balance: 200, currency: "USD", @@ -101,6 +102,7 @@ class AccountsControllerTest < ActionDispatch::IntegrationTest assert_difference -> { Account.count } => 1, -> { Account::Valuation.count } => 2 do post accounts_path, params: { account: { + name: "Test", accountable_type: "Depository", balance: 200, currency: "USD", diff --git a/test/controllers/transactions_controller_test.rb b/test/controllers/transactions_controller_test.rb index 5ef4d356..9dd63d37 100644 --- a/test/controllers/transactions_controller_test.rb +++ b/test/controllers/transactions_controller_test.rb @@ -88,7 +88,7 @@ class TransactionsControllerTest < ActionDispatch::IntegrationTest test "transaction count represents filtered total" do family = families(:empty) sign_in family.users.first - account = family.accounts.create! name: "Test", balance: 0, accountable: Depository.new + account = family.accounts.create! name: "Test", balance: 0, currency: "USD", accountable: Depository.new 3.times do create_transaction(account: account) @@ -110,7 +110,7 @@ class TransactionsControllerTest < ActionDispatch::IntegrationTest test "can paginate" do family = families(:empty) sign_in family.users.first - account = family.accounts.create! name: "Test", balance: 0, accountable: Depository.new + account = family.accounts.create! name: "Test", balance: 0, currency: "USD", accountable: Depository.new 11.times do create_transaction(account: account) diff --git a/test/models/account/entry_test.rb b/test/models/account/entry_test.rb index 71a49bc9..0beab68b 100644 --- a/test/models/account/entry_test.rb +++ b/test/models/account/entry_test.rb @@ -48,7 +48,7 @@ class Account::EntryTest < ActiveSupport::TestCase test "can search entries" do family = families(:empty) - account = family.accounts.create! name: "Test", balance: 0, accountable: Depository.new + account = family.accounts.create! name: "Test", balance: 0, currency: "USD", accountable: Depository.new category = family.categories.first merchant = family.merchants.first @@ -70,7 +70,7 @@ class Account::EntryTest < ActiveSupport::TestCase test "can calculate total spending for a group of transactions" do family = families(:empty) - account = family.accounts.create! name: "Test", balance: 0, accountable: Depository.new + account = family.accounts.create! name: "Test", balance: 0, currency: "USD", accountable: Depository.new create_transaction(account: account, amount: 100) create_transaction(account: account, amount: 100) create_transaction(account: account, amount: -500) # income, will be ignored @@ -80,7 +80,7 @@ class Account::EntryTest < ActiveSupport::TestCase test "can calculate total income for a group of transactions" do family = families(:empty) - account = family.accounts.create! name: "Test", balance: 0, accountable: Depository.new + account = family.accounts.create! name: "Test", balance: 0, currency: "USD", accountable: Depository.new create_transaction(account: account, amount: -100) create_transaction(account: account, amount: -100) create_transaction(account: account, amount: 500) # income, will be ignored @@ -95,7 +95,7 @@ class Account::EntryTest < ActiveSupport::TestCase end test "cannot sell more shares of stock than owned" do - account = families(:empty).accounts.create! name: "Test", balance: 0, accountable: Investment.new + account = families(:empty).accounts.create! name: "Test", balance: 0, currency: "USD", accountable: Investment.new security = securities(:aapl) error = assert_raises ActiveRecord::RecordInvalid do diff --git a/test/models/family_test.rb b/test/models/family_test.rb index de4b4b55..a992ef99 100644 --- a/test/models/family_test.rb +++ b/test/models/family_test.rb @@ -11,9 +11,9 @@ class FamilyTest < ActiveSupport::TestCase test "calculates assets" do assert_equal Money.new(0, @family.currency), @family.assets - @family.accounts.create!(balance: 1000, accountable: Depository.new) - @family.accounts.create!(balance: 5000, accountable: OtherAsset.new) - @family.accounts.create!(balance: 10000, accountable: CreditCard.new) # ignored + create_account(balance: 1000, accountable: Depository.new) + create_account(balance: 5000, accountable: OtherAsset.new) + create_account(balance: 10000, accountable: CreditCard.new) # ignored assert_equal Money.new(1000 + 5000, @family.currency), @family.assets end @@ -21,9 +21,9 @@ class FamilyTest < ActiveSupport::TestCase test "calculates liabilities" do assert_equal Money.new(0, @family.currency), @family.liabilities - @family.accounts.create!(balance: 1000, accountable: CreditCard.new) - @family.accounts.create!(balance: 5000, accountable: OtherLiability.new) - @family.accounts.create!(balance: 10000, accountable: Depository.new) # ignored + create_account(balance: 1000, accountable: CreditCard.new) + create_account(balance: 5000, accountable: OtherLiability.new) + create_account(balance: 10000, accountable: Depository.new) # ignored assert_equal Money.new(1000 + 5000, @family.currency), @family.liabilities end @@ -31,15 +31,15 @@ class FamilyTest < ActiveSupport::TestCase test "calculates net worth" do assert_equal Money.new(0, @family.currency), @family.net_worth - @family.accounts.create!(balance: 1000, accountable: CreditCard.new) - @family.accounts.create!(balance: 50000, accountable: Depository.new) + create_account(balance: 1000, accountable: CreditCard.new) + create_account(balance: 50000, accountable: Depository.new) assert_equal Money.new(50000 - 1000, @family.currency), @family.net_worth end test "should exclude disabled accounts from calculations" do - cc = @family.accounts.create!(balance: 1000, accountable: CreditCard.new) - @family.accounts.create!(balance: 50000, accountable: Depository.new) + cc = create_account(balance: 1000, accountable: CreditCard.new) + create_account(balance: 50000, accountable: Depository.new) assert_equal Money.new(50000 - 1000, @family.currency), @family.net_worth @@ -49,7 +49,7 @@ class FamilyTest < ActiveSupport::TestCase end test "syncs active accounts" do - account = @family.accounts.create!(balance: 1000, accountable: CreditCard.new, is_active: false) + account = create_account(balance: 1000, accountable: CreditCard.new, is_active: false) Account.any_instance.expects(:sync_later).never @@ -63,8 +63,8 @@ class FamilyTest < ActiveSupport::TestCase end test "calculates snapshot" do - asset = @family.accounts.create!(balance: 500, accountable: Depository.new) - liability = @family.accounts.create!(balance: 100, accountable: CreditCard.new) + asset = create_account(balance: 500, accountable: Depository.new) + liability = create_account(balance: 100, accountable: CreditCard.new) asset.balances.create! date: 1.day.ago.to_date, currency: "USD", balance: 450 asset.balances.create! date: Date.current, currency: "USD", balance: 500 @@ -93,8 +93,8 @@ class FamilyTest < ActiveSupport::TestCase end test "calculates top movers" do - checking_account = @family.accounts.create!(balance: 500, accountable: Depository.new) - savings_account = @family.accounts.create!(balance: 1000, accountable: Depository.new) + checking_account = create_account(balance: 500, accountable: Depository.new) + savings_account = create_account(balance: 1000, accountable: Depository.new) create_transaction(account: checking_account, date: 2.days.ago.to_date, amount: -1000) create_transaction(account: checking_account, date: 1.day.ago.to_date, amount: 10) @@ -115,7 +115,7 @@ class FamilyTest < ActiveSupport::TestCase end test "calculates rolling transaction totals" do - account = @family.accounts.create!(balance: 1000, accountable: Depository.new) + account = create_account(balance: 1000, accountable: Depository.new) create_transaction(account: account, date: 2.days.ago.to_date, amount: -500) create_transaction(account: account, date: 1.day.ago.to_date, amount: 100) create_transaction(account: account, date: Date.current, amount: 20) @@ -146,4 +146,11 @@ class FamilyTest < ActiveSupport::TestCase assert_equal expected_savings_rate_series, snapshot[:savings_rate_series].values.map(&:value).map { |v| v.round(2) } end + + private + + def create_account(attributes = {}) + account = @family.accounts.create! name: "Test", currency: "USD", **attributes + account + end end