-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
198 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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], | ||
}), | ||
], | ||
}; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
73 changes: 73 additions & 0 deletions
73
src/common/registry/test/storage/key.storage.transactions.e2e-spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); |
78 changes: 78 additions & 0 deletions
78
src/common/registry/test/storage/operator.storage.transactions.e2e-spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters