Commit b8753a43 authored by Andreas Cord-Landwehr's avatar Andreas Cord-Landwehr
Browse files

Set structure for database storage unit tests.

parent f75f4160
......@@ -135,5 +135,4 @@ void TestLanguageFiles::checkIdUniqueness()
}
}
QTEST_GUILESS_MAIN(TestLanguageFiles)
ecm_optional_add_subdirectory(src)
ecm_optional_add_subdirectory(autotests)
###
# Copyright 2016 Andreas Cord-Landwehr <cordlandwehr@kde.org>
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
#
# 1. Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in the
# documentation and/or other materials provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
# IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
# OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
# IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
# NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
# THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
###
include_directories(
../src/
../
${CMAKE_CURRENT_BINARY_DIR}
)
set(EXECUTABLE_OUTPUT_PATH ${CMAKE_CURRENT_BINARY_DIR})
set(TestLearnerStorage_SRCS
testlearnerstorage.cpp
../src/storage.cpp
../src/liblearner_debug.cpp
)
add_executable(TestLearnerStorage ${TestLearnerStorage_SRCS} )
target_link_libraries(TestLearnerStorage
artikulatelearnerprofile
Qt5::Test
)
add_test(TestLearnerStorage TestLearnerStorage)
ecm_mark_as_test(TestLearnerStorage)
/*
* Copyright 2016 Andreas Cord-Landwehr <cordlandwehr@kde.org>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation; either version 2 of
* the License or (at your option) version 3 or any later version
* accepted by the membership of KDE e.V. (or its successor approved
* by the membership of KDE e.V.), which shall act as a proxy
* defined in Section 14 of version 3 of the license.
*
* This program 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "testlearnerstorage.h"
#include <QTest>
#include <QTemporaryFile>
#include "storage.h"
using namespace LearnerProfile;
TestLearnerStorage::TestLearnerStorage()
: m_storage(nullptr)
{
}
void TestLearnerStorage::init()
{
QVERIFY(m_db.open());
m_storage = new Storage(m_db.fileName(), nullptr);
}
void TestLearnerStorage::cleanup()
{
m_db.close();
}
QTEST_GUILESS_MAIN(TestLearnerStorage)
/*
* Copyright 2016 Andreas Cord-Landwehr <cordlandwehr@kde.org>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation; either version 2 of
* the License or (at your option) version 3 or any later version
* accepted by the membership of KDE e.V. (or its successor approved
* by the membership of KDE e.V.), which shall act as a proxy
* defined in Section 14 of version 3 of the license.
*
* This program 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef TESTLEARNERSTORAGE_H
#define TESTLEARNERSTORAGE_H
#include <QObject>
#include <QTemporaryFile>
namespace LearnerProfile {
class Storage;
}
class TestLearnerStorage : public QObject
{
Q_OBJECT
public:
TestLearnerStorage();
private Q_SLOTS:
/**
* Called before every test case.
*/
void init();
/**
* Called after every test case.
*/
void cleanup();
private:
LearnerProfile::Storage *m_storage;
QTemporaryFile m_db;
};
#endif
......@@ -35,11 +35,21 @@ using namespace LearnerProfile;
Storage::Storage(QObject* parent)
: QObject(parent)
, m_databasePath(QStandardPaths::writableLocation(
QStandardPaths::DataLocation) + QLatin1Char('/') + "learnerdata.db")
, m_errorMessage(QString())
{
}
Storage::Storage(const QString databasePath, QObject* parent)
: QObject(parent)
, m_databasePath(databasePath)
, m_errorMessage(QString())
{
qCDebug(LIBLEARNER_LOG) << "Initialize with custom DB path:" << m_databasePath;
}
QString Storage::errorMessage() const
{
return m_errorMessage;
......@@ -320,7 +330,6 @@ QList< LearningGoal* > Storage::loadGoals()
return goals;
}
bool Storage::storeProgress(Learner *learner, LearningGoal *goal,
const QString &container, const QString &item, int payload,
const QString &time)
......@@ -353,17 +362,15 @@ QSqlDatabase Storage::database()
return QSqlDatabase::database(QSqlDatabase::defaultConnection);
}
QString path = QStandardPaths::writableLocation(QStandardPaths::DataLocation) + QLatin1Char('/') + "learnerdata.db";
// create data directory if it does not exist
QDir dir = QDir(QStandardPaths::writableLocation(QStandardPaths::DataLocation));
if (!dir.exists()) {
dir.mkpath(QStandardPaths::writableLocation(QStandardPaths::DataLocation));
}
qCDebug(LIBLEARNER_LOG) << "Database path: " << path;
qCDebug(LIBLEARNER_LOG) << "Database path: " << m_databasePath;
QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
db.setDatabaseName(path);
db.setDatabaseName(m_databasePath);
if (!db.open()) {
qCritical() << "Could not open database: " << db.lastError().text();
raiseError(db.lastError());
......
......@@ -32,13 +32,25 @@ namespace LearnerProfile
class Learner;
class LearningGoal;
/**
* \class Storage
* Database storage for learner information database.
*/
class Storage : public QObject
{
Q_OBJECT
Q_PROPERTY(QString errorMessage READ errorMessage NOTIFY errorMessageChanged)
public:
/**
* Default constructor, which sets a default database path at
* DataLocation + learnerdata.db
*/
explicit Storage(QObject* parent = nullptr);
/**
* \note this constructor is tailored for unit tests
*/
Storage(const QString databasePath, QObject* parent = nullptr);
QString errorMessage() const;
/**
......@@ -64,6 +76,7 @@ protected:
private:
bool updateSchema();
const QString m_databasePath;
QString m_errorMessage;
};
}
......
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