-
Couldn't load subscription status.
- Fork 101
feat(card-service): introduce testcontainers for database and redis #3533
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
Merged
beniaminmunteanu
merged 4 commits into
pos-card-services
from
raf-1115/test-container-card-service
Jul 9, 2025
Merged
Changes from 3 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
874f615
feat(card-service): add testcontainers setup
beniaminmunteanu 1b8e0a8
chore(cards-service): lockfile
beniaminmunteanu cdd37fa
feat(cards-service): fix test containers setup
beniaminmunteanu 7eaa8bb
fix(cards-service): fix type issue
beniaminmunteanu File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| import { TestEnvironment } from 'jest-environment-node' | ||
| import nock from 'nock' | ||
|
|
||
| export default class CustomEnvironment extends TestEnvironment { | ||
| constructor(config, context) { | ||
| super(config, context) | ||
| this.global.nock = nock | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| // Jest environment configuration for card-service | ||
| process.env.NODE_ENV = 'test' | ||
| process.env.LOG_LEVEL = process.env.LOG_LEVEL || 'silent' |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,88 @@ | ||
| import { knex } from 'knex' | ||
| import { GenericContainer, Wait } from 'testcontainers' | ||
| require('./jest.env') // set environment variables | ||
|
|
||
| const POSTGRES_PORT = 5432 | ||
| const REDIS_PORT = 6379 | ||
|
|
||
| const setup = async (globalConfig): Promise<void> => { | ||
| const workers = globalConfig.maxWorkers | ||
|
|
||
| const setupDatabase = async () => { | ||
| if (!process.env.DATABASE_URL) { | ||
| const postgresContainer = await new GenericContainer('postgres:15') | ||
| .withExposedPorts(POSTGRES_PORT) | ||
| .withBindMounts([ | ||
| { | ||
| source: __dirname + '/scripts/init.sh', | ||
| target: '/docker-entrypoint-initdb.d/init.sh' | ||
| } | ||
| ]) | ||
| .withEnvironment({ | ||
| POSTGRES_PASSWORD: 'password' | ||
| }) | ||
| .withHealthCheck({ | ||
| test: ['CMD-SHELL', 'pg_isready -d testing'], | ||
| interval: 10000, | ||
| timeout: 5000, | ||
| retries: 5 | ||
| }) | ||
| .withWaitStrategy(Wait.forHealthCheck()) | ||
| .start() | ||
|
|
||
| process.env.DATABASE_URL = `postgresql://postgres:password@localhost:${postgresContainer.getMappedPort( | ||
| POSTGRES_PORT | ||
| )}/testing` | ||
|
|
||
| global.__CARD_SERVICE_POSTGRES__ = postgresContainer | ||
| } | ||
|
|
||
| const db = knex({ | ||
| client: 'postgresql', | ||
| connection: process.env.DATABASE_URL, | ||
| pool: { | ||
| min: 2, | ||
| max: 10 | ||
| }, | ||
| migrations: { | ||
| tableName: 'knex_migrations' | ||
| } | ||
| }) | ||
|
|
||
| // node pg defaults to returning bigint as string. This ensures it parses to bigint | ||
| db.client.driver.types.setTypeParser( | ||
| db.client.driver.types.builtins.INT8, | ||
| 'text', | ||
| BigInt | ||
| ) | ||
| await db.migrate.latest({ | ||
| directory: __dirname + '/migrations' | ||
| }) | ||
|
|
||
| for (let i = 1; i <= workers; i++) { | ||
| const workerDatabaseName = `testing_${i}` | ||
|
|
||
| await db.raw(`DROP DATABASE IF EXISTS ${workerDatabaseName}`) | ||
| await db.raw(`CREATE DATABASE ${workerDatabaseName} TEMPLATE testing`) | ||
| } | ||
|
|
||
| global.__CARD_SERVICE_KNEX__ = db | ||
| } | ||
|
|
||
| const setupRedis = async () => { | ||
| if (!process.env.REDIS_URL) { | ||
| const redisContainer = await new GenericContainer('redis:7') | ||
| .withExposedPorts(REDIS_PORT) | ||
| .start() | ||
|
|
||
| global.__CARD_SERVICE_REDIS__ = redisContainer | ||
| process.env.REDIS_URL = `redis://localhost:${redisContainer.getMappedPort( | ||
| REDIS_PORT | ||
| )}` | ||
| } | ||
| } | ||
|
|
||
| await Promise.all([setupDatabase(), setupRedis()]) | ||
| } | ||
|
|
||
| export default setup |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| module.exports = async () => { | ||
| await global.__CARD_SERVICE_KNEX__.migrate.rollback( | ||
| { directory: __dirname + '/migrations' }, | ||
| true | ||
| ) | ||
| await global.__CARD_SERVICE_KNEX__.destroy() | ||
| if (global.__CARD_SERVICE_POSTGRES__) { | ||
| await global.__CARD_SERVICE_POSTGRES__.stop() | ||
| } | ||
| if (global.__CARD_SERVICE_REDIS__) { | ||
| await global.__CARD_SERVICE_REDIS__.stop() | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| #!/bin/bash | ||
| set -e | ||
|
|
||
| psql -v ON_ERROR_STOP=1 --username "$POSTGRES_USER" --dbname "$POSTGRES_DB" <<-EOSQL | ||
| DROP DATABASE IF EXISTS TESTING; | ||
| CREATE DATABASE testing; | ||
| CREATE DATABASE development; | ||
| EOSQL |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,37 @@ | ||
| import { Knex } from 'knex' | ||
| import { IocContract } from '@adonisjs/fold' | ||
|
|
||
| import { App, AppServices } from '../app' | ||
|
|
||
| export interface TestContainer { | ||
| cardServicePort: number | ||
| app: App | ||
| knex: Knex | ||
| connectionUrl: string | ||
| shutdown: () => Promise<void> | ||
| container: IocContract<AppServices> | ||
| } | ||
|
|
||
| export const createTestApp = async ( | ||
| container: IocContract<AppServices> | ||
| ): Promise<TestContainer> => { | ||
| const config = await container.use('config') | ||
| config.cardServicePort = 0 | ||
|
|
||
| const app = new App(container) | ||
| await app.boot() | ||
| await app.startCardServiceServer(config.cardServicePort) | ||
|
|
||
| const knex = global.__CARD_SERVICE_KNEX__ | ||
|
|
||
| return { | ||
| app, | ||
| cardServicePort: app.getCardServicePort(), | ||
| knex, | ||
| connectionUrl: process.env.DATABASE_URL || '', | ||
| shutdown: async () => { | ||
| await app.shutdown() | ||
| }, | ||
| container | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,42 @@ | ||
| import { IocContract } from '@adonisjs/fold' | ||
| import { Knex } from 'knex' | ||
| import { AppServices } from '../app' | ||
|
|
||
| export async function truncateTable( | ||
| knex: Knex, | ||
| tableName: string | ||
| ): Promise<void> { | ||
| const RAW = `TRUNCATE TABLE "${tableName}" RESTART IDENTITY CASCADE` | ||
| await knex.raw(RAW) | ||
| } | ||
|
|
||
| export async function truncateTables( | ||
| deps: IocContract<AppServices> | ||
| ): Promise<void> { | ||
| const knex = await deps.use('knex') | ||
|
|
||
| const ignoreTables = [ | ||
| 'knex_migrations', | ||
| 'knex_migrations_lock', | ||
| 'card_service_knex_migrations', | ||
| 'card_service_knex_migrations_lock' | ||
| ] | ||
|
|
||
| const tables = await getTables(knex, ignoreTables) | ||
| if (tables.length > 0) { | ||
| const RAW = `TRUNCATE TABLE "${tables.join('","')}" RESTART IDENTITY CASCADE` | ||
| await knex.raw(RAW) | ||
| } | ||
| } | ||
|
|
||
| async function getTables( | ||
| knex: Knex, | ||
| ignoredTables: string[] | ||
| ): Promise<string[]> { | ||
| const result = await knex.raw( | ||
| `SELECT tablename FROM pg_catalog.pg_tables WHERE schemaname='public'` | ||
| ) | ||
| return result.rows | ||
| .map((val: { tablename: string }) => val.tablename) | ||
| .filter((tableName: string) => !ignoredTables.includes(tableName)) | ||
| } |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.