The personal finance app for everyone
Add currency validation to account, update demo data generator (#996)
Zach Gollwitzerb200b7128432
diff --git a/app/models/account.rb b/app/models/account.rbindex 98a9f843..7d999835 100644--- a/app/models/account.rb+++ b/app/models/account.rb@@ -4,7 +4,7 @@ class Account < ApplicationRecordbroadcasts_refreshes- validates :family, presence: true+ validates :name, :balance, :currency, presence: truebelongs_to :familybelongs_to :institution, optional: truediff --git a/app/models/demo/generator.rb b/app/models/demo/generator.rbindex 351483c6..b897064b 100644--- a/app/models/demo/generator.rb+++ b/app/models/demo/generator.rb@@ -100,6 +100,7 @@ class Demo::Generatoraccountable: 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::Generatoraccountable: 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::Generatoraccountable: 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::Generatoraccountable: 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::Generatorhouse = 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::Generatorfamily.accounts.create! \accountable: Loan.new,name: "Mortgage",- balance: 495000+ balance: 495000,+ currency: "USD"enddef 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"enddef create_transaction!(attributes = {})diff --git a/test/controllers/accounts_controller_test.rb b/test/controllers/accounts_controller_test.rbindex 9f58c36a..dfed6d36 100644--- a/test/controllers/accounts_controller_test.rb+++ b/test/controllers/accounts_controller_test.rb@@ -84,6 +84,7 @@ class AccountsControllerTest < ActionDispatch::IntegrationTestassert_difference [ "Account.count", "Account::Valuation.count", "Account::Entry.count" ], 1 dopost accounts_path, params: {account: {+ name: "Test",accountable_type: "Depository",balance: 200,currency: "USD",@@ -101,6 +102,7 @@ class AccountsControllerTest < ActionDispatch::IntegrationTestassert_difference -> { Account.count } => 1, -> { Account::Valuation.count } => 2 dopost 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.rbindex 5ef4d356..9dd63d37 100644--- a/test/controllers/transactions_controller_test.rb+++ b/test/controllers/transactions_controller_test.rb@@ -88,7 +88,7 @@ class TransactionsControllerTest < ActionDispatch::IntegrationTesttest "transaction count represents filtered total" dofamily = 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.new3.times docreate_transaction(account: account)@@ -110,7 +110,7 @@ class TransactionsControllerTest < ActionDispatch::IntegrationTesttest "can paginate" dofamily = 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.new11.times docreate_transaction(account: account)diff --git a/test/models/account/entry_test.rb b/test/models/account/entry_test.rbindex 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::TestCasetest "can search entries" dofamily = 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.newcategory = family.categories.firstmerchant = family.merchants.first@@ -70,7 +70,7 @@ class Account::EntryTest < ActiveSupport::TestCasetest "can calculate total spending for a group of transactions" dofamily = 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.newcreate_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::TestCasetest "can calculate total income for a group of transactions" dofamily = 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.newcreate_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::TestCaseendtest "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.newsecurity = securities(:aapl)error = assert_raises ActiveRecord::RecordInvalid dodiff --git a/test/models/family_test.rb b/test/models/family_test.rbindex de4b4b55..a992ef99 100644--- a/test/models/family_test.rb+++ b/test/models/family_test.rb@@ -11,9 +11,9 @@ class FamilyTest < ActiveSupport::TestCasetest "calculates assets" doassert_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) # ignoredassert_equal Money.new(1000 + 5000, @family.currency), @family.assetsend@@ -21,9 +21,9 @@ class FamilyTest < ActiveSupport::TestCasetest "calculates liabilities" doassert_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) # ignoredassert_equal Money.new(1000 + 5000, @family.currency), @family.liabilitiesend@@ -31,15 +31,15 @@ class FamilyTest < ActiveSupport::TestCasetest "calculates net worth" doassert_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_worthendtest "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::TestCaseendtest "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::TestCaseendtest "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: 450asset.balances.create! date: Date.current, currency: "USD", balance: 500@@ -93,8 +93,8 @@ class FamilyTest < ActiveSupport::TestCaseendtest "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::TestCaseendtest "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::TestCaseassert_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+ endend