| 1 |
/* |
| 2 |
SPDX-FileCopyrightText: 2016 Andreas Cord-Landwehr <[email protected]> |
| 3 |
|
| 4 |
SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL |
| 5 |
*/ |
| 6 |
|
| 7 |
#include "testlearnerstorage.h" |
| 8 |
#include "learner.h" |
| 9 |
#include "learninggoal.h" |
| 10 |
#include "storage.h" |
| 11 |
|
| 12 |
#include <QTest> |
| 13 |
|
| 14 |
using namespace LearnerProfile; |
| 15 |
|
| 16 |
TestLearnerStorage::TestLearnerStorage() |
| 17 |
: m_storage(nullptr) |
| 18 |
{ |
| 19 |
} |
| 20 |
|
| 21 |
void TestLearnerStorage::init() |
| 22 |
{ |
| 23 |
QVERIFY(m_db.open()); |
| 24 |
m_storage.reset(new Storage(m_db.fileName(), nullptr)); |
| 25 |
} |
| 26 |
|
| 27 |
void TestLearnerStorage::cleanup() |
| 28 |
{ |
| 29 |
m_db.close(); |
| 30 |
} |
| 31 |
|
| 32 |
void TestLearnerStorage::testLearnerStorage() |
| 33 |
{ |
| 34 |
LearningGoal tmpGoal(LearningGoal::Language, QStringLiteral("testgoalid"), nullptr); |
| 35 |
tmpGoal.setName(QStringLiteral("testgoalname")); |
| 36 |
|
| 37 |
Learner tmpLearner; |
| 38 |
tmpLearner.addGoal(&tmpGoal); |
| 39 |
tmpLearner.setName(QStringLiteral("tester")); |
| 40 |
|
| 41 |
QVERIFY(m_storage->storeGoal(&tmpGoal)); |
| 42 |
QVERIFY(m_storage->storeProfile(&tmpLearner)); |
| 43 |
|
| 44 |
QList<LearnerProfile::LearningGoal *> loadedGoals = m_storage->loadGoals(); |
| 45 |
QCOMPARE(loadedGoals.size(), 1); |
| 46 |
QCOMPARE(loadedGoals.first()->category(), tmpGoal.category()); |
| 47 |
QCOMPARE(loadedGoals.first()->identifier(), tmpGoal.identifier()); |
| 48 |
QCOMPARE(loadedGoals.first()->name(), tmpGoal.name()); |
| 49 |
|
| 50 |
QList<LearnerProfile::Learner *> loadedLearner = m_storage->loadProfiles(loadedGoals); |
| 51 |
QCOMPARE(loadedLearner.size(), 1); |
| 52 |
QCOMPARE(loadedLearner.first()->identifier(), tmpLearner.identifier()); |
| 53 |
QCOMPARE(loadedLearner.first()->name(), tmpLearner.name()); |
| 54 |
QCOMPARE(loadedLearner.first()->goals().size(), 1); |
| 55 |
QCOMPARE(loadedLearner.first()->goals().first()->identifier(), tmpGoal.identifier()); |
| 56 |
qDeleteAll(loadedGoals); |
| 57 |
qDeleteAll(loadedLearner); |
| 58 |
} |
| 59 |
|
| 60 |
void TestLearnerStorage::testProgressLogStorage() |
| 61 |
{ |
| 62 |
LearningGoal tmpGoal(LearningGoal::Language, QStringLiteral("testgoalid"), nullptr); |
| 63 |
tmpGoal.setName(QStringLiteral("testgoalname")); |
| 64 |
|
| 65 |
Learner tmpLearner; |
| 66 |
tmpLearner.addGoal(&tmpGoal); |
| 67 |
tmpLearner.setName(QStringLiteral("tester")); |
| 68 |
|
| 69 |
QVERIFY(m_storage->storeGoal(&tmpGoal)); |
| 70 |
QVERIFY(m_storage->storeProfile(&tmpLearner)); |
| 71 |
|
| 72 |
const QDateTime time{QDateTime::currentDateTime()}; |
| 73 |
QVERIFY(m_storage->storeProgressLog(&tmpLearner, &tmpGoal, QStringLiteral("container"), QStringLiteral("item"), 1, time)); |
| 74 |
auto data = m_storage->readProgressLog(&tmpLearner, &tmpGoal, QStringLiteral("container"), QStringLiteral("item")); |
| 75 |
QCOMPARE(data.size(), 1); |
| 76 |
QCOMPARE(data.first().first.toString(Qt::ISODate), time.toString(Qt::ISODate)); |
| 77 |
} |
| 78 |
|
| 79 |
void TestLearnerStorage::testProgressValueStorage() |
| 80 |
{ |
| 81 |
LearningGoal tmpGoal(LearningGoal::Language, QStringLiteral("testgoalid"), nullptr); |
| 82 |
tmpGoal.setName(QStringLiteral("testgoalname")); |
| 83 |
|
| 84 |
Learner tmpLearner; |
| 85 |
tmpLearner.addGoal(&tmpGoal); |
| 86 |
tmpLearner.setName(QStringLiteral("tester")); |
| 87 |
|
| 88 |
QVERIFY(m_storage->storeGoal(&tmpGoal)); |
| 89 |
QVERIFY(m_storage->storeProfile(&tmpLearner)); |
| 90 |
|
| 91 |
// insert |
| 92 |
QVERIFY(m_storage->storeProgressValue(&tmpLearner, &tmpGoal, QStringLiteral("container"), QStringLiteral("itemA"), 1)); |
| 93 |
QVERIFY(m_storage->storeProgressValue(&tmpLearner, &tmpGoal, QStringLiteral("container"), QStringLiteral("itemB"), 1)); |
| 94 |
|
| 95 |
auto data = m_storage->readProgressValues(&tmpLearner, &tmpGoal, QStringLiteral("container")); |
| 96 |
QCOMPARE(data.size(), 2); |
| 97 |
|
| 98 |
// update |
| 99 |
QVERIFY(m_storage->storeProgressValue(&tmpLearner, &tmpGoal, QStringLiteral("container"), QStringLiteral("itemA"), 2)); |
| 100 |
data = m_storage->readProgressValues(&tmpLearner, &tmpGoal, QStringLiteral("container")); |
| 101 |
QCOMPARE(data.find(QStringLiteral("itemA")).value(), 2); |
| 102 |
QCOMPARE(data.find(QStringLiteral("itemB")).value(), 1); |
| 103 |
} |
| 104 |
|
| 105 |
QTEST_GUILESS_MAIN(TestLearnerStorage) |