Commit 6e0c5b6c authored by Volker Krause's avatar Volker Krause
Browse files

Factor out duplicated test setup helper functions

parent f646b0ed
......@@ -4,6 +4,8 @@
SPDX-License-Identifier: LGPL-2.0-or-later
*/
#include "testhelper.h"
#include <applicationcontroller.h>
#include <pkpassmanager.h>
#include <reservationmanager.h>
......@@ -25,29 +27,6 @@ class AppControllerTest : public QObject
{
Q_OBJECT
private:
void clearPasses(PkPassManager *mgr)
{
for (const auto &id : mgr->passes()) {
mgr->removePass(id);
}
}
void clearReservations(ReservationManager *mgr)
{
const auto batches = mgr->batches(); // copy, as this is getting modified in the process
for (const auto &id : batches) {
mgr->removeBatch(id);
}
QCOMPARE(mgr->batches().size(), 0);
}
void clearDocuments(DocumentManager *mgr)
{
for (const auto &id : mgr->documents()) {
mgr->removeDocument(id);
}
}
QByteArray readFile(const QString &fn)
{
QFile f(fn);
......@@ -65,12 +44,12 @@ private Q_SLOTS:
void testImportData()
{
PkPassManager passMgr;
clearPasses(&passMgr);
Test::clearAll(&passMgr);
QSignalSpy passSpy(&passMgr, &PkPassManager::passAdded);
QVERIFY(passSpy.isValid());
ReservationManager resMgr;
clearReservations(&resMgr);
Test::clearAll(&resMgr);
QSignalSpy resSpy(&resMgr, &ReservationManager::reservationAdded);
QVERIFY(resSpy.isValid());
......@@ -97,12 +76,12 @@ private Q_SLOTS:
void testImportFile()
{
PkPassManager passMgr;
clearPasses(&passMgr);
Test::clearAll(&passMgr);
QSignalSpy passSpy(&passMgr, &PkPassManager::passAdded);
QVERIFY(passSpy.isValid());
ReservationManager resMgr;
clearReservations(&resMgr);
Test::clearAll(&resMgr);
QSignalSpy resSpy(&resMgr, &ReservationManager::reservationAdded);
QVERIFY(resSpy.isValid());
......@@ -125,13 +104,13 @@ private Q_SLOTS:
void testExportFile()
{
PkPassManager passMgr;
clearPasses(&passMgr);
Test::clearAll(&passMgr);
ReservationManager resMgr;
clearReservations(&resMgr);
Test::clearAll(&resMgr);
DocumentManager docMgr;
clearDocuments(&docMgr);
Test::clearAll(&docMgr);
TripGroupManager tripGroupMgr;
tripGroupMgr.setReservationManager(&resMgr);
......@@ -169,9 +148,9 @@ private Q_SLOTS:
QCOMPARE(f.reservations().size(), 2);
QCOMPARE(f.passes().size(), 1);
clearPasses(&passMgr);
clearReservations(&resMgr);
clearDocuments(&docMgr);
Test::clearAll(&passMgr);
Test::clearAll(&resMgr);
Test::clearAll(&docMgr);
QCOMPARE(resMgr.batches().size(), 0);
QCOMPARE(passMgr.passes().size(), 0);
......@@ -180,8 +159,8 @@ private Q_SLOTS:
QCOMPARE(passMgr.passes().size(), 1);
QCOMPARE(infoSpy.size(), 4);
clearPasses(&passMgr);
clearReservations(&resMgr);
Test::clearAll(&passMgr);
Test::clearAll(&resMgr);
QCOMPARE(resMgr.batches().size(), 0);
QCOMPARE(passMgr.passes().size(), 0);
......
......@@ -4,6 +4,7 @@
SPDX-License-Identifier: LGPL-2.0-or-later
*/
#include "testhelper.h"
#include <documentmanager.h>
#include <KItinerary/CreativeWork>
......@@ -37,9 +38,7 @@ private Q_SLOTS:
void testAddRemove()
{
DocumentManager mgr;
for (const auto &id : mgr.documents()) {
mgr.removeDocument(id); // clean up previous test runs, if necessary
}
Test::clearAll(&mgr);
QSignalSpy addSpy(&mgr, &DocumentManager::documentAdded);
QSignalSpy rmSpy(&mgr, &DocumentManager::documentRemoved);
QCOMPARE(mgr.documents().size(), 0);
......
......@@ -4,6 +4,8 @@
SPDX-License-Identifier: LGPL-2.0-or-later
*/
#include "testhelper.h"
#include <livedata.h>
#include <livedatamanager.h>
#include <applicationcontroller.h>
......@@ -29,15 +31,6 @@ class LiveDataManagerTest : public QObject
{
Q_OBJECT
private:
void clearReservations(ReservationManager *mgr)
{
const auto batches = mgr->batches(); // copy, as this is getting modified in the process
for (const auto &id : batches) {
mgr->removeBatch(id);
}
QCOMPARE(mgr->batches().size(), 0);
}
QByteArray readFile(const QString &fn)
{
QFile f(fn);
......@@ -81,7 +74,7 @@ private Q_SLOTS:
void testLiveData()
{
ReservationManager resMgr;
clearReservations(&resMgr);
Test::clearAll(&resMgr);
QSignalSpy resChangeSpy(&resMgr, &ReservationManager::batchContentChanged);
LiveData::clearStorage();
......
......@@ -4,6 +4,8 @@
SPDX-License-Identifier: LGPL-2.0-or-later
*/
#include "testhelper.h"
#include <pkpassmanager.h>
#include <QUrl>
......@@ -22,14 +24,6 @@ Q_CONSTRUCTOR_FUNCTION(initLocale)
class PkPassManagerTest : public QObject
{
Q_OBJECT
private:
void clearPasses(PkPassManager *mgr)
{
for (const auto &id : mgr->passes()) {
mgr->removePass(id);
}
}
private Q_SLOTS:
void initTestCase()
{
......@@ -39,7 +33,7 @@ private Q_SLOTS:
void testImport()
{
PkPassManager mgr;
clearPasses(&mgr);
Test::clearAll(&mgr);
const auto now = QDateTime::currentDateTime().addSecs(-1);
QSignalSpy addSpy(&mgr, &PkPassManager::passAdded);
......@@ -74,7 +68,7 @@ private Q_SLOTS:
QCOMPARE(updateSpy.at(0).at(1).toStringList().at(0), QStringLiteral("Gate changed to G30."));
QVERIFY(mgr.pass(passId));
clearPasses(&mgr);
Test::clearAll(&mgr);
QCOMPARE(addSpy.size(), 1);
QCOMPARE(updateSpy.size(), 1);
QCOMPARE(rmSpy.size(), 1);
......
......@@ -4,6 +4,8 @@
SPDX-License-Identifier: LGPL-2.0-or-later
*/
#include "testhelper.h"
#include <applicationcontroller.h>
#include <reservationmanager.h>
#include <pkpassmanager.h>
......@@ -22,23 +24,6 @@ using namespace KItinerary;
class ReservationManagerTest : public QObject
{
Q_OBJECT
private:
void clearReservations(ReservationManager *mgr)
{
const auto batches = mgr->batches(); // copy, as this is getting modified in the process
for (const auto &id : batches) {
mgr->removeBatch(id);
}
QCOMPARE(mgr->batches().size(), 0);
}
void clearPasses(PkPassManager *mgr)
{
for (const auto &id : mgr->passes()) {
mgr->removePass(id);
}
}
private Q_SLOTS:
void initTestCase()
{
......@@ -48,7 +33,7 @@ private Q_SLOTS:
void testOperations()
{
ReservationManager mgr;
clearReservations(&mgr);
Test::clearAll(&mgr);
QSignalSpy addSpy(&mgr, &ReservationManager::reservationAdded);
QVERIFY(addSpy.isValid());
......@@ -87,7 +72,7 @@ private Q_SLOTS:
QVERIFY(mgr.batches().empty());
QVERIFY(mgr.reservation(resId).isNull());
clearReservations(&mgr);
Test::clearAll(&mgr);
auto attraction = KItinerary::TouristAttraction();
attraction.setName(QStringLiteral("Sky Tree"));
auto visit = KItinerary::TouristAttractionVisit();
......@@ -105,10 +90,10 @@ private Q_SLOTS:
void testPkPassChanges()
{
PkPassManager passMgr;
clearPasses(&passMgr);
Test::clearAll(&passMgr);
ReservationManager mgr;
clearReservations(&mgr);
Test::clearAll(&mgr);
ApplicationController ctrl;
ctrl.setPkPassManager(&passMgr);
......@@ -134,7 +119,7 @@ private Q_SLOTS:
void testBatchPersistence()
{
ReservationManager mgr;
clearReservations(&mgr);
Test::clearAll(&mgr);
ApplicationController ctrl;
ctrl.setReservationManager(&mgr);
......@@ -168,7 +153,7 @@ private Q_SLOTS:
void testBatchOperations()
{
ReservationManager mgr;
clearReservations(&mgr);
Test::clearAll(&mgr);
QCOMPARE(mgr.batches().size(), 0);
QSignalSpy batchAddSpy(&mgr, &ReservationManager::batchAdded);
......@@ -334,7 +319,7 @@ private Q_SLOTS:
void testCancellation()
{
ReservationManager mgr;
clearReservations(&mgr);
Test::clearAll(&mgr);
ApplicationController ctrl;
ctrl.setReservationManager(&mgr);
QCOMPARE(mgr.batches().size(), 0);
......
......@@ -4,6 +4,8 @@
SPDX-License-Identifier: LGPL-2.0-or-later
*/
#include "testhelper.h"
#include <statisticsmodel.h>
#include <statisticstimerangemodel.h>
#include <applicationcontroller.h>
......@@ -27,16 +29,6 @@ Q_CONSTRUCTOR_FUNCTION(initLocale)
class StatisticsTest : public QObject
{
Q_OBJECT
private:
void clearReservations(ReservationManager *mgr)
{
const auto batches = mgr->batches(); // copy, as this is getting modified in the process
for (const auto &id : batches) {
mgr->removeBatch(id);
}
QCOMPARE(mgr->batches().size(), 0);
}
private Q_SLOTS:
void initTestCase()
{
......@@ -50,7 +42,7 @@ private Q_SLOTS:
void testStats()
{
ReservationManager resMgr;
clearReservations(&resMgr);
Test::clearAll(&resMgr);
TripGroupManager::clear();
TripGroupManager tgMgr;
tgMgr.setReservationManager(&resMgr);
......@@ -146,7 +138,7 @@ private Q_SLOTS:
void testTimeRangeModel()
{
ReservationManager resMgr;
clearReservations(&resMgr);
Test::clearAll(&resMgr);
ApplicationController ctrl;
ctrl.setReservationManager(&resMgr);
......
/*
SPDX-FileCopyrightText: 2018-2021 Volker Krause <vkrause@kde.org>
SPDX-License-Identifier: LGPL-2.0-or-later
*/
#ifndef TESTHELPER_H
#define TESTHELPER_H
#include <documentmanager.h>
#include <pkpassmanager.h>
#include <reservationmanager.h>
namespace Test
{
/** Delete all reservations. */
inline void clearAll(ReservationManager *mgr)
{
const auto batches = mgr->batches(); // copy, as this is getting modified in the process
for (const auto &id : batches) {
mgr->removeBatch(id);
}
Q_ASSERT(mgr->batches().empty());
}
/** Delete all passes. */
inline void clearAll(PkPassManager *mgr)
{
for (const auto &id : mgr->passes()) {
mgr->removePass(id);
}
Q_ASSERT(mgr->passes().isEmpty());
}
/** Delete all documents. */
inline void clearAll(DocumentManager *docMgr)
{
for (const auto &id : docMgr->documents()) {
docMgr->removeDocument(id);
}
Q_ASSERT(docMgr->documents().isEmpty());
}
}
#endif
......@@ -4,6 +4,8 @@
SPDX-License-Identifier: LGPL-2.0-or-later
*/
#include "testhelper.h"
#include <timelinedelegatecontroller.h>
#include <applicationcontroller.h>
#include <livedatamanager.h>
......@@ -28,15 +30,6 @@ class TimelineDelegateControllerTest : public QObject
{
Q_OBJECT
private:
void clearReservations(ReservationManager *mgr)
{
const auto batches = mgr->batches(); // copy, as this is getting modified in the process
for (const auto &id : batches) {
mgr->removeBatch(id);
}
QCOMPARE(mgr->batches().size(), 0);
}
QByteArray readFile(const QString &fn)
{
QFile f(fn);
......@@ -83,7 +76,7 @@ private Q_SLOTS:
void testProgress()
{
ReservationManager mgr;
clearReservations(&mgr);
Test::clearAll(&mgr);
TrainTrip trip;
trip.setTrainNumber(QStringLiteral("TGV 1235"));
......@@ -120,7 +113,7 @@ private Q_SLOTS:
void testPreviousLocation()
{
ReservationManager mgr;
clearReservations(&mgr);
Test::clearAll(&mgr);
TimelineDelegateController controller;
controller.setReservationManager(&mgr);
......@@ -163,7 +156,7 @@ private Q_SLOTS:
void testJourneyRequest()
{
ReservationManager mgr;
clearReservations(&mgr);
Test::clearAll(&mgr);
ApplicationController ctrl;
ctrl.setReservationManager(&mgr);
ctrl.importFromUrl(QUrl::fromLocalFile(QLatin1String(SOURCE_DIR "/../tests/randa2017.json")));
......@@ -192,7 +185,7 @@ private Q_SLOTS:
void testApplyJourney()
{
ReservationManager mgr;
clearReservations(&mgr);
Test::clearAll(&mgr);
LiveData::clearStorage();
LiveDataManager ldm;
ldm.setReservationManager(&mgr);
......@@ -237,7 +230,7 @@ private Q_SLOTS:
void testCancel()
{
ReservationManager mgr;
clearReservations(&mgr);
Test::clearAll(&mgr);
ApplicationController ctrl;
ctrl.setReservationManager(&mgr);
ctrl.importFromUrl(QUrl::fromLocalFile(QLatin1String(SOURCE_DIR "/data/timeline/flight-cancelation.json")));
......
......@@ -5,6 +5,7 @@
*/
#include "modelverificationpoint.h"
#include "testhelper.h"
#include <applicationcontroller.h>
#include <favoritelocationmodel.h>
......@@ -41,23 +42,6 @@ Q_CONSTRUCTOR_FUNCTION(initLocale)
class TimelineModelTest : public QObject
{
Q_OBJECT
private:
void clearPasses(PkPassManager *mgr)
{
for (const auto &id : mgr->passes()) {
mgr->removePass(id);
}
}
void clearReservations(ReservationManager *mgr)
{
const auto batches = mgr->batches(); // copy, as this is getting modified in the process
for (const auto &id : batches) {
mgr->removeBatch(id);
}
QCOMPARE(mgr->batches().size(), 0);
}
private Q_SLOTS:
void initTestCase()
{
......@@ -103,9 +87,9 @@ private Q_SLOTS:
void testModel()
{
PkPassManager mgr;
clearPasses(&mgr);
Test::clearAll(&mgr);
ReservationManager resMgr;
clearReservations(&resMgr);
Test::clearAll(&resMgr);
ApplicationController ctrl;
ctrl.setPkPassManager(&mgr);
ctrl.setReservationManager(&resMgr);
......@@ -137,7 +121,7 @@ private Q_SLOTS:
QCOMPARE(updateSpy.at(0).at(0).toModelIndex().row(), 0);
QCOMPARE(model.rowCount(), 2);
clearReservations(&resMgr);
Test::clearAll(&resMgr);
QCOMPARE(insertSpy.size(), 1);
QCOMPARE(updateSpy.size(), 1);
QCOMPARE(rmSpy.size(), 1);
......@@ -147,7 +131,7 @@ private Q_SLOTS:
void testNestedElements()
{
ReservationManager resMgr;
clearReservations(&resMgr);
Test::clearAll(&resMgr);
ApplicationController ctrl;
ctrl.setReservationManager(&resMgr);
......@@ -202,7 +186,7 @@ private Q_SLOTS:
void testCountryInfos()
{
ReservationManager resMgr;
clearReservations(&resMgr);
Test::clearAll(&resMgr);
ApplicationController ctrl;
ctrl.setReservationManager(&resMgr);
......@@ -252,7 +236,7 @@ private Q_SLOTS:
using namespace KItinerary;
ReservationManager resMgr;
clearReservations(&resMgr);
Test::clearAll(&resMgr);
WeatherForecastManager weatherMgr;
weatherMgr.setTestModeEnabled(true);
......@@ -398,7 +382,7 @@ private Q_SLOTS:
using namespace KItinerary;
ReservationManager resMgr;
clearReservations(&resMgr);
Test::clearAll(&resMgr);
ApplicationController ctrl;
ctrl.setReservationManager(&resMgr);
TimelineModel model;
......@@ -464,7 +448,7 @@ private Q_SLOTS:
// removal of merged items
updateSpy.clear();
rmSpy.clear();
clearReservations(&resMgr);
Test::clearAll(&resMgr);
QCOMPARE(model.rowCount(), 1);
QCOMPARE(rmSpy.count(), 2);
QCOMPARE(updateSpy.count(), 2);
......@@ -473,7 +457,7 @@ private Q_SLOTS:
void testDayChange()
{
ReservationManager resMgr;
clearReservations(&resMgr);
Test::clearAll(&resMgr);
ApplicationController ctrl;
ctrl.setReservationManager(&resMgr);
WeatherForecastManager weatherMgr;
......@@ -525,7 +509,7 @@ private Q_SLOTS:
{
QFETCH(QString, baseName);
ReservationManager resMgr;
clearReservations(&resMgr);
Test::clearAll(&resMgr);
ApplicationController ctrl;
ctrl.setReservationManager(&resMgr);
ctrl.importFromUrl(QUrl::fromLocalFile(QLatin1String(SOURCE_DIR "/data/timeline/") + baseName + QLatin1String(".json")));
......@@ -568,7 +552,7 @@ private Q_SLOTS:
QVERIFY(vp.verify(&model));
// retry with loading during runtime
clearReservations(&resMgr);
Test::clearAll(&resMgr);
ctrl.importFromUrl(QUrl::fromLocalFile(QLatin1String(SOURCE_DIR "/data/timeline/") + baseName + QLatin1String(".json")));
QVERIFY(vp.verify(&model));
}
......
......@@ -4,6 +4,8 @@
SPDX-License-Identifier: LGPL-2.0-or-later
*/
#include "testhelper.h"
#include <transfer.h>
#include <transfermanager.h>
#include <applicationcontroller.h>
......@@ -19,16 +21,6 @@
class TransferTest : public QObject
{
Q_OBJECT
private:
void clearReservations(ReservationManager *mgr)
{
const auto batches = mgr->batches(); // copy, as this is getting modified in the process
for (const auto &id : batches) {
mgr->removeBatch(id);
}
QCOMPARE(mgr->batches().size(), 0);
}
private Q_SLOTS:
void initTestCase()
{
......@@ -41,7 +33,7 @@ private Q_SLOTS:
void testTransferManager()
{
ReservationManager resMgr;
clearReservations(&resMgr);
Test::clearAll(&resMgr);
ApplicationController ctrl;
ctrl.setReservationManager(&resMgr);
......
......@@ -4,6 +4,8 @@
SPDX-License-Identifier: LGPL-2.0-or-later
*/
#include "testhelper.h"
#include <applicationcontroller.h>
#include <locationinformation.h>
#include <reservationmanager.h>
......@@ -20,16 +22,6 @@
class TripGroupInfoProviderTest : public QObject
{
Q_OBJECT
private:
void clearReservations(ReservationManager *mgr)
{
const auto batches = mgr->batches(); // copy, as this is getting modified in the process
for (const auto &id : batches) {
mgr->removeBatch(id);
}
QCOMPARE(mgr->batches().size(), 0);
}