File indexing completed on 2024-05-19 16:31:07

0001 /*
0002     SPDX-FileCopyrightText: 2019 Harald Sitter <sitter@kde.org>
0003 
0004     SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
0005 */
0006 
0007 #include <QTest>
0008 
0009 #include <clients/bugfieldclient.h>
0010 
0011 #include "jobdouble.h"
0012 
0013 namespace Bugzilla
0014 {
0015 class ConnectionDouble : public Connection
0016 {
0017 public:
0018     using Connection::Connection;
0019 
0020     void setToken(const QString &) override
0021     {
0022         Q_UNREACHABLE();
0023     }
0024 
0025     [[nodiscard]] APIJob *get(const QString &path, const Query &query = Query()) const override
0026     {
0027         if (path == "/field/bug/rep_platform" && query.toString() == "") {
0028             return new JobDouble{QFINDTESTDATA("data/field.rep_platform.json")};
0029         }
0030         Q_ASSERT_X(false, "get", qUtf8Printable(QStringLiteral("unmapped: %1; %2").arg(path, query.toString())));
0031         return nullptr;
0032     }
0033 
0034     [[nodiscard]] APIJob *post(const QString &path, const QByteArray &, const Query &query = Query()) const override
0035     {
0036         Q_ASSERT_X(false, "post", qUtf8Printable(QStringLiteral("unmapped: %1; %2").arg(path, query.toString())));
0037         return nullptr;
0038     }
0039 
0040     [[nodiscard]] APIJob *put(const QString &path, const QByteArray &, const Query &query = Query()) const override
0041     {
0042         Q_ASSERT_X(false, "put", qUtf8Printable(QStringLiteral("unmapped: %1; %2").arg(path, query.toString())));
0043         return nullptr;
0044     }
0045 };
0046 
0047 class BugFieldTest : public QObject
0048 {
0049     Q_OBJECT
0050 private Q_SLOTS:
0051     void initTestCase()
0052     {
0053         Bugzilla::setConnection(m_doubleConnection);
0054     }
0055 
0056     void testGet()
0057     {
0058         Bugzilla::BugFieldClient client;
0059         KJob *job = client.getField("rep_platform");
0060         QVERIFY(job);
0061         job->start();
0062         auto field = client.getField(job);
0063         auto values = field->values();
0064         QCOMPARE(5, values.size());
0065         bool containsWindows = false;
0066         for (auto it = values.constBegin(); it != values.constEnd(); ++it) {
0067             if ((*it)->name() == QLatin1String("MS Windows")) {
0068                 containsWindows = true;
0069                 break;
0070             }
0071         }
0072         QVERIFY(containsWindows);
0073         // Run again to drive up coverage for converter registration
0074         job = client.getField("rep_platform");
0075         QVERIFY(job);
0076         job->start();
0077         client.getField(job);
0078     }
0079 
0080 private:
0081     Bugzilla::ConnectionDouble *m_doubleConnection = new Bugzilla::ConnectionDouble;
0082 };
0083 
0084 } // namespace Bugzilla
0085 
0086 QTEST_MAIN(Bugzilla::BugFieldTest)
0087 
0088 #include "bugfieldtest.moc"