feat(tests): add very basic ResourceModel test

______very_____ basic indeed, creating tests is super boring :c

Signed-off-by: flow <flowlnlnln@gmail.com>
This commit is contained in:
flow 2023-01-03 16:27:23 -03:00
parent 1919069b12
commit 3a168ba6dd
No known key found for this signature in database
GPG Key ID: 8D0F221F0A59F469
3 changed files with 138 additions and 0 deletions

View File

@ -24,6 +24,9 @@ ecm_add_test(ResourceFolderModel_test.cpp LINK_LIBRARIES Launcher_logic Qt${QT_V
ecm_add_test(ResourcePackParse_test.cpp LINK_LIBRARIES Launcher_logic Qt${QT_VERSION_MAJOR}::Test ecm_add_test(ResourcePackParse_test.cpp LINK_LIBRARIES Launcher_logic Qt${QT_VERSION_MAJOR}::Test
TEST_NAME ResourcePackParse) TEST_NAME ResourcePackParse)
ecm_add_test(ResourceModel_test.cpp LINK_LIBRARIES Launcher_logic Qt${QT_VERSION_MAJOR}::Test
TEST_NAME ResourceModel)
ecm_add_test(TexturePackParse_test.cpp LINK_LIBRARIES Launcher_logic Qt${QT_VERSION_MAJOR}::Test ecm_add_test(TexturePackParse_test.cpp LINK_LIBRARIES Launcher_logic Qt${QT_VERSION_MAJOR}::Test
TEST_NAME TexturePackParse) TEST_NAME TexturePackParse)

47
tests/DummyResourceAPI.h Normal file
View File

@ -0,0 +1,47 @@
#pragma once
#include <QJsonDocument>
#include <modplatform/ResourceAPI.h>
class SearchTask : public Task {
Q_OBJECT
public:
void executeTask() override { emitSucceeded(); }
};
class DummyResourceAPI : public ResourceAPI {
public:
static auto searchRequestResult()
{
static QByteArray json_response =
"{\"hits\":["
"{"
"\"author\":\"flowln\","
"\"description\":\"the bestest mod\","
"\"project_id\":\"something\","
"\"project_type\":\"mod\","
"\"slug\":\"bip_bop\","
"\"title\":\"AAAAAAAA\","
"\"versions\":[\"2.71\"]"
"}"
"]}";
return QJsonDocument::fromJson(json_response);
}
DummyResourceAPI() : ResourceAPI() {}
[[nodiscard]] auto getSortingMethods() const -> QList<SortingMethod> override { return {}; };
[[nodiscard]] Task::Ptr searchProjects(SearchArgs&&, SearchCallbacks&& callbacks) const override
{
auto task = new SearchTask;
QObject::connect(task, &Task::succeeded, [=] {
auto json = searchRequestResult();
callbacks.on_succeed(json);
});
QObject::connect(task, &Task::finished, task, &Task::deleteLater);
return task;
}
};

View File

@ -0,0 +1,88 @@
#include <QAbstractItemModelTester>
#include <QTest>
#include <QTimer>
#include <Json.h>
#include <ui/pages/modplatform/ResourceModel.h>
#include "DummyResourceAPI.h"
using ResourceDownload::ResourceModel;
#define EXEC_TASK(EXEC) \
QEventLoop loop; \
\
connect(model, &ResourceModel::dataChanged, &loop, &QEventLoop::quit); \
\
QTimer expire_timer; \
expire_timer.callOnTimeout(&loop, &QEventLoop::quit); \
expire_timer.setSingleShot(true); \
expire_timer.start(4000); \
\
EXEC; \
if (model->hasActiveSearchJob()) \
loop.exec(); \
\
QVERIFY2(expire_timer.isActive(), "Timer has expired. The search never finished."); \
expire_timer.stop(); \
\
disconnect(model, nullptr, &loop, nullptr)
class ResourceModelTest;
class DummyResourceModel : public ResourceModel {
Q_OBJECT
friend class ResourceModelTest;
public:
DummyResourceModel() : ResourceModel(new DummyResourceAPI) {}
[[nodiscard]] auto metaEntryBase() const -> QString override { return ""; };
ResourceAPI::SearchArgs createSearchArguments() override { return {}; };
ResourceAPI::VersionSearchArgs createVersionsArguments(QModelIndex&) override { return {}; };
ResourceAPI::ProjectInfoArgs createInfoArguments(QModelIndex&) override { return {}; };
QJsonArray documentToArray(QJsonDocument& doc) const override { return doc.object().value("hits").toArray(); }
void loadIndexedPack(ModPlatform::IndexedPack& pack, QJsonObject& obj) override
{
pack.authors.append({ Json::requireString(obj, "author") });
pack.description = Json::requireString(obj, "description");
pack.addonId = Json::requireString(obj, "project_id");
}
};
class ResourceModelTest : public QObject {
Q_OBJECT
private slots:
void test_abstract_item_model() { [[maybe_unused]] auto tester = new QAbstractItemModelTester(new DummyResourceModel); }
void test_search()
{
auto model = new DummyResourceModel;
QVERIFY(model->m_packs.isEmpty());
EXEC_TASK(model->search());
QVERIFY(model->m_packs.size() == 1);
QVERIFY(model->m_search_state == DummyResourceModel::SearchState::Finished);
auto processed_pack = model->m_packs.at(0);
auto search_json = DummyResourceAPI::searchRequestResult();
auto processed_response = model->documentToArray(search_json).first().toObject();
QVERIFY(processed_pack.addonId.toString() == Json::requireString(processed_response, "project_id"));
QVERIFY(processed_pack.description == Json::requireString(processed_response, "description"));
QVERIFY(processed_pack.authors.first().name == Json::requireString(processed_response, "author"));
}
};
QTEST_GUILESS_MAIN(ResourceModelTest)
#include "ResourceModel_test.moc"
#include "moc_DummyResourceAPI.cpp"