From be324ea4ecb0dfef0cdef168e6aaa1b227337754 Mon Sep 17 00:00:00 2001 From: Thomas Kaul <4159106+dtslvr@users.noreply.github.com> Date: Sat, 13 Jan 2024 08:56:25 +0100 Subject: [PATCH] Fix BTCUSD test --- ...ator-btcusd-buy-and-sell-partially.spec.ts | 52 +++++++++++++------ .../exchange-rate-data.service.mock.ts | 30 +++++++---- 2 files changed, 55 insertions(+), 27 deletions(-) diff --git a/apps/api/src/app/portfolio/portfolio-calculator-btcusd-buy-and-sell-partially.spec.ts b/apps/api/src/app/portfolio/portfolio-calculator-btcusd-buy-and-sell-partially.spec.ts index 7c590ead5..cb96751f1 100644 --- a/apps/api/src/app/portfolio/portfolio-calculator-btcusd-buy-and-sell-partially.spec.ts +++ b/apps/api/src/app/portfolio/portfolio-calculator-btcusd-buy-and-sell-partially.spec.ts @@ -1,5 +1,6 @@ import { CurrentRateService } from '@ghostfolio/api/app/portfolio/current-rate.service'; import { ExchangeRateDataService } from '@ghostfolio/api/services/exchange-rate-data/exchange-rate-data.service'; +import { ExchangeRateDataServiceMock } from '@ghostfolio/api/services/exchange-rate-data/exchange-rate-data.service.mock'; import { parseDate } from '@ghostfolio/common/helper'; import Big from 'big.js'; @@ -15,6 +16,18 @@ jest.mock('@ghostfolio/api/app/portfolio/current-rate.service', () => { }; }); +jest.mock( + '@ghostfolio/api/services/exchange-rate-data/exchange-rate-data.service', + () => { + return { + // eslint-disable-next-line @typescript-eslint/naming-convention + ExchangeRateDataService: jest.fn().mockImplementation(() => { + return ExchangeRateDataServiceMock; + }) + }; + } +); + describe('PortfolioCalculator', () => { let currentRateService: CurrentRateService; let exchangeRateDataService: ExchangeRateDataService; @@ -38,7 +51,7 @@ describe('PortfolioCalculator', () => { currency: 'CHF', orders: [ { - currency: 'CHF', // TODO: Change to USD + currency: 'USD', date: '2015-01-01', dataSource: 'YAHOO', fee: new Big(0), @@ -49,7 +62,7 @@ describe('PortfolioCalculator', () => { unitPrice: new Big(320.43) }, { - currency: 'CHF', // TODO: Change to USD + currency: 'USD', date: '2017-12-31', dataSource: 'YAHOO', fee: new Big(0), @@ -80,55 +93,60 @@ describe('PortfolioCalculator', () => { spy.mockRestore(); expect(currentPositions).toEqual({ - currentValue: new Big('13657.2'), + currentValue: new Big('13298.425356'), errors: [], grossPerformance: new Big('27172.74'), grossPerformancePercentage: new Big('42.41978276196153750666'), grossPerformancePercentageWithCurrencyEffect: new Big( - '42.41978276196153750666' + '41.6401219622042072686' ), - grossPerformanceWithCurrencyEffect: new Big('27172.74'), + grossPerformanceWithCurrencyEffect: new Big('26516.208701400000064086'), hasErrors: false, netPerformance: new Big('27172.74'), netPerformancePercentage: new Big('42.41978276196153750666'), netPerformancePercentageWithCurrencyEffect: new Big( - '42.41978276196153750666' + '41.6401219622042072686' ), - netPerformanceWithCurrencyEffect: new Big('27172.74'), + netPerformanceWithCurrencyEffect: new Big('26516.208701400000064086'), positions: [ { averagePrice: new Big('320.43'), - currency: 'CHF', // TODO: Change to USD + currency: 'USD', dataSource: 'YAHOO', fee: new Big('0'), firstBuyDate: '2015-01-01', grossPerformance: new Big('27172.74'), grossPerformancePercentage: new Big('42.41978276196153750666'), grossPerformancePercentageWithCurrencyEffect: new Big( - '42.41978276196153750666' + '41.6401219622042072686' + ), + grossPerformanceWithCurrencyEffect: new Big( + '26516.208701400000064086' ), - grossPerformanceWithCurrencyEffect: new Big('27172.74'), investment: new Big('320.43'), - investmentWithCurrencyEffect: new Big('320.43'), + investmentWithCurrencyEffect: new Big('318.542667299999967957'), + marketPrice: 13657.2, + marketPriceInBaseCurrency: 13298.425356, netPerformance: new Big('27172.74'), netPerformancePercentage: new Big('42.41978276196153750666'), netPerformancePercentageWithCurrencyEffect: new Big( - '42.41978276196153750666' + '41.6401219622042072686' + ), + netPerformanceWithCurrencyEffect: new Big( + '26516.208701400000064086' ), - netPerformanceWithCurrencyEffect: new Big('27172.74'), - marketPrice: 13657.2, - marketPriceInBaseCurrency: 13657.2, quantity: new Big('1'), symbol: 'BTCUSD', + tags: undefined, timeWeightedInvestment: new Big('640.56763686131386861314'), timeWeightedInvestmentWithCurrencyEffect: new Big( - '640.56763686131386861314' + '636.79469348020066587024' ), transactionCount: 2 } ], totalInvestment: new Big('320.43'), - totalInvestmentWithCurrencyEffect: new Big('320.43') + totalInvestmentWithCurrencyEffect: new Big('318.542667299999967957') }); expect(investments).toEqual([ diff --git a/apps/api/src/services/exchange-rate-data/exchange-rate-data.service.mock.ts b/apps/api/src/services/exchange-rate-data/exchange-rate-data.service.mock.ts index f1c8728a3..1fc88bb6a 100644 --- a/apps/api/src/services/exchange-rate-data/exchange-rate-data.service.mock.ts +++ b/apps/api/src/services/exchange-rate-data/exchange-rate-data.service.mock.ts @@ -5,15 +5,25 @@ export const ExchangeRateDataServiceMock = { startDate, targetCurrency }): Promise => { - return Promise.resolve({ - CHF: { - '2023-01-03': 1, - '2023-07-10': 1 - }, - USD: { - '2023-01-03': 0.92285, - '2023-07-10': 0.8889 - } - }); + if (targetCurrency === 'CHF') { + return Promise.resolve({ + CHF: { + '2015-01-01': 1, + '2017-12-31': 1, + '2018-01-01': 1, + '2023-01-03': 1, + '2023-07-10': 1 + }, + USD: { + '2015-01-01': 0.9941099999999999, + '2017-12-31': 0.9787, + '2018-01-01': 0.97373, + '2023-01-03': 0.9238, + '2023-07-10': 0.8854 + } + }); + } + + return Promise.resolve({}); } };