Skip to content

Open Source Wealth Management Software. Angular + NestJS + Prisma + Nx + TypeScript 🤍

remove database dependencies for test execution (#96)

Thomas5d36d3a6bb05
diff --git a/.travis.yml b/.travis.yml
index 3c7dad881a..d85e34ff71 100644
--- a/.travis.yml
+++ b/.travis.yml
@@ -7,3 +7,4 @@ before_script:
- yarn
script:
- yarn format:check
+ - yarn test
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 179730c5c8..eb9baa86a0 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -10,6 +10,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Changed
- Hid unknown exchange in the position overview
+- Refactored the portfolio unit tests to work without database
## 1.3.0 - 15.05.2021
diff --git a/apps/api/src/models/portfolio.spec.ts b/apps/api/src/models/portfolio.spec.ts
index e1f6d3cc97..ef19fde687 100644
--- a/apps/api/src/models/portfolio.spec.ts
+++ b/apps/api/src/models/portfolio.spec.ts
@@ -4,67 +4,93 @@ import {
getUtc,
getYesterday
} from '@ghostfolio/helper';
-import { Test } from '@nestjs/testing';
import { AccountType, Currency, DataSource, Role, Type } from '@prisma/client';
-import { ConfigurationService } from '../services/configuration.service';
import { DataProviderService } from '../services/data-provider.service';
-import { AlphaVantageService } from '../services/data-provider/alpha-vantage/alpha-vantage.service';
-import { GhostfolioScraperApiService } from '../services/data-provider/ghostfolio-scraper-api/ghostfolio-scraper-api.service';
-import { RakutenRapidApiService } from '../services/data-provider/rakuten-rapid-api/rakuten-rapid-api.service';
-import { YahooFinanceService } from '../services/data-provider/yahoo-finance/yahoo-finance.service';
import { ExchangeRateDataService } from '../services/exchange-rate-data.service';
import { MarketState } from '../services/interfaces/interfaces';
-import { PrismaService } from '../services/prisma.service';
import { RulesService } from '../services/rules.service';
import { Portfolio } from './portfolio';
+import { format } from 'date-fns';
+
+jest.mock('../services/data-provider.service', () => {
+ return {
+ DataProviderService: jest.fn().mockImplementation(() => {
+ const today = format(new Date(), 'yyyy-MM-dd');
+
+ return {
+ get: () => {
+ return Promise.resolve({
+ BTCUSD: {
+ currency: Currency.USD,
+ dataSource: DataSource.YAHOO,
+ exchange: UNKNOWN_KEY,
+ marketPrice: 57973.008,
+ marketState: MarketState.open,
+ name: 'Bitcoin USD',
+ type: 'Cryptocurrency'
+ },
+ ETHUSD: {
+ currency: Currency.USD,
+ dataSource: DataSource.YAHOO,
+ exchange: UNKNOWN_KEY,
+ marketPrice: 3915.337,
+ marketState: MarketState.open,
+ name: 'Ethereum USD',
+ type: 'Cryptocurrency'
+ }
+ });
+ },
+ getHistorical: () => {
+ return Promise.resolve({
+ BTCUSD: {
+ [today]: 57973.008
+ },
+ ETHUSD: {
+ [today]: 3915.337
+ }
+ });
+ }
+ };
+ })
+ };
+});
+
+jest.mock('../services/exchange-rate-data.service', () => {
+ return {
+ ExchangeRateDataService: jest.fn().mockImplementation(() => {
+ return {
+ initialize: () => Promise.resolve(),
+ toCurrency: (value: number) => value
+ };
+ })
+ };
+});
+
+jest.mock('../services/data-provider.service');
+jest.mock('../services/exchange-rate-data.service');
+jest.mock('../services/rules.service');
const DEFAULT_ACCOUNT_ID = '693a834b-eb89-42c9-ae47-35196c25d269';
const USER_ID = 'ca6ce867-5d31-495a-bce9-5942bbca9237';
describe('Portfolio', () => {
- let alphaVantageService: AlphaVantageService;
- let configurationService: ConfigurationService;
let dataProviderService: DataProviderService;
let exchangeRateDataService: ExchangeRateDataService;
- let ghostfolioScraperApiService: GhostfolioScraperApiService;
let portfolio: Portfolio;
- let prismaService: PrismaService;
- let rakutenRapidApiService: RakutenRapidApiService;
let rulesService: RulesService;
- let yahooFinanceService: YahooFinanceService;
beforeAll(async () => {
- const app = await Test.createTestingModule({
- imports: [],
- providers: [
- AlphaVantageService,
- ConfigurationService,
- DataProviderService,
- ExchangeRateDataService,
- GhostfolioScraperApiService,
- PrismaService,
- RakutenRapidApiService,
- RulesService,
- YahooFinanceService
- ]
- }).compile();
-
- alphaVantageService = app.get<AlphaVantageService>(AlphaVantageService);
- configurationService = app.get<ConfigurationService>(ConfigurationService);
- dataProviderService = app.get<DataProviderService>(DataProviderService);
- exchangeRateDataService = app.get<ExchangeRateDataService>(
- ExchangeRateDataService
- );
- ghostfolioScraperApiService = app.get<GhostfolioScraperApiService>(
- GhostfolioScraperApiService
+ dataProviderService = new DataProviderService(
+ null,
+ null,
+ null,
+ null,
+ null,
+ null
);
- prismaService = app.get<PrismaService>(PrismaService);
- rakutenRapidApiService = app.get<RakutenRapidApiService>(
- RakutenRapidApiService
- );
- rulesService = app.get<RulesService>(RulesService);
- yahooFinanceService = app.get<YahooFinanceService>(YahooFinanceService);
+ exchangeRateDataService = new ExchangeRateDataService(null);
+ rulesService = new RulesService();
await exchangeRateDataService.initialize();
@@ -600,8 +626,4 @@ describe('Portfolio', () => {
expect(portfolio.getSymbols(getYesterday())).toEqual(['ETHUSD']);
});
});
-
- afterAll(async () => {
- prismaService.$disconnect();
- });
});
diff --git a/package.json b/package.json
index 3e0aa56eac..075d54308c 100644
--- a/package.json
+++ b/package.json
@@ -37,10 +37,11 @@
"start:client": "ng serve client --hmr -o",
"start:prod": "node apps/api/main",
"start:server": "nx serve api",
- "test": "ng test",
+ "test": "nx test",
"ts-node": "ts-node --compiler-options '{\"module\":\"CommonJS\"}'",
"update": "nx migrate latest",
"watch:server": "nx build api --watch",
+ "watch:test": "nx test --watch",
"workspace-generator": "nx workspace-generator"
},
"dependencies": {