Skip to content

Commit

Permalink
Init PostgresDatabaseModule on startup (#2009)
Browse files Browse the repository at this point in the history
Init PostgresDatabaseModule on startup
  • Loading branch information
hectorgomezv authored Oct 16, 2024
1 parent 36d6cfa commit 609894e
Show file tree
Hide file tree
Showing 34 changed files with 151 additions and 5 deletions.
2 changes: 2 additions & 0 deletions src/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ import { DelegatesV2Module } from '@/routes/delegates/v2/delegates.v2.module';
import { AccountsModule } from '@/routes/accounts/accounts.module';
import { NotificationsModuleV2 } from '@/routes/notifications/v2/notifications.module';
import { TargetedMessagingModule } from '@/routes/targeted-messaging/targeted-messaging.module';
import { PostgresDatabaseModule } from '@/datasources/db/postgres-database.module';

@Module({})
export class AppModule implements NestModule {
Expand All @@ -71,6 +72,7 @@ export class AppModule implements NestModule {
return {
module: AppModule,
imports: [
PostgresDatabaseModule,
// features
AboutModule,
...(isAccountsFeatureEnabled ? [AccountsModule] : []),
Expand Down
16 changes: 16 additions & 0 deletions src/datasources/db/__tests__/test.postgres-database.module.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { Module } from '@nestjs/common';

/**
* The {@link TestPostgresDatabaseModule} should be used whenever you want to
* override the values provided by the {@link PostgresDatabase}.
*
* This will create a TestModule which uses the implementation of PostgresDatabase but
* overrides the real DB_INSTANCE with a null value.
*
*/
@Module({
providers: [{ provide: 'DB_INSTANCE', useFactory: (): null => null }],
exports: [],
imports: [],
})
export class TestPostgresDatabaseModule {}
14 changes: 9 additions & 5 deletions src/routes/about/__tests__/get-about.e2e-spec.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import type { INestApplication } from '@nestjs/common';
import { Test } from '@nestjs/testing';
import request from 'supertest';
import { AppModule } from '@/app.module';
import { expect } from '@jest/globals';
import '@/__tests__/matchers/to-be-string-or-null';
import { AppModule } from '@/app.module';
import { CacheKeyPrefix } from '@/datasources/cache/constants';
import { TestPostgresDatabaseModule } from '@/datasources/db/__tests__/test.postgres-database.module';
import { PostgresDatabaseModule } from '@/datasources/db/postgres-database.module';
import { expect } from '@jest/globals';
import type { INestApplication } from '@nestjs/common';
import { Test } from '@nestjs/testing';
import type { Server } from 'net';
import request from 'supertest';

describe('Get about e2e test', () => {
let app: INestApplication<Server>;
Expand All @@ -15,6 +17,8 @@ describe('Get about e2e test', () => {
const moduleRef = await Test.createTestingModule({
imports: [AppModule.register()],
})
.overrideModule(PostgresDatabaseModule)
.useModule(TestPostgresDatabaseModule)
.overrideProvider(CacheKeyPrefix)
.useValue(cacheKeyPrefix)
.compile();
Expand Down
4 changes: 4 additions & 0 deletions src/routes/accounts/accounts.controller.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import { TestCounterfactualSafesDataSourceModule } from '@/datasources/accounts/
import { CounterfactualSafesDatasourceModule } from '@/datasources/accounts/counterfactual-safes/counterfactual-safes.datasource.module';
import { TestCacheModule } from '@/datasources/cache/__tests__/test.cache.module';
import { CacheModule } from '@/datasources/cache/cache.module';
import { TestPostgresDatabaseModule } from '@/datasources/db/__tests__/test.postgres-database.module';
import { PostgresDatabaseModule } from '@/datasources/db/postgres-database.module';
import jwtConfiguration from '@/datasources/jwt/configuration/__tests__/jwt.configuration';
import {
JWT_CONFIGURATION_MODULE,
Expand Down Expand Up @@ -62,6 +64,8 @@ describe('AccountsController', () => {
const moduleFixture: TestingModule = await Test.createTestingModule({
imports: [AppModule.register(testConfiguration)],
})
.overrideModule(PostgresDatabaseModule)
.useModule(TestPostgresDatabaseModule)
.overrideModule(JWT_CONFIGURATION_MODULE)
.useModule(JwtConfigurationModule.register(jwtConfiguration))
.overrideModule(AccountsDatasourceModule)
Expand Down
4 changes: 4 additions & 0 deletions src/routes/alerts/alerts.controller.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ import {
import { TestQueuesApiModule } from '@/datasources/queues/__tests__/test.queues-api.module';
import { QueuesApiModule } from '@/datasources/queues/queues-api.module';
import type { Server } from 'net';
import { TestPostgresDatabaseModule } from '@/datasources/db/__tests__/test.postgres-database.module';
import { PostgresDatabaseModule } from '@/datasources/db/postgres-database.module';

// The `x-tenderly-signature` header contains a cryptographic signature. The webhook request signature is
// a HMAC SHA256 hash of concatenated signing secret, request payload, and timestamp, in this order.
Expand Down Expand Up @@ -102,6 +104,8 @@ describe('Alerts (Unit)', () => {
const moduleFixture: TestingModule = await Test.createTestingModule({
imports: [AppModule.register(testConfiguration)],
})
.overrideModule(PostgresDatabaseModule)
.useModule(TestPostgresDatabaseModule)
.overrideModule(JWT_CONFIGURATION_MODULE)
.useModule(JwtConfigurationModule.register(jwtConfiguration))
.overrideModule(ALERTS_CONFIGURATION_MODULE)
Expand Down
4 changes: 4 additions & 0 deletions src/routes/auth/auth.controller.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ import {
IBlockchainApiManager,
} from '@/domain/interfaces/blockchain-api.manager.interface';
import { TestBlockchainApiManagerModule } from '@/datasources/blockchain/__tests__/test.blockchain-api.manager';
import { TestPostgresDatabaseModule } from '@/datasources/db/__tests__/test.postgres-database.module';
import { PostgresDatabaseModule } from '@/datasources/db/postgres-database.module';

const verifySiweMessageMock = jest.fn();

Expand All @@ -49,6 +51,8 @@ describe('AuthController', () => {
const moduleFixture: TestingModule = await Test.createTestingModule({
imports: [AppModule.register(config)],
})
.overrideModule(PostgresDatabaseModule)
.useModule(TestPostgresDatabaseModule)
.overrideModule(JWT_CONFIGURATION_MODULE)
.useModule(JwtConfigurationModule.register(jwtConfiguration))
.overrideModule(BlockchainApiManagerModule)
Expand Down
4 changes: 4 additions & 0 deletions src/routes/balances/balances.controller.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import { IConfigurationService } from '@/config/configuration.service.interface'
import configuration from '@/config/entities/__tests__/configuration';
import { TestCacheModule } from '@/datasources/cache/__tests__/test.cache.module';
import { CacheModule } from '@/datasources/cache/cache.module';
import { TestPostgresDatabaseModule } from '@/datasources/db/__tests__/test.postgres-database.module';
import { PostgresDatabaseModule } from '@/datasources/db/postgres-database.module';
import { TestNetworkModule } from '@/datasources/network/__tests__/test.network.module';
import { NetworkResponseError } from '@/datasources/network/entities/network.error.entity';
import { NetworkModule } from '@/datasources/network/network.module';
Expand Down Expand Up @@ -48,6 +50,8 @@ describe('Balances Controller (Unit)', () => {
const moduleFixture: TestingModule = await Test.createTestingModule({
imports: [AppModule.register(testConfiguration)],
})
.overrideModule(PostgresDatabaseModule)
.useModule(TestPostgresDatabaseModule)
.overrideModule(CacheModule)
.useModule(TestCacheModule)
.overrideModule(RequestScopedLoggingModule)
Expand Down
4 changes: 4 additions & 0 deletions src/routes/chains/chains.controller.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ import {
} from '@/domain/interfaces/blockchain-api.manager.interface';
import { TestBlockchainApiManagerModule } from '@/datasources/blockchain/__tests__/test.blockchain-api.manager';
import type { FakeBlockchainApiManager } from '@/datasources/blockchain/__tests__/fake.blockchain-api.manager';
import { TestPostgresDatabaseModule } from '@/datasources/db/__tests__/test.postgres-database.module';
import { PostgresDatabaseModule } from '@/datasources/db/postgres-database.module';

const mockGetBlock = jest.fn();
describe('Chains Controller (Unit)', () => {
Expand Down Expand Up @@ -65,6 +67,8 @@ describe('Chains Controller (Unit)', () => {
const moduleFixture: TestingModule = await Test.createTestingModule({
imports: [AppModule.register(configuration)],
})
.overrideModule(PostgresDatabaseModule)
.useModule(TestPostgresDatabaseModule)
.overrideModule(CacheModule)
.useModule(TestCacheModule)
.overrideModule(RequestScopedLoggingModule)
Expand Down
4 changes: 4 additions & 0 deletions src/routes/collectibles/collectibles.controller.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ import { QueuesApiModule } from '@/datasources/queues/queues-api.module';
import type { Server } from 'net';
import { getAddress } from 'viem';
import { safeBuilder } from '@/domain/safe/entities/__tests__/safe.builder';
import { TestPostgresDatabaseModule } from '@/datasources/db/__tests__/test.postgres-database.module';
import { PostgresDatabaseModule } from '@/datasources/db/postgres-database.module';

describe('Collectibles Controller (Unit)', () => {
let app: INestApplication<Server>;
Expand All @@ -53,6 +55,8 @@ describe('Collectibles Controller (Unit)', () => {
const moduleFixture: TestingModule = await Test.createTestingModule({
imports: [AppModule.register(testConfiguration)],
})
.overrideModule(PostgresDatabaseModule)
.useModule(TestPostgresDatabaseModule)
.overrideModule(CacheModule)
.useModule(TestCacheModule)
.overrideModule(RequestScopedLoggingModule)
Expand Down
4 changes: 4 additions & 0 deletions src/routes/community/community.controller.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ import {
campaignActivityBuilder,
toJson as campaignActivityToJson,
} from '@/domain/community/entities/__tests__/campaign-activity.builder';
import { TestPostgresDatabaseModule } from '@/datasources/db/__tests__/test.postgres-database.module';
import { PostgresDatabaseModule } from '@/datasources/db/postgres-database.module';

describe('Community (Unit)', () => {
let app: INestApplication<Server>;
Expand All @@ -53,6 +55,8 @@ describe('Community (Unit)', () => {
const moduleFixture: TestingModule = await Test.createTestingModule({
imports: [AppModule.register(configuration)],
})
.overrideModule(PostgresDatabaseModule)
.useModule(TestPostgresDatabaseModule)
.overrideModule(CacheModule)
.useModule(TestCacheModule)
.overrideModule(RequestScopedLoggingModule)
Expand Down
4 changes: 4 additions & 0 deletions src/routes/contracts/contracts.controller.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ import { NetworkResponseError } from '@/datasources/network/entities/network.err
import { TestQueuesApiModule } from '@/datasources/queues/__tests__/test.queues-api.module';
import { QueuesApiModule } from '@/datasources/queues/queues-api.module';
import type { Server } from 'net';
import { TestPostgresDatabaseModule } from '@/datasources/db/__tests__/test.postgres-database.module';
import { PostgresDatabaseModule } from '@/datasources/db/postgres-database.module';

describe('Contracts controller', () => {
let app: INestApplication<Server>;
Expand All @@ -32,6 +34,8 @@ describe('Contracts controller', () => {
const moduleFixture: TestingModule = await Test.createTestingModule({
imports: [AppModule.register(configuration)],
})
.overrideModule(PostgresDatabaseModule)
.useModule(TestPostgresDatabaseModule)
.overrideModule(CacheModule)
.useModule(TestCacheModule)
.overrideModule(RequestScopedLoggingModule)
Expand Down
4 changes: 4 additions & 0 deletions src/routes/delegates/delegates.controller.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ import { getAddress } from 'viem';
import { TestQueuesApiModule } from '@/datasources/queues/__tests__/test.queues-api.module';
import { QueuesApiModule } from '@/datasources/queues/queues-api.module';
import type { Server } from 'net';
import { TestPostgresDatabaseModule } from '@/datasources/db/__tests__/test.postgres-database.module';
import { PostgresDatabaseModule } from '@/datasources/db/postgres-database.module';

describe('Delegates controller', () => {
let app: INestApplication<Server>;
Expand All @@ -39,6 +41,8 @@ describe('Delegates controller', () => {
const moduleFixture: TestingModule = await Test.createTestingModule({
imports: [AppModule.register(configuration)],
})
.overrideModule(PostgresDatabaseModule)
.useModule(TestPostgresDatabaseModule)
.overrideModule(CacheModule)
.useModule(TestCacheModule)
.overrideModule(RequestScopedLoggingModule)
Expand Down
4 changes: 4 additions & 0 deletions src/routes/estimations/estimations.controller.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ import { TestQueuesApiModule } from '@/datasources/queues/__tests__/test.queues-
import { QueuesApiModule } from '@/datasources/queues/queues-api.module';
import type { Server } from 'net';
import { getAddress } from 'viem';
import { TestPostgresDatabaseModule } from '@/datasources/db/__tests__/test.postgres-database.module';
import { PostgresDatabaseModule } from '@/datasources/db/postgres-database.module';

describe('Estimations Controller (Unit)', () => {
let app: INestApplication<Server>;
Expand All @@ -40,6 +42,8 @@ describe('Estimations Controller (Unit)', () => {
const moduleFixture: TestingModule = await Test.createTestingModule({
imports: [AppModule.register(configuration)],
})
.overrideModule(PostgresDatabaseModule)
.useModule(TestPostgresDatabaseModule)
.overrideModule(CacheModule)
.useModule(TestCacheModule)
.overrideModule(RequestScopedLoggingModule)
Expand Down
4 changes: 4 additions & 0 deletions src/routes/health/health.controller.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ import { TestQueuesApiModule } from '@/datasources/queues/__tests__/test.queues-
import type { IQueueReadiness } from '@/domain/interfaces/queue-readiness.interface';
import { QueueReadiness } from '@/domain/interfaces/queue-readiness.interface';
import type { Server } from 'net';
import { TestPostgresDatabaseModule } from '@/datasources/db/__tests__/test.postgres-database.module';
import { PostgresDatabaseModule } from '@/datasources/db/postgres-database.module';

describe('Health Controller tests', () => {
let app: INestApplication<Server>;
Expand All @@ -30,6 +32,8 @@ describe('Health Controller tests', () => {
const moduleFixture: TestingModule = await Test.createTestingModule({
imports: [AppModule.register(configuration)],
})
.overrideModule(PostgresDatabaseModule)
.useModule(TestPostgresDatabaseModule)
.overrideModule(CacheModule)
.useModule(TestCacheModule)
.overrideModule(RequestScopedLoggingModule)
Expand Down
4 changes: 4 additions & 0 deletions src/routes/hooks/hooks.controller.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ import { QueuesApiModule } from '@/datasources/queues/queues-api.module';
import type { Server } from 'net';
import { NotificationsDatasourceModule } from '@/datasources/notifications/notifications.datasource.module';
import { TestNotificationsDatasourceModule } from '@/datasources/notifications/__tests__/test.notifications.datasource.module';
import { TestPostgresDatabaseModule } from '@/datasources/db/__tests__/test.postgres-database.module';
import { PostgresDatabaseModule } from '@/datasources/db/postgres-database.module';

describe('Post Hook Events (Unit)', () => {
let app: INestApplication<Server>;
Expand All @@ -27,6 +29,8 @@ describe('Post Hook Events (Unit)', () => {
const moduleFixture: TestingModule = await Test.createTestingModule({
imports: [AppModule.register(config)],
})
.overrideModule(PostgresDatabaseModule)
.useModule(TestPostgresDatabaseModule)
.overrideModule(CacheModule)
.useModule(TestCacheModule)
.overrideModule(RequestScopedLoggingModule)
Expand Down
4 changes: 4 additions & 0 deletions src/routes/messages/messages.controller.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ import { NetworkResponseError } from '@/datasources/network/entities/network.err
import { TestQueuesApiModule } from '@/datasources/queues/__tests__/test.queues-api.module';
import { QueuesApiModule } from '@/datasources/queues/queues-api.module';
import type { Server } from 'net';
import { TestPostgresDatabaseModule } from '@/datasources/db/__tests__/test.postgres-database.module';
import { PostgresDatabaseModule } from '@/datasources/db/postgres-database.module';

describe('Messages controller', () => {
let app: INestApplication<Server>;
Expand All @@ -44,6 +46,8 @@ describe('Messages controller', () => {
const moduleFixture: TestingModule = await Test.createTestingModule({
imports: [AppModule.register(configuration)],
})
.overrideModule(PostgresDatabaseModule)
.useModule(TestPostgresDatabaseModule)
.overrideModule(CacheModule)
.useModule(TestCacheModule)
.overrideModule(RequestScopedLoggingModule)
Expand Down
4 changes: 4 additions & 0 deletions src/routes/notifications/v1/notifications.controller.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ import { TestQueuesApiModule } from '@/datasources/queues/__tests__/test.queues-
import { QueuesApiModule } from '@/datasources/queues/queues-api.module';
import type { Server } from 'net';
import { getAddress } from 'viem';
import { TestPostgresDatabaseModule } from '@/datasources/db/__tests__/test.postgres-database.module';
import { PostgresDatabaseModule } from '@/datasources/db/postgres-database.module';

describe('Notifications Controller (Unit)', () => {
let app: INestApplication<Server>;
Expand All @@ -36,6 +38,8 @@ describe('Notifications Controller (Unit)', () => {
const moduleFixture: TestingModule = await Test.createTestingModule({
imports: [AppModule.register(configuration)],
})
.overrideModule(PostgresDatabaseModule)
.useModule(TestPostgresDatabaseModule)
.overrideModule(CacheModule)
.useModule(TestCacheModule)
.overrideModule(RequestScopedLoggingModule)
Expand Down
4 changes: 4 additions & 0 deletions src/routes/notifications/v2/notifications.controller.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ import request from 'supertest';
import { getAddress } from 'viem';
import { CounterfactualSafesDatasourceModule } from '@/datasources/accounts/counterfactual-safes/counterfactual-safes.datasource.module';
import { TestCounterfactualSafesDataSourceModule } from '@/datasources/accounts/counterfactual-safes/__tests__/test.counterfactual-safes.datasource.module';
import { TestPostgresDatabaseModule } from '@/datasources/db/__tests__/test.postgres-database.module';
import { PostgresDatabaseModule } from '@/datasources/db/postgres-database.module';

describe('Notifications Controller V2 (Unit)', () => {
let app: INestApplication<Server>;
Expand All @@ -69,6 +71,8 @@ describe('Notifications Controller V2 (Unit)', () => {
const moduleFixture: TestingModule = await Test.createTestingModule({
imports: [AppModule.register(testConfiguration)],
})
.overrideModule(PostgresDatabaseModule)
.useModule(TestPostgresDatabaseModule)
.overrideModule(JWT_CONFIGURATION_MODULE)
.useModule(JwtConfigurationModule.register(jwtConfiguration))
.overrideModule(AccountsDatasourceModule)
Expand Down
4 changes: 4 additions & 0 deletions src/routes/owners/owners.controller.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ import {
import { TestQueuesApiModule } from '@/datasources/queues/__tests__/test.queues-api.module';
import { QueuesApiModule } from '@/datasources/queues/queues-api.module';
import type { Server } from 'net';
import { TestPostgresDatabaseModule } from '@/datasources/db/__tests__/test.postgres-database.module';
import { PostgresDatabaseModule } from '@/datasources/db/postgres-database.module';
import { ChainsRepository } from '@/domain/chains/chains.repository';

describe('Owners Controller (Unit)', () => {
Expand All @@ -38,6 +40,8 @@ describe('Owners Controller (Unit)', () => {
const moduleFixture: TestingModule = await Test.createTestingModule({
imports: [AppModule.register(configuration)],
})
.overrideModule(PostgresDatabaseModule)
.useModule(TestPostgresDatabaseModule)
.overrideModule(CacheModule)
.useModule(TestCacheModule)
.overrideModule(RequestScopedLoggingModule)
Expand Down
4 changes: 4 additions & 0 deletions src/routes/recovery/recovery.controller.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ import type { Server } from 'net';
import { RecoveryController } from '@/routes/recovery/recovery.controller';
import { checkGuardIsApplied } from '@/__tests__/util/check-guard';
import { AuthGuard } from '@/routes/auth/guards/auth.guard';
import { TestPostgresDatabaseModule } from '@/datasources/db/__tests__/test.postgres-database.module';
import { PostgresDatabaseModule } from '@/datasources/db/postgres-database.module';

describe('Recovery (Unit)', () => {
let app: INestApplication<Server>;
Expand All @@ -70,6 +72,8 @@ describe('Recovery (Unit)', () => {
const moduleFixture: TestingModule = await Test.createTestingModule({
imports: [AppModule.register(testConfiguration)],
})
.overrideModule(PostgresDatabaseModule)
.useModule(TestPostgresDatabaseModule)
.overrideModule(JWT_CONFIGURATION_MODULE)
.useModule(JwtConfigurationModule.register(jwtConfiguration))
.overrideModule(ALERTS_CONFIGURATION_MODULE)
Expand Down
Loading

0 comments on commit 609894e

Please sign in to comment.