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

test(backend): Bootstrap integration test #177

Merged
merged 4 commits into from
Mar 7, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
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,000 changes: 968 additions & 32 deletions package-lock.json

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
"postgres": "^3.4.5",
"redis": "^4.7.0",
"rxjs": "~7.8.2",
"testcontainers": "^10.18.0",
"tslib": "^2.8.1",
"wrap-ansi": "^9.0.0",
"zone.js": "~0.15.0"
Expand Down
2 changes: 1 addition & 1 deletion ts/backend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@
"test": "vitest run",
"private:serve": "npm run private:build && cd ../../out-tsc && mkdir -p backend-serve/app && cp backend/main.min.mjs backend-serve/app && mkdir -p backend-serve/config && cp -n ../ts/backend/src/config/config.jsonc \"$_\" && cd backend-serve/app && node main.min.mjs --help --log-level=ALL --log-colorful --log-stringify",
"private:build": "node esbuild.mjs",
"private:test": "vitest watch"
"private:test": "vitest watch --project=unit"
}
}
55 changes: 55 additions & 0 deletions ts/backend/test/integration/setup.mts
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import { DockerComposeEnvironment, StartedDockerComposeEnvironment } from 'testcontainers'
import { defaults } from '../../src/app/features/config/defaults.mjs'

/*
This global setup file is used to boot a Docker environment that runs in
parallel to the one used in development. To achieve that, take the dev env
and reconfigure ports and volumes, as they might conflict.
*/

const composeFilePath = '../../evaluation' // relative to ts/backend
const composeFile = 'docker-compose.yml'

// overwrite ports so test and dev environments can be up at the same time
process.env['RABBITMQ_MANAGEMENT_PORT'] = '35672'
process.env['RABBITMQ_PORT'] = '25672'
process.env['POSTGRES_PORT'] = '25432'
process.env['PGADMIN_LISTEN_PORT'] = '35432'
process.env['MINIO_PORT'] = '29000'
process.env['MINIO_CONSOLE_PORT'] = '29001'
process.env['REDIS_PORT'] = '26379'
process.env['KEYCLOAK_PORT'] = '28081'
// overwrite volumes
// TODO: test data should not be persistent, currently not possible as the volumes ar mounted
// https://github.com/flatland-association/flatland-benchmarks/issues/176
process.env['POSTGRES_VOLUME'] = './var/tmptest/postgres'
process.env['PGADMIN_VOLUME'] = './var/tmptest/pgadmin'
process.env['KEYCLOAK_VOLUME'] = './var/tmptest/keycloak'

const config = Object.assign({}, defaults)
config.api.port = 28000
config.postgres.port = 25432
config.amqp.port = 25672
// TODO: redis, keycloak: make port configurable directly
// depends on https://github.com/flatland-association/flatland-benchmarks/issues/52
config.keycloak.url.replace('6379', '26379')
config.keycloak.url.replace('8081', '28081')

export const testConfig = config

let environment: StartedDockerComposeEnvironment

// setup function - called by vitest once for test setup
export async function setup() {
// bug in node 22.12.0, localhost dns lookup only gives IPv6 address.
// manually provide IPv4
// https://github.com/testcontainers/testcontainers-node/issues/818
// https://github.com/nodejs/node/issues/56137
process.env['TESTCONTAINERS_HOST_OVERRIDE'] = '127.0.0.1'
environment = await new DockerComposeEnvironment(composeFilePath, composeFile).up()
}

// teardown function - called by vitest once for test teardown
export async function teardown() {
await environment.down()
}
54 changes: 54 additions & 0 deletions ts/backend/test/integration/sql-service.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import { SqlService } from '../../src/app/features/services/sql-service.mjs'
import { Schema } from '../../src/app/features/setup/schema.mjs'
import { testConfig } from './setup.mjs'

const SETUP_TIMEOUT = 30 * 1000 // ms

describe.sequential('SQL Service (with Postgres)', () => {
beforeAll(async () => {
SqlService.create(testConfig)
})

test(
'should be able to run Schema.setup()',
async () => {
await expect(Schema.setup()).resolves.toBeUndefined()
},
SETUP_TIMEOUT,
)

test('should execute query `SELECT NOW()`', async () => {
const sql = SqlService.getInstance()
const rows = await sql.query`
SELECT NOW()
`
expect(rows.at(0)?.now).toBeInstanceOf(Date)
})

test('should execute query on set up schema', async () => {
const sql = SqlService.getInstance()
const rows = await sql.query`
SELECT id FROM benchmarks
`
expect(rows).toEqual([{ id: 1 }])
})

test('should error on faulty query (syntax error)', async () => {
const sql = SqlService.getInstance()
const rows = await sql.query`
SELEC
`
expect(rows).toEqual([])
expect(sql.errors).toBeTruthy()
})

test('should error on faulty query (schema error)', async () => {
const sql = SqlService.getInstance()
// Should this test ever fail maybe give the developer who decided to add a column named "shit emoji" to the benchmark table a stern talking-to.
const rows = await sql.query`
SELECT 💩 FROM benchmarks
`
expect(rows).toEqual([])
expect(sql.errors).toBeTruthy()
})
})
6 changes: 6 additions & 0 deletions ts/backend/test/integration/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"extends": "../../tsconfig.json",
"compilerOptions": {
"types": ["vitest/globals", "node"]
}
}
7 changes: 0 additions & 7 deletions ts/backend/vitest.config.mjs

This file was deleted.

27 changes: 27 additions & 0 deletions ts/backend/vitest.config.mts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { defineConfig } from 'vite'
import tsconfigPaths from 'vite-tsconfig-paths'

export default defineConfig({
// tsconfigPaths is required to resolve path aliases ("paths") from tsconfig
plugins: [tsconfigPaths()],
test: {
workspace: [
{
extends: true,
test: {
name: 'unit',
include: ['./src/**/*.spec.ts'],
},
},
{
extends: true,
test: {
name: 'integration',
globalSetup: './test/integration/setup.mts',
include: ['./test/integration/**/*.spec.ts'],
globals: true,
},
},
],
},
})
Loading