Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add tests for the ‘required’ attribute
- Loading branch information
František Hába
committed
Mar 15, 2012
1 parent
b76eed4
commit 511ddec
Showing
2 changed files
with
263 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
|
||
}); | ||
|
||
|
||
|
||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
|
||
}); | ||
|
||
}); |