File indexing completed on 2024-05-19 04:01:09
0001 /* 0002 SPDX-FileCopyrightText: 2016 Volker Krause <vkrause@kde.org> 0003 0004 SPDX-License-Identifier: MIT 0005 */ 0006 0007 #include "servercontroller.h" 0008 0009 #include <rest/restapi.h> 0010 #include <rest/restclient.h> 0011 #include <model/productmodel.h> 0012 0013 #include <QDebug> 0014 #include <QtTest/qtest.h> 0015 #include <QObject> 0016 #include <QSignalSpy> 0017 #include <QStandardPaths> 0018 #if QT_VERSION >= QT_VERSION_CHECK(5, 11, 0) 0019 #include <QAbstractItemModelTester> 0020 #endif 0021 0022 using namespace KUserFeedback::Console; 0023 0024 class ProductModelTest : public QObject 0025 { 0026 Q_OBJECT 0027 private: 0028 ServerController m_server; 0029 0030 ServerInfo testServer() const 0031 { 0032 ServerInfo s; 0033 s.setUrl(m_server.url()); 0034 return s; 0035 } 0036 0037 bool waitForFinished(QNetworkReply *reply) 0038 { 0039 Q_ASSERT(reply); 0040 QSignalSpy spy(reply, &QNetworkReply::finished); 0041 Q_ASSERT(spy.isValid()); 0042 return spy.wait(); 0043 } 0044 0045 private Q_SLOTS: 0046 void initTestCase() 0047 { 0048 QStandardPaths::setTestModeEnabled(true); 0049 QVERIFY(m_server.start()); 0050 } 0051 0052 void testProductModel() 0053 { 0054 RESTClient client; 0055 client.setServerInfo(testServer()); 0056 client.setConnected(true); 0057 QVERIFY(client.isConnected()); 0058 0059 // make sure we have at least one product, but not the one we want to create for testing 0060 Product p; 0061 p.setName(QStringLiteral("org.kde.SomeOtherTestProduct")); 0062 auto reply = RESTApi::createProduct(&client, p); 0063 waitForFinished(reply); 0064 p.setName(QStringLiteral("org.kde.NewUnitTestProduct")); 0065 reply = RESTApi::deleteProduct(&client, p); 0066 waitForFinished(reply); 0067 0068 ProductModel model; 0069 #if QT_VERSION >= QT_VERSION_CHECK(5, 11, 0) 0070 QAbstractItemModelTester modelTest(&model); 0071 #endif 0072 QSignalSpy resetSpy(&model, &ProductModel::modelReset); 0073 QSignalSpy insertSpy(&model, &ProductModel::rowsInserted); 0074 QSignalSpy removeSpy(&model, &ProductModel::rowsRemoved); 0075 0076 model.setRESTClient(&client); 0077 QVERIFY(insertSpy.wait()); 0078 const auto baseCount = model.rowCount(); 0079 0080 insertSpy.clear(); 0081 reply = RESTApi::createProduct(&client, p); 0082 QVERIFY(waitForFinished(reply)); 0083 resetSpy.clear(); 0084 model.reload(); 0085 QVERIFY(insertSpy.wait()); 0086 QCOMPARE(model.rowCount(), baseCount + 1); 0087 0088 reply = RESTApi::deleteProduct(&client, p); 0089 QVERIFY(waitForFinished(reply)); 0090 resetSpy.clear(); 0091 model.reload(); 0092 QVERIFY(removeSpy.wait()); 0093 QCOMPARE(model.rowCount(), baseCount); 0094 0095 removeSpy.clear(); 0096 model.clear(); 0097 QCOMPARE(removeSpy.size(), 1); 0098 QVERIFY(resetSpy.isEmpty()); 0099 } 0100 }; 0101 0102 QTEST_MAIN(ProductModelTest) 0103 0104 #include "productmodeltest.moc"