Permalink
Browse files

Add tests for the ‘required’ attribute

  • Loading branch information...
1 parent b76eed4 commit 511ddecdbb1a81db2088d5e57ebc01e22b484688 @Baggz committed Mar 15, 2012
Showing with 263 additions and 0 deletions.
  1. +86 −0 tests/json/attributes/required/objects.js
  2. +177 −0 tests/json/attributes/required/required.js
View
86 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);
+
+ });
+
+
+
+});
View
177 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);
+
+ });
+
+});

0 comments on commit 511ddec

Please sign in to comment.