mirror of https://github.com/ghostfolio/ghostfolio
				
				
			
				 6 changed files with 125 additions and 63 deletions
			
			
		@ -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 {} | 
				
			|||
@ -1,60 +1,65 @@ | 
				
			|||
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 { 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 { | 
				
			|||
    // eslint-disable-next-line @typescript-eslint/naming-convention
 | 
				
			|||
    exchangeRateDataService: jest.fn().mockImplementation(() => { | 
				
			|||
    MarketDataService: jest.fn().mockImplementation(() => { | 
				
			|||
      return { | 
				
			|||
        toCurrency: (aValue: number, | 
				
			|||
                     aFromCurrency: Currency, | 
				
			|||
                     aToCurrency: Currency) => { | 
				
			|||
          return 1 * aValue; | 
				
			|||
        get: (date: Date, symbol: string) => { | 
				
			|||
          return Promise.resolve<MarketData>({ | 
				
			|||
            date, | 
				
			|||
            symbol, | 
				
			|||
            createdAt: date, | 
				
			|||
            id: 'aefcbe3a-ee10-4c4f-9f2d-8ffad7b05584', | 
				
			|||
            marketPrice: 1847.839966 | 
				
			|||
          }); | 
				
			|||
        } | 
				
			|||
      } | 
				
			|||
      }; | 
				
			|||
    }) | 
				
			|||
  }; | 
				
			|||
}); | 
				
			|||
 | 
				
			|||
// https://jestjs.io/docs/manual-mocks#mocking-node-modules
 | 
				
			|||
// jest.mock('?', () => {
 | 
				
			|||
//   return {
 | 
				
			|||
//     // eslint-disable-next-line @typescript-eslint/naming-convention
 | 
				
			|||
//     prismaService: jest.fn().mockImplementation(() => {
 | 
				
			|||
//       return {
 | 
				
			|||
//         marketData: {
 | 
				
			|||
//           findFirst: (data: any) => {
 | 
				
			|||
//             return {
 | 
				
			|||
//               marketPrice: 100
 | 
				
			|||
//             };
 | 
				
			|||
//           }
 | 
				
			|||
//         }
 | 
				
			|||
//       };
 | 
				
			|||
//     })
 | 
				
			|||
//   };
 | 
				
			|||
// });
 | 
				
			|||
 | 
				
			|||
xdescribe('CurrentRateService', () => { | 
				
			|||
jest.mock('../../services/exchange-rate-data.service', () => { | 
				
			|||
  return { | 
				
			|||
    ExchangeRateDataService: jest.fn().mockImplementation(() => { | 
				
			|||
      return { | 
				
			|||
        initialize: () => Promise.resolve(), | 
				
			|||
        toCurrency: (value: number) => { | 
				
			|||
          return 1 * value; | 
				
			|||
        } | 
				
			|||
      }; | 
				
			|||
    }) | 
				
			|||
  }; | 
				
			|||
}); | 
				
			|||
 | 
				
			|||
describe('CurrentRateService', () => { | 
				
			|||
  let currentRateService: CurrentRateService; | 
				
			|||
  let exchangeRateDataService: ExchangeRateDataService; | 
				
			|||
  let prismaService: PrismaService; | 
				
			|||
  let marketDataService: MarketDataService; | 
				
			|||
 | 
				
			|||
  beforeEach(() => { | 
				
			|||
    exchangeRateDataService = new ExchangeRateDataService(undefined); | 
				
			|||
    prismaService = new PrismaService(); | 
				
			|||
  }); | 
				
			|||
  beforeAll(async () => { | 
				
			|||
    exchangeRateDataService = new ExchangeRateDataService(null); | 
				
			|||
    marketDataService = new MarketDataService(null); | 
				
			|||
 | 
				
			|||
  it('getValue', () => { | 
				
			|||
    const currentRateService = new CurrentRateService(exchangeRateDataService, prismaService); | 
				
			|||
    await exchangeRateDataService.initialize(); | 
				
			|||
 | 
				
			|||
    expect(currentRateService.getValue({ | 
				
			|||
      date: new Date(), | 
				
			|||
      symbol: 'AIA', | 
				
			|||
      currency: Currency.USD, | 
				
			|||
      userCurrency: Currency.CHF | 
				
			|||
    })).toEqual(0); | 
				
			|||
    currentRateService = new CurrentRateService( | 
				
			|||
      exchangeRateDataService, | 
				
			|||
      marketDataService | 
				
			|||
    ); | 
				
			|||
  }); | 
				
			|||
 | 
				
			|||
  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); | 
				
			|||
  }); | 
				
			|||
}); | 
				
			|||
 | 
				
			|||
@ -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 } | 
				
			|||
    }); | 
				
			|||
  } | 
				
			|||
} | 
				
			|||
					Loading…
					
					
				
		Reference in new issue