mirror of https://github.com/ghostfolio/ghostfolio
6 changed files with 326 additions and 7 deletions
@ -0,0 +1,83 @@ |
|||
/** |
|||
* @jest-environment <rootDir>/jest-environment-tz.js |
|||
* @jest-environment-options {"timeZone": "Europe/Zurich"} |
|||
*/ |
|||
import { MarketDataService } from '@ghostfolio/api/services/market-data/market-data.service'; |
|||
import { SymbolProfileService } from '@ghostfolio/api/services/symbol-profile/symbol-profile.service'; |
|||
import { permissions } from '@ghostfolio/common/permissions'; |
|||
import type { RequestWithUser } from '@ghostfolio/common/types'; |
|||
|
|||
import { MarketDataController } from './market-data.controller'; |
|||
|
|||
describe('MarketDataController in a time zone ahead of UTC', () => { |
|||
let marketDataController: MarketDataController; |
|||
let updateMany: jest.Mock; |
|||
|
|||
beforeEach(() => { |
|||
updateMany = jest.fn().mockResolvedValue([]); |
|||
|
|||
marketDataController = new MarketDataController( |
|||
{ updateMany } as unknown as MarketDataService, |
|||
{ |
|||
user: { |
|||
permissions: [ |
|||
permissions.createMarketData, |
|||
permissions.updateMarketData |
|||
] |
|||
} |
|||
} as unknown as RequestWithUser, |
|||
{ |
|||
getSymbolProfiles: jest.fn().mockResolvedValue([{}]) |
|||
} as unknown as SymbolProfileService, |
|||
null |
|||
); |
|||
}); |
|||
|
|||
describe('updateMarketData', () => { |
|||
const updateMarketData = (dates: string[]) => { |
|||
return marketDataController.updateMarketData( |
|||
{ |
|||
marketData: dates.map((date) => { |
|||
return { date, marketPrice: 100 }; |
|||
}) |
|||
}, |
|||
'MANUAL', |
|||
'GF_TEST' |
|||
); |
|||
}; |
|||
|
|||
it.each([ |
|||
['a date', '2026-11-08'], |
|||
['a timestamp at midnight (UTC)', '2026-11-08T00:00:00.000Z'], |
|||
['a timestamp during the day', '2026-11-08T11:13:54.146Z'] |
|||
])('stores %s at midnight (UTC)', async (_, date) => { |
|||
await updateMarketData([date]); |
|||
|
|||
expect(updateMany.mock.calls[0][0].data).toEqual([ |
|||
{ |
|||
dataSource: 'MANUAL', |
|||
date: new Date('2026-11-08T00:00:00.000Z'), |
|||
marketPrice: 100, |
|||
state: 'CLOSE', |
|||
symbol: 'GF_TEST' |
|||
} |
|||
]); |
|||
}); |
|||
|
|||
it('deletes the item at local midnight of the same date', async () => { |
|||
await updateMarketData(['2026-11-08']); |
|||
|
|||
expect(updateMany.mock.calls[0][0].whereToDelete).toEqual({ |
|||
dataSource: 'MANUAL', |
|||
date: { in: [new Date('2026-11-07T23:00:00.000Z')] }, |
|||
symbol: 'GF_TEST' |
|||
}); |
|||
}); |
|||
|
|||
it('does not delete an item at midnight (UTC)', async () => { |
|||
await updateMarketData(['2026-11-08T00:00:00.000Z']); |
|||
|
|||
expect(updateMany.mock.calls[0][0]).not.toHaveProperty('whereToDelete'); |
|||
}); |
|||
}); |
|||
}); |
|||
@ -0,0 +1,83 @@ |
|||
/** |
|||
* @jest-environment <rootDir>/jest-environment-tz.js |
|||
* @jest-environment-options {"timeZone": "America/Toronto"} |
|||
*/ |
|||
import { MarketDataService } from '@ghostfolio/api/services/market-data/market-data.service'; |
|||
import { SymbolProfileService } from '@ghostfolio/api/services/symbol-profile/symbol-profile.service'; |
|||
import { permissions } from '@ghostfolio/common/permissions'; |
|||
import type { RequestWithUser } from '@ghostfolio/common/types'; |
|||
|
|||
import { MarketDataController } from './market-data.controller'; |
|||
|
|||
describe('MarketDataController in a time zone behind UTC', () => { |
|||
let marketDataController: MarketDataController; |
|||
let updateMany: jest.Mock; |
|||
|
|||
beforeEach(() => { |
|||
updateMany = jest.fn().mockResolvedValue([]); |
|||
|
|||
marketDataController = new MarketDataController( |
|||
{ updateMany } as unknown as MarketDataService, |
|||
{ |
|||
user: { |
|||
permissions: [ |
|||
permissions.createMarketData, |
|||
permissions.updateMarketData |
|||
] |
|||
} |
|||
} as unknown as RequestWithUser, |
|||
{ |
|||
getSymbolProfiles: jest.fn().mockResolvedValue([{}]) |
|||
} as unknown as SymbolProfileService, |
|||
null |
|||
); |
|||
}); |
|||
|
|||
describe('updateMarketData', () => { |
|||
const updateMarketData = (dates: string[]) => { |
|||
return marketDataController.updateMarketData( |
|||
{ |
|||
marketData: dates.map((date) => { |
|||
return { date, marketPrice: 100 }; |
|||
}) |
|||
}, |
|||
'MANUAL', |
|||
'GF_TEST' |
|||
); |
|||
}; |
|||
|
|||
it.each([ |
|||
['a date', '2026-11-08'], |
|||
['a timestamp at midnight (UTC)', '2026-11-08T00:00:00.000Z'], |
|||
['a timestamp during the day', '2026-11-08T11:13:54.146Z'] |
|||
])('stores %s at midnight (UTC)', async (_, date) => { |
|||
await updateMarketData([date]); |
|||
|
|||
expect(updateMany.mock.calls[0][0].data).toEqual([ |
|||
{ |
|||
dataSource: 'MANUAL', |
|||
date: new Date('2026-11-08T00:00:00.000Z'), |
|||
marketPrice: 100, |
|||
state: 'CLOSE', |
|||
symbol: 'GF_TEST' |
|||
} |
|||
]); |
|||
}); |
|||
|
|||
it('deletes the item at local midnight of the same date', async () => { |
|||
await updateMarketData(['2026-11-08']); |
|||
|
|||
expect(updateMany.mock.calls[0][0].whereToDelete).toEqual({ |
|||
dataSource: 'MANUAL', |
|||
date: { in: [new Date('2026-11-08T05:00:00.000Z')] }, |
|||
symbol: 'GF_TEST' |
|||
}); |
|||
}); |
|||
|
|||
it('does not delete an item at midnight (UTC)', async () => { |
|||
await updateMarketData(['2026-11-08T00:00:00.000Z']); |
|||
|
|||
expect(updateMany.mock.calls[0][0]).not.toHaveProperty('whereToDelete'); |
|||
}); |
|||
}); |
|||
}); |
|||
@ -0,0 +1,69 @@ |
|||
import { MarketDataService } from '@ghostfolio/api/services/market-data/market-data.service'; |
|||
import { SymbolProfileService } from '@ghostfolio/api/services/symbol-profile/symbol-profile.service'; |
|||
import { permissions } from '@ghostfolio/common/permissions'; |
|||
import type { RequestWithUser } from '@ghostfolio/common/types'; |
|||
|
|||
import { MarketDataController } from './market-data.controller'; |
|||
|
|||
describe('MarketDataController', () => { |
|||
let marketDataController: MarketDataController; |
|||
let updateMany: jest.Mock; |
|||
|
|||
beforeEach(() => { |
|||
updateMany = jest.fn().mockResolvedValue([]); |
|||
|
|||
marketDataController = new MarketDataController( |
|||
{ updateMany } as unknown as MarketDataService, |
|||
{ |
|||
user: { |
|||
permissions: [ |
|||
permissions.createMarketData, |
|||
permissions.updateMarketData |
|||
] |
|||
} |
|||
} as unknown as RequestWithUser, |
|||
{ |
|||
getSymbolProfiles: jest.fn().mockResolvedValue([{}]) |
|||
} as unknown as SymbolProfileService, |
|||
null |
|||
); |
|||
}); |
|||
|
|||
describe('updateMarketData', () => { |
|||
const updateMarketData = (dates: string[]) => { |
|||
return marketDataController.updateMarketData( |
|||
{ |
|||
marketData: dates.map((date) => { |
|||
return { date, marketPrice: 100 }; |
|||
}) |
|||
}, |
|||
'MANUAL', |
|||
'GF_TEST' |
|||
); |
|||
}; |
|||
|
|||
it.each([ |
|||
['a date', '2026-11-08'], |
|||
['a timestamp at midnight (UTC)', '2026-11-08T00:00:00.000Z'], |
|||
['a timestamp during the day', '2026-11-08T11:13:54.146Z'] |
|||
])('stores %s at midnight (UTC)', async (_, date) => { |
|||
await updateMarketData([date]); |
|||
|
|||
expect(updateMany.mock.calls[0][0].data).toEqual([ |
|||
{ |
|||
dataSource: 'MANUAL', |
|||
date: new Date('2026-11-08T00:00:00.000Z'), |
|||
marketPrice: 100, |
|||
state: 'CLOSE', |
|||
symbol: 'GF_TEST' |
|||
} |
|||
]); |
|||
}); |
|||
|
|||
it('does not delete any item', async () => { |
|||
await updateMarketData(['2026-11-08']); |
|||
|
|||
expect(updateMany.mock.calls[0][0]).not.toHaveProperty('whereToDelete'); |
|||
}); |
|||
}); |
|||
}); |
|||
Loading…
Reference in new issue