Skip to content

Commit

Permalink
fix: stream tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Amuhar committed Jan 29, 2024
1 parent ffd9815 commit 3837445
Show file tree
Hide file tree
Showing 10 changed files with 198 additions and 33 deletions.
56 changes: 31 additions & 25 deletions src/app/database-testing.module.ts
Original file line number Diff line number Diff line change
@@ -1,29 +1,35 @@
import { Module } from '@nestjs/common';
import { DynamicModule, Module } from '@nestjs/common';
import { MikroOrmModule } from '@mikro-orm/nestjs';
import config from 'mikro-orm.config';
import { ConfigModule, ConfigService } from 'common/config';

@Module({
imports: [
ConfigModule,
MikroOrmModule.forRootAsync({
async useFactory(configService: ConfigService) {
return {
...config,
dbName: configService.get('DB_NAME'),
host: configService.get('DB_HOST'),
port: configService.get('DB_PORT'),
user: configService.get('DB_USER'),
password: configService.get('DB_PASSWORD'),
autoLoadEntities: false,
cache: { enabled: false },
debug: false,
registerRequestContext: true,
allowGlobalContext: true,
};
},
inject: [ConfigService],
}),
],
})
export class DatabaseTestingModule {}
@Module({})
export class DatabaseTestingModule {
static forRoot(mikroOrmConfigOverrides: Partial<typeof config> = {}): DynamicModule {
return {
module: DatabaseTestingModule,
imports: [
ConfigModule,
MikroOrmModule.forRootAsync({
async useFactory(configService: ConfigService) {
return {
...config,
...mikroOrmConfigOverrides,
dbName: configService.get('DB_NAME'),
host: configService.get('DB_HOST'),
port: configService.get('DB_PORT'),
user: configService.get('DB_USER'),
password: configService.get('DB_PASSWORD'),
autoLoadEntities: false,
cache: { enabled: false },
debug: false,
registerRequestContext: true,
allowGlobalContext: true,
};
},
inject: [ConfigService],
}),
],
};
}
}
12 changes: 10 additions & 2 deletions src/common/registry/test/fixtures/key.fixture.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,16 @@
import { hexZeroPad } from '@ethersproject/bytes';

/**
* keys 96 = 48byte
* signatures 192 = 96byte
* https://github.com/lidofinance/lido-dao/blob/539a0faf33807d04444047d0905dce2b45260dfa/contracts/0.4.24/lib/SigningKeys.sol#L213-L238
*/
export const KEYS_LENGTH_IN_BYTES = 48;
export const SIGNATURE_LENGTH_IN_BYTES = 96;

export const key = {
key: hexZeroPad('0x12', 98),
depositSignature: hexZeroPad('0x12', 194),
key: hexZeroPad('0x12', KEYS_LENGTH_IN_BYTES),
depositSignature: hexZeroPad('0x12', SIGNATURE_LENGTH_IN_BYTES),
used: true,
};

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
import { Test } from '@nestjs/testing';
import { IsolationLevel, MikroORM } from '@mikro-orm/core';
import { key } from '../fixtures/key.fixture';
import { RegistryStorageModule, RegistryStorageService, RegistryKeyStorageService, RegistryKey } from '../..';
import { REGISTRY_CONTRACT_ADDRESSES } from '@lido-nestjs/contracts';
import * as dotenv from 'dotenv';
import { DatabaseTestingModule } from 'app';
import { EntityManager } from '@mikro-orm/knex';

dotenv.config();

describe('check that findAsStream method dont create a new connection', () => {
let keyStorageService: RegistryKeyStorageService;
let registryService: RegistryStorageService;
let entityManager: EntityManager;

if (!process.env.CHAIN_ID) {
console.error("CHAIN_ID wasn't provides");
process.exit(1);
}
const address = REGISTRY_CONTRACT_ADDRESSES[process.env.CHAIN_ID];

beforeEach(async () => {
const imports = [DatabaseTestingModule.forRoot({ pool: { min: 1, max: 1 } }), RegistryStorageModule.forFeature()];

const moduleRef = await Test.createTestingModule({ imports }).compile();
keyStorageService = moduleRef.get(RegistryKeyStorageService);
registryService = moduleRef.get(RegistryStorageService);
entityManager = moduleRef.get(EntityManager);

const generator = moduleRef.get(MikroORM).getSchemaGenerator();
await generator.refreshDatabase();
await generator.clearDatabase();
});

afterEach(async () => {
await registryService.onModuleDestroy();
});

it('should return list of keys', async () => {
const keys = [
{ operatorIndex: 1, index: 1, moduleAddress: address, ...key },
{ operatorIndex: 1, index: 2, moduleAddress: address, ...key },
];
await keyStorageService.save(keys);

const where = {};

const fields = [
'index',
'operator_index as operatorIndex',
'key',
'deposit_signature as depositSignature',
'used',
'module_address as moduleAddress',
];

await entityManager.transactional(
async () => {
const stream = keyStorageService.findAsStream(where, fields);

const result: RegistryKey[] = [];
for await (const key of stream) {
result.push(key);
}

expect(result.length).toEqual(keys.length);
expect(result).toEqual(expect.arrayContaining(keys));
},
{ isolationLevel: IsolationLevel.REPEATABLE_READ },
);
}, 30000);
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
import { Test } from '@nestjs/testing';
import { IsolationLevel, MikroORM } from '@mikro-orm/core';
import { operator } from '../fixtures/operator.fixture';
import { RegistryStorageModule, RegistryStorageService, RegistryOperatorStorageService, RegistryOperator } from '../..';
import { REGISTRY_CONTRACT_ADDRESSES } from '@lido-nestjs/contracts';
import * as dotenv from 'dotenv';
import { DatabaseTestingModule } from 'app';
import { EntityManager } from '@mikro-orm/knex';

dotenv.config();

describe('check that findAsStream method dont create a new connection', () => {
let operatorStorageService: RegistryOperatorStorageService;
let registryService: RegistryStorageService;
let entityManager: EntityManager;

if (!process.env.CHAIN_ID) {
console.error("CHAIN_ID wasn't provides");
process.exit(1);
}
const address = REGISTRY_CONTRACT_ADDRESSES[process.env.CHAIN_ID];

beforeEach(async () => {
const imports = [DatabaseTestingModule.forRoot({ pool: { min: 1, max: 1 } }), RegistryStorageModule.forFeature()];

const moduleRef = await Test.createTestingModule({ imports }).compile();
operatorStorageService = moduleRef.get(RegistryOperatorStorageService);
registryService = moduleRef.get(RegistryStorageService);
entityManager = moduleRef.get(EntityManager);

const generator = moduleRef.get(MikroORM).getSchemaGenerator();
await generator.refreshDatabase();
await generator.clearDatabase();
});

afterEach(async () => {
await registryService.onModuleDestroy();
});

it('should return list of operators', async () => {
const operators = [
{ index: 1, moduleAddress: address, ...operator },
{ index: 2, moduleAddress: address, ...operator },
];

await operatorStorageService.save(operators);

const where = {};

const fields = [
'index',
'active',
'name',
'finalized_used_signing_keys as finalizedUsedSigningKeys',
'reward_address as rewardAddress',
'staking_limit as stakingLimit',
'stopped_validators as stoppedValidators',
'total_signing_keys as totalSigningKeys',
'used_signing_keys as usedSigningKeys',
'module_address as moduleAddress',
];

await entityManager.transactional(
async () => {
const stream = operatorStorageService.findAsStream(where, fields);

const result: RegistryOperator[] = [];
for await (const key of stream) {
result.push(key);
}

expect(result.length).toEqual(operators.length);
expect(result).toEqual(expect.arrayContaining(operators));
},
{ isolationLevel: IsolationLevel.REPEATABLE_READ },
);
}, 30000);
});
2 changes: 1 addition & 1 deletion src/http/keys/keys.e2e-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ describe('KeyController (e2e)', () => {

beforeAll(async () => {
const imports = [
DatabaseTestingModule,
DatabaseTestingModule.forRoot(),
LoggerModule.forRoot({ transports: [nullTransport()] }),
KeyRegistryModule,
StakingRouterModule,
Expand Down
2 changes: 1 addition & 1 deletion src/http/sr-modules-keys/sr-modules-keys.e2e-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ describe('SRModulesKeysController (e2e)', () => {

beforeAll(async () => {
const imports = [
DatabaseTestingModule,
DatabaseTestingModule.forRoot(),
LoggerModule.forRoot({ transports: [nullTransport()] }),
KeyRegistryModule,
StakingRouterModule,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ describe('SRModulesOperatorsKeysController (e2e)', () => {

beforeAll(async () => {
const imports = [
DatabaseTestingModule,
DatabaseTestingModule.forRoot(),
LoggerModule.forRoot({ transports: [nullTransport()] }),
KeyRegistryModule,
StakingRouterModule,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ describe('SRModuleOperatorsController (e2e)', () => {

beforeAll(async () => {
const imports = [
DatabaseTestingModule,
DatabaseTestingModule.forRoot(),
LoggerModule.forRoot({ transports: [nullTransport()] }),
KeyRegistryModule,
StakingRouterModule,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ describe('SRModulesValidatorsController (e2e)', () => {

beforeAll(async () => {
const imports = [
DatabaseTestingModule,
DatabaseTestingModule.forRoot(),
LoggerModule.forRoot({ transports: [nullTransport()] }),
KeyRegistryModule,
StakingRouterModule,
Expand Down
2 changes: 1 addition & 1 deletion src/http/sr-modules/sr-modules.e2e-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ describe('SRModulesController (e2e)', () => {

beforeAll(async () => {
const imports = [
DatabaseTestingModule,
DatabaseTestingModule.forRoot(),
LoggerModule.forRoot({ transports: [nullTransport()] }),
KeyRegistryModule,
StakingRouterModule,
Expand Down

0 comments on commit 3837445

Please sign in to comment.