bridge-node-server/node_server/schemas/customKeywords/maxdp.spec.js
Martin Donnelly 57bd6c8e6a init
2018-06-24 21:15:03 +01:00

159 lines
4.6 KiB
JavaScript

/**
* @fileOverview Unit tests for the maxDecimalPlaces custom keyword
*/
/* globals describe, beforeEach, it */
'use strict';
var _ = require('lodash');
var validator = require('../validator.js');
var Ajv = require('ajv');
var chai = require('chai');
var chaiAsPromised = require('chai-as-promised');
var expect = chai.expect;
chai.use(chaiAsPromised);
/**
* Set the path prefix to a value that will work for these tests
*/
const SCHEMA_ROOT = '../schemas';
/**
* A const base schema for our testing
*/
const BASE_SCHEMA = {
$id: 'TestMaxDP',
$schema: 'http://json-schema.org/draft-06/schema#',
title: 'Test maxDecimalPlaces custom keyword',
type: 'object',
properties: {
test: {
type: 'number',
maxDecimalPlaces: 8
}
}
};
describe('Schemas: maxDecimalPlaces custom keyword', function() {
/**
* Local copy of the ajv validator so we can insert our own test schemas
*/
var ajv;
/**
* Initialise the validator with the right schema.
* Also set our own injected constructor so we can keep a copy of the validtor
*/
beforeEach(function() {
validator.setDIValidatorConstructor(
function(options) {
ajv = new Ajv(options);
return ajv;
}
);
validator.initialise([], true, SCHEMA_ROOT);
});
describe('with maxDecimalPlaces = 8', function() {
/**
* Add the test schema with 8 decimal places
*/
beforeEach(function() {
ajv.addSchema(BASE_SCHEMA);
});
/**
* Test the keyword works correctly
*/
it('should reject 0 decimal places as a string', function() {
return expect(
validator.validate('TestMaxDP', {test: '0'})
).to.eventually.be.rejected;
});
it('should accept 0 decimal places as a number', function() {
return expect(
validator.validate('TestMaxDP', {test: 0})
).to.eventually.be.fulfilled;
});
it('should accept exactly 8 decimal places as a string', function() {
return expect(
validator.validate('TestMaxDP', {test: '0.12345678'})
).to.eventually.be.rejected;
});
it('should accept exactly 8 decimal places as a number', function() {
return expect(
validator.validate('TestMaxDP', {test: 0.12345678})
).to.eventually.be.fulfilled;
});
it('should reject >8 decimal places as a string', function() {
return expect(
validator.validate('TestMaxDP', {test: '0.123456789'})
).to.eventually.be.rejected;
});
it('should reject >8 decimal places as a number', function() {
return expect(
validator.validate('TestMaxDP', {test: 0.123456789})
).to.eventually.be.rejected;
});
});
describe('with maxDecimalPlaces = 4', function() {
/**
* Add the test schema with 8 decimal places
*/
beforeEach(function() {
var schema4DP = _.clone(BASE_SCHEMA);
schema4DP.$id = 'Test4DP';
schema4DP.properties.test.maxDecimalPlaces = 4;
ajv.addSchema(schema4DP);
});
/**
* Test the keyword works correctly
*/
it('should reject 0 decimal places as a string', function() {
return expect(
validator.validate('Test4DP', {test: '0'})
).to.eventually.be.rejected;
});
it('should accept 0 decimal places as a number', function() {
return expect(
validator.validate('Test4DP', {test: 0})
).to.eventually.be.fulfilled;
});
it('should reject exactly 4 decimal places as a string', function() {
return expect(
validator.validate('Test4DP', {test: '0.1234'})
).to.eventually.be.rejected;
});
it('should accept exactly 4 decimal places as a number', function() {
return expect(
validator.validate('Test4DP', {test: 0.1234})
).to.eventually.be.fulfilled;
});
it('should reject >4 decimal places as a string', function() {
return expect(
validator.validate('Test4DP', {test: '0.12345'})
).to.eventually.be.rejected;
});
it('should reject >4 decimal places as a number', function() {
return expect(
validator.validate('Test4DP', {test: 0.12345})
).to.eventually.be.rejected;
});
});
});