Skip to content

Commit

Permalink
Add tests for metaschema context support
Browse files Browse the repository at this point in the history
  • Loading branch information
o-rumiantsev committed May 24, 2019
1 parent 0ed4347 commit 5710c76
Show file tree
Hide file tree
Showing 13 changed files with 116 additions and 0 deletions.
3 changes: 3 additions & 0 deletions test/fixtures/context/duplicate/context.context
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
({
Nomen: { domain: 'Nomen' },
});
3 changes: 3 additions & 0 deletions test/fixtures/context/duplicate/custom.domains
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
({
Nomen: { type: 'string' },
});
3 changes: 3 additions & 0 deletions test/fixtures/context/duplicate/duplicateDomain.context
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
({
Nomen: { domain: 'Nomen' },
});
3 changes: 3 additions & 0 deletions test/fixtures/context/unresolved/context.context
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
({
Nomen: { domain: 'Nomen' },
});
4 changes: 4 additions & 0 deletions test/fixtures/context/validation/invalid/context.context
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
({
MissingProp: {},
UnresolvedProp: { category: '__SOME_CATEGORY__' },
});
3 changes: 3 additions & 0 deletions test/fixtures/context/validation/system/context.category
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
({
[Symbol.for('additionallyAllowedProperties')]: ['field'],
});
3 changes: 3 additions & 0 deletions test/fixtures/context/validation/system/domain.category
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
({
type: { domain: 'type' },
});
3 changes: 3 additions & 0 deletions test/fixtures/context/validation/system/domains.category
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
({
[Symbol.for('additionallyAllowedProperties')]: ['domain'],
});
4 changes: 4 additions & 0 deletions test/fixtures/context/validation/system/field.category
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
({
domain: { domain: 'string', required: true },
required: { domain: 'boolean' },
});
8 changes: 8 additions & 0 deletions test/fixtures/context/validation/system/system.domains
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
({
string: { type: 'string' },
boolean: { type: 'boolean' },
type: Enum(
'string',
'boolean',
),
});
3 changes: 3 additions & 0 deletions test/fixtures/context/validation/valid/context.context
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
({
Nomen: { domain: 'Nomen', required: true },
});
3 changes: 3 additions & 0 deletions test/fixtures/context/validation/valid/custom.domains
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
({
Nomen: { type: 'string' },
});
73 changes: 73 additions & 0 deletions test/ms.context.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
'use strict';

const metaschema = require('metaschema');
const metatests = require('metatests');

const { default: defaultConfig } = metaschema;
const { options, config } = require('../lib/metaschema-config/config');

const {
MetaschemaError,
ValidationError,
SchemaValidationError,
} = metaschema.errors;

metatests.test('metaschema context error on duplicated domains', async test => {
await test.rejects(
metaschema.fs.load('test/fixtures/context/duplicate', options, config),
new MetaschemaError([
new SchemaValidationError('duplicate', 'duplicateDomain', {
type: 'domain',
value: 'Nomen',
}),
])
);
});

metatests.test('metaschema context error on unresolved domain', async test => {
await test.rejects(
metaschema.fs.load('test/fixtures/context/unresolved', options, config),
new MetaschemaError([
new SchemaValidationError('unresolved', 'context.Nomen', {
type: 'domain',
value: 'Nomen',
}),
])
);
});

metatests.test('metaschema context validation', async test => {
const schemaConfig = await metaschema.fs.applySystemConfig(
'test/fixtures/context/validation/system',
defaultConfig,
{ options, config }
);

await test.resolves(async () => {
await metaschema.fs.load(
'test/fixtures/context/validation/valid',
schemaConfig.options,
schemaConfig.config
);
});

await test.rejects(
metaschema.fs.load(
'test/fixtures/context/validation/invalid',
schemaConfig.options,
schemaConfig.config
),
new MetaschemaError([
new ValidationError('notAllowedAdditionalProp', 'context.MissingProp', {
allowed: ['field'],
}),
new ValidationError(
'notAllowedAdditionalProp',
'context.UnresolvedProp',
{
allowed: ['field'],
}
),
])
);
});

0 comments on commit 5710c76

Please sign in to comment.