-
-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Optimize projection func of kinds.js module. Add tests
- Loading branch information
Pavel
committed
Sep 16, 2023
1 parent
eeac141
commit 13d967c
Showing
2 changed files
with
92 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
}); |