update database test
Signed-off-by: Lykke Axlin <lykkeaxlin@hotmail.com> Co-authored-by: klaraab <klarabroman@live.se>
This commit is contained in:
@@ -21,13 +21,12 @@
|
||||
},
|
||||
"dependencies": {
|
||||
"@backstage/backend-common": "^0.9.2",
|
||||
"@backstage/backend-test-utils": "^0.1.7",
|
||||
"@backstage/config": "^0.1.5",
|
||||
"@types/express": "^4.17.6",
|
||||
"express": "^4.17.1",
|
||||
"express-promise-router": "^4.1.0",
|
||||
"knex": "^0.95.1",
|
||||
"supertest": "^6.1.6",
|
||||
"uuid": "^8.3.2",
|
||||
"winston": "^3.2.1",
|
||||
"yn": "^4.0.0"
|
||||
},
|
||||
|
||||
@@ -15,6 +15,7 @@
|
||||
*/
|
||||
|
||||
import { DatabaseHandler } from './DatabaseHandler';
|
||||
import { TestDatabaseId, TestDatabases } from '@backstage/backend-test-utils';
|
||||
|
||||
const members: Array<any> = [
|
||||
{
|
||||
@@ -24,15 +25,24 @@ const members: Array<any> = [
|
||||
];
|
||||
|
||||
describe('DatabaseHandler', () => {
|
||||
let database: DatabaseHandler;
|
||||
beforeAll(async () => {
|
||||
database = await DatabaseHandler.createTestDatabase();
|
||||
await database.addMember(members[0].user_id, members[0].entity_ref);
|
||||
const databases = TestDatabases.create({
|
||||
ids: ['POSTGRES_13'],
|
||||
});
|
||||
|
||||
it("can get members that's in the database", async () => {
|
||||
const cov: any[] = await database.getMembers('project1');
|
||||
expect(cov[0].entity_ref).toEqual(members[0].entity_ref);
|
||||
expect(cov[0].user_id).toEqual(members[0].user_id);
|
||||
});
|
||||
async function createDatabaseHandler(databaseId: TestDatabaseId) {
|
||||
const knex = await databases.init(databaseId);
|
||||
return await DatabaseHandler.create({ database: knex });
|
||||
}
|
||||
|
||||
it.each(databases.eachSupportedId())(
|
||||
'should do a full sync with the locations on connect, %p',
|
||||
async databaseId => {
|
||||
const db = await createDatabaseHandler(databaseId);
|
||||
await db.addMember(members[0].user_id, members[0].entity_ref);
|
||||
const cov: any[] = await db.getMembers('project1');
|
||||
expect(cov[0].entity_ref).toEqual(members[0].entity_ref);
|
||||
expect(cov[0].user_id).toEqual(members[0].user_id);
|
||||
},
|
||||
60_000,
|
||||
);
|
||||
});
|
||||
|
||||
@@ -15,8 +15,7 @@
|
||||
*/
|
||||
|
||||
import { resolvePackagePath } from '@backstage/backend-common';
|
||||
import knexFactory, { Knex } from 'knex';
|
||||
import { v4 as uuidv4 } from 'uuid';
|
||||
import { Knex } from 'knex';
|
||||
|
||||
const migrationsDir = resolvePackagePath(
|
||||
'@backstage/plugin-bazaar-backend',
|
||||
@@ -44,47 +43,6 @@ export class DatabaseHandler {
|
||||
this.database = options.database;
|
||||
}
|
||||
|
||||
public static async createTestDatabase(): Promise<DatabaseHandler> {
|
||||
const knex = await this.createTestDatabaseConnection();
|
||||
return await this.create({ database: knex });
|
||||
}
|
||||
|
||||
public static async createTestDatabaseConnection(): Promise<Knex> {
|
||||
const config: Knex.Config<any> = {
|
||||
client: 'pg',
|
||||
connection: {
|
||||
host: 'localhost',
|
||||
port: 5432,
|
||||
user: 'postgres',
|
||||
password: 'postgres',
|
||||
},
|
||||
/*
|
||||
client: 'sqlite3',
|
||||
connection: ':memory:',
|
||||
useNullAsDefault: true,
|
||||
*/
|
||||
};
|
||||
|
||||
let knex = knexFactory(config);
|
||||
if (typeof config.connection !== 'string') {
|
||||
const tempDbName = `d${uuidv4().replace(/-/g, '')}`;
|
||||
await knex.raw(`CREATE DATABASE ${tempDbName};`);
|
||||
knex = knexFactory({
|
||||
...config,
|
||||
connection: {
|
||||
...config.connection,
|
||||
database: tempDbName,
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
knex.client.pool.on('createSuccess', (_eventId: any, resource: any) => {
|
||||
resource.run('PRAGMA foreign_keys = ON', () => {});
|
||||
});
|
||||
|
||||
return knex;
|
||||
}
|
||||
|
||||
async getMembers(entityRef: any) {
|
||||
return await this.database
|
||||
.select('*')
|
||||
|
||||
Reference in New Issue
Block a user