mirror of https://github.com/ghostfolio/ghostfolio
committed by
GitHub
7 changed files with 403 additions and 46 deletions
@ -1,3 +1,186 @@ |
|||||
|
import { PortfolioCalculator } from '@ghostfolio/api/app/portfolio/calculator/portfolio-calculator'; |
||||
|
import { userDummyData } from '@ghostfolio/api/app/portfolio/calculator/portfolio-calculator-test-utils'; |
||||
|
import { PortfolioCalculatorFactory } from '@ghostfolio/api/app/portfolio/calculator/portfolio-calculator.factory'; |
||||
|
import { CurrentRateService } from '@ghostfolio/api/app/portfolio/current-rate.service'; |
||||
|
import { CurrentRateServiceMock } from '@ghostfolio/api/app/portfolio/current-rate.service.mock'; |
||||
|
import { RedisCacheService } from '@ghostfolio/api/app/redis-cache/redis-cache.service'; |
||||
|
import { RedisCacheServiceMock } from '@ghostfolio/api/app/redis-cache/redis-cache.service.mock'; |
||||
|
import { ConfigurationService } from '@ghostfolio/api/services/configuration/configuration.service'; |
||||
|
import { ExchangeRateDataService } from '@ghostfolio/api/services/exchange-rate-data/exchange-rate-data.service'; |
||||
|
import { PortfolioSnapshotService } from '@ghostfolio/api/services/queues/portfolio-snapshot/portfolio-snapshot.service'; |
||||
|
import { PortfolioSnapshotServiceMock } from '@ghostfolio/api/services/queues/portfolio-snapshot/portfolio-snapshot.service.mock'; |
||||
|
import { HistoricalDataItem } from '@ghostfolio/common/interfaces'; |
||||
|
import { PerformanceCalculationType } from '@ghostfolio/common/types/performance-calculation-type.type'; |
||||
|
|
||||
|
jest.mock('@ghostfolio/api/app/portfolio/current-rate.service', () => { |
||||
|
return { |
||||
|
CurrentRateService: jest.fn().mockImplementation(() => { |
||||
|
return CurrentRateServiceMock; |
||||
|
}) |
||||
|
}; |
||||
|
}); |
||||
|
|
||||
|
jest.mock( |
||||
|
'@ghostfolio/api/services/queues/portfolio-snapshot/portfolio-snapshot.service', |
||||
|
() => { |
||||
|
return { |
||||
|
PortfolioSnapshotService: jest.fn().mockImplementation(() => { |
||||
|
return PortfolioSnapshotServiceMock; |
||||
|
}) |
||||
|
}; |
||||
|
} |
||||
|
); |
||||
|
|
||||
|
jest.mock('@ghostfolio/api/app/redis-cache/redis-cache.service', () => { |
||||
|
return { |
||||
|
RedisCacheService: jest.fn().mockImplementation(() => { |
||||
|
return RedisCacheServiceMock; |
||||
|
}) |
||||
|
}; |
||||
|
}); |
||||
|
|
||||
|
function getHistoricalDataItem({ |
||||
|
date, |
||||
|
investment, |
||||
|
netPerformance |
||||
|
}: { |
||||
|
date: string; |
||||
|
investment: number; |
||||
|
netPerformance: number; |
||||
|
}): HistoricalDataItem { |
||||
|
return { |
||||
|
date, |
||||
|
netPerformance, |
||||
|
dividendInBaseCurrency: 0, |
||||
|
netPerformanceWithCurrencyEffect: netPerformance, |
||||
|
totalInvestment: investment, |
||||
|
totalInvestmentValueWithCurrencyEffect: investment, |
||||
|
value: investment + netPerformance, |
||||
|
valueWithCurrencyEffect: investment + netPerformance |
||||
|
}; |
||||
|
} |
||||
|
|
||||
describe('PortfolioCalculator', () => { |
describe('PortfolioCalculator', () => { |
||||
test.skip('Skip empty test', () => 1); |
let configurationService: ConfigurationService; |
||||
|
let currentRateService: CurrentRateService; |
||||
|
let exchangeRateDataService: ExchangeRateDataService; |
||||
|
let portfolioCalculator: PortfolioCalculator; |
||||
|
let portfolioCalculatorFactory: PortfolioCalculatorFactory; |
||||
|
let portfolioSnapshotService: PortfolioSnapshotService; |
||||
|
let redisCacheService: RedisCacheService; |
||||
|
|
||||
|
beforeEach(() => { |
||||
|
PortfolioSnapshotServiceMock.reset(); |
||||
|
RedisCacheServiceMock.reset(); |
||||
|
|
||||
|
configurationService = new ConfigurationService(); |
||||
|
|
||||
|
currentRateService = new CurrentRateService(null, null, null); |
||||
|
|
||||
|
exchangeRateDataService = new ExchangeRateDataService( |
||||
|
null, |
||||
|
null, |
||||
|
null, |
||||
|
null |
||||
|
); |
||||
|
|
||||
|
portfolioSnapshotService = new PortfolioSnapshotService(null, null); |
||||
|
|
||||
|
redisCacheService = new RedisCacheService(null, null); |
||||
|
|
||||
|
portfolioCalculatorFactory = new PortfolioCalculatorFactory( |
||||
|
configurationService, |
||||
|
currentRateService, |
||||
|
exchangeRateDataService, |
||||
|
portfolioSnapshotService, |
||||
|
redisCacheService |
||||
|
); |
||||
|
|
||||
|
portfolioCalculator = portfolioCalculatorFactory.createCalculator({ |
||||
|
activities: [], |
||||
|
calculationType: PerformanceCalculationType.ROAI, |
||||
|
currency: 'USD', |
||||
|
userId: userDummyData.id |
||||
|
}); |
||||
|
}); |
||||
|
|
||||
|
describe('calculate performance percentages for date range', () => { |
||||
|
it('with a buy followed by a gap between chart dates', () => { |
||||
|
const performancePercentagesByDate = portfolioCalculator[ |
||||
|
'calculatePerformancePercentagesForDateRange' |
||||
|
]({ |
||||
|
historicalDataItems: [ |
||||
|
getHistoricalDataItem({ |
||||
|
date: '2017-01-01', |
||||
|
investment: 100, |
||||
|
netPerformance: 0 |
||||
|
}), |
||||
|
// Buy, followed by a gap of 21 days until the next chart date
|
||||
|
getHistoricalDataItem({ |
||||
|
date: '2017-01-02', |
||||
|
investment: 200, |
||||
|
netPerformance: 0 |
||||
|
}), |
||||
|
// Sell
|
||||
|
getHistoricalDataItem({ |
||||
|
date: '2017-01-23', |
||||
|
investment: 100, |
||||
|
netPerformance: 44 |
||||
|
}) |
||||
|
] |
||||
|
}); |
||||
|
|
||||
|
// The investment of each chart date applies to each day until the next
|
||||
|
// chart date. The investment of the last chart date counts for this day
|
||||
|
// only: (1 * 100 + 21 * 200 + 1 * 100) / 23 = 4400 / 23
|
||||
|
expect( |
||||
|
performancePercentagesByDate['2017-01-23'].netPerformanceInPercentage |
||||
|
).toBeCloseTo(0.23, 10); |
||||
|
|
||||
|
expect( |
||||
|
performancePercentagesByDate['2017-01-23'] |
||||
|
.netPerformanceInPercentageWithCurrencyEffect |
||||
|
).toBeCloseTo(0.23, 10); |
||||
|
}); |
||||
|
|
||||
|
it('with an additional chart date of the same investment', () => { |
||||
|
const performancePercentagesByDate = portfolioCalculator[ |
||||
|
'calculatePerformancePercentagesForDateRange' |
||||
|
]({ |
||||
|
historicalDataItems: [ |
||||
|
getHistoricalDataItem({ |
||||
|
date: '2017-01-01', |
||||
|
investment: 100, |
||||
|
netPerformance: 0 |
||||
|
}), |
||||
|
getHistoricalDataItem({ |
||||
|
date: '2017-01-02', |
||||
|
investment: 200, |
||||
|
netPerformance: 0 |
||||
|
}), |
||||
|
// An additional chart date splits the gap but must not change the
|
||||
|
// average investment
|
||||
|
getHistoricalDataItem({ |
||||
|
date: '2017-01-12', |
||||
|
investment: 200, |
||||
|
netPerformance: 20 |
||||
|
}), |
||||
|
getHistoricalDataItem({ |
||||
|
date: '2017-01-23', |
||||
|
investment: 100, |
||||
|
netPerformance: 44 |
||||
|
}) |
||||
|
] |
||||
|
}); |
||||
|
|
||||
|
expect( |
||||
|
performancePercentagesByDate['2017-01-23'].netPerformanceInPercentage |
||||
|
).toBeCloseTo(0.23, 10); |
||||
|
|
||||
|
expect( |
||||
|
performancePercentagesByDate['2017-01-23'] |
||||
|
.netPerformanceInPercentageWithCurrencyEffect |
||||
|
).toBeCloseTo(0.23, 10); |
||||
|
}); |
||||
|
}); |
||||
}); |
}); |
||||
|
|||||
Loading…
Reference in new issue