Skip to content

Commit

Permalink
Refactor logger instantiation in asset.service.ts and asset-exchange.…
Browse files Browse the repository at this point in the history
…service.ts
  • Loading branch information
danishjoseph committed Apr 21, 2024
1 parent 8e32a7a commit e11bd41
Show file tree
Hide file tree
Showing 7 changed files with 119 additions and 20 deletions.
46 changes: 40 additions & 6 deletions src/asset-management/services/asset-exchange.service.test.ts
Original file line number Diff line number Diff line change
@@ -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<AssetExchangeRepository>;

beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({
Expand All @@ -13,20 +15,52 @@ describe('AssetExchangeService', () => {
{
provide: AssetExchangeRepository,
useValue: {
// Mock any methods you want to test
findOneBy: jest.fn(),
},
},
],
}).compile();

assetExchangeService =
module.get<AssetExchangeService>(AssetExchangeService);
assetExchangeRepository = module.get<AssetExchangeRepository>(
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,
);
});
});
1 change: 0 additions & 1 deletion src/asset-management/services/asset-exchange.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
) {}

Expand Down
68 changes: 65 additions & 3 deletions src/asset-management/services/asset.service.test.ts
Original file line number Diff line number Diff line change
@@ -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({
Expand All @@ -13,17 +16,76 @@ 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(),
},
},
],
}).compile();

assetService = module.get<AssetService>(AssetService);
assetRepository = module.get<AssetRepository>(AssetRepository);
assetExchangeRepository = module.get<AssetExchangeRepository>(
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,
});
});
});
1 change: 0 additions & 1 deletion src/asset-management/services/asset.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
) {}
Expand Down
6 changes: 5 additions & 1 deletion src/asset-management/services/delivery-data.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,11 @@ export class DeliveryDataService {
) {}

async findDeliveryDataById(id: string): Promise<DeliveryData> {
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<DeliveryData> {
Expand Down
6 changes: 3 additions & 3 deletions src/asset-management/services/exchange.service.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ describe('ExchangeService', () => {
const existingExchange = {
id: 1,
abbreviation,
name: 'Existing Exchange',
name: 'National Stock Exchange',
assetExchanges: [],
};

Expand All @@ -53,7 +53,7 @@ describe('ExchangeService', () => {
const newExchange = {
id: 1,
abbreviation,
name: 'New Exchange',
name: 'National Stock Exchange',
assetExchanges: [],
};

Expand All @@ -68,7 +68,7 @@ describe('ExchangeService', () => {
});
expect(exchangeRepository.create).toHaveBeenCalledWith({
abbreviation,
name: 'New Exchange',
name: 'National Stock Exchange',
});
});
});
Expand Down
11 changes: 6 additions & 5 deletions src/asset-management/services/trading-data.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<TradingData> {
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<TradingData> {
Expand Down

0 comments on commit e11bd41

Please sign in to comment.