testlearnerstorage.cpp 3.54 KB
Newer Older
1
/*
2
3
4
5
    SPDX-FileCopyrightText: 2016 Andreas Cord-Landwehr <cordlandwehr@kde.org>

    SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
*/
6
7

#include "testlearnerstorage.h"
8
9
10
11
#include "learner.h"
#include "learninggoal.h"
#include "storage.h"

12
#include <QTest>
13
14
15
16
17
18
19
20
21
22
23

using namespace LearnerProfile;

TestLearnerStorage::TestLearnerStorage()
    : m_storage(nullptr)
{
}

void TestLearnerStorage::init()
{
    QVERIFY(m_db.open());
24
    m_storage.reset(new Storage(m_db.fileName(), nullptr));
25
26
27
28
29
30
31
}

void TestLearnerStorage::cleanup()
{
    m_db.close();
}

32
33
34
35
36
37
38
void TestLearnerStorage::testLearnerStorage()
{
    LearningGoal tmpGoal(LearningGoal::Language, QStringLiteral("testgoalid"), nullptr);
    tmpGoal.setName(QStringLiteral("testgoalname"));

    Learner tmpLearner;
    tmpLearner.addGoal(&tmpGoal);
Laurent Montel's avatar
Laurent Montel committed
39
    tmpLearner.setName(QStringLiteral("tester"));
40
41
42
43

    QVERIFY(m_storage->storeGoal(&tmpGoal));
    QVERIFY(m_storage->storeProfile(&tmpLearner));

44
    QList<LearnerProfile::LearningGoal *> loadedGoals = m_storage->loadGoals();
45
46
47
48
49
    QCOMPARE(loadedGoals.size(), 1);
    QCOMPARE(loadedGoals.first()->category(), tmpGoal.category());
    QCOMPARE(loadedGoals.first()->identifier(), tmpGoal.identifier());
    QCOMPARE(loadedGoals.first()->name(), tmpGoal.name());

50
    QList<LearnerProfile::Learner *> loadedLearner = m_storage->loadProfiles(loadedGoals);
51
52
53
54
    QCOMPARE(loadedLearner.size(), 1);
    QCOMPARE(loadedLearner.first()->identifier(), tmpLearner.identifier());
    QCOMPARE(loadedLearner.first()->name(), tmpLearner.name());
    QCOMPARE(loadedLearner.first()->goals().size(), 1);
55
    QCOMPARE(loadedLearner.first()->goals().first()->identifier(), tmpGoal.identifier());
Laurent Montel's avatar
Laurent Montel committed
56
57
    qDeleteAll(loadedGoals);
    qDeleteAll(loadedLearner);
58
59
}

60
61
62
63
64
65
66
void TestLearnerStorage::testProgressLogStorage()
{
    LearningGoal tmpGoal(LearningGoal::Language, QStringLiteral("testgoalid"), nullptr);
    tmpGoal.setName(QStringLiteral("testgoalname"));

    Learner tmpLearner;
    tmpLearner.addGoal(&tmpGoal);
Laurent Montel's avatar
Laurent Montel committed
67
    tmpLearner.setName(QStringLiteral("tester"));
68
69
70
71

    QVERIFY(m_storage->storeGoal(&tmpGoal));
    QVERIFY(m_storage->storeProfile(&tmpLearner));

72
    const QDateTime time {QDateTime::currentDateTime()};
73
    QVERIFY(m_storage->storeProgressLog(&tmpLearner, &tmpGoal, "container", "item", 1, time));
Laurent Montel's avatar
Laurent Montel committed
74
    auto data = m_storage->readProgressLog(&tmpLearner, &tmpGoal, QStringLiteral("container"), QStringLiteral("item"));
75
76
77
78
79
80
81
82
83
84
85
    QCOMPARE(data.size(), 1);
    QCOMPARE(data.first().first.toString(Qt::ISODate), time.toString(Qt::ISODate));
}

void TestLearnerStorage::testProgressValueStorage()
{
    LearningGoal tmpGoal(LearningGoal::Language, QStringLiteral("testgoalid"), nullptr);
    tmpGoal.setName(QStringLiteral("testgoalname"));

    Learner tmpLearner;
    tmpLearner.addGoal(&tmpGoal);
Laurent Montel's avatar
Laurent Montel committed
86
    tmpLearner.setName(QStringLiteral("tester"));
87
88
89
90
91
92
93
94

    QVERIFY(m_storage->storeGoal(&tmpGoal));
    QVERIFY(m_storage->storeProfile(&tmpLearner));

    // insert
    QVERIFY(m_storage->storeProgressValue(&tmpLearner, &tmpGoal, "container", "itemA", 1));
    QVERIFY(m_storage->storeProgressValue(&tmpLearner, &tmpGoal, "container", "itemB", 1));

Laurent Montel's avatar
Laurent Montel committed
95
    auto data = m_storage->readProgressValues(&tmpLearner, &tmpGoal, QStringLiteral("container"));
96
97
98
99
    QCOMPARE(data.size(), 2);

    // update
    QVERIFY(m_storage->storeProgressValue(&tmpLearner, &tmpGoal, "container", "itemA", 2));
Laurent Montel's avatar
Laurent Montel committed
100
    data = m_storage->readProgressValues(&tmpLearner, &tmpGoal, QStringLiteral("container"));
101
102
    QCOMPARE(data.find("itemA").value(), 2);
    QCOMPARE(data.find("itemB").value(), 1);
103
104
}

105
QTEST_GUILESS_MAIN(TestLearnerStorage)