Add tests to support deprecated event listeners

This commit is contained in:
Brian Carlson 2017-06-15 14:00:37 -05:00 committed by Brian C
parent fc3634045b
commit 5f5e40f03c
3 changed files with 51 additions and 4 deletions

View File

@ -374,10 +374,8 @@ Client.prototype.query = function (config, values, callback) {
}
} else {
query = new Query(config, values, callback)
result = query.callback ? undefined : new global.Promise((resolve, reject) => {
query.once('end', resolve)
query.once('error', reject)
})
query._deprecateListeners()
result = query
}
if (this.binary && !query.binary) {

View File

@ -53,6 +53,25 @@ Query.prototype.requiresPreparation = function() {
return this.values.length > 0;
};
Query.prototype.then = function(onSuccess, onFailure) {
return this._getPromise().then(onSuccess, onFailure);
};
Query.prototype.catch = function(callback) {
return this._getPromise().catch(callback);
};
Query.prototype._getPromise = function() {
if (this._promise) return this._promise;
this._promise = new Promise(function(resolve, reject) {
this.once('end', resolve);
this.once('error', reject);
}.bind(this));
return this._promise;
};
Query.prototype._deprecateListeners = function() {
}
//associates row metadata from the supplied
//message with this query object

View File

@ -0,0 +1,30 @@
const helper = require('./test-helper')
const pg = helper.pg
const suite = new helper.Suite()
suite.test('Query with a callback should still support event-listeners', (done) => {
const client = new pg.Client()
const sink = new helper.Sink(3, 1000, () => {
client.end()
done()
})
client.connect()
const query = client.query('SELECT NOW()', (err, res) => {
sink.add()
})
query.on('row', () => sink.add())
query.on('end', () => sink.add())
})
suite.test('Query with a promise should still support event-listeners', (done) => {
const client = new pg.Client()
const sink = new helper.Sink(3, 1000, () => {
client.end()
done()
})
client.connect()
const query = client.query('SELECT NOW()')
query.on('row', () => sink.add())
query.on('end', () => sink.add())
query.then(() => sink.add())
})