Last active
November 12, 2015 11:52
-
-
Save sigriston/336e1a7878143aa74415 to your computer and use it in GitHub Desktop.
Sequelize addAssociation() tests
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
var Sequelize = require('sequelize'); | |
var sequelize = new Sequelize(null, null, null, { | |
dialect: 'sqlite' | |
}); | |
var User = sequelize.define('User', { | |
username: Sequelize.STRING, | |
birthday: Sequelize.DATE | |
}); | |
var Project = sequelize.define('Project', { | |
name: Sequelize.STRING | |
}); | |
Project.hasMany(User, { as: 'Workers' }); | |
var BusLine = sequelize.define('BusLine', { | |
linecode: Sequelize.STRING | |
}); | |
var BusStop = sequelize.define('BusStop', { | |
street: Sequelize.STRING | |
}); | |
BusLine.belongsToMany(BusStop, { through: 'BusLineStop' }); | |
BusStop.belongsToMany(BusLine, { through: 'BusLineStop' }); | |
module.exports = sequelize; |
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
{ | |
"name": "sql-gist", | |
"version": "1.0.0", | |
"description": "Sequelize addAssociation() tests", | |
"main": "index.js", | |
"scripts": { | |
"test": "mocha test.js" | |
}, | |
"author": "Thiago Sigrist <sigrist@gmail.com>", | |
"license": "MIT", | |
"dependencies": { | |
"chai": "3.4.1", | |
"mocha": "2.3.3", | |
"sequelize": "3.13.0", | |
"sqlite3": "3.1.1" | |
} | |
} |
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
var chai = require('chai'); | |
var expect = chai.expect; | |
var sequelize = require('./index'); | |
describe('Sequelize addAssociation tests', function() { | |
describe('#hasMany tests', function () { | |
before(function() { | |
return sequelize.sync() | |
.then(function() { | |
return sequelize.models.Project.create({ | |
name: 'ProjectX' | |
}); | |
}) | |
.then(function(projectX) { | |
return sequelize.models.User.create({ | |
username: 'janedoe', | |
birthday: new Date(1980, 6, 20) | |
}).then(function(jane) { | |
return projectX.addWorker(jane); | |
}); | |
}); | |
}); | |
it('should not have addWorker(null) remove jane', function() { | |
return sequelize.models.Project.findOne() | |
.then(function(projectX) { | |
return projectX.addWorker(null) | |
.then(function() { | |
return projectX; | |
}); | |
}) | |
.then(function(projectX) { | |
return projectX.getWorkers(); | |
}) | |
.then(function(workers) { | |
return expect(workers).to.not.be.null; | |
}); | |
}); | |
it('should not have addWorkers(null) remove jane', function() { | |
return sequelize.models.Project.findOne() | |
.then(function(projectX) { | |
return projectX.addWorkers(null) | |
.then(function() { | |
return projectX; | |
}); | |
}) | |
.then(function(projectX) { | |
return projectX.getWorkers(); | |
}) | |
.then(function(workers) { | |
return expect(workers).to.not.be.null; | |
}); | |
}); | |
}); | |
describe('#belongsToMany tests', function () { | |
before(function() { | |
return sequelize.sync() | |
.then(function() { | |
return sequelize.models.BusLine.create({ | |
linecode: '545' | |
}); | |
}) | |
.then(function(bus) { | |
return sequelize.models.BusStop.create({ | |
street: '5th Ave.' | |
}).then(function(bstop) { | |
return bus.addBusStop(bstop); | |
}); | |
}); | |
}); | |
it('should not have addBusStop(null) remove bus stops', function() { | |
return sequelize.models.BusLine.findOne() | |
.then(function(bus) { | |
return bus.addBusStop(null) | |
.then(function() { | |
return bus; | |
}); | |
}) | |
.then(function(bus) { | |
return bus.getBusStops(); | |
}) | |
.then(function(bstops) { | |
return expect(bstops).to.not.be.null; | |
}); | |
}); | |
it('should not have addBusStops(null) remove bus stops', function() { | |
return sequelize.models.BusLine.findOne() | |
.then(function(bus) { | |
return bus.addBusStops(null) | |
.then(function() { | |
return bus; | |
}); | |
}) | |
.then(function(bus) { | |
return bus.getBusStops(); | |
}) | |
.then(function(bstops) { | |
return expect(bstops).to.not.be.null; | |
}); | |
}); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment