Browse Source

Bugfix/stale benchmarks in markets overview (#8330)

* Fix stale benchmarks in markets overview

* Update changelog
main
Thomas Kaul 2 hours ago
committed by GitHub
parent
commit
45e9da4637
No known key found for this signature in database GPG Key ID: B5690EEEBB952194
  1. 1
      CHANGELOG.md
  2. 101
      apps/api/src/services/benchmark/benchmark.service.spec.ts
  3. 2
      apps/api/src/services/benchmark/benchmark.service.ts

1
CHANGELOG.md

@ -20,6 +20,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Fixed
- Fixed the double submission of forms in the account detail dialog, the create or update access dialog, the historical market data editor, the import activities dialog, the coupon system and the create asset profile dialog of the admin control panel
- Fixed an issue where the benchmarks in the markets overview were not updated after adding or deleting a benchmark
- Fixed an issue where users could grant a private access to themselves
- Fixed the status code of the endpoints to add and delete a benchmark if the asset profile does not exist
- Fixed the git-hook via `husky` to lint and check the formatting of the uncommitted changes before a commit

101
apps/api/src/services/benchmark/benchmark.service.spec.ts

@ -1,10 +1,33 @@
import { PrismaService } from '@ghostfolio/api/services/prisma/prisma.service';
import { PropertyService } from '@ghostfolio/api/services/property/property.service';
import { RedisCacheService } from '@ghostfolio/api/services/redis-cache/redis-cache.service';
import { PROPERTY_BENCHMARKS } from '@ghostfolio/common/config';
import { DataSource } from '@prisma/client';
import { BenchmarkService } from './benchmark.service';
describe('BenchmarkService', () => {
let benchmarkService: BenchmarkService;
let findFirst: jest.Mock;
let getByKey: jest.Mock;
let put: jest.Mock;
let remove: jest.Mock;
beforeEach(() => {
findFirst = jest.fn();
getByKey = jest.fn();
put = jest.fn().mockResolvedValue(undefined);
remove = jest.fn().mockResolvedValue(true);
beforeAll(async () => {
benchmarkService = new BenchmarkService(null, null, null, null, null, null);
benchmarkService = new BenchmarkService(
null,
null,
{ symbolProfile: { findFirst } } as unknown as PrismaService,
{ getByKey, put } as unknown as PropertyService,
{ remove } as unknown as RedisCacheService,
null
);
});
it('calculateChangeInPercentage', async () => {
@ -34,4 +57,78 @@ describe('BenchmarkService', () => {
-0.1
);
});
describe('addBenchmark', () => {
it('stores the benchmark and removes the cached benchmarks', async () => {
findFirst.mockResolvedValue({ id: 'profile-id', name: 'S&P 500' });
getByKey.mockResolvedValue([{ symbolProfileId: 'other-profile-id' }]);
await benchmarkService.addBenchmark({
dataSource: DataSource.YAHOO,
symbol: '^GSPC'
});
expect(put).toHaveBeenCalledWith({
key: PROPERTY_BENCHMARKS,
value: JSON.stringify([
{ symbolProfileId: 'other-profile-id' },
{ symbolProfileId: 'profile-id' }
])
});
expect(remove).toHaveBeenCalledWith('BENCHMARKS');
expect(put.mock.invocationCallOrder[0]).toBeLessThan(
remove.mock.invocationCallOrder[0]
);
});
it('keeps the cached benchmarks if the asset profile is not found', async () => {
findFirst.mockResolvedValue(null);
const result = await benchmarkService.addBenchmark({
dataSource: DataSource.YAHOO,
symbol: '^GSPC'
});
expect(result).toBeNull();
expect(put).not.toHaveBeenCalled();
expect(remove).not.toHaveBeenCalled();
});
});
describe('deleteBenchmark', () => {
it('stores the remaining benchmarks and removes the cached benchmarks', async () => {
findFirst.mockResolvedValue({ id: 'profile-id', name: 'S&P 500' });
getByKey.mockResolvedValue([
{ symbolProfileId: 'other-profile-id' },
{ symbolProfileId: 'profile-id' }
]);
await benchmarkService.deleteBenchmark({
dataSource: DataSource.YAHOO,
symbol: '^GSPC'
});
expect(put).toHaveBeenCalledWith({
key: PROPERTY_BENCHMARKS,
value: JSON.stringify([{ symbolProfileId: 'other-profile-id' }])
});
expect(remove).toHaveBeenCalledWith('BENCHMARKS');
expect(put.mock.invocationCallOrder[0]).toBeLessThan(
remove.mock.invocationCallOrder[0]
);
});
it('keeps the cached benchmarks if the asset profile is not found', async () => {
findFirst.mockResolvedValue(null);
const result = await benchmarkService.deleteBenchmark({
dataSource: DataSource.YAHOO,
symbol: '^GSPC'
});
expect(result).toBeNull();
expect(put).not.toHaveBeenCalled();
expect(remove).not.toHaveBeenCalled();
});
});
});

2
apps/api/src/services/benchmark/benchmark.service.ts

@ -335,5 +335,7 @@ export class BenchmarkService {
key: PROPERTY_BENCHMARKS,
value: JSON.stringify(benchmarks)
});
await this.redisCacheService.remove(this.CACHE_KEY_BENCHMARKS);
}
}

Loading…
Cancel
Save