Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Extend the TestCurrencyConverter #3249

Merged
merged 3 commits into from
Mar 31, 2023

Conversation

chirlu
Copy link
Member

@chirlu chirlu commented Mar 31, 2023

Extend the TestCurrencyConverter to support EUR↔USD and EUR↔CHF (either direction; not USD↔CHF, though). Also some refactoring to make it simpler.

Make it possible to support multiple currencies. Plus some simplification.
@@ -30,23 +31,43 @@ public class TestCurrencyConverter implements CurrencyConverter
EUR_USD.addRate(new ExchangeRate(LocalDate.parse("2015-01-09"), BigDecimal.valueOf(1.1813).setScale(10)));

EUR_USD.addRate(new ExchangeRate(LocalDate.parse("2015-01-12"), BigDecimal.valueOf(1.1804).setScale(10)));
EUR_USD.addRate(new ExchangeRate(LocalDate.parse("2015-01-13"), new BigDecimal("1.1782")));
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That day was missing for whatever reason. Calling the BigDecimal constructor with a string will ensure that the value is exact.

@@ -55,31 +76,24 @@ public String getTermCurrency()
return termCurrency;
}

@Override
public Money convert(LocalDate date, Money amount)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can leave this to the superclass. It is enough to override getRate().

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is delegating to the super class, but "just" converts them 1:1 for any currency conversation that are not part of the test currency converter. Has this now become obsolete by introducing CHF explicitly?

Copy link
Member Author

@chirlu chirlu Mar 31, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don’t understand the remark (or question?). The behaviour (1:1 conversion for unknown currencies) is exactly the same as before:

  • Currently (before this change), it is done in this function, convert(), in line 69, by creating a new Money with the desired currency and the existing amount.
  • With this change, convert() in the superclass calls our getRate() to find the exchange rate, and getRate() returns 1 for unknown currency pairs. The superclass convert() uses this rate for the conversion, with the same effect.

I don’t actually think any tests rely on this behaviour, though.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You are right. I forgot (and was looking at the PR only in the browser without full access to the code)
I added the fallback conversation rate later, which makes the overriding of getRate obsolete. Agree.

@chirlu
Copy link
Member Author

chirlu commented Mar 31, 2023

I wrote this some time ago because I am going to need it (inverse conversion, and second foreign currency) to test some other change that I hope to finish some other day. I’m submitting it now because #3248 is also trying to add inverse conversion to the TestCurrencyConverter, in an ugly way.

{
// testing: any other currency will be converted 1:1
if (!amount.getCurrencyCode().equals(series.getBaseCurrency()))
return Money.of(termCurrency, amount.getAmount());
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just to point it out, this is where the 1:1 conversion happens in the old code …

series = EUR_CHF;
else
// testing: any other currency will be converted 1:1
return new ExchangeRate(date, BigDecimal.ONE);
Copy link
Member Author

@chirlu chirlu Mar 31, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

… and this is where, in the new code, an exchange rate of 1 is passed to the superclass’s convert(), which uses it to do a 1:1 conversion.

(Edit: Just saw that it is technically not a superclass, but an interface with a default implementation. Doesn’t make a difference in the end, however.)

@buchen buchen merged commit ddac537 into portfolio-performance:master Mar 31, 2023
@chirlu chirlu deleted the currency_test branch March 31, 2023 23:35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants