Skip to content

Commit

Permalink
tear down with common clearDatabase
Browse files Browse the repository at this point in the history
  • Loading branch information
alexeh committed Oct 22, 2024
1 parent c5ac25d commit 23bd4d5
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 68 deletions.
53 changes: 0 additions & 53 deletions api/test/utils/db-helpers.ts

This file was deleted.

2 changes: 1 addition & 1 deletion api/test/utils/test-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@ import { Type } from '@nestjs/common/interfaces';
import * as request from 'supertest';

import { getDataSourceToken } from '@nestjs/typeorm';
import { clearTestDataFromDatabase } from './db-helpers';
import { User } from '@shared/entities/users/user.entity';
import { IEmailServiceToken } from '@api/modules/notifications/email/email-service.interface';
import { MockEmailService } from './mocks/mock-email.service';
import { ROLES } from '@shared/entities/users/roles.enum';
import { createUser } from '@shared/lib/entity-mocks';
import { clearTestDataFromDatabase } from '@shared/lib/db-helpers';

/**
* @description: Abstraction for NestJS testing workflow. For now its a basic implementation to create a test app, but can be extended to encapsulate
Expand Down
27 changes: 13 additions & 14 deletions shared/lib/db-helpers.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { DataSource, EntityMetadata } from 'typeorm';
import { difference } from 'lodash';
import { DataSource, EntityMetadata } from "typeorm";
import { difference } from "lodash";

export async function clearTestDataFromDatabase(
dataSource: DataSource,
Expand All @@ -11,8 +11,8 @@ export async function clearTestDataFromDatabase(
const entityTableNames: string[] = dataSource.entityMetadatas
.filter(
(entityMetadata: EntityMetadata) =>
entityMetadata.tableType === 'regular' ||
entityMetadata.tableType === 'junction',
entityMetadata.tableType === "regular" ||
entityMetadata.tableType === "junction",
)
.map((entityMetadata: EntityMetadata) => entityMetadata.tableName);

Expand All @@ -24,24 +24,23 @@ export async function clearTestDataFromDatabase(

entityTableNames.push(dataSource.metadataTableName);
entityTableNames.push(
dataSource.options.migrationsTableName || 'migrations',
dataSource.options.migrationsTableName || "migrations",
);
entityTableNames.push('spatial_ref_sys');
entityTableNames.push("spatial_ref_sys");

const databaseTableNames: string[] = (
await dataSource.query(
`SELECT table_name FROM information_schema.tables WHERE table_schema='public' AND table_type='BASE TABLE'`,
)
).map((e: Record<string, any>) => e.table_name);

// todo: Alex to take a look later
// const tablesToDrop = difference(databaseTableNames, entityTableNames);
//
// await Promise.all(
// tablesToDrop.map((tableToDrop: string) =>
// queryRunner.dropTable(tableToDrop),
// ),
// );
const tablesToDrop = difference(databaseTableNames, entityTableNames);

await Promise.all(
tablesToDrop.map((tableToDrop: string) =>
queryRunner.dropTable(tableToDrop),
),
);
await queryRunner.commitTransaction();
} catch (err) {
// rollback changes before throwing error
Expand Down

0 comments on commit 23bd4d5

Please sign in to comment.