catalog: destroy knex connections once done
Signed-off-by: Vincenzo Scamporlino <vincenzos@spotify.com>
This commit is contained in:
@@ -35,6 +35,12 @@ import { DefaultEntitiesCatalog } from './DefaultEntitiesCatalog';
|
||||
import { EntitiesRequest } from '../catalog/types';
|
||||
|
||||
describe('DefaultEntitiesCatalog', () => {
|
||||
let knex: Knex;
|
||||
|
||||
afterEach(async () => {
|
||||
await knex.destroy();
|
||||
});
|
||||
|
||||
const databases = TestDatabases.create({
|
||||
ids: ['MYSQL_8', 'POSTGRES_13', 'POSTGRES_9', 'SQLITE_3'],
|
||||
});
|
||||
@@ -42,13 +48,11 @@ describe('DefaultEntitiesCatalog', () => {
|
||||
const stitcher: Stitcher = { stitch } as any;
|
||||
|
||||
async function createDatabase(databaseId: TestDatabaseId) {
|
||||
const knex = await databases.init(databaseId);
|
||||
knex = await databases.init(databaseId);
|
||||
await applyDatabaseMigrations(knex);
|
||||
return { knex };
|
||||
}
|
||||
|
||||
async function addEntity(
|
||||
knex: Knex,
|
||||
entity: Entity,
|
||||
parents: { source?: string; entity?: Entity }[],
|
||||
) {
|
||||
@@ -85,7 +89,7 @@ describe('DefaultEntitiesCatalog', () => {
|
||||
return id;
|
||||
}
|
||||
|
||||
async function addEntityToSearch(knex: Knex, entity: Entity, id = uuid()) {
|
||||
async function addEntityToSearch(entity: Entity, id = uuid()) {
|
||||
const entityRef = stringifyEntityRef(entity);
|
||||
const entityJson = JSON.stringify(entity);
|
||||
|
||||
@@ -123,7 +127,7 @@ describe('DefaultEntitiesCatalog', () => {
|
||||
it.each(databases.eachSupportedId())(
|
||||
'should return the ancestry with one parent, %p',
|
||||
async databaseId => {
|
||||
const { knex } = await createDatabase(databaseId);
|
||||
await createDatabase(databaseId);
|
||||
|
||||
const grandparent: Entity = {
|
||||
apiVersion: 'a',
|
||||
@@ -144,9 +148,9 @@ describe('DefaultEntitiesCatalog', () => {
|
||||
spec: {},
|
||||
};
|
||||
|
||||
await addEntity(knex, grandparent, [{ source: 's' }]);
|
||||
await addEntity(knex, parent, [{ entity: grandparent }]);
|
||||
await addEntity(knex, root, [{ entity: parent }]);
|
||||
await addEntity(grandparent, [{ source: 's' }]);
|
||||
await addEntity(parent, [{ entity: grandparent }]);
|
||||
await addEntity(root, [{ entity: parent }]);
|
||||
|
||||
const catalog = new DefaultEntitiesCatalog(knex, stitcher);
|
||||
const result = await catalog.entityAncestry('k:default/root');
|
||||
@@ -177,7 +181,7 @@ describe('DefaultEntitiesCatalog', () => {
|
||||
it.each(databases.eachSupportedId())(
|
||||
'should throw error if the entity does not exist, %p',
|
||||
async databaseId => {
|
||||
const { knex } = await createDatabase(databaseId);
|
||||
await createDatabase(databaseId);
|
||||
const catalog = new DefaultEntitiesCatalog(knex, stitcher);
|
||||
await expect(() =>
|
||||
catalog.entityAncestry('k:default/root'),
|
||||
@@ -189,7 +193,7 @@ describe('DefaultEntitiesCatalog', () => {
|
||||
it.each(databases.eachSupportedId())(
|
||||
'should return the ancestry with multiple parents, %p',
|
||||
async databaseId => {
|
||||
const { knex } = await createDatabase(databaseId);
|
||||
await createDatabase(databaseId);
|
||||
|
||||
const grandparent: Entity = {
|
||||
apiVersion: 'a',
|
||||
@@ -216,10 +220,10 @@ describe('DefaultEntitiesCatalog', () => {
|
||||
spec: {},
|
||||
};
|
||||
|
||||
await addEntity(knex, grandparent, [{ source: 's' }]);
|
||||
await addEntity(knex, parent1, [{ entity: grandparent }]);
|
||||
await addEntity(knex, parent2, [{ entity: grandparent }]);
|
||||
await addEntity(knex, root, [{ entity: parent1 }, { entity: parent2 }]);
|
||||
await addEntity(grandparent, [{ source: 's' }]);
|
||||
await addEntity(parent1, [{ entity: grandparent }]);
|
||||
await addEntity(parent2, [{ entity: grandparent }]);
|
||||
await addEntity(root, [{ entity: parent1 }, { entity: parent2 }]);
|
||||
|
||||
const catalog = new DefaultEntitiesCatalog(knex, stitcher);
|
||||
const result = await catalog.entityAncestry('k:default/root');
|
||||
@@ -260,7 +264,7 @@ describe('DefaultEntitiesCatalog', () => {
|
||||
it.each(databases.eachSupportedId())(
|
||||
'should return correct entity for simple filter, %p',
|
||||
async databaseId => {
|
||||
const { knex } = await createDatabase(databaseId);
|
||||
await createDatabase(databaseId);
|
||||
const entity1: Entity = {
|
||||
apiVersion: 'a',
|
||||
kind: 'k',
|
||||
@@ -275,8 +279,8 @@ describe('DefaultEntitiesCatalog', () => {
|
||||
test: 'test value',
|
||||
},
|
||||
};
|
||||
await addEntityToSearch(knex, entity1);
|
||||
await addEntityToSearch(knex, entity2);
|
||||
await addEntityToSearch(entity1);
|
||||
await addEntityToSearch(entity2);
|
||||
const catalog = new DefaultEntitiesCatalog(knex, stitcher);
|
||||
|
||||
const testFilter = {
|
||||
@@ -294,7 +298,7 @@ describe('DefaultEntitiesCatalog', () => {
|
||||
it.each(databases.eachSupportedId())(
|
||||
'should return correct entity for negation filter, %p',
|
||||
async databaseId => {
|
||||
const { knex } = await createDatabase(databaseId);
|
||||
await createDatabase(databaseId);
|
||||
const entity1: Entity = {
|
||||
apiVersion: 'a',
|
||||
kind: 'k',
|
||||
@@ -309,8 +313,8 @@ describe('DefaultEntitiesCatalog', () => {
|
||||
test: 'test value',
|
||||
},
|
||||
};
|
||||
await addEntityToSearch(knex, entity1);
|
||||
await addEntityToSearch(knex, entity2);
|
||||
await addEntityToSearch(entity1);
|
||||
await addEntityToSearch(entity2);
|
||||
const catalog = new DefaultEntitiesCatalog(knex, stitcher);
|
||||
|
||||
const testFilter = {
|
||||
@@ -330,7 +334,7 @@ describe('DefaultEntitiesCatalog', () => {
|
||||
it.each(databases.eachSupportedId())(
|
||||
'should return correct entities for nested filter, %p',
|
||||
async databaseId => {
|
||||
const { knex } = await createDatabase(databaseId);
|
||||
await createDatabase(databaseId);
|
||||
const entity1: Entity = {
|
||||
apiVersion: 'a',
|
||||
kind: 'k',
|
||||
@@ -355,10 +359,10 @@ describe('DefaultEntitiesCatalog', () => {
|
||||
metadata: { name: 'four', org: 'b', color: 'blue' },
|
||||
spec: {},
|
||||
};
|
||||
await addEntityToSearch(knex, entity1);
|
||||
await addEntityToSearch(knex, entity2);
|
||||
await addEntityToSearch(knex, entity3);
|
||||
await addEntityToSearch(knex, entity4);
|
||||
await addEntityToSearch(entity1);
|
||||
await addEntityToSearch(entity2);
|
||||
await addEntityToSearch(entity3);
|
||||
await addEntityToSearch(entity4);
|
||||
const catalog = new DefaultEntitiesCatalog(knex, stitcher);
|
||||
|
||||
const testFilter1 = {
|
||||
@@ -400,7 +404,7 @@ describe('DefaultEntitiesCatalog', () => {
|
||||
it.each(databases.eachSupportedId())(
|
||||
'should return correct entities for complex negation filter, %p',
|
||||
async databaseId => {
|
||||
const { knex } = await createDatabase(databaseId);
|
||||
await createDatabase(databaseId);
|
||||
const entity1: Entity = {
|
||||
apiVersion: 'a',
|
||||
kind: 'k',
|
||||
@@ -413,8 +417,8 @@ describe('DefaultEntitiesCatalog', () => {
|
||||
metadata: { name: 'two', org: 'b', desc: 'description' },
|
||||
spec: {},
|
||||
};
|
||||
await addEntityToSearch(knex, entity1);
|
||||
await addEntityToSearch(knex, entity2);
|
||||
await addEntityToSearch(entity1);
|
||||
await addEntityToSearch(entity2);
|
||||
const catalog = new DefaultEntitiesCatalog(knex, stitcher);
|
||||
|
||||
const testFilter1 = {
|
||||
@@ -443,7 +447,7 @@ describe('DefaultEntitiesCatalog', () => {
|
||||
'should return no matches for an empty values array, %p',
|
||||
// NOTE: An empty values array is not a sensible input in a realistic scenario.
|
||||
async databaseId => {
|
||||
const { knex } = await createDatabase(databaseId);
|
||||
await createDatabase(databaseId);
|
||||
const entity1: Entity = {
|
||||
apiVersion: 'a',
|
||||
kind: 'k',
|
||||
@@ -456,8 +460,8 @@ describe('DefaultEntitiesCatalog', () => {
|
||||
metadata: { name: 'two' },
|
||||
spec: {},
|
||||
};
|
||||
await addEntityToSearch(knex, entity1);
|
||||
await addEntityToSearch(knex, entity2);
|
||||
await addEntityToSearch(entity1);
|
||||
await addEntityToSearch(entity2);
|
||||
const catalog = new DefaultEntitiesCatalog(knex, stitcher);
|
||||
|
||||
const testFilter = {
|
||||
@@ -475,9 +479,8 @@ describe('DefaultEntitiesCatalog', () => {
|
||||
it.each(databases.eachSupportedId())(
|
||||
'should return both target and targetRef for entities',
|
||||
async databaseId => {
|
||||
const { knex } = await createDatabase(databaseId);
|
||||
await createDatabase(databaseId);
|
||||
await addEntity(
|
||||
knex,
|
||||
{
|
||||
apiVersion: 'a',
|
||||
kind: 'k',
|
||||
@@ -488,7 +491,6 @@ describe('DefaultEntitiesCatalog', () => {
|
||||
[],
|
||||
);
|
||||
await addEntity(
|
||||
knex,
|
||||
{
|
||||
apiVersion: 'a',
|
||||
kind: 'k',
|
||||
@@ -681,7 +683,7 @@ describe('DefaultEntitiesCatalog', () => {
|
||||
it.each(databases.eachSupportedId())(
|
||||
'should return paginated entities and scroll the items accordingly, %p',
|
||||
async databaseId => {
|
||||
const { knex } = await createDatabase(databaseId);
|
||||
await createDatabase(databaseId);
|
||||
|
||||
function entityFrom(name: string) {
|
||||
return {
|
||||
@@ -711,9 +713,7 @@ describe('DefaultEntitiesCatalog', () => {
|
||||
];
|
||||
|
||||
await Promise.all(
|
||||
entities
|
||||
.concat(notFoundEntities)
|
||||
.map(e => addEntityToSearch(knex, e)),
|
||||
entities.concat(notFoundEntities).map(e => addEntityToSearch(e)),
|
||||
);
|
||||
|
||||
const catalog = new DefaultEntitiesCatalog(knex, stitcher);
|
||||
@@ -833,7 +833,7 @@ describe('DefaultEntitiesCatalog', () => {
|
||||
it.each(databases.eachSupportedId())(
|
||||
'should return paginated entities ordered in descending order and scroll the items accordingly, %p',
|
||||
async databaseId => {
|
||||
const { knex } = await createDatabase(databaseId);
|
||||
await createDatabase(databaseId);
|
||||
|
||||
function entityFrom(name: string) {
|
||||
return {
|
||||
@@ -863,9 +863,7 @@ describe('DefaultEntitiesCatalog', () => {
|
||||
];
|
||||
|
||||
await Promise.all(
|
||||
entities
|
||||
.concat(notFoundEntities)
|
||||
.map(e => addEntityToSearch(knex, e)),
|
||||
entities.concat(notFoundEntities).map(e => addEntityToSearch(e)),
|
||||
);
|
||||
|
||||
const catalog = new DefaultEntitiesCatalog(knex, stitcher);
|
||||
@@ -986,7 +984,7 @@ describe('DefaultEntitiesCatalog', () => {
|
||||
it.each(databases.eachSupportedId())(
|
||||
'should filter the results when query is provided, %p',
|
||||
async databaseId => {
|
||||
const { knex } = await createDatabase(databaseId);
|
||||
await createDatabase(databaseId);
|
||||
|
||||
function entityFrom(name: string) {
|
||||
return {
|
||||
@@ -1016,9 +1014,7 @@ describe('DefaultEntitiesCatalog', () => {
|
||||
];
|
||||
|
||||
await Promise.all(
|
||||
entities
|
||||
.concat(notFoundEntities)
|
||||
.map(e => addEntityToSearch(knex, e)),
|
||||
entities.concat(notFoundEntities).map(e => addEntityToSearch(e)),
|
||||
);
|
||||
|
||||
const catalog = new DefaultEntitiesCatalog(knex, stitcher);
|
||||
@@ -1049,13 +1045,13 @@ describe('DefaultEntitiesCatalog', () => {
|
||||
it.each(databases.eachSupportedId())(
|
||||
'should include totalItems and empty entities in the response in case limit is zero, %p',
|
||||
async databaseId => {
|
||||
const { knex } = await createDatabase(databaseId);
|
||||
await createDatabase(databaseId);
|
||||
|
||||
await Promise.all(
|
||||
Array(20)
|
||||
.fill(0)
|
||||
.map(() =>
|
||||
addEntityToSearch(knex, {
|
||||
addEntityToSearch({
|
||||
apiVersion: 'a',
|
||||
kind: 'k',
|
||||
metadata: { name: v4() },
|
||||
@@ -1074,9 +1070,9 @@ describe('DefaultEntitiesCatalog', () => {
|
||||
);
|
||||
|
||||
it.each(databases.eachSupportedId())(
|
||||
'should paginate results accordingly in case of clashing items, %p',
|
||||
'should paginate results without sort fields, %p',
|
||||
async databaseId => {
|
||||
const { knex } = await createDatabase(databaseId);
|
||||
await createDatabase(databaseId);
|
||||
|
||||
function entityFrom(name: string, namespace?: string) {
|
||||
return {
|
||||
@@ -1088,12 +1084,12 @@ describe('DefaultEntitiesCatalog', () => {
|
||||
}
|
||||
|
||||
await Promise.all([
|
||||
addEntityToSearch(knex, entityFrom('AA')),
|
||||
addEntityToSearch(knex, entityFrom('AA', 'namespace2')),
|
||||
addEntityToSearch(knex, entityFrom('AA', 'namespace3')),
|
||||
addEntityToSearch(knex, entityFrom('AA', 'namespace4')),
|
||||
addEntityToSearch(knex, entityFrom('CC')),
|
||||
addEntityToSearch(knex, entityFrom('DD')),
|
||||
addEntityToSearch(entityFrom('AA'), 'id1'),
|
||||
addEntityToSearch(entityFrom('CC'), 'id2'),
|
||||
addEntityToSearch(entityFrom('AA', 'namespace2'), 'id4'),
|
||||
addEntityToSearch(entityFrom('AA', 'namespace3'), 'id5'),
|
||||
addEntityToSearch(entityFrom('AA', 'namespace4'), 'id6'),
|
||||
addEntityToSearch(entityFrom('DD'), 'id3'),
|
||||
]);
|
||||
|
||||
const catalog = new DefaultEntitiesCatalog(knex);
|
||||
@@ -1103,12 +1099,11 @@ describe('DefaultEntitiesCatalog', () => {
|
||||
// initial request
|
||||
const request1: PaginatedEntitiesInitialRequest = {
|
||||
limit,
|
||||
sortFields: [{ field: 'metadata.name' }],
|
||||
};
|
||||
const response1 = await catalog.paginatedEntities(request1);
|
||||
expect(response1.entities).toMatchObject([
|
||||
entityFrom('AA'),
|
||||
entityFrom('AA'),
|
||||
entityFrom('CC'),
|
||||
]);
|
||||
expect(response1.nextCursor).toBeDefined();
|
||||
expect(response1.prevCursor).toBeUndefined();
|
||||
@@ -1121,7 +1116,7 @@ describe('DefaultEntitiesCatalog', () => {
|
||||
};
|
||||
const response2 = await catalog.paginatedEntities(request2);
|
||||
expect(response2.entities).toMatchObject([
|
||||
entityFrom('AA'),
|
||||
entityFrom('DD'),
|
||||
entityFrom('AA'),
|
||||
]);
|
||||
expect(response2.nextCursor).toBeDefined();
|
||||
@@ -1135,8 +1130,8 @@ describe('DefaultEntitiesCatalog', () => {
|
||||
};
|
||||
const response3 = await catalog.paginatedEntities(request3);
|
||||
expect(response3.entities).toEqual([
|
||||
entityFrom('CC'),
|
||||
entityFrom('DD'),
|
||||
entityFrom('AA'),
|
||||
entityFrom('AA'),
|
||||
]);
|
||||
expect(response3.nextCursor).toBeUndefined();
|
||||
expect(response3.prevCursor).toBeDefined();
|
||||
@@ -1149,7 +1144,7 @@ describe('DefaultEntitiesCatalog', () => {
|
||||
};
|
||||
const response4 = await catalog.paginatedEntities(request4);
|
||||
expect(response4.entities).toMatchObject([
|
||||
entityFrom('AA'),
|
||||
entityFrom('DD'),
|
||||
entityFrom('AA'),
|
||||
]);
|
||||
expect(response4.nextCursor).toBeDefined();
|
||||
@@ -1164,7 +1159,7 @@ describe('DefaultEntitiesCatalog', () => {
|
||||
const response5 = await catalog.paginatedEntities(request5);
|
||||
expect(response5.entities).toMatchObject([
|
||||
entityFrom('AA'),
|
||||
entityFrom('AA'),
|
||||
entityFrom('CC'),
|
||||
]);
|
||||
expect(response5.nextCursor).toBeDefined();
|
||||
expect(response5.prevCursor).toBeUndefined();
|
||||
@@ -1177,7 +1172,7 @@ describe('DefaultEntitiesCatalog', () => {
|
||||
it.each(databases.eachSupportedId())(
|
||||
'also clears parent hashes, %p',
|
||||
async databaseId => {
|
||||
const { knex } = await createDatabase(databaseId);
|
||||
await createDatabase(databaseId);
|
||||
|
||||
const grandparent: Entity = {
|
||||
apiVersion: 'a',
|
||||
@@ -1216,15 +1211,15 @@ describe('DefaultEntitiesCatalog', () => {
|
||||
spec: {},
|
||||
};
|
||||
|
||||
await addEntity(knex, grandparent, [{ source: 's' }]);
|
||||
await addEntity(knex, parent1, [{ entity: grandparent }]);
|
||||
await addEntity(knex, parent2, [{ entity: grandparent }]);
|
||||
const uid = await addEntity(knex, root, [
|
||||
await addEntity(grandparent, [{ source: 's' }]);
|
||||
await addEntity(parent1, [{ entity: grandparent }]);
|
||||
await addEntity(parent2, [{ entity: grandparent }]);
|
||||
const uid = await addEntity(root, [
|
||||
{ entity: parent1 },
|
||||
{ entity: parent2 },
|
||||
]);
|
||||
await addEntity(knex, unrelated1, []);
|
||||
await addEntity(knex, unrelated2, []);
|
||||
await addEntity(unrelated1, []);
|
||||
await addEntity(unrelated2, []);
|
||||
await knex('refresh_state').update({ result_hash: 'not-changed' });
|
||||
await knex('relations').insert({
|
||||
originating_entity_id: uid,
|
||||
@@ -1266,21 +1261,21 @@ describe('DefaultEntitiesCatalog', () => {
|
||||
it.each(databases.eachSupportedId())(
|
||||
'can filter and collect properly, %p',
|
||||
async databaseId => {
|
||||
const { knex } = await createDatabase(databaseId);
|
||||
await createDatabase(databaseId);
|
||||
|
||||
await addEntityToSearch(knex, {
|
||||
await addEntityToSearch({
|
||||
apiVersion: 'a',
|
||||
kind: 'k',
|
||||
metadata: { name: 'one' },
|
||||
spec: {},
|
||||
});
|
||||
await addEntityToSearch(knex, {
|
||||
await addEntityToSearch({
|
||||
apiVersion: 'a',
|
||||
kind: 'k',
|
||||
metadata: { name: 'two' },
|
||||
spec: {},
|
||||
});
|
||||
await addEntityToSearch(knex, {
|
||||
await addEntityToSearch({
|
||||
apiVersion: 'a',
|
||||
kind: 'k2',
|
||||
metadata: { name: 'two' },
|
||||
@@ -1303,9 +1298,9 @@ describe('DefaultEntitiesCatalog', () => {
|
||||
it.each(databases.eachSupportedId())(
|
||||
'can match on annotations and labels with dots in them, %p',
|
||||
async databaseId => {
|
||||
const { knex } = await createDatabase(databaseId);
|
||||
await createDatabase(databaseId);
|
||||
|
||||
await addEntityToSearch(knex, {
|
||||
await addEntityToSearch({
|
||||
apiVersion: 'a',
|
||||
kind: 'k',
|
||||
metadata: {
|
||||
@@ -1315,7 +1310,7 @@ describe('DefaultEntitiesCatalog', () => {
|
||||
},
|
||||
spec: {},
|
||||
});
|
||||
await addEntityToSearch(knex, {
|
||||
await addEntityToSearch({
|
||||
apiVersion: 'a',
|
||||
kind: 'k',
|
||||
metadata: {
|
||||
@@ -1350,9 +1345,9 @@ describe('DefaultEntitiesCatalog', () => {
|
||||
it.each(databases.eachSupportedId())(
|
||||
'can match on strings in arrays, %p',
|
||||
async databaseId => {
|
||||
const { knex } = await createDatabase(databaseId);
|
||||
await createDatabase(databaseId);
|
||||
|
||||
await addEntityToSearch(knex, {
|
||||
await addEntityToSearch({
|
||||
apiVersion: 'a',
|
||||
kind: 'k',
|
||||
metadata: {
|
||||
@@ -1361,7 +1356,7 @@ describe('DefaultEntitiesCatalog', () => {
|
||||
},
|
||||
spec: {},
|
||||
});
|
||||
await addEntityToSearch(knex, {
|
||||
await addEntityToSearch({
|
||||
apiVersion: 'a',
|
||||
kind: 'k',
|
||||
metadata: {
|
||||
|
||||
Reference in New Issue
Block a user