Browse Source

Extend current rate service test

pull/239/head
Thomas 4 years ago
parent
commit
67606e4026
  1. 2
      apps/api/src/app/app.module.ts
  2. 30
      apps/api/src/app/core/core.module.ts
  3. 89
      apps/api/src/app/core/current-rate.service.spec.ts
  4. 41
      apps/api/src/app/core/current-rate.service.ts
  5. 20
      apps/api/src/app/core/market-data.service.ts
  6. 6
      apps/api/src/models/portfolio.spec.ts

2
apps/api/src/app/app.module.ts

@ -22,6 +22,7 @@ import { AdminModule } from './admin/admin.module';
import { AppController } from './app.controller'; import { AppController } from './app.controller';
import { AuthModule } from './auth/auth.module'; import { AuthModule } from './auth/auth.module';
import { CacheModule } from './cache/cache.module'; import { CacheModule } from './cache/cache.module';
import { CoreModule } from './core/core.module';
import { ExperimentalModule } from './experimental/experimental.module'; import { ExperimentalModule } from './experimental/experimental.module';
import { ExportModule } from './export/export.module'; import { ExportModule } from './export/export.module';
import { ImportModule } from './import/import.module'; import { ImportModule } from './import/import.module';
@ -42,6 +43,7 @@ import { UserModule } from './user/user.module';
AuthModule, AuthModule,
CacheModule, CacheModule,
ConfigModule.forRoot(), ConfigModule.forRoot(),
CoreModule,
ExperimentalModule, ExperimentalModule,
ExportModule, ExportModule,
ImportModule, ImportModule,

30
apps/api/src/app/core/core.module.ts

@ -0,0 +1,30 @@
import { ConfigurationService } from '@ghostfolio/api/services/configuration.service';
import { DataProviderService } from '@ghostfolio/api/services/data-provider.service';
import { AlphaVantageService } from '@ghostfolio/api/services/data-provider/alpha-vantage/alpha-vantage.service';
import { GhostfolioScraperApiService } from '@ghostfolio/api/services/data-provider/ghostfolio-scraper-api/ghostfolio-scraper-api.service';
import { RakutenRapidApiService } from '@ghostfolio/api/services/data-provider/rakuten-rapid-api/rakuten-rapid-api.service';
import { YahooFinanceService } from '@ghostfolio/api/services/data-provider/yahoo-finance/yahoo-finance.service';
import { ExchangeRateDataService } from '@ghostfolio/api/services/exchange-rate-data.service';
import { PrismaService } from '@ghostfolio/api/services/prisma.service';
import { Module } from '@nestjs/common';
import { CurrentRateService } from './current-rate.service';
import { MarketDataService } from './market-data.service';
@Module({
imports: [],
controllers: [],
providers: [
AlphaVantageService,
ConfigurationService,
CurrentRateService,
DataProviderService,
ExchangeRateDataService,
GhostfolioScraperApiService,
MarketDataService,
PrismaService,
RakutenRapidApiService,
YahooFinanceService
]
})
export class CoreModule {}

89
apps/api/src/app/core/current-rate.service.spec.ts

@ -1,60 +1,65 @@
import { CurrentRateService } from '@ghostfolio/api/app/core/current-rate.service'; import { CurrentRateService } from '@ghostfolio/api/app/core/current-rate.service';
import { Currency } from '@prisma/client';
import { ExchangeRateDataService } from '@ghostfolio/api/services/exchange-rate-data.service'; import { ExchangeRateDataService } from '@ghostfolio/api/services/exchange-rate-data.service';
import { PrismaService } from '@ghostfolio/api/services/prisma.service'; import { Currency, MarketData } from '@prisma/client';
jest.mock('../../services/exchange-rate-data.service', () => { import { MarketDataService } from './market-data.service';
jest.mock('./market-data.service', () => {
return { return {
// eslint-disable-next-line @typescript-eslint/naming-convention MarketDataService: jest.fn().mockImplementation(() => {
exchangeRateDataService: jest.fn().mockImplementation(() => {
return { return {
toCurrency: (aValue: number, get: (date: Date, symbol: string) => {
aFromCurrency: Currency, return Promise.resolve<MarketData>({
aToCurrency: Currency) => { date,
return 1 * aValue; symbol,
createdAt: date,
id: 'aefcbe3a-ee10-4c4f-9f2d-8ffad7b05584',
marketPrice: 1847.839966
});
} }
} };
}) })
}; };
}); });
// https://jestjs.io/docs/manual-mocks#mocking-node-modules jest.mock('../../services/exchange-rate-data.service', () => {
// jest.mock('?', () => { return {
// return { ExchangeRateDataService: jest.fn().mockImplementation(() => {
// // eslint-disable-next-line @typescript-eslint/naming-convention return {
// prismaService: jest.fn().mockImplementation(() => { initialize: () => Promise.resolve(),
// return { toCurrency: (value: number) => {
// marketData: { return 1 * value;
// findFirst: (data: any) => { }
// return { };
// marketPrice: 100 })
// }; };
// } });
// }
// };
// })
// };
// });
xdescribe('CurrentRateService', () => {
describe('CurrentRateService', () => {
let currentRateService: CurrentRateService;
let exchangeRateDataService: ExchangeRateDataService; let exchangeRateDataService: ExchangeRateDataService;
let prismaService: PrismaService; let marketDataService: MarketDataService;
beforeEach(() => { beforeAll(async () => {
exchangeRateDataService = new ExchangeRateDataService(undefined); exchangeRateDataService = new ExchangeRateDataService(null);
prismaService = new PrismaService(); marketDataService = new MarketDataService(null);
});
it('getValue', () => { await exchangeRateDataService.initialize();
const currentRateService = new CurrentRateService(exchangeRateDataService, prismaService);
expect(currentRateService.getValue({ currentRateService = new CurrentRateService(
date: new Date(), exchangeRateDataService,
symbol: 'AIA', marketDataService
currency: Currency.USD, );
userCurrency: Currency.CHF
})).toEqual(0);
}); });
it('getValue', async () => {
expect(
await currentRateService.getValue({
currency: Currency.USD,
date: new Date(Date.UTC(2020, 0, 1, 0, 0, 0)),
symbol: 'AMZN',
userCurrency: Currency.CHF
})
).toEqual(1847.839966);
});
}); });

41
apps/api/src/app/core/current-rate.service.ts

@ -1,32 +1,37 @@
import { Currency } from '@prisma/client';
import { PrismaService } from '@ghostfolio/api/services/prisma.service';
import { ExchangeRateDataService } from '@ghostfolio/api/services/exchange-rate-data.service'; import { ExchangeRateDataService } from '@ghostfolio/api/services/exchange-rate-data.service';
import { Injectable } from '@nestjs/common';
import { Currency } from '@prisma/client';
export class CurrentRateService { import { MarketDataService } from './market-data.service';
@Injectable()
export class CurrentRateService {
public constructor( public constructor(
private readonly exchangeRateDataService: ExchangeRateDataService, private readonly exchangeRateDataService: ExchangeRateDataService,
private prisma: PrismaService private readonly marketDataService: MarketDataService
) {} ) {}
/** public async getValue({
* TODO: @dtslvr currency,
*/ date,
public async getValue({date, symbol, currency, userCurrency}: GetValueParams): Promise<number> { symbol,
const marketData = await this.prisma.marketData.findFirst({ userCurrency
select: { date: true, marketPrice: true }, }: GetValueParams): Promise<number> {
where: { date: date, symbol: symbol } const marketData = await this.marketDataService.get({
date,
symbol
}); });
console.log(marketData); // { date: Date, marketPrice: number } if (marketData) {
return this.exchangeRateDataService.toCurrency(
marketData.marketPrice,
currency,
userCurrency
);
}
return this.exchangeRateDataService.toCurrency( throw new Error(`Value not found for ${symbol} at ${date}`);
marketData.marketPrice,
currency,
userCurrency
);
} }
} }
export interface GetValueParams { export interface GetValueParams {

20
apps/api/src/app/core/market-data.service.ts

@ -0,0 +1,20 @@
import { PrismaService } from '@ghostfolio/api/services/prisma.service';
import { Injectable } from '@nestjs/common';
import { MarketData } from '@prisma/client';
@Injectable()
export class MarketDataService {
public constructor(private prisma: PrismaService) {}
public async get({
date,
symbol
}: {
date: Date;
symbol: string;
}): Promise<MarketData> {
return await this.prisma.marketData.findFirst({
where: { date, symbol }
});
}
}

6
apps/api/src/models/portfolio.spec.ts

@ -78,14 +78,14 @@ jest.mock('../services/exchange-rate-data.service', () => {
ExchangeRateDataService: jest.fn().mockImplementation(() => { ExchangeRateDataService: jest.fn().mockImplementation(() => {
return { return {
initialize: () => Promise.resolve(), initialize: () => Promise.resolve(),
toCurrency: (value: number) => value toCurrency: (value: number) => {
return 1 * value;
}
}; };
}) })
}; };
}); });
jest.mock('../services/data-provider.service');
jest.mock('../services/exchange-rate-data.service');
jest.mock('../services/rules.service'); jest.mock('../services/rules.service');
const DEFAULT_ACCOUNT_ID = '693a834b-eb89-42c9-ae47-35196c25d269'; const DEFAULT_ACCOUNT_ID = '693a834b-eb89-42c9-ae47-35196c25d269';

Loading…
Cancel
Save