2016-02-04 01:14:14 +03:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
var jsonSchemaTest = require('json-schema-test')
|
|
|
|
, Promise = require('./promise')
|
|
|
|
, getAjvInstances = require('./ajv_async_instances')
|
2016-10-04 23:16:18 +03:00
|
|
|
, Ajv = require('./ajv')
|
|
|
|
, suite = require('./brower_test_suite')
|
|
|
|
, after = require('./after_test');
|
2016-02-04 01:14:14 +03:00
|
|
|
|
|
|
|
|
|
|
|
var instances = getAjvInstances({ v5: true });
|
|
|
|
|
2016-02-29 01:06:54 +03:00
|
|
|
|
2016-02-04 01:14:14 +03:00
|
|
|
instances.forEach(addAsyncFormatsAndKeywords);
|
|
|
|
|
|
|
|
|
|
|
|
jsonSchemaTest(instances, {
|
|
|
|
description: 'asynchronous schemas tests of ' + instances.length + ' ajv instances with different options',
|
2016-10-04 23:16:18 +03:00
|
|
|
suites: {
|
|
|
|
'async schemas':
|
|
|
|
typeof window == 'object'
|
|
|
|
? suite(require('./async/{**/,}*.json', {mode: 'list'}))
|
|
|
|
: './async/{**/,}*.json'
|
|
|
|
},
|
2016-02-04 01:14:14 +03:00
|
|
|
async: true,
|
|
|
|
assert: require('./chai').assert,
|
|
|
|
Promise: Promise,
|
2016-10-04 23:16:18 +03:00
|
|
|
afterError: after.error,
|
|
|
|
// afterEach: after.each,
|
2016-02-04 01:14:14 +03:00
|
|
|
cwd: __dirname,
|
|
|
|
hideFolder: 'async/',
|
|
|
|
timeout: 90000
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
function addAsyncFormatsAndKeywords (ajv) {
|
|
|
|
ajv.addFormat('english_word', {
|
|
|
|
async: true,
|
|
|
|
validate: checkWordOnServer
|
|
|
|
});
|
|
|
|
|
|
|
|
ajv.addKeyword('idExists', {
|
|
|
|
async: true,
|
|
|
|
type: 'number',
|
2016-02-29 01:06:54 +03:00
|
|
|
validate: checkIdExists,
|
|
|
|
errors: false
|
|
|
|
});
|
|
|
|
|
|
|
|
ajv.addKeyword('idExistsWithError', {
|
|
|
|
async: true,
|
|
|
|
type: 'number',
|
|
|
|
validate: checkIdExistsWithError,
|
|
|
|
errors: true
|
2016-02-04 01:14:14 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
ajv.addKeyword('idExistsCompiled', {
|
|
|
|
async: true,
|
|
|
|
type: 'number',
|
|
|
|
compile: compileCheckIdExists
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function checkWordOnServer(str) {
|
|
|
|
return str == 'tomorrow' ? Promise.resolve(true)
|
|
|
|
: str == 'manana' ? Promise.resolve(false)
|
|
|
|
: Promise.reject(new Error('unknown word'));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function checkIdExists(schema, data) {
|
|
|
|
switch (schema.table) {
|
|
|
|
case 'users': return check([1, 5, 8]);
|
|
|
|
case 'posts': return check([21, 25, 28]);
|
|
|
|
default: throw new Error('no such table');
|
|
|
|
}
|
|
|
|
|
|
|
|
function check(IDs) {
|
|
|
|
return Promise.resolve(IDs.indexOf(data) >= 0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-02-29 01:06:54 +03:00
|
|
|
function checkIdExistsWithError(schema, data) {
|
|
|
|
var table = schema.table;
|
|
|
|
switch (table) {
|
|
|
|
case 'users': return check(table, [1, 5, 8]);
|
|
|
|
case 'posts': return check(table, [21, 25, 28]);
|
|
|
|
default: throw new Error('no such table');
|
|
|
|
}
|
|
|
|
|
2016-07-28 02:50:30 +03:00
|
|
|
function check(_table, IDs) {
|
|
|
|
if (IDs.indexOf(data) >= 0)
|
2016-02-29 01:06:54 +03:00
|
|
|
return Promise.resolve(true);
|
2016-07-28 02:50:30 +03:00
|
|
|
|
|
|
|
var error = {
|
|
|
|
keyword: 'idExistsWithError',
|
|
|
|
message: 'id not found in table ' + _table
|
|
|
|
};
|
|
|
|
return Promise.reject(new Ajv.ValidationError([error]));
|
2016-02-29 01:06:54 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-02-04 01:14:14 +03:00
|
|
|
function compileCheckIdExists(schema) {
|
|
|
|
switch (schema.table) {
|
|
|
|
case 'users': return compileCheck([1, 5, 8]);
|
|
|
|
case 'posts': return compileCheck([21, 25, 28]);
|
|
|
|
default: throw new Error('no such table');
|
|
|
|
}
|
|
|
|
|
|
|
|
function compileCheck(IDs) {
|
|
|
|
return function (data) {
|
|
|
|
return Promise.resolve(IDs.indexOf(data) >= 0);
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|