Skip to content

Commit

Permalink
Add more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
cressie176 committed Jan 7, 2024
1 parent bdc308e commit b3acd12
Show file tree
Hide file tree
Showing 12 changed files with 792 additions and 584 deletions.
10 changes: 1 addition & 9 deletions lib/helpers.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,9 @@
module.exports = {
tableName: (name, version) => `${name.toLowerCase().replace(/\s/g, '_')}_v${version}`,
xkeys: (obj, options) => Object.keys(obj).reduce(toString.bind(options), ''),
xvalues: (obj, options) => Object.values(obj).reduce(toString.bind(options), ''),
eq: (a, b) => a === b,
ne: (a, b) => a !== b,
lt: (a, b) => a < b,
gt: (a, b) => a > b,
lte: (a, b) => a <= b,
gte: (a, b) => a >= b,
and() {
return Array.prototype.every.call(arguments, Boolean);
},
or() {
return Array.prototype.slice.call(arguments, 0, -1).some(Boolean);
}
}

Expand Down
7 changes: 4 additions & 3 deletions lib/marv-rdf-driver.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,12 +57,14 @@ module.exports = (options) => {

function createValidationError(validate) {
let message;
const errors = validate.errors; //.filter((e => e.keyword !== 'oneOf'))
const errors = validate.errors;

const instancePath = errors[0].instancePath || 'migration script';
switch (errors[0].keyword) {
case 'required': {
const missingProperties = errors.map(e => `'${e.params.missingProperty}'`).join(' or ');
const missingProperties = errors.filter(e => e.keyword === 'required')
.map(e => `'${e.params.missingProperty}'`)
.join(' or ');
message = `${instancePath} must have required property ${missingProperties}`;
break;
}
Expand All @@ -76,7 +78,6 @@ module.exports = (options) => {

function decorateMigrationScript(script) {
script.define_entities?.forEach((entity) => {
entity.table_name = entity.name.toLowerCase().replace(/\s/g, '_');
entity.identified_by = entity.fields.filter((field) => {
return entity.identified_by.includes(field.name);
});
Expand Down
127 changes: 70 additions & 57 deletions lib/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,13 @@
"type": "string"
}
},
"identified_by": {
"type": "array",
"minItems": 1,
"items": {
"type": "string"
}
},
"checks": {
"type": "object",
"minProperties": 1,
Expand Down Expand Up @@ -197,67 +204,73 @@
}
},
"changeSetType": {
"type": "object",
"properties": {
"effective_from": {
"type": "string",
"format": "date-time"
},
"notes": {
"type": "string"
},
"frames": {
"type": "array",
"minItems": 1,
"items": {
"type": "object",
"properties": {
"entity": {
"type": "string"
},
"version": {
"type": "integer"
},
"action": {
"type": "string",
"enum": [
"POST",
"DELETE"
]
},
"data": {
"type": "array",
"minItems": 1,
"items": {
"type": "object"
"type": "array",
"minItems": 1,
"items": {
"type": "object",
"properties": {
"effective from": {
"type": "string",
"format": "date-time"
},
"effective_from": {
"type": "string",
"format": "date-time"
},
"notes": {
"type": "string"
},
"frames": {
"type": "array",
"minItems": 1,
"items": {
"type": "object",
"properties": {
"entity": {
"type": "string"
},
"version": {
"type": "integer"
},
"action": {
"type": "string",
"enum": [
"POST",
"DELETE"
]
},
"data": {
"type": "array",
"minItems": 1,
"items": {
"type": "object"
}
}
}
},
},
"required": [
"entity",
"version",
"action",
"data"
]
}
}
},
"oneOf": [
{
"required": [
"entity",
"version",
"action",
"data"
"effective from",
"frames"
]
},
{
"required": [
"effective_from",
"frames"
]
}
}
},
"oneOf": [
{
"required": [
"effective_from",
"notes",
"frames"
]
},
{
"required": [
"effective from",
"notes",
"frames"
]
}
]
]
}
}
},
"required": []
Expand Down
8 changes: 4 additions & 4 deletions lib/template.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ CREATE TYPE {{name}} AS ENUM (
{{#define_entities}}
INSERT INTO rdf_entity (name, version) VALUES ('{{name}}', {{version}});

CREATE TABLE {{table_name}}_v{{version}} (
CREATE TABLE {{tableName name version}} (
rdf_frame_id INTEGER PRIMARY KEY REFERENCES rdf_data_frame (id),
{{#fields}}
{{name}} {{type}},
Expand All @@ -22,7 +22,7 @@ CREATE TABLE {{table_name}}_v{{version}} (
{{/each}}
);

CREATE FUNCTION get_{{table_name}}_v{{version}}_aggregate(
CREATE FUNCTION get_{{tableName name version}}_aggregate(
p_change_set_id INTEGER
) RETURNS TABLE (
{{#fields}}
Expand All @@ -42,7 +42,7 @@ BEGIN
FROM
rdf_data_frame f
INNER JOIN rdf_entity e ON e.id = f.entity_id
INNER JOIN {{table_name}}_v{{version}} x ON x.rdf_frame_id = f.id
INNER JOIN {{tableName name version}} x ON x.rdf_frame_id = f.id
WHERE e.name = '{{name}}' AND e.version = {{version}}
AND f.change_set_id <= p_change_set_id
ORDER BY
Expand Down Expand Up @@ -120,7 +120,7 @@ DO $$
(v_change_set_id, v_entity_id, '{{../action}}')
RETURNING id INTO v_frame_id;

INSERT INTO {{../entity}}_v{{../version}} (rdf_frame_id, {{#xkeys .}}{{item}}{{#unless isLast}}, {{/unless}}{{/xkeys}}) VALUES
INSERT INTO {{tableName ../entity ../version}} (rdf_frame_id, {{#xkeys .}}{{item}}{{#unless isLast}}, {{/unless}}{{/xkeys}}) VALUES
(v_frame_id, {{#xvalues .}}'{{item}}'{{#unless isLast}}, {{/unless}}{{/xvalues}});
{{/data}}

Expand Down
17 changes: 15 additions & 2 deletions test/TestReferenceDataFramework.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const ReferenceDataFramework = require('..');
const noop = () => { };

module.exports = class TestReferenceDataFramework extends ReferenceDataFramework {

Expand All @@ -7,8 +8,8 @@ module.exports = class TestReferenceDataFramework extends ReferenceDataFramework

constructor(config) {
super(config);
this.#nukeCustomObjects = config.nukeCustomObjects;
this.#wipeCustomData = config.wipeCustomData;
this.#nukeCustomObjects = config.nukeCustomObjects || noop;
this.#wipeCustomData = config.wipeCustomData || noop;
}

async reset() {
Expand All @@ -26,6 +27,18 @@ module.exports = class TestReferenceDataFramework extends ReferenceDataFramework
})
}

async nukeCustomObjects() {
return this.withTransaction((tx) => {
return this.#nukeCustomObjects(tx);
})
}

async wipeRdfData() {
return this.withTransaction((tx) => {
return this.#wipeRdfData(tx);
})
}

async #wipeRdfData(tx) {
await tx.query('DELETE FROM rdf_notification');
await tx.query('DELETE FROM rdf_hook');
Expand Down
Loading

0 comments on commit b3acd12

Please sign in to comment.