Skip to content

Commit

Permalink
Simple Sequilize validation
Browse files Browse the repository at this point in the history
  • Loading branch information
Aaron Held authored and Aaron Held committed Feb 15, 2018
1 parent 306d622 commit b13b0a6
Show file tree
Hide file tree
Showing 6 changed files with 106 additions and 0 deletions.
1 change: 1 addition & 0 deletions javascript/mocha/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
node_modules
3 changes: 3 additions & 0 deletions javascript/mocha/calculator.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module.exports.add = function(input){
return 0;
}
Binary file added javascript/mocha/database.sqlite
Binary file not shown.
29 changes: 29 additions & 0 deletions javascript/mocha/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
{
"name": "calc",
"version": "1.0.0",
"description": "setup for the calculator kata",
"main": "index.js",
"scripts": {
"test": "mocha",
"testw": "mocha -w"
},
"repository": {
"type": "git",
"url": "git+https://github.com/aheld/code-kata.git"
},
"author": "",
"license": "ISC",
"bugs": {
"url": "https://github.com/aheld/code-kata/issues"
},
"homepage": "https://github.com/aheld/code-kata#readme",
"devDependencies": {
"chai": "^4.1.2",
"mocha": "^5.0.1"
},
"dependencies": {
"eslint": "^4.17.0",
"sequelize": "^4.33.4",
"sqlite3": "^3.1.13"
}
}
12 changes: 12 additions & 0 deletions javascript/mocha/test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
var should = require('chai').should();
var assert = require('chai').assert;

var calc = require('./calculator.js');

describe('Calculator', function() {
describe('#add', function() {
it('should return -1 when the value is not present', function() {
calc.add('').should.equal(0);
});
});
});
61 changes: 61 additions & 0 deletions javascript/mocha/test_model.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
const Sequelize = require('sequelize');
const sequelize = new Sequelize('database', 'username', 'password', {
host: 'localhost',
dialect: 'sqlite',

pool: {
max: 5,
min: 0,
acquire: 30000,
idle: 10000
},

// SQLite only
storage: './database.sqlite'
});

const User = sequelize.define('user', {
firstName: {
type: Sequelize.STRING
},
lastName: {
type: Sequelize.STRING,
allowNull: false,
defaultValue: '',
validate: {len: [2,50] } }
});



var should = require('chai').should();

describe('User', function() {
describe('#create', function() {
it('should validate if valid', function(done) {
User.sync({force: true}).then(() => {
User.create({
firstName: 'John',
lastName: 'Hancock'
});
done();
});
});

it('should throw a validation error if not valid', function(done) {
User.sync({force: true}).then(() => {
User.create({
firstName: 'John',
lastName: ''
}).then(function(x) {
x.should.be.a('Error');
done();
}).catch( function (e) {
e.should.be.a('Error');
done();
});

});
});

});
});

0 comments on commit b13b0a6

Please sign in to comment.