File: productmodeltest.cpp

package info (click to toggle)
kuserfeedback 1.3.0-9
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 3,496 kB
  • sloc: cpp: 13,251; php: 2,192; xml: 224; yacc: 90; lex: 82; sh: 17; makefile: 8
file content (104 lines) | stat: -rw-r--r-- 2,758 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
/*
    SPDX-FileCopyrightText: 2016 Volker Krause <vkrause@kde.org>

    SPDX-License-Identifier: MIT
*/

#include "servercontroller.h"

#include <rest/restapi.h>
#include <rest/restclient.h>
#include <model/productmodel.h>

#include <QDebug>
#include <QtTest/qtest.h>
#include <QObject>
#include <QSignalSpy>
#include <QStandardPaths>
#if QT_VERSION >= QT_VERSION_CHECK(5, 11, 0)
#include <QAbstractItemModelTester>
#endif

using namespace KUserFeedback::Console;

class ProductModelTest : public QObject
{
    Q_OBJECT
private:
    ServerController m_server;

    ServerInfo testServer() const
    {
        ServerInfo s;
        s.setUrl(m_server.url());
        return s;
    }

    bool waitForFinished(QNetworkReply *reply)
    {
        Q_ASSERT(reply);
        QSignalSpy spy(reply, &QNetworkReply::finished);
        Q_ASSERT(spy.isValid());
        return spy.wait();
    }

private slots:
    void initTestCase()
    {
        QStandardPaths::setTestModeEnabled(true);
        QVERIFY(m_server.start());
    }

    void testProductModel()
    {
        RESTClient client;
        client.setServerInfo(testServer());
        client.setConnected(true);
        QVERIFY(client.isConnected());

        // make sure we have at least one product, but not the one we want to create for testing
        Product p;
        p.setName(QStringLiteral("org.kde.SomeOtherTestProduct"));
        auto reply = RESTApi::createProduct(&client, p);
        waitForFinished(reply);
        p.setName(QStringLiteral("org.kde.NewUnitTestProduct"));
        reply = RESTApi::deleteProduct(&client, p);
        waitForFinished(reply);

        ProductModel model;
#if QT_VERSION >= QT_VERSION_CHECK(5, 11, 0)
        QAbstractItemModelTester modelTest(&model);
#endif
        QSignalSpy resetSpy(&model, &ProductModel::modelReset);
        QSignalSpy insertSpy(&model, &ProductModel::rowsInserted);
        QSignalSpy removeSpy(&model, &ProductModel::rowsRemoved);

        model.setRESTClient(&client);
        QVERIFY(insertSpy.wait());
        const auto baseCount = model.rowCount();

        insertSpy.clear();
        reply = RESTApi::createProduct(&client, p);
        QVERIFY(waitForFinished(reply));
        resetSpy.clear();
        model.reload();
        QVERIFY(insertSpy.wait());
        QCOMPARE(model.rowCount(), baseCount + 1);

        reply = RESTApi::deleteProduct(&client, p);
        QVERIFY(waitForFinished(reply));
        resetSpy.clear();
        model.reload();
        QVERIFY(removeSpy.wait());
        QCOMPARE(model.rowCount(), baseCount);

        removeSpy.clear();
        model.clear();
        QCOMPARE(removeSpy.size(), 1);
        QVERIFY(resetSpy.isEmpty());
    }
};

QTEST_MAIN(ProductModelTest)

#include "productmodeltest.moc"