Skip to content

Commit

Permalink
Added tests
Browse files Browse the repository at this point in the history
  • Loading branch information
saschagehlich committed Nov 6, 2013
1 parent 0a80ab8 commit 1de5db2
Show file tree
Hide file tree
Showing 7 changed files with 200 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
.DS_Store
node_modules
4 changes: 4 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
test:
@./node_modules/mocha/bin/mocha --colors -t 10000 --reporter dot test/*.test.js

.PHONY: test
5 changes: 5 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
function Sequenice(sequelize, options) {

};

module.exports = Sequenice;
34 changes: 34 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
{
"name": "sequenice",
"version": "0.0.1",
"description": "A nice model wrapper for `sequelize`",
"main": "index.js",
"directories": {
"example": "example"
},
"scripts": {
"test": "make test"
},
"repository": {
"type": "git",
"url": "git://github.com/saschagehlich/sequenice.git"
},
"keywords": [
"sequelize",
"model",
"mysql",
"postgres",
"database",
"sql"
],
"author": "Sascha Gehlich <[email protected]>",
"license": "MIT",
"bugs": {
"url": "https://github.com/saschagehlich/sequenice/issues"
},
"devDependencies": {
"sequelize": "~2.0.0-beta.1",
"should": "~2.0.2",
"mocha": "~1.14.0"
}
}
91 changes: 91 additions & 0 deletions test/index.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
var should = require("should")
, Sequelize = require("sequelize")
, Sequenice = require("..")
, sequelize, sequenice
, models = {};

before(function (done) {
sequelize = new Sequelize("sequenice_test", "root", "");
sequenice = new Sequenice(sequelize, {
modelDirectory: __dirname + "/models",
modelAttacher: models,
getterPrefix: "get",
setterPrefix: "set"
});
sequelize.sync({ force: true }).success(function () {
done()
});
});

describe("sequenice example", function () {
/**
* Field definitions
*/
describe("field definitions", function () {
it("creates `User` and `Project` models and attaches them to `models`", function () {
should.exist(models.User);
should.exist(models.Project);
});

it("creates a `name` field", function (done) {
models.User.create({ name: "A Name" }).success(function (user) {
should.exist(user.name);
done();
});
});

it("creates a `isAdmin` field with a default value", function (done) {
models.User.create({ name: "A Name" }).success(function (user) {
user.isAdmin.should.equal(false);
done();
});
});
});

/**
* Associations
*/
describe("associations definitions", function () {
it("creates a hasMany relation from `User` to `Project`", function (done) {
models.User.create({ name: "A Name" }).success(function (user) {
models.Project.create({ name: "Project name" }).success(function (project) {
user.setProjects([project]).success(function () {
done();
});
});
});
});

it("creates a belongsTo relation from `Project` to `User`", function (done) {
models.Project.create({ name: "Project name" }).success(function (project) {
models.User.create({ name: "A Name" }).success(function (user) {
project.setUser(user).success(function () {
done();
});
});
});
});
});

/**
* Hooks
*/
describe("hooks", function () {
it("defines a `beforeCreate` hook", function (done) {
models.User.create().success(function (user) {
user.getDataValue("beforeCreateCalled").should.be.true;
done();
});
});
});

/**
* Getters / setters
*/
it("defines getters and setters", function (done) {
models.User.build({ price: 20 }).priceInCents.should.equal(20 * 100);
models.User.build({ priceInCents: 30 * 100 }).price.should.equal("$" + 30);

done();
});
});
11 changes: 11 additions & 0 deletions test/models/project.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
function Project(s) {
/**
* Field declarations
*/
this.field("name", s.STRING);

/**
* Associations
*/
this.belongsTo("User");
}
54 changes: 54 additions & 0 deletions test/models/user.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
function User(s) {
/**
* Field declarations
*/
this.field("name", s.STRING);
this.field("isAdmin", s.BOOLEAN, { defaultValue: false });

/**
* Associations
*/
this.hasMany("Project");

/**
* Getters
*/
this.get("price");
this.get("priceInCents");

/**
* Setters
*/
this.set("price");

/**
* Hooks
*/
this.beforeCreate("myBeforeCreateMethod");

/**
* Validations
*/
this.validates("myValidationMethod")
}

User.prototype.getPrice = function() {
return "$" + (this.getDataValue('priceInCents') / 100);
};

User.prototype.getPriceInCents = function() {
return this.dataValues.priceInCents;
};

User.prototype.setPrice = function(value) {
this.dataValues.priceInCents = value * 100;
};

User.prototype.myBeforeCreateMethod = function(callback) {
this.dataValues.beforeCreateCalled = true;
callback();
};

User.prototype.myValidationMethod = function (callback) {
callback();
}

0 comments on commit 1de5db2

Please sign in to comment.