Browse Source

Fix tests

pull/1865/head
Thomas 2 years ago
parent
commit
4c4649f461
  1. 32
      apps/api/src/services/data-provider/data-enhancer/yahoo-finance/yahoo-finance.service.spec.ts
  2. 13
      apps/api/src/services/data-provider/data-enhancer/yahoo-finance/yahoo-finance.service.ts
  3. 25
      apps/api/src/services/data-provider/yahoo-finance/yahoo-finance.service.ts

32
apps/api/src/services/data-provider/yahoo-finance/yahoo-finance.service.spec.ts → apps/api/src/services/data-provider/data-enhancer/yahoo-finance/yahoo-finance.service.spec.ts

@ -1,7 +1,7 @@
import { ConfigurationService } from '@ghostfolio/api/services/configuration.service'; import { ConfigurationService } from '@ghostfolio/api/services/configuration.service';
import { CryptocurrencyService } from '@ghostfolio/api/services/cryptocurrency/cryptocurrency.service'; import { CryptocurrencyService } from '@ghostfolio/api/services/cryptocurrency/cryptocurrency.service';
import { YahooFinanceService } from './yahoo-finance.service'; import { YahooFinanceDataEnhancerService } from './yahoo-finance.service';
jest.mock( jest.mock(
'@ghostfolio/api/services/cryptocurrency/cryptocurrency.service', '@ghostfolio/api/services/cryptocurrency/cryptocurrency.service',
@ -25,16 +25,16 @@ jest.mock(
} }
); );
describe('YahooFinanceService', () => { describe('YahooFinanceDataEnhancerService', () => {
let configurationService: ConfigurationService; let configurationService: ConfigurationService;
let cryptocurrencyService: CryptocurrencyService; let cryptocurrencyService: CryptocurrencyService;
let yahooFinanceService: YahooFinanceService; let yahooFinanceDataEnhancerService: YahooFinanceDataEnhancerService;
beforeAll(async () => { beforeAll(async () => {
configurationService = new ConfigurationService(); configurationService = new ConfigurationService();
cryptocurrencyService = new CryptocurrencyService(); cryptocurrencyService = new CryptocurrencyService();
yahooFinanceService = new YahooFinanceService( yahooFinanceDataEnhancerService = new YahooFinanceDataEnhancerService(
configurationService, configurationService,
cryptocurrencyService cryptocurrencyService
); );
@ -42,25 +42,37 @@ describe('YahooFinanceService', () => {
it('convertFromYahooFinanceSymbol', async () => { it('convertFromYahooFinanceSymbol', async () => {
expect( expect(
await yahooFinanceService.convertFromYahooFinanceSymbol('BRK-B') await yahooFinanceDataEnhancerService.convertFromYahooFinanceSymbol(
'BRK-B'
)
).toEqual('BRK-B'); ).toEqual('BRK-B');
expect( expect(
await yahooFinanceService.convertFromYahooFinanceSymbol('BTC-USD') await yahooFinanceDataEnhancerService.convertFromYahooFinanceSymbol(
'BTC-USD'
)
).toEqual('BTCUSD'); ).toEqual('BTCUSD');
expect( expect(
await yahooFinanceService.convertFromYahooFinanceSymbol('EURUSD=X') await yahooFinanceDataEnhancerService.convertFromYahooFinanceSymbol(
'EURUSD=X'
)
).toEqual('EURUSD'); ).toEqual('EURUSD');
}); });
it('convertToYahooFinanceSymbol', async () => { it('convertToYahooFinanceSymbol', async () => {
expect( expect(
await yahooFinanceService.convertToYahooFinanceSymbol('BTCUSD') await yahooFinanceDataEnhancerService.convertToYahooFinanceSymbol(
'BTCUSD'
)
).toEqual('BTC-USD'); ).toEqual('BTC-USD');
expect( expect(
await yahooFinanceService.convertToYahooFinanceSymbol('DOGEUSD') await yahooFinanceDataEnhancerService.convertToYahooFinanceSymbol(
'DOGEUSD'
)
).toEqual('DOGE-USD'); ).toEqual('DOGE-USD');
expect( expect(
await yahooFinanceService.convertToYahooFinanceSymbol('USDCHF') await yahooFinanceDataEnhancerService.convertToYahooFinanceSymbol(
'USDCHF'
)
).toEqual('USDCHF=X'); ).toEqual('USDCHF=X');
}); });
}); });

13
apps/api/src/services/data-provider/data-enhancer/yahoo-finance/yahoo-finance.service.ts

@ -25,6 +25,19 @@ export class YahooFinanceDataEnhancerService implements DataEnhancerInterface {
this.baseCurrency = this.configurationService.get('BASE_CURRENCY'); this.baseCurrency = this.configurationService.get('BASE_CURRENCY');
} }
public convertFromYahooFinanceSymbol(aYahooFinanceSymbol: string) {
let symbol = aYahooFinanceSymbol.replace(
new RegExp(`-${this.baseCurrency}$`),
this.baseCurrency
);
if (symbol.includes('=X') && !symbol.includes(this.baseCurrency)) {
symbol = `${this.baseCurrency}${symbol}`;
}
return symbol.replace('=X', '');
}
/** /**
* Converts a symbol to a Yahoo Finance symbol * Converts a symbol to a Yahoo Finance symbol
* *

25
apps/api/src/services/data-provider/yahoo-finance/yahoo-finance.service.ts

@ -31,19 +31,6 @@ export class YahooFinanceService implements DataProviderInterface {
return true; return true;
} }
public convertFromYahooFinanceSymbol(aYahooFinanceSymbol: string) {
let symbol = aYahooFinanceSymbol.replace(
new RegExp(`-${this.baseCurrency}$`),
this.baseCurrency
);
if (symbol.includes('=X') && !symbol.includes(this.baseCurrency)) {
symbol = `${this.baseCurrency}${symbol}`;
}
return symbol.replace('=X', '');
}
public async getAssetProfile( public async getAssetProfile(
aSymbol: string aSymbol: string
): Promise<Partial<SymbolProfile>> { ): Promise<Partial<SymbolProfile>> {
@ -184,7 +171,10 @@ export class YahooFinanceService implements DataProviderInterface {
for (const quote of quotes) { for (const quote of quotes) {
// Convert symbols back // Convert symbols back
const symbol = this.convertFromYahooFinanceSymbol(quote.symbol); const symbol =
this.yahooFinanceDataEnhancerService.convertFromYahooFinanceSymbol(
quote.symbol
);
response[symbol] = { response[symbol] = {
currency: quote.currency, currency: quote.currency,
@ -301,9 +291,10 @@ export class YahooFinanceService implements DataProviderInterface {
return currentQuote.symbol === marketDataItem.symbol; return currentQuote.symbol === marketDataItem.symbol;
}); });
const symbol = this.convertFromYahooFinanceSymbol( const symbol =
marketDataItem.symbol this.yahooFinanceDataEnhancerService.convertFromYahooFinanceSymbol(
); marketDataItem.symbol
);
const { assetClass, assetSubClass } = const { assetClass, assetSubClass } =
this.yahooFinanceDataEnhancerService.parseAssetClass({ this.yahooFinanceDataEnhancerService.parseAssetClass({

Loading…
Cancel
Save