Add locationKey tests

Signed-off-by: Johan Haals <johan.haals@gmail.com>
This commit is contained in:
Johan Haals
2021-06-29 11:31:59 +02:00
committed by Patrik Oldsberg
parent 03f1076fa6
commit 72849d6495
@@ -57,6 +57,105 @@ describe('Default Processing Database', () => {
await db<DbRefreshStateRow>('refresh_state').insert(ref);
};
describe('addUprocessedEntities', () => {
function mockEntity(name: string, type: string): Entity {
return {
apiVersion: '1',
kind: 'Component',
metadata: {
name,
},
spec: {
type,
},
};
}
it.each(databases.eachSupportedId())(
'updates refresh state with varying location keys, %p',
async databaseId => {
const { db } = await createDatabase(databaseId);
await db.transaction(async tx => {
const knexTx = tx as Knex.Transaction;
const steps = [
{
locationKey: undefined,
expectedLocationKey: null,
type: 'a',
expectedType: 'a',
},
{
locationKey: undefined,
expectedLocationKey: null,
type: 'b',
expectedType: 'b',
},
{
locationKey: 'x',
expectedLocationKey: 'x',
type: 'c',
expectedType: 'c',
},
{
locationKey: 'y',
expectedLocationKey: 'x',
type: 'd',
expectedType: 'c',
},
{
locationKey: undefined,
expectedLocationKey: 'x',
type: 'e',
expectedType: 'c',
},
{
locationKey: 'x',
expectedLocationKey: 'x',
type: 'f',
expectedType: 'f',
},
];
for (const step of steps) {
await db.addUnprocessedEntities(tx, {
sourceKey: 'testing',
entities: [
{
entity: mockEntity('1', step.type),
locationKey: step.locationKey,
},
],
});
const entities = await knexTx<DbRefreshStateRow>(
'refresh_state',
).select();
expect(entities).toEqual([
expect.objectContaining({
entity_ref: 'component:default/1',
location_key: step.expectedLocationKey,
}),
]);
const entity = JSON.parse(entities[0].unprocessed_entity) as Entity;
expect(entity.spec?.type).toEqual(step.expectedType);
await expect(
knexTx<DbRefreshStateReferencesRow>(
'refresh_state_references',
).select(),
).resolves.toEqual([
expect.objectContaining({
source_key: 'testing',
target_entity_ref: 'component:default/1',
}),
]);
}
});
},
60_000,
);
});
describe('updateProcessedEntity', () => {
let id: string;
let processedEntity: Entity;
@@ -77,7 +176,7 @@ describe('Default Processing Database', () => {
});
it.each(databases.eachSupportedId())(
'fails when there is no processing state for the entity, %p',
'fails when an entity is processed with a different locationKey, %p',
async databaseId => {
const { db } = await createDatabase(databaseId);
await db.transaction(async tx => {
@@ -95,6 +194,45 @@ describe('Default Processing Database', () => {
60_000,
);
it.each(databases.eachSupportedId())(
'fails when the locationKey is different, %p',
async databaseId => {
const options = {
id,
processedEntity,
state: new Map<string, JsonObject>(),
relations: [],
deferredEntities: [],
locationKey: 'key',
errors: "['something broke']",
};
const { knex, db } = await createDatabase(databaseId);
await insertRefreshStateRow(knex, {
entity_id: id,
entity_ref: 'location:default/fakelocation',
unprocessed_entity: '{}',
processed_entity: '{}',
location_key: 'key',
errors: '[]',
next_update_at: '2021-04-01 13:37:00',
last_discovery_at: '2021-04-01 13:37:00',
});
await db.transaction(tx => db.updateProcessedEntity(tx, options));
const entities = await knex<DbRefreshStateRow>(
'refresh_state',
).select();
expect(entities.length).toBe(1);
await db.transaction(tx =>
expect(
db.updateProcessedEntity(tx, { ...options, locationKey: 'fail' }),
).rejects.toThrow(`Processing state not found for ${id}`),
);
},
60_000,
);
it.each(databases.eachSupportedId())(
'updates the refresh state entry with the cache, processed entity and errors, %p',
async databaseId => {
@@ -119,6 +257,7 @@ describe('Default Processing Database', () => {
state,
relations: [],
deferredEntities: [],
locationKey: 'key',
errors: "['something broke']",
}),
);
@@ -132,6 +271,7 @@ describe('Default Processing Database', () => {
);
expect(entities[0].cache).toEqual(JSON.stringify(state));
expect(entities[0].errors).toEqual("['something broke']");
expect(entities[0].location_key).toEqual('key');
},
60_000,
);
@@ -206,11 +346,14 @@ describe('Default Processing Database', () => {
const deferredEntities = [
{
apiVersion: '1',
kind: 'Location',
metadata: {
name: 'next',
entity: {
apiVersion: '1',
kind: 'Location',
metadata: {
name: 'next',
},
},
locationKey: 'mock',
},
];
@@ -227,7 +370,7 @@ describe('Default Processing Database', () => {
const refreshStateEntries = await knex<DbRefreshStateRow>(
'refresh_state',
)
.where({ entity_ref: stringifyEntityRef(deferredEntities[0]) })
.where({ entity_ref: stringifyEntityRef(deferredEntities[0].entity) })
.select();
expect(refreshStateEntries).toHaveLength(1);
@@ -297,12 +440,15 @@ describe('Default Processing Database', () => {
sourceKey: 'config',
items: [
{
apiVersion: '1.0.0',
metadata: {
name: 'new-root',
},
kind: 'Location',
} as Entity,
entity: {
apiVersion: '1.0.0',
metadata: {
name: 'new-root',
},
kind: 'Location',
} as Entity,
locationKey: 'file:/tmp/foobar',
},
],
}),
);
@@ -411,12 +557,15 @@ describe('Default Processing Database', () => {
sourceKey: 'config',
items: [
{
apiVersion: '1.0.0',
metadata: {
name: 'new-root',
},
kind: 'Location',
} as Entity,
entity: {
apiVersion: '1.0.0',
metadata: {
name: 'new-root',
},
kind: 'Location',
} as Entity,
locationKey: 'file:/tmp/foobar',
},
],
});
});
@@ -510,12 +659,15 @@ describe('Default Processing Database', () => {
removed: [],
added: [
{
apiVersion: '1.0.0',
metadata: {
name: 'new-root',
},
kind: 'Location',
} as Entity,
entity: {
apiVersion: '1.0.0',
metadata: {
name: 'new-root',
},
kind: 'Location',
} as Entity,
locationKey: 'file:/tmp/foobar',
},
],
});
});
@@ -614,12 +766,15 @@ describe('Default Processing Database', () => {
added: [],
removed: [
{
apiVersion: '1.0.0',
metadata: {
name: 'new-root',
},
kind: 'Location',
} as Entity,
entity: {
apiVersion: '1.0.0',
metadata: {
name: 'new-root',
},
kind: 'Location',
} as Entity,
locationKey: 'file:/tmp/foobar',
},
],
});
});