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

Commit 80f5c3bf by Meg Sharkey

fixed logging, added tests

1 parent 070dd44b
...@@ -26,8 +26,8 @@ module.exports = (function() { ...@@ -26,8 +26,8 @@ module.exports = (function() {
if(this.options.logging) if(this.options.logging)
console.log('Executing: ' + this.sql) console.log('Executing: ' + this.sql)
this.client.query(this.sql, function(err, results, fields) { this.client.query(this.sql, function(err, results, fields) {
//allow clients to listen to sql to do their own logging or whatnot
self.emit('sql', self.sql) self.emit('sql', self.sql)
err ? onFailure.call(self, err) : onSuccess.call(self, results, fields) err ? onFailure.call(self, err) : onSuccess.call(self, results, fields)
}).setMaxListeners(100) }).setMaxListeners(100)
...@@ -76,6 +76,7 @@ module.exports = (function() { ...@@ -76,6 +76,7 @@ module.exports = (function() {
unbindClient.call(this) unbindClient.call(this)
this.emit('success', result) this.emit('success', result)
} }
var onFailure = function(err) { var onFailure = function(err) {
......
...@@ -20,5 +20,10 @@ module.exports = (function() { ...@@ -20,5 +20,10 @@ module.exports = (function() {
return this return this
} }
Query.prototype.sql_logging = function(fct) {
this.on('sql', fct)
return this
}
return Query return Query
})() })()
...@@ -25,10 +25,9 @@ module.exports = (function() { ...@@ -25,10 +25,9 @@ module.exports = (function() {
, isUpdateCommand = (self.sql.toLowerCase().indexOf('update') == 0) , isUpdateCommand = (self.sql.toLowerCase().indexOf('update') == 0)
, databaseMethod = (isInsertCommand || isUpdateCommand) ? 'run' : 'all' , databaseMethod = (isInsertCommand || isUpdateCommand) ? 'run' : 'all'
//allow clients to listen to sql to do their own logging or whatnot
self.emit('sql', self.sql)
self.database[databaseMethod](self.sql, function(err, results) { self.database[databaseMethod](self.sql, function(err, results) {
//allow clients to listen to sql to do their own logging or whatnot
self.emit('sql', self.sql)
err ? onFailure.call(self, err) : onSuccess.call(self, results, this) err ? onFailure.call(self, err) : onSuccess.call(self, results, this)
}) })
}) })
......
...@@ -33,5 +33,6 @@ module.exports = (function() { ...@@ -33,5 +33,6 @@ module.exports = (function() {
return this return this
} }
return CustomEventEmitter return CustomEventEmitter
})() })()
...@@ -201,9 +201,10 @@ module.exports = (function() { ...@@ -201,9 +201,10 @@ module.exports = (function() {
return new Utils.CustomEventEmitter(function(emitter) { return new Utils.CustomEventEmitter(function(emitter) {
var sql = self.QueryGenerator.selectQuery(tableName, options) var sql = self.QueryGenerator.selectQuery(tableName, options)
self.sequelize var qry = self.sequelize
.query(sql, null, { plain: true, raw: true }) .query(sql, null, { plain: true, raw: true })
.success(function(data) {
qry.success(function(data) {
self.emit('rawSelect', null) self.emit('rawSelect', null)
emitter.emit('success', data[attributeSelector]) emitter.emit('success', data[attributeSelector])
}) })
...@@ -211,6 +212,9 @@ module.exports = (function() { ...@@ -211,6 +212,9 @@ module.exports = (function() {
self.emit('rawSelect', err) self.emit('rawSelect', err)
emitter.emit('failure', err) emitter.emit('failure', err)
}) })
.sql_logging(function(sql) {
emitter.emit('sql', sql)
})
}).run() }).run()
} }
...@@ -243,6 +247,8 @@ module.exports = (function() { ...@@ -243,6 +247,8 @@ module.exports = (function() {
options.error && options.error(err) options.error && options.error(err)
self.emit(methodName, err) self.emit(methodName, err)
emitter.emit('failure', err) emitter.emit('failure', err)
}).sql_logging(function(sql) {
emitter.emit('sql', sql)
}) })
}).run() }).run()
} }
......
...@@ -183,6 +183,19 @@ describe('ModelFactory', function() { ...@@ -183,6 +183,19 @@ describe('ModelFactory', function() {
}) })
}) })
}) })
it('allows sql logging', function() {
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() { describe('destroy', function() {
...@@ -210,6 +223,29 @@ describe('ModelFactory', function() { ...@@ -210,6 +223,29 @@ describe('ModelFactory', function() {
}) })
}) })
it('allows sql logging of delete statements', function() {
Helpers.async(function(done) {
User = sequelize.define('User', {
name: Sequelize.STRING,
bio: Sequelize.TEXT
})
User.sync({force: true}).success(done)
})
Helpers.async(function(done) {
User.create({name: 'hallo', bio: 'welt'}).success(function(u) {
User.all().success(function(users) {
expect(users.length).toEqual(1)
u.destroy().on('sql', function(sql) {
expect(sql).toBeDefined()
expect(sql.toUpperCase().indexOf("DELETE")).toBeGreaterThan(-1)
done()
}).error(function(err) { console.log(err) })
}).error(function(err) { console.log(err) })
})
})
})
it('marks the database entry as deleted if model is paranoid', function() { it('marks the database entry as deleted if model is paranoid', function() {
Helpers.async(function(done) { Helpers.async(function(done) {
User = sequelize.define('User', { User = sequelize.define('User', {
...@@ -228,6 +264,27 @@ describe('ModelFactory', function() { ...@@ -228,6 +264,27 @@ describe('ModelFactory', function() {
}) })
}) })
}) })
it('allows sql logging of update statements', function() {
Helpers.async(function(done) {
User = sequelize.define('User', {
name: Sequelize.STRING, bio: Sequelize.TEXT
}, { paranoid:true })
User.sync({ force: true }).success(done)
})
Helpers.async(function(done) {
User.create({ name: 'meg', bio: 'none' }).success(function(u) {
expect(u).toBeDefined()
expect(u).not.toBe(null)
u.destroy().on('sql', function(sql) {
expect(sql).toBeDefined()
expect(sql.toUpperCase().indexOf("UPDATE")).toBeGreaterThan(-1)
done()
})
})
})
})
}) })
describe('find', function() { describe('find', function() {
...@@ -276,6 +333,17 @@ describe('ModelFactory', function() { ...@@ -276,6 +333,17 @@ describe('ModelFactory', function() {
}) })
}) })
it('allows sql logging', function() {
Helpers.async(function(done) {
User.find({ where: { name: 'foo' } })
.on('sql', function(sql) {
expect(sql).toBeDefined()
expect(sql.toUpperCase().indexOf("SELECT")).toBeGreaterThan(-1)
done()
})
})
})
it('ignores passed limit option', function() { it('ignores passed limit option', function() {
Helpers.async(function(done) { Helpers.async(function(done) {
User.find({limit: 10}).success(function(user) { User.find({limit: 10}).success(function(user) {
...@@ -433,6 +501,17 @@ describe('ModelFactory', function() { ...@@ -433,6 +501,17 @@ describe('ModelFactory', function() {
}) })
}) })
it('allows sql logging', function() {
Helpers.async(function(done) {
User.count()
.on('sql', function(sql) {
expect(sql).toBeDefined()
expect(sql.toUpperCase().indexOf("SELECT")).toBeGreaterThan(-1)
done()
})
})
})
it('filters object', function() { it('filters object', function() {
Helpers.async(function(done) { Helpers.async(function(done) {
User.create({name: 'user1'}).success(function() { User.create({name: 'user1'}).success(function() {
...@@ -459,6 +538,16 @@ describe('ModelFactory', function() { ...@@ -459,6 +538,16 @@ describe('ModelFactory', function() {
}) })
}) })
}) })
it('allows sql logging', function() {
Helpers.async(function(done) {
User.min('age')
.on('sql', function(sql) {
expect(sql).toBeDefined()
expect(sql.toUpperCase().indexOf("SELECT")).toBeGreaterThan(-1)
done()
})
})
})
}) })
describe('max', function() { describe('max', function() {
...@@ -471,6 +560,16 @@ describe('ModelFactory', function() { ...@@ -471,6 +560,16 @@ describe('ModelFactory', function() {
}) })
}) })
}) })
it('allows sql logging', function() {
Helpers.async(function(done) {
User.max('age')
.on('sql', function(sql) {
expect(sql).toBeDefined()
expect(sql.toUpperCase().indexOf("SELECT")).toBeGreaterThan(-1)
done()
})
})
})
}) })
describe('equals', function() { describe('equals', function() {
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!