mirror of
https://github.com/maybe-finance/maybe.git
synced 2025-07-24 23:59:40 +02:00
Handle missing exchange rate provider, allow fallback for missing rates (#955)
* Clean up exchange rate logic * Remove stale method
This commit is contained in:
parent
bef335c631
commit
6767aaed1d
20 changed files with 383 additions and 609 deletions
388
test/fixtures/exchange_rates.yml
vendored
388
test/fixtures/exchange_rates.yml
vendored
|
@ -1,383 +1,11 @@
|
|||
day_31_ago_eur_to_usd:
|
||||
base_currency: EUR
|
||||
converted_currency: USD
|
||||
one:
|
||||
from_currency: EUR
|
||||
to_currency: GBP
|
||||
rate: 1.0986
|
||||
date: <%= 31.days.ago.to_date %>
|
||||
date: <%= Date.current %>
|
||||
|
||||
day_30_ago_eur_to_usd:
|
||||
base_currency: EUR
|
||||
converted_currency: USD
|
||||
two:
|
||||
from_currency: EUR
|
||||
to_currency: GBP
|
||||
rate: 1.0926
|
||||
date: <%= 30.days.ago.to_date %>
|
||||
|
||||
day_29_ago_eur_to_usd:
|
||||
base_currency: EUR
|
||||
converted_currency: USD
|
||||
rate: 1.094
|
||||
date: <%= 29.days.ago.to_date %>
|
||||
|
||||
day_28_ago_eur_to_usd:
|
||||
base_currency: EUR
|
||||
converted_currency: USD
|
||||
rate: 1.095
|
||||
date: <%= 28.days.ago.to_date %>
|
||||
|
||||
day_27_ago_eur_to_usd:
|
||||
base_currency: EUR
|
||||
converted_currency: USD
|
||||
rate: 1.0898
|
||||
date: <%= 27.days.ago.to_date %>
|
||||
|
||||
day_26_ago_eur_to_usd:
|
||||
base_currency: EUR
|
||||
converted_currency: USD
|
||||
rate: 1.0858
|
||||
date: <%= 26.days.ago.to_date %>
|
||||
|
||||
day_25_ago_eur_to_usd:
|
||||
base_currency: EUR
|
||||
converted_currency: USD
|
||||
rate: 1.0856
|
||||
date: <%= 25.days.ago.to_date %>
|
||||
|
||||
day_24_ago_eur_to_usd:
|
||||
base_currency: EUR
|
||||
converted_currency: USD
|
||||
rate: 1.084
|
||||
date: <%= 24.days.ago.to_date %>
|
||||
|
||||
day_23_ago_eur_to_usd:
|
||||
base_currency: EUR
|
||||
converted_currency: USD
|
||||
rate: 1.0807
|
||||
date: <%= 23.days.ago.to_date %>
|
||||
|
||||
day_22_ago_eur_to_usd:
|
||||
base_currency: EUR
|
||||
converted_currency: USD
|
||||
rate: 1.0839
|
||||
date: <%= 22.days.ago.to_date %>
|
||||
|
||||
day_21_ago_eur_to_usd:
|
||||
base_currency: EUR
|
||||
converted_currency: USD
|
||||
rate: 1.0845
|
||||
date: <%= 21.days.ago.to_date %>
|
||||
|
||||
day_20_ago_eur_to_usd:
|
||||
base_currency: EUR
|
||||
converted_currency: USD
|
||||
rate: 1.0854
|
||||
date: <%= 20.days.ago.to_date %>
|
||||
|
||||
day_19_ago_eur_to_usd:
|
||||
base_currency: EUR
|
||||
converted_currency: USD
|
||||
rate: 1.0822
|
||||
date: <%= 19.days.ago.to_date %>
|
||||
|
||||
day_18_ago_eur_to_usd:
|
||||
base_currency: EUR
|
||||
converted_currency: USD
|
||||
rate: 1.0824
|
||||
date: <%= 18.days.ago.to_date %>
|
||||
|
||||
day_17_ago_eur_to_usd:
|
||||
base_currency: EUR
|
||||
converted_currency: USD
|
||||
rate: 1.0818
|
||||
date: <%= 17.days.ago.to_date %>
|
||||
|
||||
day_16_ago_eur_to_usd:
|
||||
base_currency: EUR
|
||||
converted_currency: USD
|
||||
rate: 1.0809
|
||||
date: <%= 16.days.ago.to_date %>
|
||||
|
||||
day_15_ago_eur_to_usd:
|
||||
base_currency: EUR
|
||||
converted_currency: USD
|
||||
rate: 1.078
|
||||
date: <%= 15.days.ago.to_date %>
|
||||
|
||||
day_14_ago_eur_to_usd:
|
||||
base_currency: EUR
|
||||
converted_currency: USD
|
||||
rate: 1.0778
|
||||
date: <%= 14.days.ago.to_date %>
|
||||
|
||||
day_13_ago_eur_to_usd:
|
||||
base_currency: EUR
|
||||
converted_currency: USD
|
||||
rate: 1.0773
|
||||
date: <%= 13.days.ago.to_date %>
|
||||
|
||||
day_12_ago_eur_to_usd:
|
||||
base_currency: EUR
|
||||
converted_currency: USD
|
||||
rate: 1.0729
|
||||
date: <%= 12.days.ago.to_date %>
|
||||
|
||||
day_11_ago_eur_to_usd:
|
||||
base_currency: EUR
|
||||
converted_currency: USD
|
||||
rate: 1.0709
|
||||
date: <%= 11.days.ago.to_date %>
|
||||
|
||||
day_10_ago_eur_to_usd:
|
||||
base_currency: EUR
|
||||
converted_currency: USD
|
||||
rate: 1.0773
|
||||
date: <%= 10.days.ago.to_date %>
|
||||
|
||||
day_9_ago_eur_to_usd:
|
||||
base_currency: EUR
|
||||
converted_currency: USD
|
||||
rate: 1.0783
|
||||
date: <%= 9.days.ago.to_date %>
|
||||
|
||||
day_8_ago_eur_to_usd:
|
||||
base_currency: EUR
|
||||
converted_currency: USD
|
||||
rate: 1.0778
|
||||
date: <%= 8.days.ago.to_date %>
|
||||
|
||||
day_7_ago_eur_to_usd:
|
||||
base_currency: EUR
|
||||
converted_currency: USD
|
||||
rate: 1.0774
|
||||
date: <%= 7.days.ago.to_date %>
|
||||
|
||||
day_6_ago_eur_to_usd:
|
||||
base_currency: EUR
|
||||
converted_currency: USD
|
||||
rate: 1.0755
|
||||
date: <%= 6.days.ago.to_date %>
|
||||
|
||||
day_5_ago_eur_to_usd:
|
||||
base_currency: EUR
|
||||
converted_currency: USD
|
||||
rate: 1.0743
|
||||
date: <%= 5.days.ago.to_date %>
|
||||
|
||||
day_4_ago_eur_to_usd:
|
||||
base_currency: EUR
|
||||
converted_currency: USD
|
||||
rate: 1.0788
|
||||
date: <%= 4.days.ago.to_date %>
|
||||
|
||||
day_3_ago_eur_to_usd:
|
||||
base_currency: EUR
|
||||
converted_currency: USD
|
||||
rate: 1.0872
|
||||
date: <%= 3.days.ago.to_date %>
|
||||
|
||||
day_2_ago_eur_to_usd:
|
||||
base_currency: EUR
|
||||
converted_currency: USD
|
||||
rate: 1.0819
|
||||
date: <%= 2.days.ago.to_date %>
|
||||
|
||||
day_1_ago_eur_to_usd:
|
||||
base_currency: EUR
|
||||
converted_currency: USD
|
||||
rate: 1.0845
|
||||
date: <%= 1.days.ago.to_date %>
|
||||
|
||||
today_eur_to_usd:
|
||||
base_currency: EUR
|
||||
converted_currency: USD
|
||||
rate: 1.0834
|
||||
date: <%= Date.current %>
|
||||
|
||||
day_31_ago_usd_to_eur:
|
||||
base_currency: USD
|
||||
converted_currency: EUR
|
||||
rate: 0.9279
|
||||
date: <%= 31.days.ago.to_date %>
|
||||
|
||||
day_30_ago_usd_to_eur:
|
||||
base_currency: USD
|
||||
converted_currency: EUR
|
||||
rate: 0.9179
|
||||
date: <%= 30.days.ago.to_date %>
|
||||
|
||||
day_29_ago_usd_to_eur:
|
||||
base_currency: USD
|
||||
converted_currency: EUR
|
||||
rate: 0.9154
|
||||
date: <%= 29.days.ago.to_date %>
|
||||
|
||||
day_28_ago_usd_to_eur:
|
||||
base_currency: USD
|
||||
converted_currency: EUR
|
||||
rate: 0.9107
|
||||
date: <%= 28.days.ago.to_date %>
|
||||
|
||||
day_27_ago_usd_to_eur:
|
||||
base_currency: USD
|
||||
converted_currency: EUR
|
||||
rate: 0.9139
|
||||
date: <%= 27.days.ago.to_date %>
|
||||
|
||||
day_26_ago_usd_to_eur:
|
||||
base_currency: USD
|
||||
converted_currency: EUR
|
||||
rate: 0.9082
|
||||
date: <%= 26.days.ago.to_date %>
|
||||
|
||||
day_25_ago_usd_to_eur:
|
||||
base_currency: USD
|
||||
converted_currency: EUR
|
||||
rate: 0.9077
|
||||
date: <%= 25.days.ago.to_date %>
|
||||
|
||||
day_24_ago_usd_to_eur:
|
||||
base_currency: USD
|
||||
converted_currency: EUR
|
||||
rate: 0.9054
|
||||
date: <%= 24.days.ago.to_date %>
|
||||
|
||||
day_23_ago_usd_to_eur:
|
||||
base_currency: USD
|
||||
converted_currency: EUR
|
||||
rate: 0.9004
|
||||
date: <%= 23.days.ago.to_date %>
|
||||
|
||||
day_22_ago_usd_to_eur:
|
||||
base_currency: USD
|
||||
converted_currency: EUR
|
||||
rate: 0.9040
|
||||
date: <%= 22.days.ago.to_date %>
|
||||
|
||||
day_21_ago_usd_to_eur:
|
||||
base_currency: USD
|
||||
converted_currency: EUR
|
||||
rate: 0.9060
|
||||
date: <%= 21.days.ago.to_date %>
|
||||
|
||||
day_20_ago_usd_to_eur:
|
||||
base_currency: USD
|
||||
converted_currency: EUR
|
||||
rate: 0.9052
|
||||
date: <%= 20.days.ago.to_date %>
|
||||
|
||||
day_19_ago_usd_to_eur:
|
||||
base_currency: USD
|
||||
converted_currency: EUR
|
||||
rate: 0.9139
|
||||
date: <%= 19.days.ago.to_date %>
|
||||
|
||||
day_18_ago_usd_to_eur:
|
||||
base_currency: USD
|
||||
converted_currency: EUR
|
||||
rate: 0.9155
|
||||
date: <%= 18.days.ago.to_date %>
|
||||
|
||||
day_17_ago_usd_to_eur:
|
||||
base_currency: USD
|
||||
converted_currency: EUR
|
||||
rate: 0.9135
|
||||
date: <%= 17.days.ago.to_date %>
|
||||
|
||||
day_16_ago_usd_to_eur:
|
||||
base_currency: USD
|
||||
converted_currency: EUR
|
||||
rate: 0.9141
|
||||
date: <%= 16.days.ago.to_date %>
|
||||
|
||||
day_15_ago_usd_to_eur:
|
||||
base_currency: USD
|
||||
converted_currency: EUR
|
||||
rate: 0.9131
|
||||
date: <%= 15.days.ago.to_date %>
|
||||
|
||||
day_14_ago_usd_to_eur:
|
||||
base_currency: USD
|
||||
converted_currency: EUR
|
||||
rate: 0.9147
|
||||
date: <%= 14.days.ago.to_date %>
|
||||
|
||||
day_13_ago_usd_to_eur:
|
||||
base_currency: USD
|
||||
converted_currency: EUR
|
||||
rate: 0.9112
|
||||
date: <%= 13.days.ago.to_date %>
|
||||
|
||||
day_12_ago_usd_to_eur:
|
||||
base_currency: USD
|
||||
converted_currency: EUR
|
||||
rate: 0.9115
|
||||
date: <%= 12.days.ago.to_date %>
|
||||
|
||||
day_11_ago_usd_to_eur:
|
||||
base_currency: USD
|
||||
converted_currency: EUR
|
||||
rate: 0.9132
|
||||
date: <%= 11.days.ago.to_date %>
|
||||
|
||||
day_10_ago_usd_to_eur:
|
||||
base_currency: USD
|
||||
converted_currency: EUR
|
||||
rate: 0.9130
|
||||
date: <%= 10.days.ago.to_date %>
|
||||
|
||||
day_9_ago_usd_to_eur:
|
||||
base_currency: USD
|
||||
converted_currency: EUR
|
||||
rate: 0.9192
|
||||
date: <%= 9.days.ago.to_date %>
|
||||
|
||||
day_8_ago_usd_to_eur:
|
||||
base_currency: USD
|
||||
converted_currency: EUR
|
||||
rate: 0.9188
|
||||
date: <%= 8.days.ago.to_date %>
|
||||
|
||||
day_7_ago_usd_to_eur:
|
||||
base_currency: USD
|
||||
converted_currency: EUR
|
||||
rate: 0.9194
|
||||
date: <%= 7.days.ago.to_date %>
|
||||
|
||||
day_6_ago_usd_to_eur:
|
||||
base_currency: USD
|
||||
converted_currency: EUR
|
||||
rate: 0.9177
|
||||
date: <%= 6.days.ago.to_date %>
|
||||
|
||||
day_5_ago_usd_to_eur:
|
||||
base_currency: USD
|
||||
converted_currency: EUR
|
||||
rate: 0.9187
|
||||
date: <%= 5.days.ago.to_date %>
|
||||
|
||||
day_4_ago_usd_to_eur:
|
||||
base_currency: USD
|
||||
converted_currency: EUR
|
||||
rate: 0.9213
|
||||
date: <%= 4.days.ago.to_date %>
|
||||
|
||||
day_3_ago_usd_to_eur:
|
||||
base_currency: USD
|
||||
converted_currency: EUR
|
||||
rate: 0.9186
|
||||
date: <%= 3.days.ago.to_date %>
|
||||
|
||||
day_2_ago_usd_to_eur:
|
||||
base_currency: USD
|
||||
converted_currency: EUR
|
||||
rate: 0.9218
|
||||
date: <%= 2.days.ago.to_date %>
|
||||
|
||||
day_1_ago_usd_to_eur:
|
||||
base_currency: USD
|
||||
converted_currency: EUR
|
||||
rate: 0.9213
|
||||
date: <%= 1.days.ago.to_date %>
|
||||
|
||||
today_usd_to_eur:
|
||||
base_currency: USD
|
||||
converted_currency: EUR
|
||||
rate: 0.9141
|
||||
date: <%= Date.current %>
|
||||
date: <%= 1.day.ago.to_date %>
|
||||
|
|
|
@ -1,99 +1,112 @@
|
|||
require "test_helper"
|
||||
require "ostruct"
|
||||
|
||||
class MoneyTest < ActiveSupport::TestCase
|
||||
test "can create with default currency" do
|
||||
value = Money.new(1000)
|
||||
assert_equal 1000, value.amount
|
||||
end
|
||||
test "can create with default currency" do
|
||||
value = Money.new(1000)
|
||||
assert_equal 1000, value.amount
|
||||
end
|
||||
|
||||
test "can create with custom currency" do
|
||||
value1 = Money.new(1000, :EUR)
|
||||
value2 = Money.new(1000, :eur)
|
||||
value3 = Money.new(1000, "eur")
|
||||
value4 = Money.new(1000, "EUR")
|
||||
test "can create with custom currency" do
|
||||
value1 = Money.new(1000, :EUR)
|
||||
value2 = Money.new(1000, :eur)
|
||||
value3 = Money.new(1000, "eur")
|
||||
value4 = Money.new(1000, "EUR")
|
||||
|
||||
assert_equal value1.currency.iso_code, value2.currency.iso_code
|
||||
assert_equal value2.currency.iso_code, value3.currency.iso_code
|
||||
assert_equal value3.currency.iso_code, value4.currency.iso_code
|
||||
end
|
||||
assert_equal value1.currency.iso_code, value2.currency.iso_code
|
||||
assert_equal value2.currency.iso_code, value3.currency.iso_code
|
||||
assert_equal value3.currency.iso_code, value4.currency.iso_code
|
||||
end
|
||||
|
||||
test "equality tests amount and currency" do
|
||||
assert_equal Money.new(1000), Money.new(1000)
|
||||
assert_not_equal Money.new(1000), Money.new(1001)
|
||||
assert_not_equal Money.new(1000, :usd), Money.new(1000, :eur)
|
||||
end
|
||||
test "equality tests amount and currency" do
|
||||
assert_equal Money.new(1000), Money.new(1000)
|
||||
assert_not_equal Money.new(1000), Money.new(1001)
|
||||
assert_not_equal Money.new(1000, :usd), Money.new(1000, :eur)
|
||||
end
|
||||
|
||||
test "can compare with zero Numeric" do
|
||||
assert_equal Money.new(0), 0
|
||||
assert_raises(TypeError) { Money.new(1) == 1 }
|
||||
end
|
||||
test "can compare with zero Numeric" do
|
||||
assert_equal Money.new(0), 0
|
||||
assert_raises(TypeError) { Money.new(1) == 1 }
|
||||
end
|
||||
|
||||
test "can negate" do
|
||||
assert_equal (-Money.new(1000)), Money.new(-1000)
|
||||
end
|
||||
test "can negate" do
|
||||
assert_equal (-Money.new(1000)), Money.new(-1000)
|
||||
end
|
||||
|
||||
test "can use comparison operators" do
|
||||
assert_operator Money.new(1000), :>, Money.new(999)
|
||||
assert_operator Money.new(1000), :>=, Money.new(1000)
|
||||
assert_operator Money.new(1000), :<, Money.new(1001)
|
||||
assert_operator Money.new(1000), :<=, Money.new(1000)
|
||||
end
|
||||
test "can use comparison operators" do
|
||||
assert_operator Money.new(1000), :>, Money.new(999)
|
||||
assert_operator Money.new(1000), :>=, Money.new(1000)
|
||||
assert_operator Money.new(1000), :<, Money.new(1001)
|
||||
assert_operator Money.new(1000), :<=, Money.new(1000)
|
||||
end
|
||||
|
||||
test "can add and subtract" do
|
||||
assert_equal Money.new(1000) + Money.new(1000), Money.new(2000)
|
||||
assert_equal Money.new(1000) + 1000, Money.new(2000)
|
||||
assert_equal Money.new(1000) - Money.new(1000), Money.new(0)
|
||||
assert_equal Money.new(1000) - 1000, Money.new(0)
|
||||
end
|
||||
test "can add and subtract" do
|
||||
assert_equal Money.new(1000) + Money.new(1000), Money.new(2000)
|
||||
assert_equal Money.new(1000) + 1000, Money.new(2000)
|
||||
assert_equal Money.new(1000) - Money.new(1000), Money.new(0)
|
||||
assert_equal Money.new(1000) - 1000, Money.new(0)
|
||||
end
|
||||
|
||||
test "can multiply" do
|
||||
assert_equal Money.new(1000) * 2, Money.new(2000)
|
||||
assert_raises(TypeError) { Money.new(1000) * Money.new(2) }
|
||||
end
|
||||
test "can multiply" do
|
||||
assert_equal Money.new(1000) * 2, Money.new(2000)
|
||||
assert_raises(TypeError) { Money.new(1000) * Money.new(2) }
|
||||
end
|
||||
|
||||
test "can divide" do
|
||||
assert_equal Money.new(1000) / 2, Money.new(500)
|
||||
assert_equal Money.new(1000) / Money.new(500), 2
|
||||
assert_raise(TypeError) { 1000 / Money.new(2) }
|
||||
end
|
||||
test "can divide" do
|
||||
assert_equal Money.new(1000) / 2, Money.new(500)
|
||||
assert_equal Money.new(1000) / Money.new(500), 2
|
||||
assert_raise(TypeError) { 1000 / Money.new(2) }
|
||||
end
|
||||
|
||||
test "operator order does not matter" do
|
||||
assert_equal Money.new(1000) + 1000, 1000 + Money.new(1000)
|
||||
assert_equal Money.new(1000) - 1000, 1000 - Money.new(1000)
|
||||
assert_equal Money.new(1000) * 2, 2 * Money.new(1000)
|
||||
end
|
||||
test "operator order does not matter" do
|
||||
assert_equal Money.new(1000) + 1000, 1000 + Money.new(1000)
|
||||
assert_equal Money.new(1000) - 1000, 1000 - Money.new(1000)
|
||||
assert_equal Money.new(1000) * 2, 2 * Money.new(1000)
|
||||
end
|
||||
|
||||
test "can get absolute value" do
|
||||
assert_equal Money.new(1000).abs, Money.new(1000)
|
||||
assert_equal Money.new(-1000).abs, Money.new(1000)
|
||||
end
|
||||
test "can get absolute value" do
|
||||
assert_equal Money.new(1000).abs, Money.new(1000)
|
||||
assert_equal Money.new(-1000).abs, Money.new(1000)
|
||||
end
|
||||
|
||||
test "can test if zero" do
|
||||
assert Money.new(0).zero?
|
||||
assert_not Money.new(1000).zero?
|
||||
end
|
||||
test "can test if zero" do
|
||||
assert Money.new(0).zero?
|
||||
assert_not Money.new(1000).zero?
|
||||
end
|
||||
|
||||
test "can test if negative" do
|
||||
assert Money.new(-1000).negative?
|
||||
assert_not Money.new(1000).negative?
|
||||
end
|
||||
test "can test if negative" do
|
||||
assert Money.new(-1000).negative?
|
||||
assert_not Money.new(1000).negative?
|
||||
end
|
||||
|
||||
test "can test if positive" do
|
||||
assert Money.new(1000).positive?
|
||||
assert_not Money.new(-1000).positive?
|
||||
end
|
||||
test "can test if positive" do
|
||||
assert Money.new(1000).positive?
|
||||
assert_not Money.new(-1000).positive?
|
||||
end
|
||||
|
||||
test "can cast to string with basic formatting" do
|
||||
assert_equal "$1,000.90", Money.new(1000.899).format
|
||||
assert_equal "€1.000,12", Money.new(1000.12, :eur).format
|
||||
end
|
||||
test "can cast to string with basic formatting" do
|
||||
assert_equal "$1,000.90", Money.new(1000.899).format
|
||||
assert_equal "€1.000,12", Money.new(1000.12, :eur).format
|
||||
end
|
||||
|
||||
test "can exchange to another currency" do
|
||||
er = exchange_rates(:today_usd_to_eur)
|
||||
assert_equal Money.new(1000).exchange_to(:eur), Money.new(1000 * er.rate, :eur)
|
||||
end
|
||||
test "converts currency when rate available" do
|
||||
ExchangeRate.expects(:find_rate).returns(OpenStruct.new(rate: 1.2))
|
||||
|
||||
test "returns nil if exchange rate not available" do
|
||||
assert_nil Money.new(1000).exchange_to(:jpy)
|
||||
assert_equal Money.new(1000).exchange_to(:eur), Money.new(1000 * 1.2, :eur)
|
||||
end
|
||||
|
||||
test "raises when no conversion rate available and no fallback rate provided" do
|
||||
ExchangeRate.expects(:find_rate).returns(nil)
|
||||
|
||||
assert_raises Money::ConversionError do
|
||||
Money.new(1000).exchange_to(:jpy)
|
||||
end
|
||||
end
|
||||
|
||||
test "converts currency with a fallback rate" do
|
||||
ExchangeRate.expects(:find_rate).returns(nil).twice
|
||||
|
||||
assert_equal 0, Money.new(1000).exchange_to(:jpy, fallback_rate: 0)
|
||||
assert_equal Money.new(1000, :jpy), Money.new(1000, :usd).exchange_to(:jpy, fallback_rate: 1)
|
||||
end
|
||||
end
|
||||
|
|
|
@ -25,6 +25,17 @@ class Account::Balance::CalculatorTest < ActiveSupport::TestCase
|
|||
end
|
||||
|
||||
test "syncs foreign checking account balances" do
|
||||
required_exchange_rates_for_sync = [
|
||||
1.0834, 1.0845, 1.0819, 1.0872, 1.0788, 1.0743, 1.0755, 1.0774,
|
||||
1.0778, 1.0783, 1.0773, 1.0709, 1.0729, 1.0773, 1.0778, 1.078,
|
||||
1.0809, 1.0818, 1.0824, 1.0822, 1.0854, 1.0845, 1.0839, 1.0807,
|
||||
1.084, 1.0856, 1.0858, 1.0898, 1.095, 1.094, 1.0926, 1.0986
|
||||
]
|
||||
|
||||
required_exchange_rates_for_sync.each_with_index do |exchange_rate, idx|
|
||||
ExchangeRate.create! date: idx.days.ago.to_date, from_currency: "EUR", to_currency: "USD", rate: exchange_rate
|
||||
end
|
||||
|
||||
# Foreign accounts will generate balances for all currencies
|
||||
expected_usd_balances = get_expected_balances_for(:eur_checking_usd)
|
||||
expected_eur_balances = get_expected_balances_for(:eur_checking_eur)
|
||||
|
@ -38,6 +49,13 @@ class Account::Balance::CalculatorTest < ActiveSupport::TestCase
|
|||
end
|
||||
|
||||
test "syncs multi-currency checking account balances" do
|
||||
required_exchange_rates_for_sync = [
|
||||
{ from_currency: "EUR", to_currency: "USD", date: 4.days.ago.to_date, rate: 1.0788 },
|
||||
{ from_currency: "EUR", to_currency: "USD", date: 19.days.ago.to_date, rate: 1.0822 }
|
||||
]
|
||||
|
||||
ExchangeRate.insert_all(required_exchange_rates_for_sync)
|
||||
|
||||
expected_balances = get_expected_balances_for(:multi_currency)
|
||||
assert_account_balances calculated_balances_for(:multi_currency), expected_balances
|
||||
end
|
||||
|
|
|
@ -18,15 +18,14 @@ class Account::SyncableTest < ActiveSupport::TestCase
|
|||
assert_equal 32, @account.balances.count
|
||||
end
|
||||
|
||||
test "syncs foreign currency account" do
|
||||
account = accounts(:eur_checking)
|
||||
account.sync
|
||||
assert_equal "ok", account.status
|
||||
assert_equal 32, account.balances.where(currency: "USD").count
|
||||
assert_equal 32, account.balances.where(currency: "EUR").count
|
||||
end
|
||||
|
||||
test "syncs multi currency account" do
|
||||
required_exchange_rates_for_sync = [
|
||||
{ from_currency: "EUR", to_currency: "USD", date: 4.days.ago.to_date, rate: 1.0788 },
|
||||
{ from_currency: "EUR", to_currency: "USD", date: 19.days.ago.to_date, rate: 1.0822 }
|
||||
]
|
||||
|
||||
ExchangeRate.insert_all(required_exchange_rates_for_sync)
|
||||
|
||||
account = accounts(:multi_currency)
|
||||
account.sync
|
||||
assert_equal "ok", account.status
|
||||
|
@ -91,6 +90,17 @@ class Account::SyncableTest < ActiveSupport::TestCase
|
|||
end
|
||||
|
||||
test "foreign currency account has balances in each currency after syncing" do
|
||||
required_exchange_rates_for_sync = [
|
||||
1.0834, 1.0845, 1.0819, 1.0872, 1.0788, 1.0743, 1.0755, 1.0774,
|
||||
1.0778, 1.0783, 1.0773, 1.0709, 1.0729, 1.0773, 1.0778, 1.078,
|
||||
1.0809, 1.0818, 1.0824, 1.0822, 1.0854, 1.0845, 1.0839, 1.0807,
|
||||
1.084, 1.0856, 1.0858, 1.0898, 1.095, 1.094, 1.0926, 1.0986
|
||||
]
|
||||
|
||||
required_exchange_rates_for_sync.each_with_index do |exchange_rate, idx|
|
||||
ExchangeRate.create! date: idx.days.ago.to_date, from_currency: "EUR", to_currency: "USD", rate: exchange_rate
|
||||
end
|
||||
|
||||
account = accounts(:eur_checking)
|
||||
account.sync
|
||||
|
||||
|
|
|
@ -1,50 +1,95 @@
|
|||
require "test_helper"
|
||||
require "ostruct"
|
||||
|
||||
class ExchangeRateTest < ActiveSupport::TestCase
|
||||
test "find rate in db" do
|
||||
assert_equal exchange_rates(:day_29_ago_eur_to_usd),
|
||||
ExchangeRate.find_rate_or_fetch(from: "EUR", to: "USD", date: 29.days.ago.to_date)
|
||||
setup do
|
||||
@provider = mock
|
||||
|
||||
ExchangeRate.stubs(:exchange_rates_provider).returns(@provider)
|
||||
end
|
||||
|
||||
test "fetch rate from provider when it's not found in db" do
|
||||
with_env_overrides SYNTH_API_KEY: "true" do
|
||||
ExchangeRate
|
||||
.expects(:fetch_rate_from_provider)
|
||||
.returns(ExchangeRate.new(base_currency: "USD", converted_currency: "MXN", rate: 1.0, date: Date.current))
|
||||
test "exchange rate provider nil if no api key configured" do
|
||||
ExchangeRate.unstub(:exchange_rates_provider)
|
||||
|
||||
ExchangeRate.find_rate_or_fetch from: "USD", to: "MXN", date: Date.current
|
||||
with_env_overrides SYNTH_API_KEY: nil do
|
||||
assert_nil ExchangeRate.exchange_rates_provider
|
||||
end
|
||||
end
|
||||
|
||||
test "provided rates are saved to the db" do
|
||||
with_env_overrides SYNTH_API_KEY: "true" do
|
||||
VCR.use_cassette "synth_exchange_rate" do
|
||||
assert_difference "ExchangeRate.count", 1 do
|
||||
ExchangeRate.find_rate_or_fetch from: "USD", to: "MXN", date: Date.current
|
||||
end
|
||||
end
|
||||
test "finds single rate in DB" do
|
||||
@provider.expects(:fetch_exchange_rate).never
|
||||
|
||||
rate = exchange_rates(:one)
|
||||
|
||||
assert_equal exchange_rates(:one), ExchangeRate.find_rate(from: rate.from_currency, to: rate.to_currency, date: rate.date)
|
||||
end
|
||||
|
||||
test "finds single rate from provider and caches to DB" do
|
||||
expected_rate = 1.21
|
||||
@provider.expects(:fetch_exchange_rate).once.returns(OpenStruct.new(success?: true, rate: expected_rate))
|
||||
|
||||
fetched_rate = ExchangeRate.find_rate(from: "USD", to: "EUR", date: Date.current, cache: true)
|
||||
refetched_rate = ExchangeRate.find_rate(from: "USD", to: "EUR", date: Date.current, cache: true)
|
||||
|
||||
assert_equal expected_rate, fetched_rate.rate
|
||||
assert_equal expected_rate, refetched_rate.rate
|
||||
end
|
||||
|
||||
test "nil if rate is not found in DB and provider throws an error" do
|
||||
@provider.expects(:fetch_exchange_rate).with(from: "USD", to: "EUR", date: Date.current).once.returns(OpenStruct.new(success?: false))
|
||||
|
||||
assert_nil ExchangeRate.find_rate(from: "USD", to: "EUR", date: Date.current)
|
||||
end
|
||||
|
||||
test "nil if rate is not found in DB and provider is disabled" do
|
||||
ExchangeRate.unstub(:exchange_rates_provider)
|
||||
|
||||
with_env_overrides SYNTH_API_KEY: nil do
|
||||
assert_nil ExchangeRate.find_rate(from: "USD", to: "EUR", date: Date.current)
|
||||
end
|
||||
end
|
||||
|
||||
test "retrying, then raising on provider error" do
|
||||
with_env_overrides SYNTH_API_KEY: "true" do
|
||||
Faraday.expects(:get).returns(OpenStruct.new(success?: false)).times(3)
|
||||
test "finds multiple rates in DB" do
|
||||
@provider.expects(:fetch_exchange_rate).never
|
||||
|
||||
error = assert_raises Provider::Base::ProviderError do
|
||||
ExchangeRate.find_rate_or_fetch from: "USD", to: "MXN", date: Date.current
|
||||
end
|
||||
rate1 = exchange_rates(:one) # EUR -> GBP, today
|
||||
rate2 = exchange_rates(:two) # EUR -> GBP, yesterday
|
||||
|
||||
assert_match "Failed to fetch exchange rate from Provider::Synth", error.message
|
||||
end
|
||||
fetched_rates = ExchangeRate.find_rates(from: rate1.from_currency, to: rate1.to_currency, start_date: 1.day.ago.to_date).sort_by(&:date)
|
||||
|
||||
assert_equal rate1, fetched_rates[1]
|
||||
assert_equal rate2, fetched_rates[0]
|
||||
end
|
||||
|
||||
test "retrying, then raising on network error" do
|
||||
with_env_overrides SYNTH_API_KEY: "true" do
|
||||
Faraday.expects(:get).raises(Faraday::TimeoutError).times(3)
|
||||
test "finds multiple rates from provider and caches to DB" do
|
||||
@provider.expects(:fetch_exchange_rate).with(from: "EUR", to: "USD", date: 1.day.ago.to_date).returns(OpenStruct.new(success?: true, rate: 1.1)).once
|
||||
@provider.expects(:fetch_exchange_rate).with(from: "EUR", to: "USD", date: Date.current).returns(OpenStruct.new(success?: true, rate: 1.2)).once
|
||||
|
||||
assert_raises Faraday::TimeoutError do
|
||||
ExchangeRate.find_rate_or_fetch from: "USD", to: "MXN", date: Date.current
|
||||
end
|
||||
fetched_rates = ExchangeRate.find_rates(from: "EUR", to: "USD", start_date: 1.day.ago.to_date, cache: true)
|
||||
refetched_rates = ExchangeRate.find_rates(from: "EUR", to: "USD", start_date: 1.day.ago.to_date)
|
||||
|
||||
assert_equal [ 1.1, 1.2 ], fetched_rates.sort_by(&:date).map(&:rate)
|
||||
assert_equal [ 1.1, 1.2 ], refetched_rates.sort_by(&:date).map(&:rate)
|
||||
end
|
||||
|
||||
test "finds missing db rates from provider and appends to results" do
|
||||
@provider.expects(:fetch_exchange_rate).with(from: "EUR", to: "GBP", date: 2.days.ago.to_date).returns(OpenStruct.new(success?: true, rate: 1.1)).once
|
||||
|
||||
rate1 = exchange_rates(:one) # EUR -> GBP, today
|
||||
rate2 = exchange_rates(:two) # EUR -> GBP, yesterday
|
||||
|
||||
fetched_rates = ExchangeRate.find_rates(from: "EUR", to: "GBP", start_date: 2.days.ago.to_date, cache: true)
|
||||
refetched_rates = ExchangeRate.find_rates(from: "EUR", to: "GBP", start_date: 2.days.ago.to_date)
|
||||
|
||||
assert_equal [ 1.1, rate2.rate, rate1.rate ], fetched_rates.sort_by(&:date).map(&:rate)
|
||||
assert_equal [ 1.1, rate2.rate, rate1.rate ], refetched_rates.sort_by(&:date).map(&:rate)
|
||||
end
|
||||
|
||||
test "returns empty array if no rates found in DB or provider" do
|
||||
ExchangeRate.unstub(:exchange_rates_provider)
|
||||
|
||||
with_env_overrides SYNTH_API_KEY: nil do
|
||||
assert_equal [], ExchangeRate.find_rates(from: "USD", to: "JPY", start_date: 10.days.ago.to_date)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -6,6 +6,18 @@ class FamilyTest < ActiveSupport::TestCase
|
|||
|
||||
def setup
|
||||
@family = families(:dylan_family)
|
||||
|
||||
required_exchange_rates_for_family = [
|
||||
1.0834, 1.0845, 1.0819, 1.0872, 1.0788, 1.0743, 1.0755, 1.0774,
|
||||
1.0778, 1.0783, 1.0773, 1.0709, 1.0729, 1.0773, 1.0778, 1.078,
|
||||
1.0809, 1.0818, 1.0824, 1.0822, 1.0854, 1.0845, 1.0839, 1.0807,
|
||||
1.084, 1.0856, 1.0858, 1.0898, 1.095, 1.094, 1.0926, 1.0986
|
||||
]
|
||||
|
||||
required_exchange_rates_for_family.each_with_index do |exchange_rate, idx|
|
||||
ExchangeRate.create! date: idx.days.ago.to_date, from_currency: "EUR", to_currency: "USD", rate: exchange_rate
|
||||
end
|
||||
|
||||
@family.accounts.each do |account|
|
||||
account.sync
|
||||
end
|
||||
|
|
|
@ -1,9 +1,26 @@
|
|||
require "test_helper"
|
||||
require "ostruct"
|
||||
|
||||
class Provider::SynthTest < ActiveSupport::TestCase
|
||||
include ExchangeRateProviderInterfaceTest
|
||||
|
||||
setup do
|
||||
@subject = Provider::Synth.new
|
||||
@subject = @synth = Provider::Synth.new("fookey")
|
||||
end
|
||||
|
||||
test "retries then provides failed response" do
|
||||
Faraday.expects(:get).returns(OpenStruct.new(success?: false)).times(3)
|
||||
|
||||
response = @synth.fetch_exchange_rate from: "USD", to: "MXN", date: Date.current
|
||||
|
||||
assert_match "Failed to fetch exchange rate from Provider::Synth", response.error.message
|
||||
end
|
||||
|
||||
test "retrying, then raising on network error" do
|
||||
Faraday.expects(:get).raises(Faraday::TimeoutError).times(3)
|
||||
|
||||
assert_raises Faraday::TimeoutError do
|
||||
@synth.fetch_exchange_rate from: "USD", to: "MXN", date: Date.current
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue