mirror of https://github.com/ghostfolio/ghostfolio
committed by
GitHub
16 changed files with 540 additions and 45 deletions
@ -0,0 +1,97 @@ |
|||
import { getAssetProfileIdentifier } from '@ghostfolio/common/helper'; |
|||
|
|||
import { DataSource } from '@prisma/client'; |
|||
|
|||
import { DataProviderService } from './data-provider.service'; |
|||
|
|||
describe('DataProviderService', () => { |
|||
let dataProviderService: DataProviderService; |
|||
let getAssetProfile: jest.Mock; |
|||
|
|||
beforeEach(() => { |
|||
getAssetProfile = jest.fn(); |
|||
|
|||
const dataProviderInterface = { |
|||
getAssetProfile, |
|||
getName: () => { |
|||
return DataSource.YAHOO; |
|||
} |
|||
}; |
|||
|
|||
dataProviderService = new DataProviderService( |
|||
null, |
|||
[dataProviderInterface] as any, |
|||
null, |
|||
null, |
|||
null, |
|||
null |
|||
); |
|||
}); |
|||
|
|||
describe('getAssetProfiles', () => { |
|||
it('Corrects the letter case of the symbol', async () => { |
|||
getAssetProfile.mockResolvedValue({ |
|||
currency: 'USD', |
|||
dataSource: DataSource.YAHOO, |
|||
name: 'Apple Inc.', |
|||
symbol: 'AAPL' |
|||
}); |
|||
|
|||
const assetProfiles = await dataProviderService.getAssetProfiles([ |
|||
{ dataSource: DataSource.YAHOO, symbol: 'aapl' } |
|||
]); |
|||
|
|||
expect( |
|||
assetProfiles[ |
|||
getAssetProfileIdentifier({ |
|||
dataSource: DataSource.YAHOO, |
|||
symbol: 'aapl' |
|||
}) |
|||
].symbol |
|||
).toEqual('AAPL'); |
|||
}); |
|||
|
|||
it('Keeps the requested symbol if the data provider resolves it to a different symbol', async () => { |
|||
getAssetProfile.mockResolvedValue({ |
|||
currency: 'USD', |
|||
dataSource: DataSource.YAHOO, |
|||
name: 'Meta Platforms, Inc.', |
|||
symbol: 'META' |
|||
}); |
|||
|
|||
const assetProfiles = await dataProviderService.getAssetProfiles([ |
|||
{ dataSource: DataSource.YAHOO, symbol: 'FB' } |
|||
]); |
|||
|
|||
expect( |
|||
assetProfiles[ |
|||
getAssetProfileIdentifier({ |
|||
dataSource: DataSource.YAHOO, |
|||
symbol: 'FB' |
|||
}) |
|||
].symbol |
|||
).toEqual('FB'); |
|||
}); |
|||
|
|||
it('Keeps the requested symbol if the data provider reports no symbol', async () => { |
|||
getAssetProfile.mockResolvedValue({ |
|||
currency: 'USD', |
|||
dataSource: DataSource.YAHOO, |
|||
name: 'Apple Inc.' |
|||
}); |
|||
|
|||
const assetProfiles = await dataProviderService.getAssetProfiles([ |
|||
{ dataSource: DataSource.YAHOO, symbol: 'aapl' } |
|||
]); |
|||
|
|||
expect( |
|||
assetProfiles[ |
|||
getAssetProfileIdentifier({ |
|||
dataSource: DataSource.YAHOO, |
|||
symbol: 'aapl' |
|||
}) |
|||
].symbol |
|||
).toEqual('aapl'); |
|||
}); |
|||
}); |
|||
}); |
|||
@ -0,0 +1,113 @@ |
|||
import { DataSource } from '@prisma/client'; |
|||
|
|||
import { SymbolProfileService } from './symbol-profile.service'; |
|||
|
|||
describe('SymbolProfileService', () => { |
|||
let symbolProfileService: SymbolProfileService; |
|||
|
|||
let prismaService: { |
|||
symbolProfile: { findMany: jest.Mock; findUnique: jest.Mock }; |
|||
}; |
|||
|
|||
beforeEach(() => { |
|||
prismaService = { |
|||
symbolProfile: { |
|||
findMany: jest.fn().mockResolvedValue([]), |
|||
findUnique: jest.fn().mockResolvedValue(null) |
|||
} |
|||
}; |
|||
|
|||
symbolProfileService = new SymbolProfileService(prismaService as any); |
|||
}); |
|||
|
|||
describe('getSymbolOfAssetProfile', () => { |
|||
it('Keeps the symbol of the existing asset profile', async () => { |
|||
prismaService.symbolProfile.findUnique.mockResolvedValue({ |
|||
symbol: 'AAPL' |
|||
}); |
|||
|
|||
const symbol = await symbolProfileService.getSymbolOfAssetProfile({ |
|||
dataSource: DataSource.YAHOO, |
|||
symbol: 'AAPL', |
|||
symbolOfDataProvider: 'AAPL' |
|||
}); |
|||
|
|||
expect(symbol).toEqual('AAPL'); |
|||
expect(prismaService.symbolProfile.findMany).not.toHaveBeenCalled(); |
|||
}); |
|||
|
|||
it('Keeps the letter case of the existing asset profile', async () => { |
|||
prismaService.symbolProfile.findMany.mockResolvedValue([ |
|||
{ symbol: 'aapl' } |
|||
]); |
|||
|
|||
const symbol = await symbolProfileService.getSymbolOfAssetProfile({ |
|||
dataSource: DataSource.YAHOO, |
|||
symbol: 'AAPL', |
|||
symbolOfDataProvider: 'AAPL' |
|||
}); |
|||
|
|||
expect(symbol).toEqual('aapl'); |
|||
}); |
|||
|
|||
it('Prefers the asset profile with the same letter case', async () => { |
|||
prismaService.symbolProfile.findMany.mockResolvedValue([ |
|||
{ symbol: 'AAPL' }, |
|||
{ symbol: 'aapl' } |
|||
]); |
|||
|
|||
const symbol = await symbolProfileService.getSymbolOfAssetProfile({ |
|||
dataSource: DataSource.YAHOO, |
|||
symbol: 'aapl', |
|||
symbolOfDataProvider: 'AAPL' |
|||
}); |
|||
|
|||
expect(symbol).toEqual('aapl'); |
|||
}); |
|||
|
|||
it('Ignores an asset profile which a wildcard of the query matched', async () => { |
|||
prismaService.symbolProfile.findMany.mockResolvedValue([ |
|||
{ symbol: 'AAPL' } |
|||
]); |
|||
|
|||
const symbol = await symbolProfileService.getSymbolOfAssetProfile({ |
|||
dataSource: DataSource.YAHOO, |
|||
symbol: 'aa_l', |
|||
symbolOfDataProvider: 'AA_L' |
|||
}); |
|||
|
|||
expect(symbol).toEqual('AA_L'); |
|||
}); |
|||
|
|||
it('Uses the symbol of the data provider if no asset profile exists', async () => { |
|||
const symbol = await symbolProfileService.getSymbolOfAssetProfile({ |
|||
dataSource: DataSource.YAHOO, |
|||
symbol: 'aapl', |
|||
symbolOfDataProvider: 'AAPL' |
|||
}); |
|||
|
|||
expect(symbol).toEqual('AAPL'); |
|||
}); |
|||
|
|||
it('Keeps the requested symbol if the data provider reports no symbol', async () => { |
|||
const symbol = await symbolProfileService.getSymbolOfAssetProfile({ |
|||
dataSource: DataSource.YAHOO, |
|||
symbol: 'aapl' |
|||
}); |
|||
|
|||
expect(symbol).toEqual('aapl'); |
|||
}); |
|||
|
|||
it('Keeps the symbol of a custom asset profile', async () => { |
|||
const symbol = await symbolProfileService.getSymbolOfAssetProfile({ |
|||
dataSource: DataSource.MANUAL, |
|||
symbol: 'GF_apple', |
|||
symbolOfDataProvider: 'GF_APPLE' |
|||
}); |
|||
|
|||
expect(symbol).toEqual('GF_apple'); |
|||
expect(prismaService.symbolProfile.findMany).not.toHaveBeenCalled(); |
|||
expect(prismaService.symbolProfile.findUnique).not.toHaveBeenCalled(); |
|||
}); |
|||
}); |
|||
}); |
|||
Loading…
Reference in new issue