forked from pgte/alfred
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
172 additions
and
6 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
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
Empty file.
File renamed without changes.
Empty file.
This file was deleted.
Oops, something went wrong.
Empty file.
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,58 @@ | ||
// Test doc.inspect() | ||
|
||
var assert = require('assert') | ||
, fs = require('fs') | ||
, util = require('util'); | ||
|
||
var DB_PATH = __dirname + '/../../tmp/db'; | ||
|
||
var USER = {name: 'Pedro', age: 35, sex: 'm'}; | ||
var EXPECTED_INSPECT = "{ name: 'Pedro',\n\ | ||
age: 35,\n\ | ||
sex: 'm',\n\ | ||
id: '<<<ID>>>' }"; | ||
|
||
module.exports.setup = function(next) { | ||
fs.readdirSync(DB_PATH).forEach(function(dir) { | ||
fs.unlinkSync(DB_PATH + '/' + dir); | ||
}); | ||
next(); | ||
}; | ||
|
||
module.exports.run = function(next) { | ||
var alfred = require('../../lib/alfred'); | ||
|
||
var timeout = setTimeout(function() { | ||
throw new Error('timeout'); | ||
}, 5000); | ||
|
||
alfred.open(DB_PATH, function(err, db) { | ||
if (err) { next(err); return; } | ||
|
||
db.on('error', function(err) { | ||
next(err); | ||
}); | ||
|
||
var User = db.define('User'); | ||
User.property('name'); | ||
User.property('age', Number); | ||
User.property('sex', 'string', { | ||
required: true, | ||
minimum: 1, | ||
maximum: 1 | ||
}); | ||
|
||
var user = User.new(USER); | ||
user.save(function(errors) { | ||
if (errors) { next(new Error('validation errors: ' + util.inspect(errors))); return; } | ||
User.get(user.id, function(gotUser) { | ||
assert.ok(user.equal(gotUser), 'user ('+user.toString()+') and gotten user ('+gotUser.toString()+') are different'); | ||
EXPECTED_INSPECT = EXPECTED_INSPECT.replace('<<<ID>>>', user.id); | ||
assert.equal(gotUser.inspect(), EXPECTED_INSPECT); | ||
clearTimeout(timeout); | ||
db.close(next); | ||
}); | ||
}); | ||
|
||
}); | ||
}; |
File renamed without changes.
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,101 @@ | ||
var assert = require('assert') | ||
, fs = require('fs') | ||
, util = require('util'); | ||
|
||
var DB_PATH = __dirname + '/../../tmp/db'; | ||
|
||
var WRONG_USER = {name: 'Pedro Gonçalves Teixeira', sex: 'k', address: 'a', married: 1, email: 'pupup@gugu'}; | ||
var EXPECTED_ERRORS = [ | ||
{ attribute: 'maxLength', | ||
property: 'name', | ||
expected: 10, | ||
actual: 24, | ||
message: 'is too long' }, | ||
{ attribute: 'minLength', | ||
property: 'address', | ||
expected: 3, | ||
actual: 1, | ||
message: 'is too short' }, | ||
{ attribute: 'optional', | ||
property: 'age', | ||
expected: undefined, | ||
actual: true, | ||
message: 'is not optional' }, | ||
{ attribute: 'type', | ||
property: 'age', | ||
expected: 'number', | ||
actual: 'undefined', | ||
message: 'is of the wrong type' }, | ||
{ attribute: 'enum', | ||
property: 'sex', | ||
expected: [ 'f', 'm' ], | ||
actual: 'k', | ||
message: 'is invalid' }, | ||
{ attribute: 'type', | ||
property: 'married', | ||
expected: 'boolean', | ||
actual: 'number', | ||
message: 'is of the wrong type' }, | ||
{ attribute: 'pattern', | ||
property: 'email', | ||
expected: /^([a-zA-Z0-9_\.-])+@(([a-zA-Z0-9-])+\.)+[a-zA-Z0-9]{2,4}/, | ||
actual: 'pupup@gugu', | ||
message: 'IINNVVAALLIID' } | ||
]; | ||
|
||
var RIGHT_USER = {name: 'Pedro', age: 30, sex: 'm', address: 'Estrada 1', email: 'pupup@gugu.co.uk', married: true}; | ||
|
||
module.exports.setup = function(next) { | ||
fs.readdirSync(DB_PATH).forEach(function(dir) { | ||
fs.unlinkSync(DB_PATH + '/' + dir); | ||
}); | ||
next(); | ||
}; | ||
|
||
module.exports.run = function(next) { | ||
var alfred = require('../../lib/alfred'); | ||
|
||
var timeout = setTimeout(function() { | ||
throw new Error('timeout'); | ||
}, 5000); | ||
|
||
alfred.open(DB_PATH, function(err, db) { | ||
if (err) { next(err); return; } | ||
|
||
db.on('error', function(err) { | ||
next(err); | ||
}); | ||
|
||
var User = db.define('User'); | ||
User.property('name', 'string', { | ||
minLength: 3, | ||
maxLength: 10 | ||
}); | ||
User.property('address', 'string', { | ||
minLength: 3, | ||
maxLength: 10 | ||
}); | ||
User.property('age', Number, { required: true }); | ||
User.property('sex', 'string', { | ||
enum: ['f', 'm'], | ||
required: true, | ||
}); | ||
User.property('married', 'boolean'); | ||
User.property('email', 'string', { | ||
pattern: /^([a-zA-Z0-9_\.-])+@(([a-zA-Z0-9-])+\.)+[a-zA-Z0-9]{2,4}/, | ||
messages: {pattern: 'IINNVVAALLIID'} | ||
}); | ||
|
||
var user = User.new(WRONG_USER); | ||
user.save(function(errors) { | ||
assert.ok(errors !== null, 'errors is null'); | ||
assert.deepEqual(errors, EXPECTED_ERRORS); | ||
var rightUser = User.new(RIGHT_USER); | ||
rightUser.save(function(errors) { | ||
assert.ok(errors === null, util.inspect(errors) + " is not null"); | ||
db.close(next); | ||
}); | ||
}); | ||
|
||
}); | ||
}; |