Skip to content

Commit

Permalink
Add tests for the ‘required’ attribute
Browse files Browse the repository at this point in the history
  • Loading branch information
František Hába committed Mar 15, 2012
1 parent b76eed4 commit 511ddec
Show file tree
Hide file tree
Showing 2 changed files with 263 additions and 0 deletions.
86 changes: 86 additions & 0 deletions 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);

});



});
177 changes: 177 additions & 0 deletions 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.