From 511ddecdbb1a81db2088d5e57ebc01e22b484688 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Frantis=CC=8Cek=20Ha=CC=81ba?= Date: Thu, 15 Mar 2012 21:46:53 +0100 Subject: [PATCH] =?UTF-8?q?Add=20tests=20for=20the=20=E2=80=98required?= =?UTF-8?q?=E2=80=99=20attribute?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- tests/json/attributes/required/objects.js | 86 ++++++++++ tests/json/attributes/required/required.js | 177 +++++++++++++++++++++ 2 files changed, 263 insertions(+) create mode 100644 tests/json/attributes/required/objects.js create mode 100644 tests/json/attributes/required/required.js diff --git a/tests/json/attributes/required/objects.js b/tests/json/attributes/required/objects.js new file mode 100644 index 0000000..dfe1ba7 --- /dev/null +++ b/tests/json/attributes/required/objects.js @@ -0,0 +1,86 @@ +if (typeof module !== 'undefined' && module.exports) { + var expect = require('expect.js'); + var amanda = require('../../../../releases/latest/amanda.js'); +} + +/** + * Required + * -------------------- + */ +suite('JSON/Attribute/required#objects', function() { + + /** + * Schema + */ + var schema = { + type: 'object', + properties: { + user: { + type: 'object', + properties: { + name: { + required: true + }, + surname: { + required: true + } + } + } + } + }; + + /** + * Validator + */ + var jsonSchemaValidator = amanda('json'); + + test('should not return an error', function() { + + var count = 0; + + jsonSchemaValidator.validate({ + user: { + name: 'František', + surname: 'Hába' + } + }, schema, function(error) { + count += 1; + expect(error).to.not.be.ok(); + }); + + expect(count).to.be.eql(1); + + }); + + test('should return an error', function() { + + var count = 0; + + jsonSchemaValidator.validate({}, schema, function(error) { + count += 1; + expect(error).to.be.ok(); + }); + + jsonSchemaValidator.validate({ + user: {} + }, schema, function(error) { + count += 1; + expect(error).to.be.ok(); + }); + + jsonSchemaValidator.validate({ + user: { + name: 'František' + } + }, schema, function(error) { + count += 1; + expect(error).to.be.ok(); + }); + + expect(count).to.be.eql(3); + + }); + + + +}); \ No newline at end of file diff --git a/tests/json/attributes/required/required.js b/tests/json/attributes/required/required.js new file mode 100644 index 0000000..1ab803e --- /dev/null +++ b/tests/json/attributes/required/required.js @@ -0,0 +1,177 @@ +if (typeof module !== 'undefined' && module.exports) { + var expect = require('expect.js'); + var amanda = require('../../../../releases/latest/amanda.js'); +} + +/** + * Required + * -------------------- + */ +suite('JSON/Attribute/required#string', function() { + + /** + * Schema + */ + var schema = { + required: true, + type: 'string' + }; + + /** + * Validator + */ + var jsonSchemaValidator = amanda('json'); + + test('should not return an error', function() { + + var count = 0; + + jsonSchemaValidator.validate('Hello', schema, function(error) { + count += 1; + expect(error).to.not.be.ok(); + }); + + expect(count).to.be.eql(1); + + }); + + test('should return an error', function() { + + var count = 0; + + jsonSchemaValidator.validate(null, schema, function(error) { + count += 1; + expect(error).to.be.ok(); + }); + + jsonSchemaValidator.validate(undefined, schema, function(error) { + count += 1; + expect(error).to.be.ok(); + }); + + jsonSchemaValidator.validate('', schema, function(error) { + count += 1; + expect(error).to.be.ok(); + }); + + expect(count).to.be.eql(3); + + }); + +}); + +/** + * Required + * -------------------- + */ +suite('JSON/Attribute/required#array', function() { + + /** + * Schema + */ + var schema = { + required: true, + type: 'array' + }; + + /** + * Validator + */ + var jsonSchemaValidator = amanda('json'); + + test('should not return an error', function() { + + var count = 0; + + jsonSchemaValidator.validate([1, 2, 3], schema, function(error) { + count += 1; + expect(error).to.not.be.ok(); + }); + + expect(count).to.be.eql(1); + + }); + + test('should return an error', function() { + + var count = 0; + + jsonSchemaValidator.validate(null, schema, function(error) { + count += 1; + expect(error).to.be.ok(); + }); + + jsonSchemaValidator.validate(undefined, schema, function(error) { + count += 1; + expect(error).to.be.ok(); + }); + + jsonSchemaValidator.validate([], schema, function(error) { + count += 1; + expect(error).to.be.ok(); + }); + + expect(count).to.be.eql(3); + + }); + +}); + +/** + * Required + * -------------------- + */ +suite('JSON/Attribute/required#object', function() { + + /** + * Schema + */ + var schema = { + required: true, + type: 'object' + }; + + /** + * Validator + */ + var jsonSchemaValidator = amanda('json'); + + test('should not return an error', function() { + + var count = 0; + + jsonSchemaValidator.validate({ + foo: 'bar' + }, schema, function(error) { + count += 1; + expect(error).to.not.be.ok(); + }); + + expect(count).to.be.eql(1); + + }); + + test('should return an error', function() { + + var count = 0; + + jsonSchemaValidator.validate(null, schema, function(error) { + count += 1; + expect(error).to.be.ok(); + }); + + jsonSchemaValidator.validate(undefined, schema, function(error) { + count += 1; + expect(error).to.be.ok(); + }); + + jsonSchemaValidator.validate({}, schema, function(error) { + count += 1; + expect(error).to.be.ok(); + }); + + expect(count).to.be.eql(3); + + }); + +}); \ No newline at end of file