Skip to content

Commit

Permalink
[RND-467] Run Integration Tests for Postgres with TestContainers (#234)
Browse files Browse the repository at this point in the history
  • Loading branch information
andonyns authored Apr 18, 2023
1 parent dc1b4b1 commit fb06ff5
Show file tree
Hide file tree
Showing 17 changed files with 87 additions and 95 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import { existsSync } from 'fs';
import dotenv from 'dotenv';
import { CachedEnvironmentConfigProvider, Config } from '@edfi/meadowlark-utilities';
import { join, resolve } from 'path';
import { join } from 'path';

let hasRunAlready = false;

Expand All @@ -15,8 +15,7 @@ export const setupConfigForIntegration = async () => {
return;
}

// Backup to the root directory to find a .env file
const path = resolve(join(__dirname, '..', '..', '..', '..', '.env'));
const path = join(process.cwd(), '.env');

if (!existsSync(path)) {
// eslint-disable-next-line no-console
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
"@types/pg-format": "^1.0.2",
"copyfiles": "^2.4.1",
"dotenv": "^16.0.3",
"rimraf": "^3.0.2"
"rimraf": "^3.0.2",
"testcontainers": "^9.4.0"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ const defaultConfig = require(`${rootDir}/tests/config/jest.config`);
module.exports = {
displayName: 'Integration Tests: Postgresql',
...defaultConfig,
globalSetup: '<rootDir>/backends/meadowlark-postgresql-backend/test/setup/Setup.ts',
globalTeardown: '<rootDir>/backends/meadowlark-postgresql-backend/test/setup/Teardown.ts',
setupFilesAfterEnv: ['<rootDir>/backends/meadowlark-postgresql-backend/test/setup/Global.ts'],
testMatch: ['**/meadowlark-postgresql-backend/test/integration/**/*.(spec|test).[jt]s?(x)'],
coverageThreshold: {
global: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,11 @@

import type { PoolClient, QueryResult } from 'pg';
import { resetSharedClient, getSharedClient } from '../../src/repository/Db';
import { setupConfigForIntegration } from './Config';

describe('Test Connection to Postgres Successful', () => {
let client: PoolClient;

beforeAll(async () => {
await setupConfigForIntegration();

client = await getSharedClient();
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ import { getSharedClient, resetSharedClient } from '../../src/repository/Db';
import { deleteDocumentById } from '../../src/repository/Delete';
import { upsertDocument } from '../../src/repository/Upsert';
import { findDocumentByIdSql } from '../../src/repository/SqlHelper';
import { setupConfigForIntegration } from './Config';

const newUpsertRequest = (): UpsertRequest => ({
meadowlarkId: '' as MeadowlarkId,
Expand Down Expand Up @@ -61,8 +60,6 @@ describe('given the delete of a non-existent document', () => {
const documentUuid = generateDocumentUuid();

beforeAll(async () => {
await setupConfigForIntegration();

client = await getSharedClient();

deleteResult = await deleteDocumentById(
Expand Down Expand Up @@ -98,8 +95,6 @@ describe('given the delete of an existing document', () => {
const meadowlarkId = meadowlarkIdForDocumentIdentity(resourceInfo, documentInfo.documentIdentity);

beforeAll(async () => {
await setupConfigForIntegration();

client = await getSharedClient();
const upsertRequest: UpsertRequest = {
...newUpsertRequest(),
Expand Down Expand Up @@ -173,8 +168,6 @@ describe('given an delete of a document referenced by an existing document with
);

beforeAll(async () => {
await setupConfigForIntegration();

client = (await getSharedClient()) as PoolClient;

// The document that will be referenced
Expand Down Expand Up @@ -261,8 +254,6 @@ describe('given an delete of a document with an outbound reference only, with va
);

beforeAll(async () => {
await setupConfigForIntegration();

client = (await getSharedClient()) as PoolClient;

// The document that will be referenced
Expand Down Expand Up @@ -349,8 +340,6 @@ describe('given an delete of a document referenced by an existing document with
);

beforeAll(async () => {
await setupConfigForIntegration();

client = (await getSharedClient()) as PoolClient;

// The document that will be referenced
Expand Down Expand Up @@ -451,8 +440,6 @@ describe('given the delete of a subclass document referenced by an existing docu
);

beforeAll(async () => {
await setupConfigForIntegration();

client = (await getSharedClient()) as PoolClient;
// The document that will be referenced
await upsertDocument(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ import { deleteAll } from './TestHelper';
import { rejectByOwnershipSecurity } from '../../src/repository/OwnershipSecurity';
import { upsertDocument } from '../../src/repository/Upsert';
import { SecurityResult } from '../../src/security/SecurityResult';
import { setupConfigForIntegration } from './Config';

describe('given the upsert where no document id is specified', () => {
let client: PoolClient;
Expand All @@ -41,8 +40,6 @@ describe('given the upsert where no document id is specified', () => {
};

beforeAll(async () => {
await setupConfigForIntegration();

client = await getSharedClient();

// Act
Expand Down Expand Up @@ -74,8 +71,6 @@ describe('given the getById of a non-existent document', () => {
};

beforeAll(async () => {
await setupConfigForIntegration();

client = await getSharedClient();

// Act
Expand Down Expand Up @@ -132,8 +127,6 @@ describe('given the getById of a document owned by the requestor', () => {
};

beforeAll(async () => {
await setupConfigForIntegration();

client = await getSharedClient();

// Insert owned document
Expand Down Expand Up @@ -192,8 +185,6 @@ describe('given the getById of a document not owned by the requestor', () => {
};

beforeAll(async () => {
await setupConfigForIntegration();

client = await getSharedClient();

// Insert non-owned document
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ import { deleteAll } from './TestHelper';
import { getDocumentById } from '../../src/repository/Get';
import { findDocumentByIdSql } from '../../src/repository/SqlHelper';
import { upsertDocument } from '../../src/repository/Upsert';
import { setupConfigForIntegration } from './Config';

const newGetRequest = (): GetRequest => ({
documentUuid: 'deb6ea15-fa93-4389-89a8-1428fb617490' as DocumentUuid,
Expand Down Expand Up @@ -59,8 +58,6 @@ describe('given the get of a non-existent document', () => {
const meadowlarkId = meadowlarkIdForDocumentIdentity(resourceInfo, documentInfo.documentIdentity);

beforeAll(async () => {
await setupConfigForIntegration();

client = await getSharedClient();

getResult = await getDocumentById(
Expand Down Expand Up @@ -101,8 +98,6 @@ describe('given the get of an existing document', () => {
const meadowlarkId = meadowlarkIdForDocumentIdentity(resourceInfo, documentInfo.documentIdentity);

beforeAll(async () => {
await setupConfigForIntegration();

client = await getSharedClient();
const upsertRequest: UpsertRequest = { ...newUpsertRequest(), meadowlarkId, documentInfo, edfiDoc: { inserted: 'yes' } };

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ import { resetSharedClient, getSharedClient } from '../../src/repository/Db';
import { securityMiddleware } from '../../src/security/SecurityMiddleware';
import { upsertDocument } from '../../src/repository/Upsert';
import { deleteAll } from './TestHelper';
import { setupConfigForIntegration } from './Config';

describe('given the upsert where no document id is specified', () => {
let client: PoolClient;
Expand All @@ -42,8 +41,6 @@ describe('given the upsert where no document id is specified', () => {
};

beforeAll(async () => {
await setupConfigForIntegration();

client = await getSharedClient();

// Act
Expand Down Expand Up @@ -75,8 +72,6 @@ describe('given the getById of a non-existent document', () => {
};

beforeAll(async () => {
await setupConfigForIntegration();

client = await getSharedClient();

// Act
Expand Down Expand Up @@ -134,8 +129,6 @@ describe('given the getById of a document owned by the requestor', () => {
};

beforeAll(async () => {
await setupConfigForIntegration();

client = await getSharedClient();

// Insert owned document
Expand Down Expand Up @@ -194,8 +187,6 @@ describe('given the getById of a document not owned by the requestor', () => {
};

beforeAll(async () => {
await setupConfigForIntegration();

client = await getSharedClient();

// Insert non-owned document
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ import { upsertDocument } from '../../src/repository/Upsert';
import { deleteAll, retrieveReferencesByDocumentIdSql, verifyAliasId } from './TestHelper';
import { getDocumentById } from '../../src/repository/Get';
import { findDocumentByIdSql } from '../../src/repository/SqlHelper';
import { setupConfigForIntegration } from './Config';

const newUpsertRequest = (): UpsertRequest => ({
meadowlarkId: '' as MeadowlarkId,
Expand Down Expand Up @@ -76,8 +75,6 @@ describe('given the update of a non-existent document', () => {
const meadowlarkId = meadowlarkIdForDocumentIdentity(resourceInfo, documentInfo.documentIdentity);

beforeAll(async () => {
await setupConfigForIntegration();

client = await getSharedClient();

updateResult = await updateDocumentById(
Expand Down Expand Up @@ -128,8 +125,6 @@ describe('given the update of an existing document', () => {
const meadowlarkId = meadowlarkIdForDocumentIdentity(resourceInfo, documentInfo.documentIdentity);

beforeAll(async () => {
await setupConfigForIntegration();

client = await getSharedClient();
const upsertRequest: UpsertRequest = {
...newUpsertRequest(),
Expand Down Expand Up @@ -198,8 +193,6 @@ describe('given an update of a document that references a non-existent document
};

beforeAll(async () => {
await setupConfigForIntegration();

client = await getSharedClient();

// Insert the original document with no reference
Expand Down Expand Up @@ -292,8 +285,6 @@ describe('given an update of a document that references an existing document wit
);

beforeAll(async () => {
await setupConfigForIntegration();

client = (await getSharedClient()) as PoolClient;

// The document that will be referenced
Expand Down Expand Up @@ -402,8 +393,6 @@ describe('given an update of a document with one existing and one non-existent r
);

beforeAll(async () => {
await setupConfigForIntegration();

client = (await getSharedClient()) as PoolClient;

// The document that will be referenced
Expand Down Expand Up @@ -524,8 +513,6 @@ describe('given an update of a subclass document referenced by an existing docum
);

beforeAll(async () => {
await setupConfigForIntegration();

client = (await getSharedClient()) as PoolClient;
// The document that will be referenced
await upsertDocument(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ import type { PoolClient } from 'pg';
import { getSharedClient, resetSharedClient } from '../../src/repository/Db';
import { findDocumentByIdSql, findAliasIdsForDocumentSql } from '../../src/repository/SqlHelper';
import { upsertDocument } from '../../src/repository/Upsert';
import { setupConfigForIntegration } from './Config';
import { deleteAll, retrieveReferencesByDocumentIdSql, verifyAliasId } from './TestHelper';

const newUpsertRequest = (): UpsertRequest => ({
Expand Down Expand Up @@ -53,8 +52,6 @@ describe('given the upsert of a new document', () => {
const meadowlarkId = meadowlarkIdForDocumentIdentity(resourceInfo, documentInfo.documentIdentity);

beforeAll(async () => {
await setupConfigForIntegration();

client = await getSharedClient();

upsertResult = await upsertDocument(
Expand Down Expand Up @@ -104,8 +101,6 @@ describe('given the upsert of an existing document twice', () => {
const meadowlarkId = meadowlarkIdForDocumentIdentity(resourceInfo, documentInfo.documentIdentity);

beforeAll(async () => {
await setupConfigForIntegration();

client = await getSharedClient();
const upsertRequest: UpsertRequest = {
...newUpsertRequest(),
Expand Down Expand Up @@ -153,8 +148,6 @@ describe('given an upsert of an existing document that changes the edfiDoc', ()
const meadowlarkId = meadowlarkIdForDocumentIdentity(resourceInfo, documentInfo.documentIdentity);

beforeAll(async () => {
await setupConfigForIntegration();

const upsertRequest: UpsertRequest = { ...newUpsertRequest(), meadowlarkId, resourceInfo, documentInfo };

client = await getSharedClient();
Expand Down Expand Up @@ -203,8 +196,6 @@ describe('given an upsert of a new document that references a non-existent docum
documentWithReferencesInfo.documentReferences = [invalidReference];

beforeAll(async () => {
await setupConfigForIntegration();

client = await getSharedClient();

// The new document with an invalid reference
Expand Down Expand Up @@ -275,8 +266,6 @@ describe('given an upsert of a new document that references an existing document
);

beforeAll(async () => {
await setupConfigForIntegration();

client = (await getSharedClient()) as PoolClient;

// The document that will be referenced
Expand Down Expand Up @@ -365,8 +354,6 @@ describe('given an upsert of a new document with one existing and one non-existe
);

beforeAll(async () => {
await setupConfigForIntegration();

client = (await getSharedClient()) as PoolClient;

// The document that will be referenced
Expand Down Expand Up @@ -466,8 +453,6 @@ describe('given an upsert of a subclass document when a different subclass has t
);

beforeAll(async () => {
await setupConfigForIntegration();

client = (await getSharedClient()) as PoolClient;

// The existing subclass
Expand Down Expand Up @@ -541,8 +526,6 @@ describe('given an update of a document that references a non-existent document
};

beforeAll(async () => {
await setupConfigForIntegration();

client = (await getSharedClient()) as PoolClient;

// Insert the original document with no reference
Expand Down Expand Up @@ -633,8 +616,6 @@ describe('given an update of a document that references an existing document wit
);

beforeAll(async () => {
await setupConfigForIntegration();

client = (await getSharedClient()) as PoolClient;

// The document that will be referenced
Expand Down Expand Up @@ -744,8 +725,6 @@ describe('given an update of a document with one existing and one non-existent r
);

beforeAll(async () => {
await setupConfigForIntegration();

client = (await getSharedClient()) as PoolClient;

// The document that will be referenced
Expand Down Expand Up @@ -868,8 +847,6 @@ describe('given an update of a subclass document referenced by an existing docum
);

beforeAll(async () => {
await setupConfigForIntegration();

client = (await getSharedClient()) as PoolClient;

// The document that will be referenced
Expand Down
Loading

0 comments on commit fb06ff5

Please sign in to comment.