不要怂,就是干,撸起袖子干!

Commit bb8033d9 by Sascha Depold

more buster tests

1 parent 914f1ab5
......@@ -42,114 +42,6 @@ describe('DAOFactory', function() {
beforeEach(function() { setup() })
afterEach(function() { Helpers.dropAllTables() })
describe('create', function() {
it("doesn't allow duplicated records with unique:true", function() {
setup({ username: {type: Sequelize.STRING, unique: true} })
Helpers.async(function(done) {
User.create({ username:'foo' }).success(function() {
User.create({ username: 'foo' }).error(function(err) {
expect(err).toBeDefined()
checkMatchForDialects(err.message, {
sqlite: /.*SQLITE_CONSTRAINT.*/,
mysql: /.*Duplicate\ entry.*/,
postgres: /.*duplicate\ key\ value.*/
})
done()
})
})
})
})
it("raises an error if created object breaks definition contraints", function() {
setup({
username: {type: Sequelize.STRING, unique: true},
smth: {type: Sequelize.STRING, allowNull: false}
})
Helpers.async(function(done) {
User.create({ username: 'foo', smth: null }).error(function(err) {
expect(err).toBeDefined()
checkMatchForDialects(err.message, {
sqlite: /.*SQLITE_CONSTRAINT.*/,
mysql: "Column 'smth' cannot be null",
postgres: /.*column "smth" violates not-null.*/
})
User.create({username: 'foo', smth: 'foo'}).success(function() {
User.create({username: 'foo', smth: 'bar'}).error(function(err) {
expect(err).toBeDefined()
checkMatchForDialects(err.message, {
sqlite: /.*SQLITE_CONSTRAINT.*/,
mysql: "Duplicate entry 'foo' for key 'username'",
postgres: /.*duplicate key value violates unique constraint.*/
})
done()
})
})
})
})
})
it('sets auto increment fields', function() {
setup({
userid: {type: Sequelize.INTEGER, autoIncrement: true, primaryKey: true, allowNull: false}
})
Helpers.async(function(done) {
User.create({}).on('success', function(user) {
expect(user.userid).toEqual(1)
done()
})
})
Helpers.async(function(done) {
User.create({}).on('success', function(user) {
expect(user.userid).toEqual(2)
done()
})
})
})
it('allows the usage of options as attribute', function() {
setup({ name: Sequelize.STRING, options: Sequelize.TEXT })
Helpers.async(function(done) {
var options = JSON.stringify({ foo: 'bar', bar: 'foo' })
User
.create({ name: 'John Doe', options: options })
.success(function(user) {
expect(user.options).toEqual(options)
done()
})
})
})
it('allows sql logging', function() {
setup({
name: {type: Sequelize.STRING, unique: true},
smth: {type: Sequelize.STRING, allowNull: false}
})
Helpers.async(function(done) {
User
.create({ name: 'Fluffy Bunny', smth: 'else' })
.on('sql', function(sql) {
expect(sql).toBeDefined()
expect(sql.toUpperCase().indexOf("INSERT")).toBeGreaterThan(-1)
done()
})
})
})
})
describe('destroy', function() {
it('deletes a record from the database if dao is not paranoid', function() {
Helpers.async(function(done) {
......
......@@ -9,10 +9,10 @@ buster.testRunner.timeout = 500
describe('BelongsTo', function() {
before(function(done) {
var self = this
Helpers.initTests({
beforeComplete: function(sequelize) { self.sequelize = sequelize },
beforeComplete: function(sequelize) {
this.sequelize = sequelize
}.bind(this),
onComplete: done
})
})
......
......@@ -48,5 +48,13 @@ var BusterHelpers = module.exports = {
return fs.readdirSync(__dirname + '/../lib/dialects').filter(function(file) {
return (file.indexOf('.js') === -1)
})
},
checkMatchForDialects: function(dialect, value, expectations) {
if (!!expectations[dialect]) {
expect(value).toMatch(expectations[dialect])
} else {
throw new Error('Undefined expectation for "' + dialect + '"!')
}
}
}
......@@ -94,6 +94,113 @@ dialects.forEach(function(dialect) {
})
describe('create', function() {
it("doesn't allow duplicated records with unique:true", function(done) {
var User = this.sequelize.define('UserWithUniqueUsername', {
username: { type: Sequelize.STRING, unique: true }
})
User.sync({ force: true }).success(function() {
User.create({ username:'foo' }).success(function() {
User.create({ username: 'foo' }).error(function(err) {
expect(err).toBeDefined()
Helpers.checkMatchForDialects(dialect, err.message, {
sqlite: /.*SQLITE_CONSTRAINT.*/,
mysql: /.*Duplicate\ entry.*/,
postgres: /.*duplicate\ key\ value.*/
})
done()
})
})
})
})
it("raises an error if created object breaks definition contraints", function(done) {
var User = this.sequelize.define('UserWithNonNullSmth', {
username: { type: Sequelize.STRING, unique: true },
smth: { type: Sequelize.STRING, allowNull: false }
})
User.sync({ force: true }).success(function() {
User.create({ username: 'foo', smth: null }).error(function(err) {
expect(err).toBeDefined()
Helpers.checkMatchForDialects(dialect, err.message, {
sqlite: /.*SQLITE_CONSTRAINT.*/,
mysql: "Column 'smth' cannot be null",
postgres: /.*column "smth" violates not-null.*/
})
User.create({ username: 'foo', smth: 'foo' }).success(function() {
User.create({ username: 'foo', smth: 'bar' }).error(function(err) {
expect(err).toBeDefined()
Helpers.checkMatchForDialects(dialect, err.message, {
sqlite: /.*SQLITE_CONSTRAINT.*/,
mysql: "Duplicate entry 'foo' for key 'username'",
postgres: /.*duplicate key value violates unique constraint.*/
})
done()
})
})
})
})
})
it('sets auto increment fields', function(done) {
var User = this.sequelize.define('UserWithAutoIncrementField', {
userid: { type: Sequelize.INTEGER, autoIncrement: true, primaryKey: true, allowNull: false }
})
User.sync({ force: true }).success(function() {
User.create({}).on('success', function(user) {
expect(user.userid).toEqual(1)
User.create({}).on('success', function(user) {
expect(user.userid).toEqual(2)
done()
})
})
})
})
it('allows the usage of options as attribute', function(done) {
var User = this.sequelize.define('UserWithNameAndOptions', {
name: Sequelize.STRING,
options: Sequelize.TEXT
})
var options = JSON.stringify({ foo: 'bar', bar: 'foo' })
User.sync({ force: true }).success(function() {
User
.create({ name: 'John Doe', options: options })
.success(function(user) {
expect(user.options).toEqual(options)
done()
})
})
})
it('allows sql logging', function(done) {
var User = this.sequelize.define('UserWithUniqueNameAndNonNullSmth', {
name: {type: Sequelize.STRING, unique: true},
smth: {type: Sequelize.STRING, allowNull: false}
})
User.sync({ force: true }).success(function() {
User
.create({ name: 'Fluffy Bunny', smth: 'else' })
.on('sql', function(sql) {
expect(sql).toBeDefined()
expect(sql.toUpperCase().indexOf("INSERT")).toBeGreaterThan(-1)
done()
})
})
})
it('should only store the values passed in the witelist', function(done) {
var self = this
, data = { username: 'Peter', secretValue: '42' }
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!