Skip to content

Commit

Permalink
Refactored TestDatabases to no longer depend on backend-common
Browse files Browse the repository at this point in the history
Signed-off-by: Fredrik Adelöw <freben@gmail.com>
  • Loading branch information
freben committed May 24, 2024
1 parent 09c05a1 commit 6f8d45d
Show file tree
Hide file tree
Showing 20 changed files with 836 additions and 684 deletions.
5 changes: 5 additions & 0 deletions .changeset/nervous-queens-fly.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@backstage/backend-test-utils': patch
---

Refactored `TestDatabases` to no longer depend on `backend-common`
6 changes: 6 additions & 0 deletions .changeset/pretty-berries-live.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'@backstage/backend-defaults': patch
'@backstage/backend-common': patch
---

Deprecated `dropDatabase`
2 changes: 1 addition & 1 deletion packages/backend-common/api-report.md
Original file line number Diff line number Diff line change
Expand Up @@ -312,7 +312,7 @@ export class DockerContainerRunner implements ContainerRunner {
runContainer(options: RunContainerOptions): Promise<void>;
}

// @public
// @public @deprecated
export function dropDatabase(
dbConfig: Config,
...databaseNames: string[]
Expand Down
2 changes: 1 addition & 1 deletion packages/backend-defaults/api-report-database.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ export const databaseServiceFactory: () => ServiceFactory<
'plugin'
>;

// @public
// @public @deprecated
export function dropDatabase(
dbConfig: Config,
...databaseNames: string[]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -234,9 +234,10 @@ export class DatabaseManager implements LegacyRootDatabaseService {
}

/**
* Helper for deleting databases, only exists for backend-test-utils for now.
* Helper for deleting databases.
*
* @public
* @deprecated Will be removed in a future release.
*/
export async function dropDatabase(
dbConfig: Config,
Expand Down
5 changes: 3 additions & 2 deletions packages/backend-test-utils/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@
},
"dependencies": {
"@backstage/backend-app-api": "workspace:^",
"@backstage/backend-common": "workspace:^",
"@backstage/backend-plugin-api": "workspace:^",
"@backstage/config": "workspace:^",
"@backstage/errors": "workspace:^",
Expand All @@ -65,9 +64,11 @@
"msw": "^1.0.0",
"mysql2": "^3.0.0",
"pg": "^8.11.3",
"pg-connection-string": "^2.3.0",
"testcontainers": "^10.0.0",
"textextensions": "^5.16.0",
"uuid": "^9.0.0"
"uuid": "^9.0.0",
"yn": "^4.0.0"
},
"devDependencies": {
"@backstage/cli": "workspace:^",
Expand Down
267 changes: 0 additions & 267 deletions packages/backend-test-utils/src/database/TestDatabases.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,28 +14,11 @@
* limitations under the License.
*/

import knexFactory from 'knex';
import { isDockerDisabledForTests } from '../util/isDockerDisabledForTests';
import { startMysqlContainer } from './startMysqlContainer';
import { startPostgresContainer } from './startPostgresContainer';
import { TestDatabases } from './TestDatabases';

const itIfDocker = isDockerDisabledForTests() ? it.skip : it;

jest.setTimeout(60_000);

describe('TestDatabases', () => {
const OLD_ENV = process.env;

beforeEach(() => {
jest.resetModules();
process.env = { ...OLD_ENV };
});

afterAll(() => {
process.env = OLD_ENV;
});

describe('each create', () => {
const dbs = TestDatabases.create();

Expand All @@ -55,254 +38,4 @@ describe('TestDatabases', () => {
},
);
});

describe('each connect', () => {
const dbs = TestDatabases.create();

itIfDocker(
'obeys a provided connection string for postgres 16',
async () => {
const { host, port, user, password, stop } =
await startPostgresContainer('postgres:16');

try {
// Leave a mark
process.env.BACKSTAGE_TEST_DATABASE_POSTGRES16_CONNECTION_STRING = `postgresql://${user}:${password}@${host}:${port}`;
const input = await dbs.init('POSTGRES_16');
await input.schema.createTable('a', table =>
table.string('x').primary(),
);
await input.insert({ x: 'y' }).into('a');

// Look for the mark
const database = input.client.config.connection.database;
const output = knexFactory({
client: 'pg',
connection: { host, port, user, password, database },
});
// eslint-disable-next-line jest/no-standalone-expect
await expect(output.select('x').from('a')).resolves.toEqual([
{ x: 'y' },
]);
} finally {
await stop();
}
},
);

itIfDocker(
'obeys a provided connection string for postgres 15',
async () => {
const { host, port, user, password, stop } =
await startPostgresContainer('postgres:15');

try {
// Leave a mark
process.env.BACKSTAGE_TEST_DATABASE_POSTGRES15_CONNECTION_STRING = `postgresql://${user}:${password}@${host}:${port}`;
const input = await dbs.init('POSTGRES_15');
await input.schema.createTable('a', table =>
table.string('x').primary(),
);
await input.insert({ x: 'y' }).into('a');

// Look for the mark
const database = input.client.config.connection.database;
const output = knexFactory({
client: 'pg',
connection: { host, port, user, password, database },
});
// eslint-disable-next-line jest/no-standalone-expect
await expect(output.select('x').from('a')).resolves.toEqual([
{ x: 'y' },
]);
} finally {
await stop();
}
},
);

itIfDocker(
'obeys a provided connection string for postgres 14',
async () => {
const { host, port, user, password, stop } =
await startPostgresContainer('postgres:14');

try {
// Leave a mark
process.env.BACKSTAGE_TEST_DATABASE_POSTGRES14_CONNECTION_STRING = `postgresql://${user}:${password}@${host}:${port}`;
const input = await dbs.init('POSTGRES_14');
await input.schema.createTable('a', table =>
table.string('x').primary(),
);
await input.insert({ x: 'y' }).into('a');

// Look for the mark
const database = input.client.config.connection.database;
const output = knexFactory({
client: 'pg',
connection: { host, port, user, password, database },
});
// eslint-disable-next-line jest/no-standalone-expect
await expect(output.select('x').from('a')).resolves.toEqual([
{ x: 'y' },
]);
} finally {
await stop();
}
},
);

itIfDocker(
'obeys a provided connection string for postgres 13',
async () => {
const { host, port, user, password, stop } =
await startPostgresContainer('postgres:13');

try {
// Leave a mark
process.env.BACKSTAGE_TEST_DATABASE_POSTGRES13_CONNECTION_STRING = `postgresql://${user}:${password}@${host}:${port}`;
const input = await dbs.init('POSTGRES_13');
await input.schema.createTable('a', table =>
table.string('x').primary(),
);
await input.insert({ x: 'y' }).into('a');

// Look for the mark
const database = input.client.config.connection.database;
const output = knexFactory({
client: 'pg',
connection: { host, port, user, password, database },
});
// eslint-disable-next-line jest/no-standalone-expect
await expect(output.select('x').from('a')).resolves.toEqual([
{ x: 'y' },
]);
} finally {
await stop();
}
},
);

itIfDocker(
'obeys a provided connection string for postgres 12',
async () => {
const { host, port, user, password, stop } =
await startPostgresContainer('postgres:12');

try {
// Leave a mark
process.env.BACKSTAGE_TEST_DATABASE_POSTGRES12_CONNECTION_STRING = `postgresql://${user}:${password}@${host}:${port}`;
const input = await dbs.init('POSTGRES_12');
await input.schema.createTable('a', table =>
table.string('x').primary(),
);
await input.insert({ x: 'y' }).into('a');

// Look for the mark
const database = input.client.config.connection.database;
const output = knexFactory({
client: 'pg',
connection: { host, port, user, password, database },
});
// eslint-disable-next-line jest/no-standalone-expect
await expect(output.select('x').from('a')).resolves.toEqual([
{ x: 'y' },
]);
} finally {
await stop();
}
},
);

itIfDocker(
'obeys a provided connection string for postgres 11',
async () => {
const { host, port, user, password, stop } =
await startPostgresContainer('postgres:11');

try {
// Leave a mark
process.env.BACKSTAGE_TEST_DATABASE_POSTGRES11_CONNECTION_STRING = `postgresql://${user}:${password}@${host}:${port}`;
const input = await dbs.init('POSTGRES_11');
await input.schema.createTable('a', table =>
table.string('x').primary(),
);
await input.insert({ x: 'y' }).into('a');

// Look for the mark
const database = input.client.config.connection.database;
const output = knexFactory({
client: 'pg',
connection: { host, port, user, password, database },
});
// eslint-disable-next-line jest/no-standalone-expect
await expect(output.select('x').from('a')).resolves.toEqual([
{ x: 'y' },
]);
} finally {
await stop();
}
},
);

itIfDocker(
'obeys a provided connection string for postgres 9',
async () => {
const { host, port, user, password, stop } =
await startPostgresContainer('postgres:9');

try {
// Leave a mark
process.env.BACKSTAGE_TEST_DATABASE_POSTGRES9_CONNECTION_STRING = `postgresql://${user}:${password}@${host}:${port}`;
const input = await dbs.init('POSTGRES_9');
await input.schema.createTable('a', table =>
table.string('x').primary(),
);
await input.insert({ x: 'y' }).into('a');

// Look for the mark
const database = input.client.config.connection.database;
const output = knexFactory({
client: 'pg',
connection: { host, port, user, password, database },
});
// eslint-disable-next-line jest/no-standalone-expect
await expect(output.select('x').from('a')).resolves.toEqual([
{ x: 'y' },
]);
} finally {
await stop();
}
},
);

itIfDocker('obeys a provided connection string for mysql 8', async () => {
const { host, port, user, password, stop } = await startMysqlContainer(
'mysql:8',
);

try {
// Leave a mark
process.env.BACKSTAGE_TEST_DATABASE_MYSQL8_CONNECTION_STRING = `mysql://${user}:${password}@${host}:${port}/ignored`;
const input = await dbs.init('MYSQL_8');
await input.schema.createTable('a', table =>
table.string('x').primary(),
);
await input.insert({ x: 'y' }).into('a');

// Look for the mark
const database = input.client.config.connection.database;
const output = knexFactory({
client: 'mysql2',
connection: { host, port, user, password, database },
});
// eslint-disable-next-line jest/no-standalone-expect
await expect(output.select('x').from('a')).resolves.toEqual([
{ x: 'y' },
]);
} finally {
await stop();
}
});
});
});
Loading

0 comments on commit 6f8d45d

Please sign in to comment.