82 lines
2.5 KiB
C++
82 lines
2.5 KiB
C++
#include <QCoreApplication>
|
|
#include <QFileInfo>
|
|
#include <QSettings>
|
|
#include <QTemporaryDir>
|
|
#include <QtTest>
|
|
|
|
#include <QxOrm.h>
|
|
|
|
#include "context.h"
|
|
#include "service.h"
|
|
#include "../countryregister/data/countrydata.h"
|
|
|
|
class PersistenceTests : public QObject
|
|
{
|
|
Q_OBJECT
|
|
|
|
private slots:
|
|
void crudCycleOnTemporaryDatabase()
|
|
{
|
|
QTemporaryDir dir;
|
|
QVERIFY(dir.isValid());
|
|
const QString dbPath = dir.filePath(QStringLiteral("countries.db"));
|
|
|
|
qx::QxSqlDatabase::getSingleton()->setDriverName(QStringLiteral("QSQLITE"));
|
|
qx::QxSqlDatabase::getSingleton()->setDatabaseName(dbPath);
|
|
|
|
const QSqlError createError = qx::dao::create_table<CountryData>();
|
|
QVERIFY2(!createError.isValid(), qPrintable(createError.text()));
|
|
QVERIFY(QFileInfo::exists(dbPath));
|
|
|
|
Service<CountryData> service;
|
|
|
|
auto country = QSharedPointer<CountryData>::create();
|
|
country->setCode2(QStringLiteral("CZ"));
|
|
country->setCode3(QStringLiteral("CZE"));
|
|
country->setCzechName(QStringLiteral("Česko"));
|
|
country->setEnglishName(QStringLiteral("Czechia"));
|
|
|
|
QSignalSpy errorSpy(&service, &Service<CountryData>::dbError);
|
|
service.save(country);
|
|
QCOMPARE(errorSpy.count(), 0);
|
|
QVERIFY(country->id() > 0);
|
|
|
|
const QList<QSharedPointer<CountryData>> all = service.all();
|
|
QCOMPARE(all.size(), 1);
|
|
QCOMPARE(all.first()->code2(), QStringLiteral("CZ"));
|
|
QCOMPARE(all.first()->czechName(), QStringLiteral("Česko"));
|
|
|
|
auto loaded = service.loadById(country->id());
|
|
QVERIFY(!loaded.isNull());
|
|
QCOMPARE(loaded->code3(), QStringLiteral("CZE"));
|
|
|
|
loaded->setCzechName(QStringLiteral("Česká republika"));
|
|
service.update(loaded);
|
|
QCOMPARE(errorSpy.count(), 0);
|
|
|
|
auto reloaded = service.loadById(country->id());
|
|
QCOMPARE(reloaded->czechName(), QStringLiteral("Česká republika"));
|
|
|
|
service.erase(reloaded);
|
|
QCOMPARE(errorSpy.count(), 0);
|
|
QVERIFY(service.all().isEmpty());
|
|
}
|
|
};
|
|
|
|
int main(int argc, char *argv[])
|
|
{
|
|
QCoreApplication app(argc, argv);
|
|
QTemporaryDir settingsDirectory;
|
|
if (!settingsDirectory.isValid()) {
|
|
return 1;
|
|
}
|
|
|
|
QSettings::setDefaultFormat(QSettings::IniFormat);
|
|
QSettings::setPath(QSettings::IniFormat, QSettings::UserScope, settingsDirectory.path());
|
|
|
|
PersistenceTests tests;
|
|
return QTest::qExec(&tests, argc, argv);
|
|
}
|
|
|
|
#include "persistence_tests.moc"
|