diff --git a/src/common/registry/test/storage/key.storage.spec.ts b/src/common/registry/test/storage/key.storage.spec.ts index f96e4686..a60dd53a 100644 --- a/src/common/registry/test/storage/key.storage.spec.ts +++ b/src/common/registry/test/storage/key.storage.spec.ts @@ -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(); @@ -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; @@ -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([]); diff --git a/src/common/registry/test/storage/key.storage.transactions.e2e-spec.ts b/src/common/registry/test/storage/key.storage.transactions.e2e-spec.ts index ff12e284..1f6b0b4e 100644 --- a/src/common/registry/test/storage/key.storage.transactions.e2e-spec.ts +++ b/src/common/registry/test/storage/key.storage.transactions.e2e-spec.ts @@ -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); diff --git a/src/common/registry/test/storage/operator.storage.spec.ts b/src/common/registry/test/storage/operator.storage.spec.ts index 5831c626..f4ff4dda 100644 --- a/src/common/registry/test/storage/operator.storage.spec.ts +++ b/src/common/registry/test/storage/operator.storage.spec.ts @@ -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(); @@ -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; @@ -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([]);