|
| 1 | +require "test_helper" |
| 2 | + |
| 3 | +class Balance::SyncCacheTest < ActiveSupport::TestCase |
| 4 | + include EntriesTestHelper |
| 5 | + include ExchangeRateTestHelper |
| 6 | + |
| 7 | + setup do |
| 8 | + @account = families(:empty).accounts.create!( |
| 9 | + name: "Test", |
| 10 | + balance: 20000, |
| 11 | + cash_balance: 20000, |
| 12 | + currency: "USD", |
| 13 | + accountable: Investment.new |
| 14 | + ) |
| 15 | + create_transaction(account: @account, date: 1.day.ago.to_date, amount: 100, currency: "CAD") |
| 16 | + @sync_cache = Balance::SyncCache.new(@account) |
| 17 | + end |
| 18 | + |
| 19 | + test "convert currency when rate available by cache" do |
| 20 | + load_exchange_prices |
| 21 | + money = Money.new(100, "CAD") |
| 22 | + converted_money = nil |
| 23 | + assert_queries_count(3) do |
| 24 | + converted_money = @sync_cache.find_rate_by_cache(money, @account.currency, date: 1.day.ago.to_date) |
| 25 | + end |
| 26 | + expected_money = money.exchange_to(@account.currency, date: 1.day.ago.to_date) |
| 27 | + |
| 28 | + assert_equal expected_money.amount, converted_money.amount |
| 29 | + assert_equal expected_money.currency, converted_money.currency |
| 30 | + end |
| 31 | + |
| 32 | + test "convert currency after fetching rate" do |
| 33 | + ExchangeRate.expects(:fetch_rate).returns(Provider::ExchangeRateConcept::Rate.new(date: 1.day.ago.to_date, from: "JPY", to: "USD", rate: 0.007)) |
| 34 | + money = Money.new(1000, "JPY") |
| 35 | + |
| 36 | + assert_equal Money.new(7, "USD"), @sync_cache.find_rate_by_cache(money, @account.currency, date: 1.day.ago.to_date) |
| 37 | + end |
| 38 | + |
| 39 | + test "converts currency with a fallback rate" do |
| 40 | + ExchangeRate.expects(:fetch_rate).returns(nil).twice |
| 41 | + money = Money.new(1000, "CAD") |
| 42 | + |
| 43 | + assert_queries_count(3) do |
| 44 | + assert_equal Money.new(0, "USD"), @sync_cache.find_rate_by_cache(money, @account.currency, date: 1.day.ago.to_date, fallback_rate: 0) |
| 45 | + end |
| 46 | + |
| 47 | + assert_equal Money.new(1000, "USD"), @sync_cache.find_rate_by_cache(money, @account.currency, date: 1.day.ago.to_date, fallback_rate: 1) |
| 48 | + end |
| 49 | + |
| 50 | + test "raises when no conversion rate available and no fallback rate provided" do |
| 51 | + money = Money.new(1000, "JPY") |
| 52 | + ExchangeRate.expects(:fetch_rate).returns(nil) |
| 53 | + |
| 54 | + assert_raises Money::ConversionError do |
| 55 | + @sync_cache.find_rate_by_cache(money, @account.currency, date: 1.day.ago.to_date, fallback_rate: nil) |
| 56 | + end |
| 57 | + end |
| 58 | + |
| 59 | + test "raises if input is not Money-like" do |
| 60 | + assert_raises(TypeError) do |
| 61 | + @sync_cache.find_rate_by_cache(12345, "USD") |
| 62 | + end |
| 63 | + end |
| 64 | + |
| 65 | + test "returns original money when source and target currency are the same" do |
| 66 | + money = Money.new(500, "USD") |
| 67 | + result = nil |
| 68 | + assert_queries_count(0) do |
| 69 | + result = @sync_cache.find_rate_by_cache(money, "USD", date: Date.today) |
| 70 | + end |
| 71 | + assert_same money, result |
| 72 | + end |
| 73 | + |
| 74 | + test "query and cache exchange rates" do |
| 75 | + load_exchange_prices |
| 76 | + assert_queries_count(3) do |
| 77 | + @sync_cache.find_rate_by_cache(Money.new(100, "CAD"), @account.currency, date: 1.day.ago.to_date) |
| 78 | + end |
| 79 | + |
| 80 | + assert_queries_count(0) do |
| 81 | + @sync_cache.find_rate_by_cache(Money.new(100, "CAD"), @account.currency, date: 1.day.ago.to_date) |
| 82 | + @sync_cache.find_rate_by_cache(Money.new(200, "CAD"), @account.currency, date: 1.day.ago.to_date) |
| 83 | + end |
| 84 | + end |
| 85 | +end |
0 commit comments