[cpp-qt] Add option for download progress, add test (#19297)

* add option for download progress, add test

* add cmake file

* remove test

* update
This commit is contained in:
William Cheng
2024-08-05 16:06:36 +08:00
committed by GitHub
parent ffd03b7e51
commit eff3e6df53
56 changed files with 9576 additions and 3 deletions

View File

@@ -0,0 +1,235 @@
#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");
}

View File

@@ -0,0 +1,17 @@
#pragma once
#include "../client/PFXPetApi.h"
using namespace test_namespace;
class PetApiTests : public QObject {
Q_OBJECT
PFXPet createRandomPet();
private Q_SLOTS:
void findPetsByStatusTest();
void createAndGetPetTest();
void updatePetTest();
void updatePetWithFormTest();
};

View File

@@ -0,0 +1,31 @@
#-------------------------------------------------
#
# Project created by QtCreator 2015-05-14T20:56:31
#
#-------------------------------------------------
QT += core gui testlib network
TARGET = PetStore
CONFIG += console
CONFIG -= app_bundle
CONFIG += c++11
TEMPLATE = app
include(../client/PFXclient.pri)
INCLUDEPATH += ../client
SOURCES += main.cpp \
PetApiTests.cpp \
StoreApiTests.cpp \
UserApiTests.cpp
HEADERS += PetApiTests.h \
StoreApiTests.h \
UserApiTests.h
# Disable optimisation for better valgrind report
QMAKE_CXXFLAGS_DEBUG += -O0

View File

@@ -0,0 +1,85 @@
#include "StoreApiTests.h"
#include <QDebug>
#include <QTest>
#include <QTimer>
void StoreApiTests::placeOrderTest() {
PFXStoreApi api;
// api.setUsername("TestName");
// api.setPassword("TestPassword");
QEventLoop loop;
bool orderPlaced = false;
connect(&api, &PFXStoreApi::placeOrderSignal, [&](PFXOrder order) {
orderPlaced = true;
// QVERIFY(order.getPetId() == 10000);
// QVERIFY((order.getId() == 500));
qDebug() << order.getShipDate();
QTimer::singleShot(0, &loop, &QEventLoop::quit);
});
connect(&api, &PFXStoreApi::placeOrderSignalError, [&](PFXOrder, QNetworkReply::NetworkError, const QString &error_str) {
qDebug() << "Error happened while issuing request : " << error_str;
QTimer::singleShot(0, &loop, &QEventLoop::quit);
});
PFXOrder order;
order.setId(500);
order.setQuantity(10);
order.setPetId(10000);
order.setComplete(false);
order.setStatus("shipping");
order.setShipDate(QDateTime::currentDateTime());
api.placeOrder(order);
QTimer::singleShot(14000, &loop, &QEventLoop::quit);
loop.exec();
QVERIFY2(orderPlaced, "didn't finish within timeout");
}
void StoreApiTests::getOrderByIdTest() {
PFXStoreApi api;
api.setApiKey("api_key_2","testKey");
QEventLoop loop;
bool orderFetched = false;
connect(&api, &PFXStoreApi::getOrderByIdSignal, [&](PFXOrder order) {
orderFetched = true;
// QVERIFY(order.getPetId() == 10000);
// QVERIFY((order.getId() == 500));
qDebug() << order.getShipDate();
QTimer::singleShot(0, &loop, &QEventLoop::quit);
});
connect(&api, &PFXStoreApi::getOrderByIdSignalError, [&](PFXOrder, QNetworkReply::NetworkError, const QString &error_str) {
qDebug() << "Error happened while issuing request : " << error_str;
QTimer::singleShot(0, &loop, &QEventLoop::quit);
});
api.getOrderById(500);
QTimer::singleShot(14000, &loop, &QEventLoop::quit);
loop.exec();
QVERIFY2(orderFetched, "didn't finish within timeout");
}
void StoreApiTests::getInventoryTest() {
PFXStoreApi api;
api.setApiKey("api_key","special-key");
QEventLoop loop;
bool inventoryFetched = false;
connect(&api, &PFXStoreApi::getInventorySignal, [&](QMap<QString, qint32> status) {
inventoryFetched = true;
for (const auto &key : status.keys()) {
qDebug() << (key) << " Quantities " << status.value(key);
}
QTimer::singleShot(0, &loop, &QEventLoop::quit);
});
connect(&api, &PFXStoreApi::getInventorySignalError, [&](QMap<QString, qint32>, QNetworkReply::NetworkError, const QString &error_str) {
qDebug() << "Error happened while issuing request : " << error_str;
QTimer::singleShot(0, &loop, &QEventLoop::quit);
});
api.getInventory();
QTimer::singleShot(14000, &loop, &QEventLoop::quit);
loop.exec();
QVERIFY2(inventoryFetched, "didn't finish within timeout");
}

View File

@@ -0,0 +1,14 @@
#pragma once
#include "../client/PFXStoreApi.h"
using namespace test_namespace;
class StoreApiTests : public QObject {
Q_OBJECT
private Q_SLOTS:
void placeOrderTest();
void getOrderByIdTest();
void getInventoryTest();
};

View File

@@ -0,0 +1,199 @@
#include "UserApiTests.h"
#include <QDebug>
#include <QTest>
#include <QTimer>
PFXUser UserApiTests::createRandomUser() {
PFXUser user;
user.setId(QDateTime::currentMSecsSinceEpoch());
user.setEmail("Jane.Doe@openapitools.io");
user.setFirstName("Jane");
user.setLastName("Doe");
user.setPhone("123456789");
user.setUsername("janedoe");
user.setPassword("secretPassword");
user.setUserStatus(static_cast<int>(rand()));
return user;
}
void UserApiTests::createUserTest() {
PFXUserApi api;
QEventLoop loop;
bool userCreated = false;
connect(&api, &PFXUserApi::createUserSignal, [&]() {
userCreated = true;
QTimer::singleShot(0, &loop, &QEventLoop::quit);
});
connect(&api, &PFXUserApi::createUserSignalError, [&](QNetworkReply::NetworkError, const QString &error_str) {
qDebug() << "Error happened while issuing request : " << error_str;
QTimer::singleShot(0, &loop, &QEventLoop::quit);
});
api.createUser(createRandomUser());
QTimer::singleShot(14000, &loop, &QEventLoop::quit);
loop.exec();
QVERIFY2(userCreated, "didn't finish within timeout");
}
void UserApiTests::createUsersWithArrayInputTest() {
PFXUserApi api;
QEventLoop loop;
bool usersCreated = false;
connect(&api, &PFXUserApi::createUsersWithArrayInputSignal, [&]() {
usersCreated = true;
QTimer::singleShot(0, &loop, &QEventLoop::quit);
});
connect(&api, &PFXUserApi::createUsersWithArrayInputSignalError, [&](QNetworkReply::NetworkError, const QString &error_str) {
qDebug() << "Error happened while issuing request : " << error_str;
QTimer::singleShot(0, &loop, &QEventLoop::quit);
});
QList<PFXUser> users;
users.append(createRandomUser());
users.append(createRandomUser());
users.append(createRandomUser());
api.createUsersWithArrayInput(users);
QTimer::singleShot(14000, &loop, &QEventLoop::quit);
loop.exec();
QVERIFY2(usersCreated, "didn't finish within timeout");
}
void UserApiTests::createUsersWithListInputTest() {
PFXUserApi api;
QEventLoop loop;
bool usersCreated = false;
connect(&api, &PFXUserApi::createUsersWithListInputSignal, [&]() {
usersCreated = true;
QTimer::singleShot(0, &loop, &QEventLoop::quit);
});
connect(&api, &PFXUserApi::createUsersWithListInputSignalError, [&](QNetworkReply::NetworkError, const QString &error_str) {
qDebug() << "Error happened while issuing request : " << error_str;
QTimer::singleShot(0, &loop, &QEventLoop::quit);
});
QList<PFXUser> users;
auto johndoe = createRandomUser();
johndoe.setUsername("johndoe");
auto rambo = createRandomUser();
rambo.setUsername("rambo");
users.append(johndoe);
users.append(rambo);
users.append(createRandomUser());
api.createUsersWithListInput(users);
QTimer::singleShot(14000, &loop, &QEventLoop::quit);
loop.exec();
QVERIFY2(usersCreated, "didn't finish within timeout");
}
void UserApiTests::deleteUserTest() {
PFXUserApi api;
QEventLoop loop;
bool userDeleted = false;
connect(&api, &PFXUserApi::deleteUserSignal, [&]() {
userDeleted = true;
QTimer::singleShot(0, &loop, &QEventLoop::quit);
});
connect(&api, &PFXUserApi::deleteUserSignalError, [&](QNetworkReply::NetworkError, const QString &error_str) {
userDeleted = true;
qDebug() << "Error happened while issuing request : " << error_str;
QTimer::singleShot(0, &loop, &QEventLoop::quit);
});
api.deleteUser("rambo");
QTimer::singleShot(14000, &loop, &QEventLoop::quit);
loop.exec();
QVERIFY2(userDeleted, "didn't finish within timeout");
}
void UserApiTests::getUserByNameTest() {
PFXUserApi api;
QEventLoop loop;
bool userFetched = false;
connect(&api, &PFXUserApi::getUserByNameSignal, [&](PFXUser summary) {
userFetched = true;
qDebug() << summary.getUsername();
// QVERIFY(summary.getUsername() == "johndoe");
QTimer::singleShot(0, &loop, &QEventLoop::quit);
});
connect(&api, &PFXUserApi::getUserByNameSignalError, [&](PFXUser, QNetworkReply::NetworkError, const QString &error_str) {
userFetched = true;
qDebug() << "Error happened while issuing request : " << error_str;
QTimer::singleShot(0, &loop, &QEventLoop::quit);
});
api.getUserByName("johndoe");
QTimer::singleShot(14000, &loop, &QEventLoop::quit);
loop.exec();
QVERIFY2(userFetched, "didn't finish within timeout");
}
void UserApiTests::loginUserTest() {
PFXUserApi api;
QEventLoop loop;
bool userLogged = false;
connect(&api, &PFXUserApi::loginUserSignal, [&](QString summary) {
userLogged = true;
qDebug() << summary;
QTimer::singleShot(0, &loop, &QEventLoop::quit);
});
connect(&api, &PFXUserApi::loginUserSignalError, [&](QString, QNetworkReply::NetworkError, const QString &error_str) {
userLogged = true;
qDebug() << "Error happened while issuing request : " << error_str;
QTimer::singleShot(0, &loop, &QEventLoop::quit);
});
api.loginUser("johndoe", "123456789");
QTimer::singleShot(14000, &loop, &QEventLoop::quit);
loop.exec();
QVERIFY2(userLogged, "didn't finish within timeout");
}
void UserApiTests::logoutUserTest() {
PFXUserApi api;
QEventLoop loop;
bool userLoggedOut = false;
connect(&api, &PFXUserApi::logoutUserSignal, [&]() {
userLoggedOut = true;
QTimer::singleShot(0, &loop, &QEventLoop::quit);
});
connect(&api, &PFXUserApi::logoutUserSignalError, [&](QNetworkReply::NetworkError, const QString &error_str) {
qDebug() << "Error happened while issuing request : " << error_str;
QTimer::singleShot(0, &loop, &QEventLoop::quit);
});
api.logoutUser();
QTimer::singleShot(14000, &loop, &QEventLoop::quit);
loop.exec();
QVERIFY2(userLoggedOut, "didn't finish within timeout");
}
void UserApiTests::updateUserTest() {
PFXUserApi api;
QEventLoop loop;
bool userUpdated = false;
connect(&api, &PFXUserApi::updateUserSignal, [&]() {
userUpdated = true;
QTimer::singleShot(0, &loop, &QEventLoop::quit);
});
connect(&api, &PFXUserApi::updateUserSignalError, [&](QNetworkReply::NetworkError, const QString &error_str) {
qDebug() << "Error happened while issuing request : " << error_str;
QTimer::singleShot(0, &loop, &QEventLoop::quit);
});
auto johndoe = createRandomUser();
johndoe.setUsername("johndoe");
api.updateUser("johndoe", johndoe);
QTimer::singleShot(14000, &loop, &QEventLoop::quit);
loop.exec();
QVERIFY2(userUpdated, "didn't finish within timeout");
}

View File

@@ -0,0 +1,21 @@
#pragma once
#include "../client/PFXUserApi.h"
using namespace test_namespace;
class UserApiTests : public QObject {
Q_OBJECT
PFXUser createRandomUser();
private Q_SLOTS:
void createUserTest();
void createUsersWithArrayInputTest();
void createUsersWithListInputTest();
void deleteUserTest();
void getUserByNameTest();
void loginUserTest();
void logoutUserTest();
void updateUserTest();
};

View File

@@ -0,0 +1,22 @@
#include <QCoreApplication>
#include <QTest>
#include "PFXHelpers.h"
#include "PetApiTests.h"
#include "StoreApiTests.h"
#include "UserApiTests.h"
int main(int argc, char *argv[]) {
QCoreApplication a(argc, argv);
::test_namespace::setDateTimeFormat("yyyy-MM-ddTHH:mm:ss.zzzZ");
PetApiTests petApiTests;
StoreApiTests storeApiTests;
UserApiTests userApiTests;
int failedTests = 0;
failedTests += QTest::qExec(&petApiTests);
failedTests += QTest::qExec(&storeApiTests);
failedTests += QTest::qExec(&userApiTests);
return failedTests;
}