-
-
Notifications
You must be signed in to change notification settings - Fork 6.9k
/
Copy pathPetApiTests.cpp
235 lines (203 loc) · 8.1 KB
/
PetApiTests.cpp
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
#include "PetApiTests.h"
#include <QTest>
#include <QTimer>
PFXPet PetApiTests::createRandomPet() {
PFXPet pet;
qint64 id = QDateTime::currentMSecsSinceEpoch();
pet.setName("monster");
pet.setId(id);
pet.setStatus("freaky");
return pet;
}
void PetApiTests::findPetsByStatusTest() {
PFXPetApi api;
QEventLoop loop;
bool petFound = false;
connect(&api, &PFXPetApi::findPetsByStatusSignal, [&](QList<PFXPet> pets) {
petFound = true;
foreach (PFXPet pet, pets) {
QVERIFY(pet.getStatus().startsWith("available") || pet.getStatus().startsWith("sold"));
}
QTimer::singleShot(0, &loop, &QEventLoop::quit);
});
connect(&api, &PFXPetApi::findPetsByStatusSignalError, [&](QList<PFXPet>, QNetworkReply::NetworkError, const QString &error_str) {
qDebug() << "Error happened while issuing request : " << error_str;
QTimer::singleShot(0, &loop, &QEventLoop::quit);
});
api.findPetsByStatus({"available", "sold"});
QTimer::singleShot(5000, &loop, &QEventLoop::quit);
loop.exec();
QVERIFY2(petFound, "didn't finish within timeout");
}
void PetApiTests::createAndGetPetTest() {
PFXPetApi api;
api.setApiKey("api_key","special-key");
QEventLoop loop;
bool petCreated = false;
connect(&api, &PFXPetApi::addPetSignal, [&]() {
// pet created
petCreated = true;
QTimer::singleShot(0, &loop, &QEventLoop::quit);
});
connect(&api, &PFXPetApi::addPetSignalError, [&](QNetworkReply::NetworkError, const QString &error_str) {
qDebug() << "Error happened while issuing request : " << error_str;
QTimer::singleShot(0, &loop, &QEventLoop::quit);
});
PFXPet pet = createRandomPet();
qint64 id = pet.getId();
api.addPet(pet);
QTimer::singleShot(14000, &loop, &QEventLoop::quit);
loop.exec();
QVERIFY2(petCreated, "didn't finish within timeout");
bool petFetched = false;
connect(&api, &PFXPetApi::getPetByIdSignal, [&](PFXPet pet) {
QTimer::singleShot(0, &loop, &QEventLoop::quit);
QVERIFY(pet.getId() > 0);
// QVERIFY(pet.getStatus().compare("freaky") == 0);
petFetched = true;
});
connect(&api, &PFXPetApi::getPetByIdSignalError, [&](PFXPet, QNetworkReply::NetworkError, const QString &error_str) {
qDebug() << "Error happened while issuing request : " << error_str;
QTimer::singleShot(0, &loop, &QEventLoop::quit);
});
api.getPetById(id);
QTimer::singleShot(14000, &loop, &QEventLoop::quit);
loop.exec();
QVERIFY2(petFetched, "didn't finish within timeout");
}
void PetApiTests::updatePetTest() {
PFXPetApi api;
PFXPet pet = createRandomPet();
PFXPet petToCheck;
qint64 id = pet.getId();
QEventLoop loop;
bool petAdded = false;
connect(&api, &PFXPetApi::addPetSignal, [&]() {
petAdded = true;
QTimer::singleShot(0, &loop, &QEventLoop::quit);
});
connect(&api, &PFXPetApi::addPetSignalError, [&](QNetworkReply::NetworkError, const QString &error_str) {
qDebug() << "Error happened while issuing request : " << error_str;
QTimer::singleShot(0, &loop, &QEventLoop::quit);
});
// create pet
api.addPet(pet);
QTimer::singleShot(5000, &loop, &QEventLoop::quit);
loop.exec();
QVERIFY2(petAdded, "didn't finish within timeout");
// fetch it
bool petFetched = false;
connect(&api, &PFXPetApi::getPetByIdSignal, this, [&](PFXPet pet) {
petFetched = true;
petToCheck = pet;
QTimer::singleShot(0, &loop, &QEventLoop::quit);
});
connect(&api, &PFXPetApi::getPetByIdSignalError, this, [&](PFXPet, QNetworkReply::NetworkError, const QString &error_str) {
qDebug() << "Error happened while issuing request : " << error_str;
QTimer::singleShot(0, &loop, &QEventLoop::quit);
});
// create pet
api.getPetById(id);
QTimer::singleShot(5000, &loop, &QEventLoop::quit);
loop.exec();
QVERIFY2(petFetched, "didn't finish within timeout");
// update it
bool petUpdated = false;
connect(&api, &PFXPetApi::updatePetSignal, [&]() {
petUpdated = true;
QTimer::singleShot(0, &loop, &QEventLoop::quit);
});
connect(&api, &PFXPetApi::updatePetSignalError, [&](QNetworkReply::NetworkError, const QString &error_str) {
qDebug() << "Error happened while issuing request : " << error_str;
QTimer::singleShot(0, &loop, &QEventLoop::quit);
});
// update pet
petToCheck.setStatus(QString("scary"));
api.updatePet(petToCheck);
QTimer::singleShot(5000, &loop, &QEventLoop::quit);
loop.exec();
QVERIFY2(petUpdated, "didn't finish within timeout");
// check it
bool petFetched2 = false;
connect(&api, &PFXPetApi::getPetByIdSignal, [&](PFXPet pet) {
petFetched2 = true;
QVERIFY(pet.getId() == petToCheck.getId());
QVERIFY(pet.getStatus().compare(petToCheck.getStatus()) == 0);
QTimer::singleShot(0, &loop, &QEventLoop::quit);
});
connect(&api, &PFXPetApi::getPetByIdSignalError, [&](PFXPet, QNetworkReply::NetworkError, const QString &error_str) {
qDebug() << "Error happened while issuing request : " << error_str;
QTimer::singleShot(0, &loop, &QEventLoop::quit);
});
api.getPetById(id);
QTimer::singleShot(5000, &loop, &QEventLoop::quit);
loop.exec();
QVERIFY2(petFetched2, "didn't finish within timeout");
}
void PetApiTests::updatePetWithFormTest() {
PFXPetApi api;
PFXPet pet = createRandomPet();
PFXPet petToCheck;
qint64 id = pet.getId();
QEventLoop loop;
// create pet
bool petAdded = false;
connect(&api, &PFXPetApi::addPetSignal, [&]() {
petAdded = true;
QTimer::singleShot(0, &loop, &QEventLoop::quit);
});
connect(&api, &PFXPetApi::addPetSignalError, [&](QNetworkReply::NetworkError, const QString &error_str) {
qDebug() << "Error happened while issuing request : " << error_str;
QTimer::singleShot(0, &loop, &QEventLoop::quit);
});
api.addPet(pet);
QTimer::singleShot(5000, &loop, &QEventLoop::quit);
loop.exec();
QVERIFY2(petAdded, "didn't finish within timeout");
// fetch it
bool petFetched = false;
connect(&api, &PFXPetApi::getPetByIdSignal, [&](PFXPet pet) {
petFetched = true;
petToCheck = pet;
QTimer::singleShot(0, &loop, &QEventLoop::quit);
});
connect(&api, &PFXPetApi::getPetByIdSignalError, [&](PFXPet, QNetworkReply::NetworkError, const QString &error_str) {
qDebug() << "Error happened while issuing request : " << error_str;
QTimer::singleShot(0, &loop, &QEventLoop::quit);
});
api.getPetById(id);
QTimer::singleShot(5000, &loop, &QEventLoop::quit);
loop.exec();
QVERIFY2(petFetched, "didn't finish within timeout");
// update it
bool petUpdated = false;
connect(&api, &PFXPetApi::updatePetWithFormSignal, [&]() {
petUpdated = true;
QTimer::singleShot(0, &loop, &QEventLoop::quit);
});
connect(&api, &PFXPetApi::updatePetWithFormSignalError, [&](QNetworkReply::NetworkError, const QString &error_str) {
qDebug() << "Error happened while issuing request : " << error_str;
QTimer::singleShot(0, &loop, &QEventLoop::quit);
});
QString name("gorilla");
api.updatePetWithForm(id, name);
QTimer::singleShot(5000, &loop, &QEventLoop::quit);
loop.exec();
QVERIFY2(petUpdated, "didn't finish within timeout");
// fetch it
bool petUpdated2 = false;
connect(&api, &PFXPetApi::getPetByIdSignal, [&](PFXPet pet) {
Q_UNUSED(pet);
petUpdated2 = true;
// QVERIFY(pet.getName().compare(QString("gorilla")) == 0);
QTimer::singleShot(0, &loop, &QEventLoop::quit);
});
connect(&api, &PFXPetApi::getPetByIdSignalError, [&](PFXPet, QNetworkReply::NetworkError, const QString &error_str) {
qDebug() << "Error happened while issuing request : " << error_str;
QTimer::singleShot(0, &loop, &QEventLoop::quit);
});
api.getPetById(id);
QTimer::singleShot(5000, &loop, &QEventLoop::quit);
loop.exec();
QVERIFY2(petUpdated2, "didn't finish within timeout");
}