1
0
mirror of https://github.com/qTox/qTox.git synced 2024-03-22 14:00:36 +08:00

refactor(test): Use temporary files in dbschema_test

Files are conceptually temporary, having extra conflict handling logic
is unnecessary, and having the test fail to run if it crashed on last
invocation is annoying.
This commit is contained in:
Anthony Bilinski 2022-03-25 16:03:19 -07:00
parent f777aa885c
commit d1a8f5d6ae
No known key found for this signature in database
GPG Key ID: 2AA8E0DA1B31FB3C

View File

@ -21,8 +21,9 @@
#include "src/persistence/dbupgrader.h"
#include "src/core/toxfile.h"
#include <QtTest/QtTest>
#include <QString>
#include <QTemporaryFile>
#include <QtTest/QtTest>
#include <algorithm>
#include <memory>
@ -84,7 +85,8 @@ class TestDbSchema : public QObject
{
Q_OBJECT
private slots:
void initTestCase();
void init();
void cleanup();
void testCreation();
void testIsNewDb();
void test0to1();
@ -98,12 +100,11 @@ private slots:
// test8to9 omitted, data corruption correction upgrade with no schema change
void test9to10();
// test suite
void cleanupTestCase() const;
private:
bool initSucess{false};
void createSchemaAtVersion(std::shared_ptr<RawDatabase> db, const std::vector<SqliteMasterEntry>& schema);
void verifyDb(std::shared_ptr<RawDatabase> db, const std::vector<SqliteMasterEntry>& expectedSql);
std::unique_ptr<QTemporaryFile> testDatabaseFile;
};
const QString testFileList[] = {"testCreation.db", "testIsNewDbTrue.db", "testIsNewDbFalse.db",
@ -208,25 +209,6 @@ const std::vector<SqliteMasterEntry> schema7{
const std::vector<SqliteMasterEntry> schema9 = schema7;
const std::vector<SqliteMasterEntry> schema10 = schema9;
void TestDbSchema::initTestCase()
{
for (const auto& path : testFileList) {
QVERIFY(!QFileInfo{path}.exists());
}
initSucess = true;
}
void TestDbSchema::cleanupTestCase() const
{
if (!initSucess) {
qWarning() << "init failed, skipping cleanup to avoid loss of data";
return;
}
for (const auto& path : testFileList) {
QFile::remove(path);
}
}
void TestDbSchema::verifyDb(std::shared_ptr<RawDatabase> db, const std::vector<SqliteMasterEntry>& expectedSql)
{
QVERIFY(db->execNow(RawDatabase::Query(QStringLiteral(
@ -257,22 +239,36 @@ void TestDbSchema::createSchemaAtVersion(std::shared_ptr<RawDatabase> db, const
QVERIFY(db->execNow(queries));
}
void TestDbSchema::init()
{
testDatabaseFile = std::unique_ptr<QTemporaryFile>(new QTemporaryFile());
// fileName is only defined once the file is opened. Since RawDatabase
// will be openening the file itself not using QFile, open and close it now.
QVERIFY(testDatabaseFile->open());
testDatabaseFile->close();
}
void TestDbSchema::cleanup()
{
testDatabaseFile.reset();
}
void TestDbSchema::testCreation()
{
QVector<RawDatabase::Query> queries;
auto db = std::shared_ptr<RawDatabase>{new RawDatabase{"testCreation.db", {}, {}}};
auto db = std::shared_ptr<RawDatabase>{new RawDatabase{testDatabaseFile->fileName(), {}, {}}};
QVERIFY(DbUpgrader::createCurrentSchema(*db));
verifyDb(db, schema7);
}
void TestDbSchema::testIsNewDb()
{
auto db = std::shared_ptr<RawDatabase>{new RawDatabase{"testIsNewDbTrue.db", {}, {}}};
auto db = std::shared_ptr<RawDatabase>{new RawDatabase{testDatabaseFile->fileName(), {}, {}}};
bool success = false;
bool newDb = DbUpgrader::isNewDb(db, success);
QVERIFY(success);
QVERIFY(newDb == true);
db = std::shared_ptr<RawDatabase>{new RawDatabase{"testIsNewDbFalse.db", {}, {}}};
db = std::shared_ptr<RawDatabase>{new RawDatabase{testDatabaseFile->fileName(), {}, {}}};
createSchemaAtVersion(db, schema0);
newDb = DbUpgrader::isNewDb(db, success);
QVERIFY(success);
@ -281,7 +277,7 @@ void TestDbSchema::testIsNewDb()
void TestDbSchema::test0to1()
{
auto db = std::shared_ptr<RawDatabase>{new RawDatabase{"test0to1.db", {}, {}}};
auto db = std::shared_ptr<RawDatabase>{new RawDatabase{testDatabaseFile->fileName(), {}, {}}};
createSchemaAtVersion(db, schema0);
QVERIFY(DbUpgrader::dbSchema0to1(*db));
verifyDb(db, schema1);
@ -302,7 +298,7 @@ void TestDbSchema::test1to2()
https://github.com/qTox/qTox/issues/5776
*/
auto db = std::shared_ptr<RawDatabase>{new RawDatabase{"test1to2.db", {}, {}}};
auto db = std::shared_ptr<RawDatabase>{new RawDatabase{testDatabaseFile->fileName(), {}, {}}};
createSchemaAtVersion(db, schema1);
const QString myPk = "AC18841E56CCDEE16E93E10E6AB2765BE54277D67F1372921B5B418A6B330D3D";
@ -384,7 +380,7 @@ void TestDbSchema::test1to2()
void TestDbSchema::test2to3()
{
auto db = std::shared_ptr<RawDatabase>{new RawDatabase{"test2to3.db", {}, {}}};
auto db = std::shared_ptr<RawDatabase>{new RawDatabase{testDatabaseFile->fileName(), {}, {}}};
createSchemaAtVersion(db, schema2);
// since we don't enforce foreign key contraints in the db, we can stick in IDs to other tables
@ -446,7 +442,7 @@ void TestDbSchema::test2to3()
void TestDbSchema::test3to4()
{
auto db = std::shared_ptr<RawDatabase>{new RawDatabase{"test3to4.db", {}, {}}};
auto db = std::shared_ptr<RawDatabase>{new RawDatabase{testDatabaseFile->fileName(), {}, {}}};
createSchemaAtVersion(db, schema3);
QVERIFY(DbUpgrader::dbSchema3to4(*db));
verifyDb(db, schema4);
@ -454,7 +450,7 @@ void TestDbSchema::test3to4()
void TestDbSchema::test4to5()
{
auto db = std::shared_ptr<RawDatabase>{new RawDatabase{"test4to5.db", {}, {}}};
auto db = std::shared_ptr<RawDatabase>{new RawDatabase{testDatabaseFile->fileName(), {}, {}}};
createSchemaAtVersion(db, schema4);
QVERIFY(DbUpgrader::dbSchema4to5(*db));
verifyDb(db, schema5);
@ -462,7 +458,7 @@ void TestDbSchema::test4to5()
void TestDbSchema::test5to6()
{
auto db = std::shared_ptr<RawDatabase>{new RawDatabase{"test5to6.db", {}, {}}};
auto db = std::shared_ptr<RawDatabase>{new RawDatabase{testDatabaseFile->fileName(), {}, {}}};
createSchemaAtVersion(db, schema5);
QVERIFY(DbUpgrader::dbSchema5to6(*db));
verifyDb(db, schema6);
@ -470,7 +466,7 @@ void TestDbSchema::test5to6()
void TestDbSchema::test6to7()
{
auto db = std::shared_ptr<RawDatabase>{new RawDatabase{"test6to7.db", {}, {}}};
auto db = std::shared_ptr<RawDatabase>{new RawDatabase{testDatabaseFile->fileName(), {}, {}}};
// foreign_keys are enabled by History constructor and required for this upgrade to work on older sqlite versions
db->execNow(
"PRAGMA foreign_keys = ON;");
@ -481,7 +477,7 @@ void TestDbSchema::test6to7()
void TestDbSchema::test9to10()
{
auto db = std::shared_ptr<RawDatabase>{new RawDatabase{"test9to10.db", {}, {}}};
auto db = std::shared_ptr<RawDatabase>{new RawDatabase{testDatabaseFile->fileName(), {}, {}}};
createSchemaAtVersion(db, schema9);
QVERIFY(insertFileId(*db, 1, true));