I am really struggling for some time with a test to check the db running and making a test query. My code looks like this currently:
var vows=require('vows');
var assert=require('assert');
var pg = require('pg');
var suite=vows.describe('Database');
var client = new pg.Client("pg://user:aaa@127.0.0.1:5432/mydb");
suite.addBatch({
'When providing database with correct credentials': {
topic: function(){
client.connect(this.callback)
},
'we get authorized': function (topic) {
assert.isUndefined(topic[0]);
client.end();
},
'we try to make a select': {
topic: function () {
client = new pg.Client("pg://user:aaa@127.0.0.1:5432/mydb");
client.connect(function(err){
console.log(err);
});
query = client.query("SELECT * FROM uuid_audit_table;");
var sel_error,sel_data;
query.on('err',function(err){
sel_error=err;
return err;
});
query.on('row',function(data){
sel_data=data;
return data;
});
query.on('end',function(){
console.log("end");
client.end();
});
//return ["ERROR: no event emitted"];
},
'we get some data and no error': function(topic) {
console.log(topic);
assert.isUndefined(topic[0]);
assert.isNotNull(topic[1]);
}
The problem is, that if I do comment the line "//return ["ERROR: no event emitted"];
the topic function ends, before the callbacks from "data" and "end" events are emitted and I get an errror Callback not fired.
I was trying several things and I am not able to make the topic function wait for the events to be emitted. I always get myself into the vow without the variables set or with the Callback not fired error.
I tried to solve that with:
process.nextTick(function(){
return ["ERROR: no event emitted"];
});
But that ends with an error:
node.js:201
throw e; // process.nextTick error, or 'error' event on first tick
^
TypeError: Property '1' of object function () {
callback.apply(null, args);
}, is not a function
at EventEmitter._tickCallback (node.js:192:40)
I am really lost to this, maybe on a dead end and I should use the vow test differently for this purpose, but I have no clue how to catch on that.
Any help much appreciated, thank you.