From e11bd41dedc57b021bd231131f555f26ab6c0ce5 Mon Sep 17 00:00:00 2001 From: Danish Joseph Date: Mon, 22 Apr 2024 00:44:35 +0200 Subject: [PATCH] Refactor logger instantiation in asset.service.ts and asset-exchange.service.ts --- .../services/asset-exchange.service.test.ts | 46 +++++++++++-- .../services/asset-exchange.service.ts | 1 - .../services/asset.service.test.ts | 68 ++++++++++++++++++- .../services/asset.service.ts | 1 - .../services/delivery-data.service.ts | 6 +- .../services/exchange.service.test.ts | 6 +- .../services/trading-data.service.ts | 11 +-- 7 files changed, 119 insertions(+), 20 deletions(-) diff --git a/src/asset-management/services/asset-exchange.service.test.ts b/src/asset-management/services/asset-exchange.service.test.ts index 981cbb6..fe308fd 100644 --- a/src/asset-management/services/asset-exchange.service.test.ts +++ b/src/asset-management/services/asset-exchange.service.test.ts @@ -1,10 +1,12 @@ import { Test, TestingModule } from '@nestjs/testing'; import { AssetExchangeRepository } from '../repositories'; import { AssetExchangeService } from './asset-exchange.service'; +import { Exchange } from '../types/enums'; +import { AssetExchange } from '../entities'; describe('AssetExchangeService', () => { let assetExchangeService: AssetExchangeService; - let assetExchangeRepository: AssetExchangeRepository; + let assetExchangeRepository: jest.Mocked; beforeEach(async () => { const module: TestingModule = await Test.createTestingModule({ @@ -13,7 +15,7 @@ describe('AssetExchangeService', () => { { provide: AssetExchangeRepository, useValue: { - // Mock any methods you want to test + findOneBy: jest.fn(), }, }, ], @@ -21,12 +23,44 @@ describe('AssetExchangeService', () => { assetExchangeService = module.get(AssetExchangeService); - assetExchangeRepository = module.get( - AssetExchangeRepository, - ); + assetExchangeRepository = module.get(AssetExchangeRepository); expect(assetExchangeService).toBeDefined(); expect(assetExchangeRepository).toBeDefined(); }); - // Add more test cases for other methods in the AssetExchangeService class + it('should find by symbol with NSE exchange', async () => { + const symbol = 'test-symbol'; + const exchange = { + abbreviation: Exchange.NSE, + name: 'National Stock Exchange', + }; + const relations = ['relation1', 'relation2']; + const expectedData = { + id: 1, + symbol, + exchange, + asset: jest.fn(), + tradingData: jest.fn(), + deliveryData: jest.fn(), + }; + + assetExchangeRepository.findOneBy.mockResolvedValue( + expectedData as unknown as AssetExchange, + ); + + const result = await assetExchangeService.findBySymbol( + symbol, + exchange, + relations, + ); + + expect(result).toEqual(expectedData); + expect(assetExchangeRepository.findOneBy).toHaveBeenCalledWith( + { + asset: { symbol }, + exchange, + }, + relations, + ); + }); }); diff --git a/src/asset-management/services/asset-exchange.service.ts b/src/asset-management/services/asset-exchange.service.ts index 0e852fb..66fa539 100644 --- a/src/asset-management/services/asset-exchange.service.ts +++ b/src/asset-management/services/asset-exchange.service.ts @@ -6,7 +6,6 @@ import { Exchange as Ex } from '../types/enums'; @Injectable() export class AssetExchangeService { constructor( - private readonly logger: Logger = new Logger(AssetExchangeService.name), private readonly assetExchangeRepository: AssetExchangeRepository, ) {} diff --git a/src/asset-management/services/asset.service.test.ts b/src/asset-management/services/asset.service.test.ts index 166339f..51b5c50 100644 --- a/src/asset-management/services/asset.service.test.ts +++ b/src/asset-management/services/asset.service.test.ts @@ -1,10 +1,13 @@ import { Test, TestingModule } from '@nestjs/testing'; import { AssetService } from './asset.service'; -import { AssetRepository } from '../repositories'; +import { AssetRepository, AssetExchangeRepository } from '../repositories'; +import { AssetDto } from '../dto'; +import { Exchange, Asset, AssetExchange } from '../entities'; describe('AssetService', () => { let assetService: AssetService; let assetRepository: AssetRepository; + let assetExchangeRepository: AssetExchangeRepository; beforeEach(async () => { const module: TestingModule = await Test.createTestingModule({ @@ -13,7 +16,15 @@ describe('AssetService', () => { { provide: AssetRepository, useValue: { - // Mock any methods you want to test + findOneBy: jest.fn(), + create: jest.fn(), + }, + }, + { + provide: AssetExchangeRepository, + useValue: { + findOneBy: jest.fn(), + create: jest.fn(), }, }, ], @@ -21,9 +32,60 @@ describe('AssetService', () => { assetService = module.get(AssetService); assetRepository = module.get(AssetRepository); + assetExchangeRepository = module.get( + AssetExchangeRepository, + ); expect(assetService).toBeDefined(); expect(assetRepository).toBeDefined(); }); - // Add more test cases for other methods in the AssetService class + it('should create asset and asset exchange', async () => { + const assetData: AssetDto = { + isin: 'test-isin', + name: 'Test Asset', + symbol: 'TST', + assetExchangeCode: 'TST123', + faceValue: 0, + industry: '', + sector: '', + assetExchanges: [], + }; + const exchange = { + id: 1, + name: 'Test Exchange', + abbreviation: 'TST', + } as Exchange; + const asset: Asset = { id: 1, ...assetData }; + const assetExchange: AssetExchange = { + id: 1, + asset, + exchange, + tradingData: [], + deliveryData: [], + }; + + assetRepository.findOneBy = jest.fn().mockResolvedValueOnce(null); + assetExchangeRepository.findOneBy = jest.fn().mockResolvedValueOnce(null); + assetRepository.create = jest + .fn() + .mockResolvedValueOnce({ id: 1, ...assetData }); + assetExchangeRepository.create = jest + .fn() + .mockResolvedValueOnce(assetExchange); + + await assetService.createAsset(assetData, exchange); + + expect(assetRepository.findOneBy).toHaveBeenCalledWith({ + isin: assetData.isin, + }); + expect(assetExchangeRepository.findOneBy).toHaveBeenCalledWith({ + asset: { isin: assetData.isin }, + exchange, + }); + expect(assetRepository.create).toHaveBeenCalledWith(assetData); + expect(assetExchangeRepository.create).toHaveBeenCalledWith({ + asset, + exchange, + }); + }); }); diff --git a/src/asset-management/services/asset.service.ts b/src/asset-management/services/asset.service.ts index 1d5ebd7..3de69fa 100644 --- a/src/asset-management/services/asset.service.ts +++ b/src/asset-management/services/asset.service.ts @@ -7,7 +7,6 @@ import { AssetDto } from '../dto'; @Injectable() export class AssetService { constructor( - private readonly logger: Logger = new Logger(AssetService.name), private readonly assetRepository: AssetRepository, private assetExchangeRepository: AssetExchangeRepository, ) {} diff --git a/src/asset-management/services/delivery-data.service.ts b/src/asset-management/services/delivery-data.service.ts index 5d98fd1..7398c58 100644 --- a/src/asset-management/services/delivery-data.service.ts +++ b/src/asset-management/services/delivery-data.service.ts @@ -11,7 +11,11 @@ export class DeliveryDataService { ) {} async findDeliveryDataById(id: string): Promise { - return this.deliveryDataRepository.findById(id); + const deliveryData = await this.deliveryDataRepository.findById(id); + if (!deliveryData) { + throw new Error(`Delivery data with id ${id} not found`); + } + return deliveryData; } async saveDeliveryData(deliveryData: DeliveryDataDTO): Promise { diff --git a/src/asset-management/services/exchange.service.test.ts b/src/asset-management/services/exchange.service.test.ts index 0dbee1d..7e623fa 100644 --- a/src/asset-management/services/exchange.service.test.ts +++ b/src/asset-management/services/exchange.service.test.ts @@ -31,7 +31,7 @@ describe('ExchangeService', () => { const existingExchange = { id: 1, abbreviation, - name: 'Existing Exchange', + name: 'National Stock Exchange', assetExchanges: [], }; @@ -53,7 +53,7 @@ describe('ExchangeService', () => { const newExchange = { id: 1, abbreviation, - name: 'New Exchange', + name: 'National Stock Exchange', assetExchanges: [], }; @@ -68,7 +68,7 @@ describe('ExchangeService', () => { }); expect(exchangeRepository.create).toHaveBeenCalledWith({ abbreviation, - name: 'New Exchange', + name: 'National Stock Exchange', }); }); }); diff --git a/src/asset-management/services/trading-data.service.ts b/src/asset-management/services/trading-data.service.ts index b19b153..46665d9 100644 --- a/src/asset-management/services/trading-data.service.ts +++ b/src/asset-management/services/trading-data.service.ts @@ -6,13 +6,14 @@ import { validateAndThrowError } from '../utils/validate-dto-error'; @Injectable() export class TradingDataService { - constructor( - private readonly logger: Logger = new Logger(TradingDataService.name), - private readonly tradingDataRepository: TradingDataRepository, - ) {} + constructor(private readonly tradingDataRepository: TradingDataRepository) {} async findTradingDataById(id: string): Promise { - return this.tradingDataRepository.findById(id); + const tradingData = await this.tradingDataRepository.findById(id); + if (!tradingData) { + throw new Error(`Trading data with id ${id} not found`); + } + return tradingData; } async saveTradingData(tradingData: TradingDataDTO): Promise {