Reputation: 31
I am running the following:
it('should be able to run sql', function () {
var success = true;
var testsCompleted;
for (var i = 0; i < sqlToRun[1].length; i++) {
sql.runEtlQuery(config, sqlToRun, i).then(function () {
testsCompleted++;
}).catch(function (err) {
console.log(err);
success = false;
});
}
expect(testsCompleted == sqlToRun[1].length).toBeTruthy();
expect(success).toBeTruthy();
});
However, it is running the expect queries and failing the test before it finishes the sql async calls, and the test will fail before it starts printing out the results of the of the sql queries.
The sql query looks like this:
runEtlQuery: function (config, sqlToRun, i) {
var defer = protractor.promise.defer();
var connection = new sql.ConnectionPool(config, function (err) {
var request = new sql.Request(connection);
request.query(sqlToRun[1][i], function (err, recordset) {
if (err) defer.reject("#" + i + ": " + sqlToRun[0][i] +"\x1b[31m Error: \x1b[0m" + err);
else {
console.log("#" + i + ": " + sqlToRun[0][i] + " - \x1b[32mPassed\x1b[0m");
defer.fulfill();
connection.close();
}
});
});
return defer.promise;
},
The console output looks like:
[13:12:42] I/launcher - Running 1 instances of WebDriver
[13:12:42] I/local - Starting selenium standalone server...
[13:12:43] I/local - Selenium standalone server started at http://10.197.244.125:62251/wd/hub
Started
..F
Failures:
1) ETL tests should be able to run sql
Message:
Expected false to be truthy.
Stack:
Error: Failed expectation
---<removed stacktrace>---
3 specs, 1 failure
Finished in 270.903 seconds
#17: CDW_Test - Passed
#15: CDW_LabTestName - Passed
#4: CDW_removed - Passed
#5: CDW_removed - Passed
#1: CDW_moreremoved - Passed
#30: CDW_Stuff - Passed
<ect>
I'm trying to figure out how to make it wait for all of the sql queries to finish before it checks to see if the test failed or passed.
I used to have it so it would fail on the first error, but wanted to change it so it kept on processing the sql queries to get a list of all of the failures before failing the test.
Upvotes: 1
Views: 820
Reputation: 31
I did find a way to make it work correctly, even if it's not the most elegant solution:
it('should be able to run sql', function (done) {
sql.recursiveRunEtlQuery(config, sqlToRun, sqlToRun[1].length - 1).then(function () {
done();
}).catch(function (err) {
success = false;
done.fail(err);
});
Then the recursiveRunEtlQuery function looks like this:
recursiveRunEtlQuery: function (config, sqlToRun, i) {
var defer = protractor.promise.defer();
if (i >= 0) {
module.exports.recursiveRunEtlQuery(config, sqlToRun, i - 1).then(function () {
module.exports.runEtlQuery(config, sqlToRun, i).then(function () {
defer.fulfill();
}).catch(err => {
defer.reject(utilities.stringFormat("Error Running ETL: \x1b[36m{0}\x1b[0m:\nSQL:\n\x1b[33m{1}\x1b[0m\nError:\n\x1b[31m{2}\x1b[0m", sqlToRun[0][i], sqlToRun[1][i], err));
});
}).catch(err => {
module.exports.runEtlQuery(config, sqlToRun, i).then(function () {
defer.reject(err);
}).catch(err2 => {
defer.reject(utilities.stringFormat("Error Running ETL: \x1b[36m{0}\x1b[0m:\nSQL:\n\x1b[33m{1}\x1b[0m\nError:\n\x1b[31m{2}\x1b[0m\n{3}", sqlToRun[0][i], sqlToRun[1][i], err2, err));
});
});
}
else defer.fulfill();
return defer.promise;
The main problem with this solution is it forces it to run synchronously instead of running Async, which will cause it to run longer as some of the queries take a while to finish.
Upvotes: 2
Reputation: 3731
First off, this is a fine example of why I don't like multiple expect
s in a test... because I can't tell which of your expect
s is failing.
That said, and assuming it's not you runEtlQuery
method that is failing, you might be running into the infamous looping with promises issue.
TLDR; you can use an Immediately-Invoked Function Expression to ensure your index is bound as expected.
it('should be able to run sql', function () {
var success = true;
var testsCompleted;
for (var i = 0; i < sqlToRun[1].length; i++) {
(funciton(index) {
sql.runEtlQuery(config, sqlToRun, index).then(function () {
testsCompleted++;
}).catch(function (err) {
console.log(err);
success = false;
});
})(i);
}
expect(testsCompleted == sqlToRun[1].length).toBeTruthy();
expect(success).toBeTruthy();
});
Upvotes: 0