Skip to content

Commit

Permalink
[libcommhistory] Use standalone test database when running tests. JB#…
Browse files Browse the repository at this point in the history
…52802

Use a separate test database instead of polluting the general database
when running the tests. This also means the test database can be
freely deleted before running each tests, to ensure stray database
entries from one tests (e.g. left behind if a test crashes) do not
cause failures in following tests when the tests are run in a batch.
  • Loading branch information
blammit committed Mar 17, 2021
1 parent fcd71fc commit 2b5ade9
Show file tree
Hide file tree
Showing 20 changed files with 62 additions and 46 deletions.
12 changes: 10 additions & 2 deletions src/commhistorydatabase.cpp
Expand Up @@ -28,12 +28,15 @@
#include <QSqlQuery>
#include <QDebug>
#include <QStandardPaths>
#include <QCoreApplication>

// Appended to GenericDataLocation (or a hardcoded equivalent on Qt4)
#define COMMHISTORY_DATABASE_DIR "/commhistory/"
#define COMMHISTORY_DATABASE_NAME "commhistory.db"
#define COMMHISTORY_DATA_DIR COMMHISTORY_DATABASE_DIR "data/"

static QString db_root_dir = QStandardPaths::writableLocation(QStandardPaths::GenericDataLocation);

static const char *db_setup[] = {
"PRAGMA temp_store = MEMORY",
"PRAGMA journal_mode = WAL",
Expand Down Expand Up @@ -345,9 +348,14 @@ QSqlQuery CommHistoryDatabase::prepare(const char *statement, const QSqlDatabase
return query;
}

void CommHistoryDatabasePath::setRootDir(const QString &rootDir)
{
db_root_dir = rootDir;
}

QString CommHistoryDatabasePath::databaseDir()
{
return QStandardPaths::writableLocation(QStandardPaths::GenericDataLocation) + QLatin1String(COMMHISTORY_DATABASE_DIR);
return db_root_dir + QLatin1String(COMMHISTORY_DATABASE_DIR);
}

QString CommHistoryDatabasePath::databaseFile()
Expand All @@ -357,7 +365,7 @@ QString CommHistoryDatabasePath::databaseFile()

QString CommHistoryDatabasePath::dataDir()
{
return QStandardPaths::writableLocation(QStandardPaths::GenericDataLocation) + QStringLiteral(COMMHISTORY_DATA_DIR);
return db_root_dir + QStringLiteral(COMMHISTORY_DATA_DIR);
}

QString CommHistoryDatabasePath::dataDir(int id)
Expand Down
2 changes: 2 additions & 0 deletions src/commhistorydatabasepath.h
Expand Up @@ -32,6 +32,8 @@ class LIBCOMMHISTORY_EXPORT CommHistoryDatabasePath
static QString databaseFile();
static QString dataDir();
static QString dataDir(int id);

static void setRootDir(const QString &rootDir);
};

#endif
44 changes: 20 additions & 24 deletions tests/common.cpp
Expand Up @@ -56,6 +56,7 @@
#include "common.h"
#include "commonutils.h"
#include "contactlistener.h"
#include "commhistorydatabasepath.h"

QTCONTACTS_USE_NAMESPACE

Expand Down Expand Up @@ -142,6 +143,13 @@ quint64 idleTicks = 0;
QSet<QContactId> addedContactIds;
QSet<int> addedEventIds;

void initTestDatabase()
{
deleteAll();

CommHistoryDatabasePath::setRootDir(TEST_DATABASE_DIR);
}

int addTestEvent(EventModel &model,
Event::EventType type,
Event::EventDirection direction,
Expand Down Expand Up @@ -369,29 +377,6 @@ void deleteTestContact(int id)
addedContactIds.remove(contactId);
}

void cleanUpTestContacts()
{
if (!addedContactIds.isEmpty()) {
QString aggregatesType = QContactRelationship::Aggregates();

const QList<QContactId> contactIdsToRemove = addedContactIds.toList();
QList<QContact> contactsToRemove;
for (const QContactId &contactId : addedContactIds) {
const QContactId localId = localContactForAggregate(contactId);
QContact localContact = manager()->contact(localId);
if (!localContact.isEmpty()) {
contactsToRemove.append(localContact);
}
}

if (!SeasideCache::removeContacts(contactsToRemove)) {
qWarning() << "Unable to remove test contacts:" << addedContactIds;
}

addedContactIds.clear();
}
}

void cleanupTestGroups()
{
GroupModel groupModel;
Expand Down Expand Up @@ -503,9 +488,20 @@ void deleteAll()
{
qDebug() << Q_FUNC_INFO << "- Deleting all";

cleanUpTestContacts();
cleanupTestGroups();
cleanupTestEvents();

if (!QDir(TEST_DATABASE_DIR).removeRecursively()) {
qWarning() << "Unable to remove test database directory:" << TEST_DATABASE_DIR;
}

if (!qgetenv("LIBCONTACTS_TEST_MODE").isEmpty()) {
QString contactsDbDir = QStandardPaths::writableLocation(QStandardPaths::GenericDataLocation)
+ QStringLiteral("/system/privileged/Contacts/qtcontacts-sqlite-test");
if (!QDir(contactsDbDir).removeRecursively()) {
qWarning() << "Unable to remove test contacts database directory:" << contactsDbDir;
}
}
}

QString randomMessage(int words)
Expand Down
7 changes: 6 additions & 1 deletion tests/common.h
Expand Up @@ -30,6 +30,7 @@

#include <QFile>
#include <QSignalSpy>
#include <QStandardPaths>

#include <iterator>

Expand All @@ -51,6 +52,9 @@ void waitForSignal(QObject *object, const char *signal);

using namespace CommHistory;

const QString TEST_DATABASE_DIR = QStandardPaths::writableLocation(QStandardPaths::TempLocation)
+ "/tests";

const QString ACCOUNT1 = "/org/freedesktop/Telepathy/Account/gabble/jabber/dut_40localhost0";
const QString ACCOUNT2 = "/org/freedesktop/Telepathy/Account/gabble/jabber/dut2_40localhost0";

Expand All @@ -65,6 +69,8 @@ const int WAIT_SIGNAL_TIMEOUT = 5000;
/* Duration of phone calls added with addTestEvent */
const int TESTCALL_SECS = 100;

void initTestDatabase();

int addTestEvent(EventModel &model,
Event::EventType type,
Event::EventDirection direction,
Expand All @@ -87,7 +93,6 @@ bool addTestContactAddress(int contactId, const QString &remoteUid, const QStrin
void modifyTestContact(int id, const QString &name, bool favorite = false);
void deleteTestContact(int id);
QContactId localContactForAggregate(const QContactId &contactId);
void cleanUpTestContacts();
void cleanupTestGroups();
void cleanupTestEvents();
bool compareEvents(Event &e1, Event &e2);
Expand Down
2 changes: 1 addition & 1 deletion tests/mem_eventmodel/mem_eventmodel.cpp
Expand Up @@ -40,7 +40,7 @@ Group group;

void MemEventModelTest::initTestCase()
{
deleteAll();
initTestDatabase();

MALLINFO_DUMP("INIT");

Expand Down
4 changes: 2 additions & 2 deletions tests/perf_callmodel/callmodelperftest.cpp
Expand Up @@ -32,15 +32,15 @@ using namespace CommHistory;

void CallModelPerfTest::initTestCase()
{
initTestDatabase();

logFile = new QFile("libcommhistory-performance-test.log");
if(!logFile->open(QIODevice::Append)) {
qDebug() << "!!!! Failed to open log file !!!!";
logFile = 0;
}

qsrand( QDateTime::currentDateTime().toTime_t() );

deleteAll();
}

void CallModelPerfTest::init()
Expand Down
4 changes: 2 additions & 2 deletions tests/perf_conversationmodel/conversationmodelperftest.cpp
Expand Up @@ -33,15 +33,15 @@ using namespace CommHistory;

void ConversationModelPerfTest::initTestCase()
{
initTestDatabase();

logFile = new QFile("libcommhistory-performance-test.log");
if(!logFile->open(QIODevice::Append)) {
qDebug() << "!!!! Failed to open log file !!!!";
logFile = 0;
}

qsrand( QDateTime::currentDateTime().toTime_t() );

deleteAll();
}

void ConversationModelPerfTest::init()
Expand Down
4 changes: 2 additions & 2 deletions tests/perf_groupmodel/groupmodelperftest.cpp
Expand Up @@ -33,15 +33,15 @@ using namespace CommHistory;

void GroupModelPerfTest::initTestCase()
{
initTestDatabase();

logFile = new QFile("libcommhistory-performance-test.log");
if(!logFile->open(QIODevice::Append)) {
qDebug() << "!!!! Failed to open log file !!!!";
logFile = 0;
}

qsrand( QDateTime::currentDateTime().toTime_t() );

deleteAll();
}

void GroupModelPerfTest::init()
Expand Down
Expand Up @@ -33,15 +33,15 @@ using namespace CommHistory;

void RecentContactsModelPerfTest::initTestCase()
{
initTestDatabase();

logFile = new QFile("libcommhistory-performance-test.log");
if(!logFile->open(QIODevice::Append)) {
qDebug() << "!!!! Failed to open log file !!!!";
logFile = 0;
}

qsrand( QDateTime::currentDateTime().toTime_t() );

deleteAll();
}

void RecentContactsModelPerfTest::init()
Expand Down
2 changes: 2 additions & 0 deletions tests/profile_callmodel/callmodelprofiletest.cpp
Expand Up @@ -34,6 +34,8 @@ using namespace CommHistory;

void CallModelProfileTest::initTestCase()
{
initTestDatabase();

logFile = 0;

qsrand( QDateTime::currentDateTime().toTime_t() );
Expand Down
Expand Up @@ -35,6 +35,8 @@ using namespace CommHistory;

void ConversationModelProfileTest::initTestCase()
{
initTestDatabase();

logFile = 0;

qsrand( QDateTime::currentDateTime().toTime_t() );
Expand Down
2 changes: 2 additions & 0 deletions tests/profile_groupmodel/groupmodelprofiletest.cpp
Expand Up @@ -33,6 +33,8 @@ using namespace CommHistory;

void GroupModelProfileTest::initTestCase()
{
initTestDatabase();

logFile = 0;

qsrand( QDateTime::currentDateTime().toTime_t() );
Expand Down
Expand Up @@ -33,6 +33,8 @@ using namespace CommHistory;

void RecentContactsModelProfileTest::initTestCase()
{
initTestDatabase();

logFile = 0;

qsrand( QDateTime::currentDateTime().toTime_t() );
Expand Down
2 changes: 1 addition & 1 deletion tests/ut_callmodel/callmodeltest.cpp
Expand Up @@ -60,9 +60,9 @@ QList<TestCallItem> testCalls;

void CallModelTest::initTestCase()
{
initTestDatabase();
QVERIFY( QDBusConnection::sessionBus().isConnected() );

deleteAll();
QTest::qWait(100);

qsrand( QDateTime::currentDateTime().toTime_t() );
Expand Down
3 changes: 1 addition & 2 deletions tests/ut_conversationmodel/conversationmodeltest.cpp
Expand Up @@ -40,10 +40,9 @@ ModelWatcher watcher, watcher2;

void ConversationModelTest::initTestCase()
{
initTestDatabase();
QVERIFY(QDBusConnection::sessionBus().isConnected());

deleteAll();

loop = new QEventLoop(this);

qsrand(QDateTime::currentDateTime().toTime_t());
Expand Down
2 changes: 1 addition & 1 deletion tests/ut_eventmodel/eventmodeltest.cpp
Expand Up @@ -60,7 +60,7 @@ void EventModelTest::groupsDeletedSlot(const QList<int> &groupIds)

void EventModelTest::initTestCase()
{
deleteAll();
initTestDatabase();

qsrand(QDateTime::currentDateTime().toTime_t());

Expand Down
3 changes: 1 addition & 2 deletions tests/ut_groupmodel/groupmodeltest.cpp
Expand Up @@ -99,8 +99,7 @@ void GroupModelTest::dataChangedSlot(const QModelIndex &start, const QModelIndex

void GroupModelTest::initTestCase()
{
deleteAll();
QTest::qWait(100);
initTestDatabase();

QVERIFY(QDBusConnection::sessionBus().isConnected());

Expand Down
3 changes: 1 addition & 2 deletions tests/ut_recentcontactsmodel/recentcontactsmodeltest.cpp
Expand Up @@ -126,10 +126,9 @@ private Q_SLOTS:

void RecentContactsModelTest::initTestCase()
{
initTestDatabase();
QVERIFY(QDBusConnection::sessionBus().isConnected());

deleteAll();

qsrand(QDateTime::currentDateTime().toTime_t());

addTestGroups( group1, group2 );
Expand Down
2 changes: 1 addition & 1 deletion tests/ut_recipienteventmodel/recipienteventmodeltest.cpp
Expand Up @@ -34,7 +34,7 @@ Group group1, group2;

void RecipientEventModelTest::initTestCase()
{
deleteAll();
initTestDatabase();

qsrand(QDateTime::currentDateTime().toTime_t());
}
Expand Down
2 changes: 1 addition & 1 deletion tests/ut_singleeventmodel/singleeventmodeltest.cpp
Expand Up @@ -18,7 +18,7 @@ ModelWatcher watcher;

void SingleEventModelTest::initTestCase()
{
deleteAll();
initTestDatabase();

qsrand(QDateTime::currentDateTime().toTime_t());

Expand Down

0 comments on commit 2b5ade9

Please sign in to comment.