Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: enable database persistance across test suites #505

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions packages/server-core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@
"zod": "^3.23.7"
},
"devDependencies": {
"@faker-js/faker": "^8.4.1",
"@types/busboy": "^1.5.3",
"@types/cors": "^2.8.17",
"@types/module-alias": "^2.0.4",
Expand Down
2 changes: 2 additions & 0 deletions packages/server-core/test/jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@

module.exports = {
testSequencer: './test/utils/test-sequencer.js',
globalSetup: './test/utils/setup.js',
globalTeardown: './test/utils/teardown.js',
testEnvironment: 'node',
transform: {
'^.+\\.tsx?$': 'ts-jest',
Expand Down
12 changes: 3 additions & 9 deletions packages/server-core/test/unit/http/analysis-bucket-file.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,9 @@
import type { AnalysisBucketFile, AnalysisNode } from '@privateaim/core-kit';
import { AnalysisBucketType } from '@privateaim/core-kit';
import {
dropTestDatabase,
expectPropertiesEqualToSrc,
removeDateProperties,
useSuperTest,
useTestDatabase,
initDataSource,
removeDateProperties, useSuperTest,
} from '../../utils';
import {
createSuperTestAnalysis,
Expand All @@ -24,11 +22,7 @@ describe('controllers/analysis-bucket-file', () => {
const superTest = useSuperTest();

beforeAll(async () => {
await useTestDatabase();
});

afterAll(async () => {
await dropTestDatabase();
await initDataSource();
});

let details : AnalysisNode;
Expand Down
14 changes: 4 additions & 10 deletions packages/server-core/test/unit/http/analysis-bucket.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,9 @@
import type { AnalysisBucket, AnalysisNode } from '@privateaim/core-kit';
import { AnalysisBucketType } from '@privateaim/core-kit';
import {
dropTestDatabase,
expectPropertiesEqualToSrc,
removeDateProperties,
useSuperTest,
useTestDatabase,
initDataSource,
removeDateProperties, useSuperTest,
} from '../../utils';
import {
createSuperTestAnalysis,
Expand All @@ -23,11 +21,7 @@ describe('controllers/analysis-file', () => {
const superTest = useSuperTest();

beforeAll(async () => {
await useTestDatabase();
});

afterAll(async () => {
await dropTestDatabase();
await initDataSource();
});

let details : AnalysisNode;
Expand Down Expand Up @@ -63,7 +57,7 @@ describe('controllers/analysis-file', () => {
expect(response.status).toEqual(200);
expect(response.body).toBeDefined();
expect(response.body.data).toBeDefined();
expect(response.body.data.length).toEqual(1);
expect(response.body.data.length).toBeGreaterThanOrEqual(1);
});

it('should read resource', async () => {
Expand Down
8 changes: 2 additions & 6 deletions packages/server-core/test/unit/http/analysis-node.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

import type { AnalysisNode } from '@privateaim/core-kit';
import {
dropTestDatabase, expectPropertiesEqualToSrc, removeDateProperties, useSuperTest, useTestDatabase,
expectPropertiesEqualToSrc, initDataSource, removeDateProperties, useSuperTest,
} from '../../utils';
import {
createSuperTestAnalysis,
Expand All @@ -20,11 +20,7 @@ describe('src/controllers/core/analysis-node', () => {
const superTest = useSuperTest();

beforeAll(async () => {
await useTestDatabase();
});

afterAll(async () => {
await dropTestDatabase();
await initDataSource();
});

let details : AnalysisNode;
Expand Down
17 changes: 8 additions & 9 deletions packages/server-core/test/unit/http/analysis.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,23 +8,22 @@
import type { Analysis } from '@privateaim/core-kit';
import { buildHTTPValidationErrorMessage } from '@privateaim/server-http-kit';
import {
dropTestDatabase, expectPropertiesEqualToSrc, removeDateProperties, useSuperTest, useTestDatabase,
expectPropertiesEqualToSrc,
initDataSource,
removeDateProperties,
useSuperTest,
} from '../../utils';
import { TEST_DEFAULT_ANALYSIS, createSuperTestAnalysis, createSuperTestProject } from '../../utils/domains';

describe('src/controllers/core/analysis', () => {
const superTest = useSuperTest();

beforeAll(async () => {
await useTestDatabase();
});
let details : Analysis;

afterAll(async () => {
await dropTestDatabase();
beforeAll(async () => {
await initDataSource();
});

let details : Analysis;

it('should create resource', async () => {
const proposal = await createSuperTestProject(superTest);
const response = await createSuperTestAnalysis(superTest, {
Expand All @@ -47,7 +46,7 @@ describe('src/controllers/core/analysis', () => {
expect(response.status).toEqual(200);
expect(response.body).toBeDefined();
expect(response.body.data).toBeDefined();
expect(response.body.data.length).toEqual(1);
expect(response.body.data.length).toBeGreaterThanOrEqual(1);
});

it('should read resource', async () => {
Expand Down
8 changes: 2 additions & 6 deletions packages/server-core/test/unit/http/master-image.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,13 @@

import type { MasterImage, MasterImageGroup } from '@privateaim/core-kit';
import { MasterImageCommand } from '@privateaim/core-kit';
import { dropTestDatabase, useSuperTest, useTestDatabase } from '../../utils';
import { initDataSource, useSuperTest } from '../../utils';

describe('src/controllers/core/master-image', () => {
const superTest = useSuperTest();

beforeAll(async () => {
await useTestDatabase();
});

afterAll(async () => {
await dropTestDatabase();
await initDataSource();
});

let masterImage : MasterImage;
Expand Down
10 changes: 3 additions & 7 deletions packages/server-core/test/unit/http/node.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,15 @@ import type {
Node,
} from '@privateaim/core-kit';
import {
dropTestDatabase, expectPropertiesEqualToSrc, removeDateProperties, useSuperTest, useTestDatabase,
expectPropertiesEqualToSrc, initDataSource, removeDateProperties, useSuperTest,
} from '../../utils';
import { createSuperTestNode } from '../../utils/domains';

describe('src/controllers/core/node', () => {
const superTest = useSuperTest();

beforeAll(async () => {
await useTestDatabase();
});

afterAll(async () => {
await dropTestDatabase();
await initDataSource();
});

let details: Node;
Expand All @@ -43,7 +39,7 @@ describe('src/controllers/core/node', () => {
expect(response.status).toEqual(200);
expect(response.body).toBeDefined();
expect(response.body.data).toBeDefined();
expect(response.body.data.length).toEqual(1);
expect(response.body.data.length).toBeGreaterThanOrEqual(1);
});

it('should read resource', async () => {
Expand Down
10 changes: 3 additions & 7 deletions packages/server-core/test/unit/http/project-node.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,15 @@

import type { ProjectNode } from '@privateaim/core-kit';
import {
dropTestDatabase, expectPropertiesEqualToSrc, useSuperTest, useTestDatabase,
expectPropertiesEqualToSrc, initDataSource, useSuperTest,
} from '../../utils';
import { createSuperTestNode, createSuperTestProject } from '../../utils/domains';

describe('src/controllers/core/project-node', () => {
const superTest = useSuperTest();

beforeAll(async () => {
await useTestDatabase();
});

afterAll(async () => {
await dropTestDatabase();
await initDataSource();
});

let details : ProjectNode;
Expand Down Expand Up @@ -50,7 +46,7 @@ describe('src/controllers/core/project-node', () => {
expect(response.status).toEqual(200);
expect(response.body).toBeDefined();
expect(response.body.data).toBeDefined();
expect(response.body.data.length).toEqual(1);
expect(response.body.data.length).toBeGreaterThanOrEqual(1);
});

it('should read resource', async () => {
Expand Down
10 changes: 3 additions & 7 deletions packages/server-core/test/unit/http/project.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,15 @@

import type { Project } from '@privateaim/core-kit';
import {
dropTestDatabase, expectPropertiesEqualToSrc, removeDateProperties, useSuperTest, useTestDatabase,
expectPropertiesEqualToSrc, initDataSource, removeDateProperties, useSuperTest,
} from '../../utils';
import { createSuperTestProject } from '../../utils/domains';

describe('src/controllers/core/project', () => {
const superTest = useSuperTest();

beforeAll(async () => {
await useTestDatabase();
});

afterAll(async () => {
await dropTestDatabase();
await initDataSource();
});

let details : Project;
Expand All @@ -42,7 +38,7 @@ describe('src/controllers/core/project', () => {
expect(response.status).toEqual(200);
expect(response.body).toBeDefined();
expect(response.body.data).toBeDefined();
expect(response.body.data.length).toEqual(1);
expect(response.body.data.length).toBeGreaterThanOrEqual(1);
});

it('should read resource', async () => {
Expand Down
27 changes: 27 additions & 0 deletions packages/server-core/test/utils/data-source.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/*
* Copyright (c) 2024.
* Author Peter Placzek (tada5hi)
* For the full copyright and license information,
* view the LICENSE file that was distributed with this source code.
*/

import path from 'node:path';
import { DataSource } from 'typeorm';
import { setDataSource } from 'typeorm-extension';
import { extendDataSourceOptions } from '../../src/database';

export async function initDataSource() {
const options = await extendDataSourceOptions({
type: 'better-sqlite3',
database: path.join(process.cwd(), 'writable', 'test.sql'),
});

Object.assign(options, {
subscribers: [],
});

const dataSource = new DataSource(options);
await dataSource.initialize();

setDataSource(dataSource);
}
47 changes: 0 additions & 47 deletions packages/server-core/test/utils/database.ts

This file was deleted.

3 changes: 2 additions & 1 deletion packages/server-core/test/utils/domains/analysis.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,12 @@
* view the LICENSE file that was distributed with this source code.
*/

import { faker } from '@faker-js/faker';
import type { Analysis } from '@privateaim/core-kit';
import type { TestAgent } from '../supertest';

export const TEST_DEFAULT_ANALYSIS : Partial<Analysis> = {
name: 'development',
name: faker.lorem.slug(),
};

export async function createSuperTestAnalysis(superTest: TestAgent, entity?: Partial<Analysis>) {
Expand Down
1 change: 0 additions & 1 deletion packages/server-core/test/utils/domains/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
* view the LICENSE file that was distributed with this source code.
*/

export * from './master-image';
export * from './project';
export * from './project-node';
export * from './node';
Expand Down
26 changes: 0 additions & 26 deletions packages/server-core/test/utils/domains/master-image.ts

This file was deleted.

Loading
Loading