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

Commit 99ea44d8 by Ruben Bridgewater

Refactor .on('sql', fn) to use logging instead

1 parent cc439041
...@@ -870,9 +870,11 @@ describe(Support.getTestDialectTeaser('BelongsToMany'), function() { ...@@ -870,9 +870,11 @@ describe(Support.getTestDialectTeaser('BelongsToMany'), function() {
this.Task.create({ id: 12, title: 'task1' }), this.Task.create({ id: 12, title: 'task1' }),
this.Task.create({ id: 15, title: 'task2' }) this.Task.create({ id: 15, title: 'task2' })
]).spread(function(user, task1, task2) { ]).spread(function(user, task1, task2) {
return user.setTasks([task1, task2]).on('sql', spy); return user.setTasks([task1, task2], {
logging: spy
});
}).then(function() { }).then(function() {
expect(spy.calledTwice).to.be.ok; // Once for SELECT, once for INSERT expect(spy.calledOnce).to.be.ok;
}); });
}); });
...@@ -886,9 +888,11 @@ describe(Support.getTestDialectTeaser('BelongsToMany'), function() { ...@@ -886,9 +888,11 @@ describe(Support.getTestDialectTeaser('BelongsToMany'), function() {
]).spread(function(user, task1, task2) { ]).spread(function(user, task1, task2) {
return user.setTasks([task1, task2]).return (user); return user.setTasks([task1, task2]).return (user);
}).then(function(user) { }).then(function(user) {
return user.setTasks(null).on('sql', spy); return user.setTasks(null, {
logging: spy
});
}).then(function() { }).then(function() {
expect(spy.calledTwice).to.be.ok; // Once for SELECT, once for DELETE expect(spy.calledOnce).to.be.ok;
}); });
}); });
}); // end optimization using bulk create, destroy and update }); // end optimization using bulk create, destroy and update
...@@ -1099,6 +1103,7 @@ describe(Support.getTestDialectTeaser('BelongsToMany'), function() { ...@@ -1099,6 +1103,7 @@ describe(Support.getTestDialectTeaser('BelongsToMany'), function() {
it('should correctly get associations when doubly linked', function() { it('should correctly get associations when doubly linked', function() {
var self = this; var self = this;
var spy = sinon.spy();
return this.sequelize.sync({force: true}).then(function() { return this.sequelize.sync({force: true}).then(function() {
return Promise.all([ return Promise.all([
self.User.create({name: 'Matt'}), self.User.create({name: 'Matt'}),
...@@ -1114,10 +1119,11 @@ describe(Support.getTestDialectTeaser('BelongsToMany'), function() { ...@@ -1114,10 +1119,11 @@ describe(Support.getTestDialectTeaser('BelongsToMany'), function() {
var project = projects[0]; var project = projects[0];
expect(project).to.be.ok; expect(project).to.be.ok;
return self.user.removeProject(project).on('sql', function(sql) { return self.user.removeProject(project, {
// TODO: rewrite this to use logging and check the generated sql logging: spy
}).return (project); }).return (project);
}).then(function(project) { }).then(function(project) {
expect(spy.calledTwice).to.be.ok; // Once for SELECT, once for REMOVE
return self.user.setProjects([project]); return self.user.setProjects([project]);
}); });
}); });
......
...@@ -108,6 +108,7 @@ describe(Support.getTestDialectTeaser('Self'), function() { ...@@ -108,6 +108,7 @@ describe(Support.getTestDialectTeaser('Self'), function() {
expect(foreignIdentifiers).to.have.members(['preexisting_parent', 'preexisting_child']); expect(foreignIdentifiers).to.have.members(['preexisting_parent', 'preexisting_child']);
expect(rawAttributes).to.have.members(['preexisting_parent', 'preexisting_child']); expect(rawAttributes).to.have.members(['preexisting_parent', 'preexisting_child']);
var count = 0;
return this.sequelize.sync({ force: true }).bind(this).then(function() { return this.sequelize.sync({ force: true }).bind(this).then(function() {
return Promise.all([ return Promise.all([
Person.create({ name: 'Mary' }), Person.create({ name: 'Mary' }),
...@@ -118,26 +119,36 @@ describe(Support.getTestDialectTeaser('Self'), function() { ...@@ -118,26 +119,36 @@ describe(Support.getTestDialectTeaser('Self'), function() {
this.mary = mary; this.mary = mary;
this.chris = chris; this.chris = chris;
this.john = john; this.john = john;
return mary.setParents([john]).on('sql', function(sql) { return mary.setParents([john], {
if (sql.match(/INSERT/)) { logging: function(sql) {
expect(sql).to.have.string('preexisting_child'); if (sql.match(/INSERT/)) {
expect(sql).to.have.string('preexisting_parent'); count++;
expect(sql).to.have.string('preexisting_child');
expect(sql).to.have.string('preexisting_parent');
}
} }
}); });
}).then(function() { }).then(function() {
return this.mary.addParent(this.chris).on('sql', function(sql) { return this.mary.addParent(this.chris, {
if (sql.match(/INSERT/)) { logging: function(sql) {
expect(sql).to.have.string('preexisting_child'); if (sql.match(/INSERT/)) {
expect(sql).to.have.string('preexisting_parent'); count++;
expect(sql).to.have.string('preexisting_child');
expect(sql).to.have.string('preexisting_parent');
}
} }
}); });
}).then(function() { }).then(function() {
return this.john.getChildren().on('sql', function(sql) { return this.john.getChildren(undefined, {
var whereClause = sql.split('FROM')[1]; // look only in the whereClause logging: function(sql) {
expect(whereClause).to.have.string('preexisting_child'); count++;
expect(whereClause).to.have.string('preexisting_parent'); var whereClause = sql.split('FROM')[1]; // look only in the whereClause
expect(whereClause).to.have.string('preexisting_child');
expect(whereClause).to.have.string('preexisting_parent');
}
}); });
}).then(function(children) { }).then(function(children) {
expect(count).to.be.equal(3);
expect(_.map(children, 'id')).to.have.members([this.mary.id]); expect(_.map(children, 'id')).to.have.members([this.mary.id]);
}); });
}); });
......
...@@ -39,8 +39,10 @@ if (dialect.match(/^postgres/)) { ...@@ -39,8 +39,10 @@ if (dialect.match(/^postgres/)) {
}); });
it('should be able to search within an array', function() { it('should be able to search within an array', function() {
return this.User.findAll({where: {email: ['hello', 'world']}, attributes: ['id','username','email','settings','document','phones','emergency_contact','friends']}).on('sql', function(sql) { return this.User.findAll({where: {email: ['hello', 'world']}, attributes: ['id','username','email','settings','document','phones','emergency_contact','friends']}, {
expect(sql).to.equal('SELECT "id", "username", "email", "settings", "document", "phones", "emergency_contact", "friends" FROM "Users" AS "User" WHERE "User"."email" = ARRAY[\'hello\',\'world\']::TEXT[];'); logging: function (sql) {
expect(sql).to.equal('Executing (default): SELECT "id", "username", "email", "settings", "document", "phones", "emergency_contact", "friends" FROM "Users" AS "User" WHERE "User"."email" = ARRAY[\'hello\',\'world\']::TEXT[];');
}
}); });
}); });
...@@ -95,10 +97,11 @@ if (dialect.match(/^postgres/)) { ...@@ -95,10 +97,11 @@ if (dialect.match(/^postgres/)) {
username: 'bob', username: 'bob',
emergency_contact: { name: 'joe', phones: [1337, 42] } emergency_contact: { name: 'joe', phones: [1337, 42] }
}, { }, {
fields: ['id', 'username', 'document', 'emergency_contact'] fields: ['id', 'username', 'document', 'emergency_contact'],
}).on('sql', function(sql) { logging: function(sql) {
var expected = '\'{"name":"joe","phones":[1337,42]}\''; var expected = '\'{"name":"joe","phones":[1337,42]}\'';
expect(sql.indexOf(expected)).not.to.equal(-1); expect(sql.indexOf(expected)).not.to.equal(-1);
}
}); });
}); });
...@@ -294,9 +297,11 @@ if (dialect.match(/^postgres/)) { ...@@ -294,9 +297,11 @@ if (dialect.match(/^postgres/)) {
username: 'bob', username: 'bob',
email: ['myemail@email.com'], email: ['myemail@email.com'],
settings: {mailing: false, push: 'facebook', frequency: 3} settings: {mailing: false, push: 'facebook', frequency: 3}
}).on('sql', function(sql) { }, {
var expected = '\'"mailing"=>"false","push"=>"facebook","frequency"=>"3"\',\'"default"=>"\'\'value\'\'"\''; logging: function (sql) {
expect(sql.indexOf(expected)).not.to.equal(-1); var expected = '\'"mailing"=>"false","push"=>"facebook","frequency"=>"3"\',\'"default"=>"\'\'value\'\'"\'';
expect(sql.indexOf(expected)).not.to.equal(-1);
}
}); });
}); });
...@@ -375,25 +380,26 @@ if (dialect.match(/^postgres/)) { ...@@ -375,25 +380,26 @@ if (dialect.match(/^postgres/)) {
mood: DataTypes.ENUM('neutral', 'happy', 'sad', 'ecstatic', 'meh', 'joyful') mood: DataTypes.ENUM('neutral', 'happy', 'sad', 'ecstatic', 'meh', 'joyful')
}); });
return User.sync().then(function() { return User.sync({
expect(User.rawAttributes.mood.values).to.deep.equal(['neutral', 'happy', 'sad', 'ecstatic', 'meh', 'joyful']); logging: function (sql) {
count++; console.log(sql);
}).on('sql', function(sql) { if (sql.indexOf('neutral') > -1) {
if (sql.indexOf('neutral') > -1) { expect(sql).to.equal("ALTER TYPE \"enum_UserEnums_mood\" ADD VALUE 'neutral' BEFORE 'happy'");
expect(sql).to.equal("ALTER TYPE \"enum_UserEnums_mood\" ADD VALUE 'neutral' BEFORE 'happy'"); count++;
count++; }
} else if (sql.indexOf('ecstatic') > -1) {
else if (sql.indexOf('ecstatic') > -1) { expect(sql).to.equal("ALTER TYPE \"enum_UserEnums_mood\" ADD VALUE 'ecstatic' BEFORE 'meh'");
expect(sql).to.equal("ALTER TYPE \"enum_UserEnums_mood\" ADD VALUE 'ecstatic' BEFORE 'meh'"); count++;
count++; }
} else if (sql.indexOf('joyful') > -1) {
else if (sql.indexOf('joyful') > -1) { expect(sql).to.equal("ALTER TYPE \"enum_UserEnums_mood\" ADD VALUE 'joyful' AFTER 'meh'");
expect(sql).to.equal("ALTER TYPE \"enum_UserEnums_mood\" ADD VALUE 'joyful' AFTER 'meh'"); count++;
count++; }
} }
}).then(function() {
expect(User.rawAttributes.mood.values).to.deep.equal(['neutral', 'happy', 'sad', 'ecstatic', 'meh', 'joyful']);
expect(count).to.equal(3);
}); });
}).then(function() {
expect(count).to.equal(4);
}); });
}); });
}); });
...@@ -475,8 +481,10 @@ if (dialect.match(/^postgres/)) { ...@@ -475,8 +481,10 @@ if (dialect.match(/^postgres/)) {
it('should use postgres "TIMESTAMP WITH TIME ZONE" instead of "DATETIME"', function() { it('should use postgres "TIMESTAMP WITH TIME ZONE" instead of "DATETIME"', function() {
return this.User.create({ return this.User.create({
dates: [] dates: []
}).on('sql', function(sql) { }, {
expect(sql.indexOf('TIMESTAMP WITH TIME ZONE')).to.be.greaterThan(0); logging: function(sql) {
expect(sql.indexOf('TIMESTAMP WITH TIME ZONE')).to.be.greaterThan(0);
}
}); });
}); });
}); });
......
...@@ -1767,9 +1767,11 @@ describe(Support.getTestDialectTeaser('Instance'), function() { ...@@ -1767,9 +1767,11 @@ describe(Support.getTestDialectTeaser('Instance'), function() {
return UserDelete.create({name: 'hallo', bio: 'welt'}).then(function(u) { return UserDelete.create({name: 'hallo', bio: 'welt'}).then(function(u) {
return UserDelete.findAll().then(function(users) { return UserDelete.findAll().then(function(users) {
expect(users.length).to.equal(1); expect(users.length).to.equal(1);
return u.destroy().on('sql', function(sql) { return u.destroy({
expect(sql).to.exist; logging: function (sql) {
expect(sql.toUpperCase().indexOf('DELETE')).to.be.above(-1); expect(sql).to.exist;
expect(sql.toUpperCase().indexOf('DELETE')).to.be.above(-1);
}
}); });
}); });
}); });
......
...@@ -71,18 +71,26 @@ describe(Support.getTestDialectTeaser('Model'), function() { ...@@ -71,18 +71,26 @@ describe(Support.getTestDialectTeaser('Model'), function() {
it('should be ignored in find, findAll and includes', function() { it('should be ignored in find, findAll and includes', function() {
return Promise.all([ return Promise.all([
this.User.find().on('sql', this.sqlAssert), this.User.find(null, {
this.User.findAll().on('sql', this.sqlAssert), logging: this.sqlAssert
}),
this.User.findAll(null, {
logging: this.sqlAssert
}),
this.Task.findAll({ this.Task.findAll({
include: [ include: [
this.User this.User
] ]
}).on('sql', this.sqlAssert), }, {
logging: this.sqlAssert
}),
this.Project.findAll({ this.Project.findAll({
include: [ include: [
this.User this.User
] ]
}).on('sql', this.sqlAssert) }, {
logging: this.sqlAssert
})
]); ]);
}); });
...@@ -132,7 +140,9 @@ describe(Support.getTestDialectTeaser('Model'), function() { ...@@ -132,7 +140,9 @@ describe(Support.getTestDialectTeaser('Model'), function() {
var self = this; var self = this;
return this.User.bulkCreate([{ return this.User.bulkCreate([{
field1: 'something' field1: 'something'
}]).on('sql', this.sqlAssert).then(function() { }], {
logging: this.sqlAssert
}).then(function() {
return self.User.findAll(); return self.User.findAll();
}).then(function(users) { }).then(function(users) {
expect(users[0].storage).to.equal('something'); expect(users[0].storage).to.equal('something');
......
...@@ -675,11 +675,12 @@ describe(Support.getTestDialectTeaser('Model'), function() { ...@@ -675,11 +675,12 @@ describe(Support.getTestDialectTeaser('Model'), function() {
return this.User.create({ return this.User.create({
intVal: this.sequelize.cast('1', type) intVal: this.sequelize.cast('1', type)
}).on('sql', function(sql) { }, {
expect(sql).to.match(new RegExp("CAST\\('1' AS " + type.toUpperCase() + '\\)')); logging: function(sql) {
match = true; expect(sql).to.match(new RegExp("CAST\\('1' AS " + type.toUpperCase() + '\\)'));
}) match = true;
.then(function(user) { }
}).then(function(user) {
return self.User.find(user.id).then(function(user) { return self.User.find(user.id).then(function(user) {
expect(user.intVal).to.equal(1); expect(user.intVal).to.equal(1);
expect(match).to.equal(true); expect(match).to.equal(true);
...@@ -698,14 +699,15 @@ describe(Support.getTestDialectTeaser('Model'), function() { ...@@ -698,14 +699,15 @@ describe(Support.getTestDialectTeaser('Model'), function() {
return this.User.create({ return this.User.create({
intVal: type intVal: type
}).on('sql', function(sql) { }, {
if (Support.dialectIsMySQL()) { logging: function(sql) {
expect(sql).to.contain('CAST(CAST(1-2 AS UNSIGNED) AS SIGNED)'); if (Support.dialectIsMySQL()) {
} else { expect(sql).to.contain('CAST(CAST(1-2 AS UNSIGNED) AS SIGNED)');
expect(sql).to.contain('CAST(CAST(1-2 AS INTEGER) AS INTEGER)'); } else {
expect(sql).to.contain('CAST(CAST(1-2 AS INTEGER) AS INTEGER)');
}
match = true;
} }
match = true;
}).then(function(user) { }).then(function(user) {
return self.User.find(user.id).then(function(user) { return self.User.find(user.id).then(function(user) {
expect(user.intVal).to.equal(-1); expect(user.intVal).to.equal(-1);
...@@ -811,11 +813,17 @@ describe(Support.getTestDialectTeaser('Model'), function() { ...@@ -811,11 +813,17 @@ describe(Support.getTestDialectTeaser('Model'), function() {
mystr: { type: Sequelize.ARRAY(Sequelize.STRING) } mystr: { type: Sequelize.ARRAY(Sequelize.STRING) }
}); });
var test = false;
return User.sync({force: true}).then(function() { return User.sync({force: true}).then(function() {
return User.create({myvals: [], mystr: []}).on('sql', function(sql) { return User.create({myvals: [], mystr: []}, {
expect(sql.indexOf('ARRAY[]::INTEGER[]')).to.be.above(-1); logging: function(sql) {
expect(sql.indexOf('ARRAY[]::VARCHAR[]')).to.be.above(-1); test = true;
expect(sql.indexOf('ARRAY[]::INTEGER[]')).to.be.above(-1);
expect(sql.indexOf('ARRAY[]::VARCHAR[]')).to.be.above(-1);
}
}); });
}).then(function() {
expect(test).to.be.true;
}); });
}); });
...@@ -829,16 +837,22 @@ describe(Support.getTestDialectTeaser('Model'), function() { ...@@ -829,16 +837,22 @@ describe(Support.getTestDialectTeaser('Model'), function() {
myvals: { type: Sequelize.ARRAY(Sequelize.INTEGER) }, myvals: { type: Sequelize.ARRAY(Sequelize.INTEGER) },
mystr: { type: Sequelize.ARRAY(Sequelize.STRING) } mystr: { type: Sequelize.ARRAY(Sequelize.STRING) }
}); });
var test = false;
return User.sync({force: true}).then(function() { return User.sync({force: true}).then(function() {
return User.create({myvals: [1, 2, 3, 4], mystr: ['One', 'Two', 'Three', 'Four']}).then(function(user) { return User.create({myvals: [1, 2, 3, 4], mystr: ['One', 'Two', 'Three', 'Four']}).then(function(user) {
user.myvals = []; user.myvals = [];
user.mystr = []; user.mystr = [];
return user.save().on('sql', function(sql) { return user.save(undefined, {
expect(sql.indexOf('ARRAY[]::INTEGER[]')).to.be.above(-1); logging: function(sql) {
expect(sql.indexOf('ARRAY[]::VARCHAR[]')).to.be.above(-1); test = true;
expect(sql.indexOf('ARRAY[]::INTEGER[]')).to.be.above(-1);
expect(sql.indexOf('ARRAY[]::VARCHAR[]')).to.be.above(-1);
}
}); });
}); });
}).then(function() {
expect(test).to.be.true;
}); });
}); });
...@@ -986,13 +1000,18 @@ describe(Support.getTestDialectTeaser('Model'), function() { ...@@ -986,13 +1000,18 @@ describe(Support.getTestDialectTeaser('Model'), function() {
smth: {type: Sequelize.STRING, allowNull: false} smth: {type: Sequelize.STRING, allowNull: false}
}); });
var test = false;
return User.sync({ force: true }).then(function() { return User.sync({ force: true }).then(function() {
return User return User
.create({ name: 'Fluffy Bunny', smth: 'else' }) .create({ name: 'Fluffy Bunny', smth: 'else' }, {
.on('sql', function(sql) { logging: function(sql) {
expect(sql).to.exist; expect(sql).to.exist;
expect(sql.toUpperCase().indexOf('INSERT')).to.be.above(-1); test = true;
expect(sql.toUpperCase().indexOf('INSERT')).to.be.above(-1);
}
}); });
}).then(function() {
expect(test).to.be.true;
}); });
}); });
......
...@@ -117,10 +117,16 @@ describe(Support.getTestDialectTeaser('Model'), function() { ...@@ -117,10 +117,16 @@ describe(Support.getTestDialectTeaser('Model'), function() {
}); });
it('treats questionmarks in an array', function() { it('treats questionmarks in an array', function() {
var test = false;
return this.UserPrimary.find({ return this.UserPrimary.find({
where: ['specialkey = ?', 'awesome'] where: ['specialkey = ?', 'awesome']
}).on('sql', function(sql) { }, {
expect(sql).to.contain("WHERE specialkey = 'awesome'"); logging: function(sql) {
test = true;
expect(sql).to.contain("WHERE specialkey = 'awesome'");
}
}).then(function() {
expect(test).to.be.true;
}); });
}); });
...@@ -190,9 +196,15 @@ describe(Support.getTestDialectTeaser('Model'), function() { ...@@ -190,9 +196,15 @@ describe(Support.getTestDialectTeaser('Model'), function() {
}); });
it('allows sql logging', function() { it('allows sql logging', function() {
return this.User.find({ where: { username: 'foo' } }).on('sql', function(sql) { var test = false;
expect(sql).to.exist; return this.User.find({ where: { username: 'foo' } }, {
expect(sql.toUpperCase().indexOf('SELECT')).to.be.above(-1); logging: function(sql) {
test = true;
expect(sql).to.exist;
expect(sql.toUpperCase().indexOf('SELECT')).to.be.above(-1);
}
}).then(function() {
expect(test).to.be.true;
}); });
}); });
...@@ -257,16 +269,17 @@ describe(Support.getTestDialectTeaser('Model'), function() { ...@@ -257,16 +269,17 @@ describe(Support.getTestDialectTeaser('Model'), function() {
return this.User.bulkCreate([{username: 'jack'}, {username: 'jack'}]).then(function() { return this.User.bulkCreate([{username: 'jack'}, {username: 'jack'}]).then(function() {
return self.sequelize.Promise.map(permutations, function(perm) { return self.sequelize.Promise.map(permutations, function(perm) {
return self.User.find(perm).then(function(user) { return self.User.find(perm, {
logging: function(s) {
expect(s.indexOf(0)).not.to.equal(-1);
count++;
}
}).then(function(user) {
expect(user).to.be.null; expect(user).to.be.null;
count++;
}).on('sql', function(s) {
expect(s.indexOf(0)).not.to.equal(-1);
count++;
}); });
}); });
}).then(function() { }).then(function() {
expect(count).to.be.equal(2 * permutations.length); expect(count).to.be.equal(permutations.length);
}); });
}); });
......
...@@ -10,7 +10,7 @@ var chai = require('chai') ...@@ -10,7 +10,7 @@ var chai = require('chai')
chai.config.includeStack = true; chai.config.includeStack = true;
describe(Support.getTestDialectTeaser('Promise'), function() { describe.skip(Support.getTestDialectTeaser('Promise'), function() {
beforeEach(function() { beforeEach(function() {
return Support.prepareTransactionTest(this.sequelize).bind(this).then(function(sequelize) { return Support.prepareTransactionTest(this.sequelize).bind(this).then(function(sequelize) {
this.sequelize = sequelize; this.sequelize = sequelize;
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!