Skip to content

Commit

Permalink
fix: unit test for streams
Browse files Browse the repository at this point in the history
  • Loading branch information
Amuhar committed Jan 29, 2024
1 parent 30cbc7c commit 7901f94
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 62 deletions.
61 changes: 30 additions & 31 deletions src/common/registry/test/storage/key.storage.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,27 @@ import { key } from '../fixtures/key.fixture';
import { RegistryKeyStorageService, RegistryKey, RegistryKeyRepository } from '../../';
import { REGISTRY_CONTRACT_ADDRESSES } from '@lido-nestjs/contracts';
import * as streamUtils from '../../utils/stream.utils';
// import { STREAM_KEYS_TIMEOUT_MESSAGE, STREAM_TIMEOUT } from '../../../registry/storage/constants';
import { STREAM_KEYS_TIMEOUT_MESSAGE, STREAM_TIMEOUT } from '../../../registry/storage/constants';

describe('Keys', () => {
const CHAIN_ID = process.env.CHAIN_ID || 1;
const address = REGISTRY_CONTRACT_ADDRESSES[CHAIN_ID];
const registryKey = { index: 1, operatorIndex: 1, moduleAddress: address, ...key };

// async function* findKeysAsStream() {
// yield registryKey;
// }
async function* findKeysAsStream() {
yield registryKey;
}

// const mockedKnex = {
// select: jest.fn().mockReturnThis(),
// from: jest.fn().mockReturnThis(),
// where: jest.fn().mockReturnThis(),
// orderBy: jest.fn().mockReturnThis(),
// stream: jest.fn().mockReturnValue(findKeysAsStream()),
// };
const streamValue = jest.fn().mockReturnValue(findKeysAsStream());

const mockedCreateQueryBuilder = {
select: jest.fn().mockReturnThis(),
where: jest.fn().mockReturnThis(),
orderBy: jest.fn().mockReturnThis(),
getKnexQuery: jest.fn().mockReturnValue({
stream: streamValue,
}),
};

const addTimeoutToStream = jest.spyOn(streamUtils, 'addTimeoutToStream').mockReturnValue();

Expand All @@ -46,8 +49,7 @@ describe('Keys', () => {
nativeDelete: jest.fn().mockImplementation(() => {
return 1;
}),
// getKnex: jest.fn().mockReturnValue(mockedKnex),
// createQueryBuilder:
createQueryBuilder: jest.fn().mockReturnValue(mockedCreateQueryBuilder),
};

let storageService: RegistryKeyStorageService;
Expand All @@ -72,24 +74,21 @@ describe('Keys', () => {
expect(mockRegistryKeyRepository.find).toBeCalledWith({ used: true }, { limit: 1 });
});

// test('findAsStream', async () => {
// const stream = storageService.findAsStream({ used: true });
// const actualResult: RegistryKey[] = [];
// for await (const item of stream) {
// actualResult.push(item);
// }
// expect(actualResult).toEqual([registryKey]);
// expect(mockRegistryKeyRepository.getKnex).toBeCalledTimes(1);
// expect(mockedKnex.select).toBeCalledWith('*');
// expect(mockedKnex.from).toBeCalledWith('registry_key');
// expect(mockedKnex.where).toBeCalledWith({ used: true });
// expect(mockedKnex.orderBy).toBeCalledWith([
// { column: 'operatorIndex', order: 'asc' },
// { column: 'index', order: 'asc' },
// ]);
// expect(mockedKnex.stream).toBeCalledTimes(1);
// expect(addTimeoutToStream).toBeCalledWith(stream, STREAM_TIMEOUT, STREAM_KEYS_TIMEOUT_MESSAGE);
// });
test('findAsStream', async () => {
const stream = storageService.findAsStream({ used: true });
const actualResult: RegistryKey[] = [];
for await (const item of stream) {
actualResult.push(item);
}
expect(actualResult).toEqual([registryKey]);
expect(mockRegistryKeyRepository.createQueryBuilder).toBeCalledTimes(1);
expect(mockedCreateQueryBuilder.select).toBeCalledWith('*');
expect(mockedCreateQueryBuilder.where).toBeCalledWith({ used: true });
expect(mockedCreateQueryBuilder.orderBy).toBeCalledWith({ index: 'asc', operator_index: 'asc' });
expect(mockedCreateQueryBuilder.getKnexQuery).toBeCalledTimes(1);
expect(streamValue).toBeCalledTimes(1);
expect(addTimeoutToStream).toBeCalledWith(stream, STREAM_TIMEOUT, STREAM_KEYS_TIMEOUT_MESSAGE);
});

test('findAll', async () => {
await expect(storageService.findAll(address)).resolves.toEqual([]);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ describe('check that findAsStream method dont create a new connection', () => {
it('should return list of keys', async () => {
const keys = [
{ operatorIndex: 1, index: 1, moduleAddress: address, ...key },
{ operatorIndex: 1, index: 2, moduleAddress: address, ...key },
{ operatorIndex: 2, index: 2, moduleAddress: address, ...key },
];
await keyStorageService.save(keys);

Expand Down
60 changes: 30 additions & 30 deletions src/common/registry/test/storage/operator.storage.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,27 @@ import { operator } from '../fixtures/operator.fixture';
import { RegistryOperatorStorageService, RegistryOperator, RegistryOperatorRepository } from '../../';
import { REGISTRY_CONTRACT_ADDRESSES } from '@lido-nestjs/contracts';
import * as streamUtils from '../../utils/stream.utils';
// import { STREAM_OPERATORS_TIMEOUT_MESSAGE, STREAM_TIMEOUT } from '../../../registry/storage/constants';
import { STREAM_OPERATORS_TIMEOUT_MESSAGE, STREAM_TIMEOUT } from '../../../registry/storage/constants';

describe('Operators', () => {
const CHAIN_ID = process.env.CHAIN_ID || 1;
const address = REGISTRY_CONTRACT_ADDRESSES[CHAIN_ID];
const registryOperator = { index: 1, moduleAddress: address, ...operator };

// async function* findKeysAsStream() {
// yield registryOperator;
// }
async function* findOperatorsAsStream() {
yield registryOperator;
}

// const mockedKnex = {
// select: jest.fn().mockReturnThis(),
// from: jest.fn().mockReturnThis(),
// where: jest.fn().mockReturnThis(),
// orderBy: jest.fn().mockReturnThis(),
// stream: jest.fn().mockReturnValue(findKeysAsStream()),
// };
const streamValue = jest.fn().mockReturnValue(findOperatorsAsStream());

const mockedCreateQueryBuilder = {
select: jest.fn().mockReturnThis(),
where: jest.fn().mockReturnThis(),
orderBy: jest.fn().mockReturnThis(),
getKnexQuery: jest.fn().mockReturnValue({
stream: streamValue,
}),
};

const addTimeoutToStream = jest.spyOn(streamUtils, 'addTimeoutToStream').mockReturnValue();

Expand All @@ -46,7 +49,7 @@ describe('Operators', () => {
nativeDelete: jest.fn().mockImplementation(() => {
return 1;
}),
// getKnex: jest.fn().mockReturnValue(mockedKnex),
createQueryBuilder: jest.fn().mockReturnValue(mockedCreateQueryBuilder),
};

let storageService: RegistryOperatorStorageService;
Expand Down Expand Up @@ -77,24 +80,21 @@ describe('Operators', () => {
expect(mockRegistryOperatorRepository.find).toBeCalledWith({ active: true }, { limit: 1 });
});

// test('findAsStream', async () => {
// const stream = storageService.findAsStream({ active: true });
// const actualResult: RegistryOperator[] = [];
// for await (const item of stream) {
// actualResult.push(item);
// }
// expect(actualResult).toEqual([registryOperator]);
// expect(mockRegistryOperatorRepository.getKnex).toBeCalledTimes(1);
// expect(mockedKnex.select).toBeCalledWith('*');
// expect(mockedKnex.from).toBeCalledWith('registry_operator');
// expect(mockedKnex.where).toBeCalledWith({ active: true });
// expect(mockedKnex.orderBy).toBeCalledWith([
// { column: 'moduleAddress', order: 'asc' },
// { column: 'index', order: 'asc' },
// ]);
// expect(mockedKnex.stream).toBeCalledTimes(1);
// expect(addTimeoutToStream).toBeCalledWith(stream, STREAM_TIMEOUT, STREAM_OPERATORS_TIMEOUT_MESSAGE);
// });
test('findAsStream', async () => {
const stream = storageService.findAsStream({ active: true });
const actualResult: RegistryOperator[] = [];
for await (const item of stream) {
actualResult.push(item);
}
expect(actualResult).toEqual([registryOperator]);
expect(mockRegistryOperatorRepository.createQueryBuilder).toBeCalledTimes(1);
expect(mockedCreateQueryBuilder.select).toBeCalledWith('*');
expect(mockedCreateQueryBuilder.where).toBeCalledWith({ active: true });
expect(mockedCreateQueryBuilder.orderBy).toBeCalledWith({ index: 'asc', module_address: 'asc' });
expect(mockedCreateQueryBuilder.getKnexQuery).toBeCalledTimes(1);
expect(streamValue).toBeCalledTimes(1);
expect(addTimeoutToStream).toBeCalledWith(stream, STREAM_TIMEOUT, STREAM_OPERATORS_TIMEOUT_MESSAGE);
});

test('findAll', async () => {
await expect(storageService.findAll(address)).resolves.toEqual([]);
Expand Down

0 comments on commit 7901f94

Please sign in to comment.