mirror of
https://github.com/maybe-finance/maybe.git
synced 2025-08-05 05:25:24 +02:00
Account namespace updates: part 5 (valuations) (#901)
* Move Valuation to Account namespace * Move account history to controller * Clean up valuation controller and views * Translations and cleanup * Remove unused scopes and methods * Pass brakeman
This commit is contained in:
parent
0bc0d87768
commit
12380dc8ad
45 changed files with 478 additions and 346 deletions
71
test/controllers/account/valuations_controller_test.rb
Normal file
71
test/controllers/account/valuations_controller_test.rb
Normal file
|
@ -0,0 +1,71 @@
|
|||
require "test_helper"
|
||||
|
||||
class Account::ValuationsControllerTest < ActionDispatch::IntegrationTest
|
||||
setup do
|
||||
sign_in @user = users(:family_admin)
|
||||
@valuation = account_valuations(:savings_one)
|
||||
@account = @valuation.account
|
||||
end
|
||||
|
||||
test "get valuations for an account" do
|
||||
get account_valuations_url(@account)
|
||||
assert_response :success
|
||||
end
|
||||
|
||||
test "new" do
|
||||
get new_account_valuation_url(@account)
|
||||
assert_response :success
|
||||
end
|
||||
|
||||
test "should create valuation" do
|
||||
assert_difference("Account::Valuation.count") do
|
||||
post account_valuations_url(@account), params: {
|
||||
account_valuation: {
|
||||
value: 19800,
|
||||
date: Date.current
|
||||
}
|
||||
}
|
||||
end
|
||||
|
||||
assert_equal "Valuation created", flash[:notice]
|
||||
assert_enqueued_with job: AccountSyncJob
|
||||
assert_redirected_to account_path(@account)
|
||||
end
|
||||
|
||||
test "error when valuation already exists for date" do
|
||||
assert_difference("Account::Valuation.count", 0) do
|
||||
post account_valuations_url(@account), params: {
|
||||
account_valuation: {
|
||||
value: 19800,
|
||||
date: @valuation.date
|
||||
}
|
||||
}
|
||||
end
|
||||
|
||||
assert_equal "Date has already been taken", flash[:error]
|
||||
assert_redirected_to account_path(@account)
|
||||
end
|
||||
|
||||
test "should update valuation" do
|
||||
patch account_valuation_url(@account, @valuation), params: {
|
||||
account_valuation: {
|
||||
value: 19550,
|
||||
date: Date.current
|
||||
}
|
||||
}
|
||||
|
||||
assert_equal "Valuation updated", flash[:notice]
|
||||
assert_enqueued_with job: AccountSyncJob
|
||||
assert_redirected_to account_path(@account)
|
||||
end
|
||||
|
||||
test "should destroy valuation" do
|
||||
assert_difference("Account::Valuation.count", -1) do
|
||||
delete account_valuation_url(@account, @valuation)
|
||||
end
|
||||
|
||||
assert_equal "Valuation deleted", flash[:notice]
|
||||
assert_enqueued_with job: AccountSyncJob
|
||||
assert_redirected_to account_path(@account)
|
||||
end
|
||||
end
|
|
@ -44,7 +44,7 @@ class AccountsControllerTest < ActionDispatch::IntegrationTest
|
|||
end
|
||||
|
||||
test "should create an account" do
|
||||
assert_difference [ "Account.count", "Valuation.count" ], 1 do
|
||||
assert_difference [ "Account.count", "Account::Valuation.count" ], 1 do
|
||||
post accounts_path, params: {
|
||||
account: {
|
||||
accountable_type: "Depository",
|
||||
|
@ -60,7 +60,7 @@ class AccountsControllerTest < ActionDispatch::IntegrationTest
|
|||
end
|
||||
|
||||
test "can add optional start date and balance to an account on create" do
|
||||
assert_difference -> { Account.count } => 1, -> { Valuation.count } => 2 do
|
||||
assert_difference -> { Account.count } => 1, -> { Account::Valuation.count } => 2 do
|
||||
post accounts_path, params: {
|
||||
account: {
|
||||
accountable_type: "Depository",
|
||||
|
|
|
@ -75,7 +75,7 @@ class TransactionsControllerTest < ActionDispatch::IntegrationTest
|
|||
end
|
||||
|
||||
assert_equal transaction_params[:amount].to_d, Transaction.order(created_at: :desc).first.amount
|
||||
assert_equal flash[:notice], "New transaction created successfully"
|
||||
assert_equal "New transaction created successfully", flash[:notice]
|
||||
assert_enqueued_with(job: AccountSyncJob)
|
||||
assert_redirected_to transactions_url
|
||||
end
|
||||
|
|
|
@ -1,74 +0,0 @@
|
|||
require "test_helper"
|
||||
|
||||
class ValuationsControllerTest < ActionDispatch::IntegrationTest
|
||||
setup do
|
||||
sign_in @user = users(:family_admin)
|
||||
@valuation = valuations(:savings_one)
|
||||
@account = @valuation.account
|
||||
end
|
||||
|
||||
test "new" do
|
||||
get new_account_valuation_url(@account)
|
||||
assert_response :success
|
||||
end
|
||||
|
||||
test "should create valuation" do
|
||||
assert_difference("Valuation.count") do
|
||||
post account_valuations_url(@account), params: { valuation: { value: 1, date: Date.current, type: "Appraisal" } }
|
||||
end
|
||||
|
||||
assert_redirected_to account_path(@valuation.account)
|
||||
end
|
||||
|
||||
test "should create valuation with account's currency" do
|
||||
foreign_account = accounts(:eur_checking)
|
||||
post account_valuations_url(foreign_account), params: { valuation: { value: 1, date: Date.current, type: "Appraisal" } }
|
||||
assert_equal foreign_account.currency, Valuation.order(created_at: :desc).first.currency
|
||||
end
|
||||
|
||||
test "create should sync account with correct start date" do
|
||||
date = Date.current - 1.day
|
||||
|
||||
assert_enqueued_with(job: AccountSyncJob, args: [ @account, date ]) do
|
||||
post account_valuations_url(@account), params: { valuation: { value: 2, date:, type: "Appraisal" } }
|
||||
end
|
||||
end
|
||||
|
||||
test "should update valuation" do
|
||||
date = @valuation.date
|
||||
patch valuation_url(@valuation), params: { valuation: { account_id: @valuation.account_id, value: 1, date:, type: "Appraisal" } }
|
||||
assert_redirected_to account_path(@valuation.account)
|
||||
end
|
||||
|
||||
test "update should sync account with correct start date" do
|
||||
new_date = @valuation.date - 1.day
|
||||
assert_enqueued_with(job: AccountSyncJob, args: [ @account, new_date ]) do
|
||||
patch valuation_url(@valuation), params: { valuation: { account_id: @valuation.account_id, value: @valuation.value, date: new_date, type: "Appraisal" } }
|
||||
end
|
||||
|
||||
new_date = @valuation.reload.date + 1.day
|
||||
assert_enqueued_with(job: AccountSyncJob, args: [ @account, @valuation.date ]) do
|
||||
patch valuation_url(@valuation), params: { valuation: { account_id: @valuation.account_id, value: @valuation.value, date: new_date, type: "Appraisal" } }
|
||||
end
|
||||
end
|
||||
|
||||
test "should destroy valuation" do
|
||||
assert_difference("Valuation.count", -1) do
|
||||
delete valuation_url(@valuation)
|
||||
end
|
||||
|
||||
assert_redirected_to account_path(@account)
|
||||
end
|
||||
|
||||
test "destroy should sync account with correct start date" do
|
||||
first, second = @account.valuations.order(:date).all
|
||||
|
||||
assert_enqueued_with(job: AccountSyncJob, args: [ @account, first.date ]) do
|
||||
delete valuation_url(second)
|
||||
end
|
||||
|
||||
assert_enqueued_with(job: AccountSyncJob, args: [ @account, nil ]) do
|
||||
delete valuation_url(first)
|
||||
end
|
||||
end
|
||||
end
|
|
@ -1,6 +1,6 @@
|
|||
require "test_helper"
|
||||
|
||||
class TransferTest < ActiveSupport::TestCase
|
||||
class Account::TransferTest < ActiveSupport::TestCase
|
||||
setup do
|
||||
# Transfers can be posted on different dates
|
||||
@outflow = accounts(:checking).transactions.create! date: 1.day.ago.to_date, name: "Transfer to Savings", amount: 100, marked_as_transfer: true
|
39
test/models/account/valuation_test.rb
Normal file
39
test/models/account/valuation_test.rb
Normal file
|
@ -0,0 +1,39 @@
|
|||
require "test_helper"
|
||||
|
||||
class Account::ValuationTest < ActiveSupport::TestCase
|
||||
setup do
|
||||
@valuation = account_valuations :savings_one
|
||||
@family = families :dylan_family
|
||||
end
|
||||
|
||||
test "one valuation per day" do
|
||||
assert_equal 12.days.ago.to_date, account_valuations(:savings_one).date
|
||||
invalid_valuation = Account::Valuation.new date: 12.days.ago.to_date, value: 20000
|
||||
assert invalid_valuation.invalid?
|
||||
end
|
||||
|
||||
test "triggers sync with correct start date when valuation is set to prior date" do
|
||||
prior_date = @valuation.date - 1
|
||||
@valuation.update! date: prior_date
|
||||
|
||||
@valuation.account.expects(:sync_later).with(prior_date)
|
||||
@valuation.sync_account_later
|
||||
end
|
||||
|
||||
test "triggers sync with correct start date when valuation is set to future date" do
|
||||
prior_date = @valuation.date
|
||||
@valuation.update! date: @valuation.date + 1
|
||||
|
||||
@valuation.account.expects(:sync_later).with(prior_date)
|
||||
@valuation.sync_account_later
|
||||
end
|
||||
|
||||
test "triggers sync with correct start date when valuation deleted" do
|
||||
prior_valuation = account_valuations :savings_two # 25 days ago
|
||||
current_valuation = account_valuations :savings_one # 12 days ago
|
||||
current_valuation.destroy!
|
||||
|
||||
current_valuation.account.expects(:sync_later).with(prior_valuation.date)
|
||||
current_valuation.sync_account_later
|
||||
end
|
||||
end
|
|
@ -111,7 +111,7 @@ class AccountTest < ActiveSupport::TestCase
|
|||
end
|
||||
|
||||
test "should destroy dependent valuations" do
|
||||
assert_difference("Valuation.count", -@account.valuations.count) do
|
||||
assert_difference("Account::Valuation.count", -@account.valuations.count) do
|
||||
@account.destroy
|
||||
end
|
||||
end
|
||||
|
|
|
@ -11,11 +11,13 @@ class TimeSeries::TrendTest < ActiveSupport::TestCase
|
|||
test "up" do
|
||||
trend = TimeSeries::Trend.new(current: 100, previous: 50)
|
||||
assert_equal "up", trend.direction
|
||||
assert_equal "#10A861", trend.color
|
||||
end
|
||||
|
||||
test "down" do
|
||||
trend = TimeSeries::Trend.new(current: 50, previous: 100)
|
||||
assert_equal "down", trend.direction
|
||||
assert_equal "#F13636", trend.color
|
||||
end
|
||||
|
||||
test "flat" do
|
||||
|
@ -25,6 +27,7 @@ class TimeSeries::TrendTest < ActiveSupport::TestCase
|
|||
assert_equal "flat", trend1.direction
|
||||
assert_equal "flat", trend2.direction
|
||||
assert_equal "flat", trend3.direction
|
||||
assert_equal "#737373", trend1.color
|
||||
end
|
||||
|
||||
test "infinitely up" do
|
||||
|
|
|
@ -1,4 +0,0 @@
|
|||
require "test_helper"
|
||||
|
||||
class ValuationTest < ActiveSupport::TestCase
|
||||
end
|
Loading…
Add table
Add a link
Reference in a new issue