Skip to content

Commit d7934d2

Browse files
committed
Fixed tests to give more feedback and to work on Release builds.
1 parent d578026 commit d7934d2

File tree

2 files changed

+18
-17
lines changed

2 files changed

+18
-17
lines changed

test-shared/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,4 @@ set(CMAKE_CXX_FLAGS "-std=c++14")
44
add_executable(qsqlcipher-test main.cpp)
55
target_link_libraries(qsqlcipher-test Qt5::Sql)
66

7-
add_test(NAME qsqlcipher-test COMMAND qsqlcipher-test)
7+
#add_test(NAME qsqlcipher-test COMMAND qsqlcipher-test)

test-shared/main.cpp

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,31 @@
11
#include <QCoreApplication>
2-
#include <QDebug>
32
#include <QtSql>
43
#include <QTemporaryDir>
54
#include <iostream>
5+
#include <cstdlib>
66

7-
#ifndef QT_DEBUG
8-
#error Must be built in debug mode!
9-
#endif
7+
#define QSQLCIPHER_TEST_ASSERT(cond, message) if(!(cond)) { std::cerr << message << std::endl; exit(-1); }
108

119
int main(int argc, char *argv[])
1210
{
1311
QCoreApplication app(argc, argv);
1412

1513
for (const auto& driver : QSqlDatabase::drivers()) {
16-
qDebug() << "Available QSqlDatabase driver: " << driver;
14+
std::cout << "Available QSqlDatabase driver: " << driver.toStdString() << std::endl;
1715
}
1816

19-
Q_ASSERT(QSqlDatabase::isDriverAvailable("QSQLITE")); // from Qt
20-
Q_ASSERT(QSqlDatabase::isDriverAvailable("QSQLCIPHER")); // from our plugin
17+
QSQLCIPHER_TEST_ASSERT(QSqlDatabase::isDriverAvailable("QSQLITE"), "QSQLITE driver is not available!"); // from Qt
18+
QSQLCIPHER_TEST_ASSERT(QSqlDatabase::isDriverAvailable("QSQLCIPHER"), "QSQLCIPHER driver is not available!"); // from our plugin
2119

2220
QTemporaryDir tmp;
23-
Q_ASSERT(tmp.isValid());
21+
QSQLCIPHER_TEST_ASSERT(tmp.isValid(), "Could not get temp directory.");
2422

2523
auto withDB = [&](const char *driver, auto fn) {
2624
QString path = QDir(tmp.path()).absoluteFilePath(QString(driver) + ".db");
2725
{
2826
QSqlDatabase db = QSqlDatabase::addDatabase(driver, "db");
2927
db.setDatabaseName(path);
30-
Q_ASSERT(db.open());
28+
QSQLCIPHER_TEST_ASSERT(db.open(), "Could not open database!");
3129
fn(db);
3230
}
3331
QSqlDatabase::removeDatabase("db");
@@ -46,27 +44,30 @@ int main(int argc, char *argv[])
4644
// Check that we can read from the SQLite db
4745
withDB("QSQLITE", [](auto db){
4846
QSqlQuery q = db.exec("select bar from foo");
49-
Q_ASSERT(q.next());
50-
Q_ASSERT(q.value(0).toInt() == 42);
47+
QSQLCIPHER_TEST_ASSERT(q.next(), "Expected a fetchable entry in Query object.");
48+
QSQLCIPHER_TEST_ASSERT(q.value(0).toInt() == 42, "Database returned invalid value for row!");
5149
});
5250

5351
std::cout << "Running Task 3..." << std::endl;
5452
// Check that SQLite is not SQLCipher
5553
withDB("QSQLITE", [](auto db){
5654
QSqlQuery q = db.exec("select sqlcipher_export()");
5755
QString errmsg = q.lastError().databaseText();
58-
Q_ASSERT(errmsg.startsWith("no such function"));
56+
QSQLCIPHER_TEST_ASSERT(errmsg.startsWith("no such function"), "Database did not respond with the expected error message.");
5957
});
6058
}
6159

6260
// QSQLCIPHER
6361
{
62+
#if defined(_WIN32) || defined(_WIN64) || defined(_MSC_VER )
63+
std::cout << "Hint: If you get an error in the following task like \"QSQLCIPHER driver not loaded\" then check if library dependencies (e.g. libeay32.dll) are in the same folder as the test executable." << std::endl;
64+
#endif
6465
std::cout << "Running Task 4..." << std::endl;
6566
// Check that SQLCipher is not SQLite
6667
withDB("QSQLCIPHER", [](auto db){
6768
QSqlQuery q = db.exec("select sqlcipher_export()");
6869
QString errmsg = q.lastError().databaseText();
69-
Q_ASSERT(errmsg.startsWith("wrong number of arguments"));
70+
QSQLCIPHER_TEST_ASSERT(errmsg.startsWith("wrong number of arguments"), "Database did not respond with the expected error message.");
7071
});
7172

7273
std::cout << "Running Task 5..." << std::endl;
@@ -84,16 +85,16 @@ int main(int argc, char *argv[])
8485
// Check that we can't read from the SQLCipher db without the passphrase
8586
withDB("QSQLCIPHER", [](auto db){
8687
QSqlQuery q = db.exec("select bar from foo");
87-
Q_ASSERT(!q.next());
88+
QSQLCIPHER_TEST_ASSERT(!q.next(), "Query returned a fetchable row, this should not be the case.");
8889
});
8990

9091
std::cout << "Running Task 7..." << std::endl;
9192
// Check that we can read from the SQLCipher db with the passphrase
9293
withDB("QSQLCIPHER", [](auto db){
9394
db.exec("PRAGMA key = 'foobar';");
9495
QSqlQuery q = db.exec("select bar from foo");
95-
Q_ASSERT(q.next());
96-
Q_ASSERT(q.value(0).toInt() == 42);
96+
QSQLCIPHER_TEST_ASSERT(q.next(), "Expected a fetchable entry in Query object.");
97+
QSQLCIPHER_TEST_ASSERT(q.value(0).toInt() == 42, "Database returned invalid value for row!");
9798
});
9899
std::cout << "Success! All tests completed." << std::endl;
99100
}

0 commit comments

Comments
 (0)