geopro/tests/data/test_dataset_load_handles.cpp

66 lines
2.4 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#include <gtest/gtest.h>
#include <stdexcept>
#include <QSignalSpy>
#include "api/DatasetLoadHandles.hpp"
#include "net/FakeApiCall.hpp"
using namespace geopro::data;
using geopro::net::ApiBatch;
using geopro::net::ApiResponse;
using geopro::net::test::FakeApiCall;
namespace {
ApiResponse ok() { ApiResponse r; r.code = 200; r.httpStatus = 200; return r; }
ApiResponse bad() { ApiResponse r; r.code = 500; r.httpStatus = 200; r.msg = QStringLiteral("boom"); return r; }
auto isFailure = [](const ApiResponse& r) { return r.code != 200 || !r.rawError.isEmpty(); };
}
TEST(DatasetLoadHandles, ChartLoadEmitsDoneOnSuccess) {
auto* a = new FakeApiCall;
auto* b = new FakeApiCall;
auto* batch = new ApiBatch({a, b}, isFailure);
bool parsed = false;
auto* load = new ApiChartLoad(batch, [&](const QList<ApiResponse>& resps) {
parsed = (resps.size() == 2);
return ChartParts{};
});
QSignalSpy doneSpy(load, &ChartLoad::done);
a->fire(ok());
b->fire(ok());
EXPECT_EQ(doneSpy.count(), 1);
EXPECT_TRUE(parsed);
}
TEST(DatasetLoadHandles, ChartLoadEmitsFailedOnBatchFailure) {
auto* a = new FakeApiCall;
auto* b = new FakeApiCall;
auto* batch = new ApiBatch({a, b}, isFailure);
auto* load = new ApiChartLoad(batch, [](const QList<ApiResponse>&) { return ChartParts{}; });
QSignalSpy failSpy(load, &ChartLoad::failed);
a->fire(bad());
EXPECT_EQ(failSpy.count(), 1);
}
TEST(DatasetLoadHandles, ChartLoadEmitsFailedWhenParseThrows) {
auto* a = new FakeApiCall;
auto* batch = new ApiBatch({a}, isFailure);
auto* load = new ApiChartLoad(batch, [](const QList<ApiResponse>&) -> ChartParts {
throw std::runtime_error("parse boom");
});
QSignalSpy doneSpy(load, &ChartLoad::done);
QSignalSpy failSpy(load, &ChartLoad::failed);
a->fire(ok()); // batch 成功 → parse 抛异常 → failedemit done 已移出 try
EXPECT_EQ(doneSpy.count(), 0);
EXPECT_EQ(failSpy.count(), 1);
}
TEST(DatasetLoadHandles, GridLoadAbortSuppressesLateDone) {
auto* a = new FakeApiCall;
auto* batch = new ApiBatch({a}, isFailure);
auto* load = new ApiGridLoad(batch, [](const QList<ApiResponse>&) { return GridParts{}; });
QSignalSpy doneSpy(load, &GridLoad::done);
load->abort();
a->fire(ok()); // 迟到
EXPECT_EQ(doneSpy.count(), 0); // batch.aborted_ + load.aborted_ 双闸门
}