Commit a2447974 authored by Boudewijn Rempt's avatar Boudewijn Rempt

Add a basic unittest to check whether the database is created

parent 0e7683d3
add_subdirectory(tests)
set(kritaresources_LIB_SRCS
KisResourceCacheDb.cpp
)
qt5_add_resources(kritawidgetutils_LIB_SRCS sql.qrc)
# help plugin trader find installed plugins when run from uninstalled tests
if (BUILD_TESTING)
add_definitions(-DCMAKE_INSTALL_PREFIX="${CMAKE_INSTALL_PREFIX}")
endif()
qt5_add_resources(kritaresources_LIB_SRCS sql.qrc)
add_library(kritaresources SHARED ${kritaresources_LIB_SRCS})
generate_export_header(kritaresources BASE_NAME kritaresources)
target_link_libraries(kritaresources
......
......@@ -20,6 +20,8 @@
#include <QtSql>
#include <QStandardPaths>
#include <QDir>
#include <QDirIterator>
#include <kconfiggroup.h>
#include <ksharedconfig.h>
......@@ -34,11 +36,13 @@ public:
QSqlError initDb();
};
KisResourceCacheDb::KisResourceCacheDb(QObject *parent)
: QObject(parent)
, d(new Private())
KisResourceCacheDb::KisResourceCacheDb()
: d(new Private())
{
QSqlError err = d->initDb();
if (err.isValid()) {
qWarning() << "Could not initialize the database:" << err;
}
}
KisResourceCacheDb::~KisResourceCacheDb()
......@@ -50,8 +54,15 @@ QSqlError KisResourceCacheDb::Private::initDb()
QSqlDatabase db = QSqlDatabase::addDatabase(dbDriver);
const KConfigGroup group(KSharedConfig::openConfig(), "ResourceManagement");
QString dbFile(group.readEntry<QString>("cachedb", QStandardPaths::writableLocation(QStandardPaths::AppDataLocation) + "/resourcecache"));
QDir dbLocation(group.readEntry<QString>("cachedb", QStandardPaths::writableLocation(QStandardPaths::AppDataLocation)));
if (!dbLocation.exists()) {
dbLocation.mkpath(dbLocation.path());
}
QString dbFile(group.readEntry<QString>("cachedb", QStandardPaths::writableLocation(QStandardPaths::AppDataLocation) + "/" + ResourceCacheDbFilename));
db.setDatabaseName(dbFile);
if (!db.open()) {
return db.lastError();
}
......@@ -65,12 +76,11 @@ QSqlError KisResourceCacheDb::Private::initDb()
<< "versioned_resources"
<< "resource_tags";
QSqlQuery query;
Q_FOREACH(const QString &table, tables) {
QFile f(":/create_" + table + ".sql");
Q_ASSERT(f.exists());
qDebug() << "Running SQL" << f;
if (f.open(QFile::ReadOnly)) {
QString sql(f.readAll());
if (!query.exec(sql)) {
......
......@@ -24,15 +24,23 @@
#include <QScopedPointer>
#include <kritaresources_export.h>
const QString ResourceCacheDbFilename = "resourcecache.sqlite";
/**
* @brief The KisResourceCacheDb class encapsulates the database that
* caches information about the resources available to the user.
*/
class KisResourceCacheDb : public QObject
class KRITARESOURCES_EXPORT KisResourceCacheDb
{
Q_OBJECT
public:
explicit KisResourceCacheDb(QObject *parent = nullptr);
enum class ResourceCacheDbStatus {
OK,
NoDatabase,
};
KisResourceCacheDb();
~KisResourceCacheDb();
private:
......
set( EXECUTABLE_OUTPUT_PATH ${CMAKE_CURRENT_BINARY_DIR} )
include(ECMAddTests)
include(KritaAddBrokenUnitTest)
macro_add_unittest_definitions()
ecm_add_tests(
TestResourceCacheDb
NAME_PREFIX "libs-kritaresources-"
LINK_LIBRARIES kritaresources KF5::ConfigCore Qt5::Sql Qt5::Test)
/*
* Copyright (C) 2017 Boudewijn Rempt <boud@valdyas.org>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public License
* along with this library; see the file COPYING.LIB. If not, write to
* the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301, USA.
*/
#include "TestResourceCacheDb.h"
#include <QTest>
#include <QtSql>
#include <QStandardPaths>
#include <QDir>
#include <kconfiggroup.h>
#include <ksharedconfig.h>
#include <KisResourceCacheDb.h>
void TestResourceCacheDb::initTestCase()
{
const KConfigGroup group(KSharedConfig::openConfig(), "ResourceManagement");
QDir dbLocation(group.readEntry<QString>("cachedb", QStandardPaths::writableLocation(QStandardPaths::AppDataLocation)));
if (dbLocation.exists()) {
QFile(dbLocation.path() + "/" + ResourceCacheDbFilename).remove();
dbLocation.rmpath(dbLocation.path());
}
}
void TestResourceCacheDb::testCreateDatabase()
{
KisResourceCacheDb cacheDb;
Q_UNUSED(cacheDb);
QSqlDatabase sqlDb = QSqlDatabase::database();
QStringList tables = QStringList() << "origin_types"
<< "resource_types"
<< "stores"
<< "tags"
<< "resources"
<< "translations"
<< "versioned_resources"
<< "resource_tags";
QStringList dbTables = sqlDb.tables();
Q_FOREACH(const QString &table, tables) {
QVERIFY2(dbTables.contains(table), table.toLatin1());
}
}
void TestResourceCacheDb::cleanupTestCase()
{
const KConfigGroup group(KSharedConfig::openConfig(), "ResourceManagement");
QDir dbLocation(group.readEntry<QString>("cachedb", QStandardPaths::writableLocation(QStandardPaths::AppDataLocation)));
bool res = QFile(dbLocation.path() + "/" + ResourceCacheDbFilename).remove();
Q_ASSERT(res);
res = dbLocation.rmpath(dbLocation.path());
Q_ASSERT(res);
}
QTEST_MAIN(TestResourceCacheDb)
/*
* Copyright (C) 2017 Boudewijn Rempt <boud@valdyas.org>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public License
* along with this library; see the file COPYING.LIB. If not, write to
* the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301, USA.
*/
#ifndef TESTRESOURCECACHEDB_H
#define TESTRESOURCECACHEDB_H
#include <QObject>
class TestResourceCacheDb : public QObject
{
Q_OBJECT
private Q_SLOTS:
void initTestCase();
void testCreateDatabase();
void cleanupTestCase();
private:
};
#endif
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment