Changeset View
Changeset View
Standalone View
Standalone View
src/bugzillaintegration/libbugzilla/autotests/commenttest.cpp
- This file was added.
1 | /* | ||||
---|---|---|---|---|---|
2 | Copyright 2019 Harald Sitter <sitter@kde.org> | ||||
3 | | ||||
4 | This library is free software; you can redistribute it and/or | ||||
5 | modify it under the terms of the GNU Lesser General Public | ||||
6 | License as published by the Free Software Foundation; either | ||||
7 | version 2.1 of the License, or (at your option) version 3, or any | ||||
8 | later version accepted by the membership of KDE e.V. (or its | ||||
9 | successor approved by the membership of KDE e.V.), which shall | ||||
10 | act as a proxy defined in Section 6 of version 3 of the license. | ||||
11 | | ||||
12 | This library is distributed in the hope that it will be useful, | ||||
13 | but WITHOUT ANY WARRANTY; without even the implied warranty of | ||||
14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||||
15 | Lesser General Public License for more details. | ||||
16 | | ||||
17 | You should have received a copy of the GNU Lesser General Public | ||||
18 | License along with this library. If not, see <http://www.gnu.org/licenses/>. | ||||
19 | */ | ||||
20 | | ||||
21 | #include <QTest> | ||||
22 | | ||||
23 | #include <QDebug> | ||||
24 | | ||||
25 | #include "../clients/commentclient.h" | ||||
26 | | ||||
27 | namespace Bugzilla | ||||
28 | { | ||||
29 | | ||||
30 | class JobDouble : public APIJob | ||||
31 | { | ||||
32 | Q_OBJECT | ||||
33 | public: | ||||
34 | using APIJob::APIJob; | ||||
35 | | ||||
36 | JobDouble(QString fixture) | ||||
37 | : m_fixture(fixture) | ||||
38 | { | ||||
39 | } | ||||
40 | | ||||
41 | virtual QByteArray data() const override | ||||
42 | { | ||||
43 | Q_ASSERT(!m_fixture.isEmpty()); | ||||
44 | QFile file(m_fixture); | ||||
45 | Q_ASSERT(file.open(QFile::ReadOnly | QFile::Text)); | ||||
46 | QTextStream in(&file); | ||||
47 | return in.readAll().toUtf8(); | ||||
48 | } | ||||
49 | | ||||
50 | QString m_fixture; | ||||
51 | }; | ||||
52 | | ||||
53 | class ConnectionDouble : public Connection | ||||
54 | { | ||||
55 | public: | ||||
56 | using Connection::Connection; | ||||
57 | | ||||
58 | virtual void setToken(const QString &) override | ||||
59 | { | ||||
60 | Q_UNREACHABLE(); | ||||
61 | } | ||||
62 | | ||||
63 | virtual APIJob *get(const QString &path, | ||||
64 | const QUrlQuery &query = QUrlQuery()) const override | ||||
65 | { | ||||
66 | if (path == "/bug/407363/comment" && query.toString().isEmpty()) { | ||||
67 | return new JobDouble { QFINDTESTDATA("data/comments.json") }; | ||||
68 | } | ||||
69 | if (path == "/bug/1/comment" && query.toString().isEmpty()) { | ||||
70 | return new JobDouble { QFINDTESTDATA("data/error.nobug.invalid.json") }; | ||||
71 | } | ||||
72 | Q_ASSERT_X(false, "get", | ||||
73 | qUtf8Printable(QStringLiteral("unmapped: %1; %2").arg(path, query.toString()))); | ||||
74 | return nullptr; | ||||
75 | } | ||||
76 | | ||||
77 | virtual APIJob *post(const QString &path, | ||||
78 | const QByteArray &, | ||||
79 | const QUrlQuery &query = QUrlQuery()) const override | ||||
80 | { | ||||
81 | Q_ASSERT_X(false, "post", | ||||
82 | qUtf8Printable(QStringLiteral("unmapped: %1; %2").arg(path, query.toString()))); | ||||
83 | return nullptr; | ||||
84 | } | ||||
85 | | ||||
86 | virtual APIJob *put(const QString &path, | ||||
87 | const QByteArray &, | ||||
88 | const QUrlQuery &query = QUrlQuery()) const override | ||||
89 | { | ||||
90 | Q_ASSERT_X(false, "put", | ||||
91 | qUtf8Printable(QStringLiteral("unmapped: %1; %2").arg(path, query.toString()))); | ||||
92 | return nullptr; | ||||
93 | } | ||||
94 | }; | ||||
95 | | ||||
96 | class CommentTest : public QObject | ||||
97 | { | ||||
98 | Q_OBJECT | ||||
99 | private Q_SLOTS: | ||||
100 | | ||||
101 | void initTestCase() | ||||
102 | { | ||||
103 | Bugzilla::setConnection(m_doubleConnection); | ||||
104 | } | ||||
105 | | ||||
106 | void testSearch() | ||||
107 | { | ||||
108 | Bugzilla::CommentClient c; | ||||
109 | auto job = c.getFromBug(407363); | ||||
110 | job->start(); | ||||
111 | auto comments = c.getFromBug(job); | ||||
112 | QCOMPARE(comments.size(), 3); | ||||
113 | | ||||
114 | Comment::Ptr uno = comments[0]; | ||||
115 | QCOMPARE(uno->bug_id(), 407363); | ||||
116 | QCOMPARE(uno->text(), "uno"); | ||||
117 | | ||||
118 | Comment::Ptr tre = comments[2]; | ||||
119 | QCOMPARE(tre->bug_id(), 407363); | ||||
120 | QCOMPARE(tre->text(), "tre"); | ||||
121 | } | ||||
122 | | ||||
123 | void testSearchNoBugInvalid() | ||||
124 | { | ||||
125 | // Our bugzilla has a bug where errors do not have error:true! | ||||
126 | // Make sure we correctly handle objects that are errors but do not | ||||
127 | // necessarily have error:true. | ||||
128 | // This is particularly relevant for comments because we make | ||||
129 | // expectations about bugs being valid/invalid/throwing. | ||||
130 | Bugzilla::CommentClient c; | ||||
131 | auto job = c.getFromBug(1); | ||||
132 | job->start(); | ||||
133 | QVERIFY_EXCEPTION_THROWN(c.getFromBug(job), Bugzilla::APIException); | ||||
134 | } | ||||
135 | | ||||
136 | private: | ||||
137 | Bugzilla::ConnectionDouble *m_doubleConnection = new Bugzilla::ConnectionDouble; | ||||
138 | }; | ||||
139 | | ||||
140 | } // namespace Bugzilla | ||||
141 | | ||||
142 | QTEST_MAIN(Bugzilla::CommentTest) | ||||
143 | | ||||
144 | #include "commenttest.moc" |