Skip to content

Commit

Permalink
Optimize projection func of kinds.js module. Add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Pavel committed Sep 16, 2023
1 parent eeac141 commit 13d967c
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 8 deletions.
19 changes: 11 additions & 8 deletions lib/kinds.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,19 @@ const STORE = ['persistent', 'memory'];
const ALLOW = ['write', 'append', 'read'];

const projection = (kind, meta, root) => {
const { scope = 'local', store = 'memory', allow = 'write' } = meta;
const metadata = { ...meta, kind, scope, store, allow };
const { schema, fields } = meta;
if (!schema && !fields) throw new Error('Invalid Projection');
metadata.parent = schema;
const parent = root.findReference(schema);
const { schema: schemaName, fields: fieldNames } = meta;
if (!schemaName) throw new Error('Invalid Projection: "schema" expected');
if (!Array.isArray(fieldNames) || fieldNames.length === 0)
throw new Error('Invalid Projection: non-empty "fields" array expected');
if (!root || typeof root.findReference !== 'function')
throw new Error('Invalid Projection: "root" should satisfy Schema API');
const defs = {};
for (const key of fields) {
defs[key] = parent.fields[key];
const referencedFields = root.findReference(schemaName)?.fields;
if (referencedFields) {
for (const name of fieldNames) defs[name] = referencedFields[name];
}
const { scope = 'local', store = 'memory', allow = 'write', ...rest } = meta;
const metadata = { kind, scope, store, allow, parent: schemaName, ...rest };
return { defs, metadata };
};

Expand Down
81 changes: 81 additions & 0 deletions test/kinds.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
'use strict';

const metatests = require('metatests');
const { getKindMetadata } = require('../lib/kinds.js');

metatests.test('Kinds: Projection', (test) => {
const getProjectionMeta = getKindMetadata.bind(null, 'projection');
{
const meta = {
schema: 'Account',
fields: ['login', 'password'],
etc: ['another', 'field'],
};
test.throws(
() => getProjectionMeta({}, null),
new Error('Invalid Projection: "schema" expected'),
);
test.throws(
() => getProjectionMeta({ schema: 'Account' }, null),
new Error('Invalid Projection: non-empty "fields" array expected'),
);
test.throws(
() => getProjectionMeta(meta, null),
new Error('Invalid Projection: "root" should satisfy Schema API'),
);
test.doesNotThrow(() =>
getProjectionMeta(meta, { findReference: new Function() }),
);
}

{
const meta = {
schema: 'Account',
fields: ['login', 'password'],
etc: ['another', 'field'],
};
const { defs, metadata } = getProjectionMeta(meta, {
findReference: new Function(),
});
test.strictEqual(metadata.kind, 'projection');
test.strictEqual(metadata.scope, 'local');
test.strictEqual(metadata.store, 'memory');
test.strictEqual(metadata.allow, 'write');
test.strictEqual(metadata.parent, 'Account');
test.strictEqual(metadata.fields, ['login', 'password']);
test.strictEqual(Object.keys(defs).length, 0);
}

{
const meta = {
scope: 'system',
store: 'persistent',
allow: 'append',
schema: 'Account',
fields: ['login', 'password', 'otp'],
};
const root = {
findReference: (schemaName) => ({
name: schemaName,
fields: {
login: 'any',
password: 'string',
otpNotMentioned: 'here',
},
}),
};
const { defs, metadata } = getProjectionMeta(meta, root);
test.strictEqual(metadata.kind, 'projection');
test.strictEqual(metadata.scope, 'system');
test.strictEqual(metadata.store, 'persistent');
test.strictEqual(metadata.allow, 'append');
test.strictEqual(metadata.parent, 'Account');
test.strictEqual(metadata.fields, ['login', 'password', 'otp']);
test.strictEqual(Object.keys(defs).length, 3);
test.strictEqual(defs.login, 'any');
test.strictEqual(defs.password, 'string');
test.strictEqual(defs.otp, undefined);
}

test.end();
});

0 comments on commit 13d967c

Please sign in to comment.