104 lines
3.4 KiB
C++
104 lines
3.4 KiB
C++
#include <gtest/gtest.h>
|
||
#include <stdexcept>
|
||
#include <QCoreApplication>
|
||
#include <QVariant>
|
||
#include "ApiClient.hpp"
|
||
#include "api/ApiDatasetRepository.hpp"
|
||
#include "api/DatasetLoadHandles.hpp"
|
||
#include "model/detail/DetailPayloads.hpp"
|
||
|
||
using geopro::data::ApiDatasetRepository;
|
||
using geopro::data::DetailLoad;
|
||
using geopro::core::ContourPayload;
|
||
using geopro::core::ScatterPayload;
|
||
|
||
namespace {
|
||
// ApiClient 构造 + getAsync/postJsonAsync 创建网络句柄需 QCoreApplication(QNAM 事件循环)。
|
||
QCoreApplication* ensureApp() {
|
||
if (!QCoreApplication::instance()) {
|
||
static int argc = 0;
|
||
static char** argv = nullptr;
|
||
new QCoreApplication(argc, argv);
|
||
}
|
||
return QCoreApplication::instance();
|
||
}
|
||
} // namespace
|
||
|
||
// 已知 loaderKey 返回非空句柄;返回后立即 abort 取消在飞请求(不依赖网络可达)。
|
||
TEST(AsyncRepoDispatch, KnownKeysReturnNonNullHandle) {
|
||
ensureApp();
|
||
geopro::net::ApiClient api(QStringLiteral("http://tenant.geomative.cn/pop-api"));
|
||
ApiDatasetRepository repo(api);
|
||
|
||
DetailLoad* scatter = repo.loadAsync("inversion.scatter", "ds1");
|
||
ASSERT_NE(scatter, nullptr);
|
||
scatter->abort();
|
||
|
||
DetailLoad* grid = repo.loadAsync("inversion.grid", "ds1");
|
||
ASSERT_NE(grid, nullptr);
|
||
grid->abort();
|
||
|
||
DetailLoad* measScatter = repo.loadAsync("ert_measurement.scatter", "ds1");
|
||
ASSERT_NE(measScatter, nullptr);
|
||
measScatter->abort();
|
||
|
||
DetailLoad* measRows = repo.loadAsync("ert_measurement.rows", "ds1");
|
||
ASSERT_NE(measRows, nullptr);
|
||
measRows->abort();
|
||
|
||
DetailLoad* grBar = repo.loadAsync("gr.bar", "ds1");
|
||
ASSERT_NE(grBar, nullptr);
|
||
grBar->abort();
|
||
|
||
DetailLoad* grRows = repo.loadAsync("gr.rows", "ds1");
|
||
ASSERT_NE(grRows, nullptr);
|
||
grRows->abort();
|
||
|
||
DetailLoad* trajRows = repo.loadAsync("traj.rows", "ds1");
|
||
ASSERT_NE(trajRows, nullptr);
|
||
trajRows->abort();
|
||
|
||
DetailLoad* trajElev = repo.loadAsync("traj.elev", "ds1");
|
||
ASSERT_NE(trajElev, nullptr);
|
||
trajElev->abort();
|
||
|
||
DetailLoad* trajMap = repo.loadAsync("traj.map", "ds1");
|
||
ASSERT_NE(trajMap, nullptr);
|
||
trajMap->abort();
|
||
|
||
DetailLoad* gridRows = repo.loadAsync("grid.rows", "ds1", 1, 50); // 分页型:带页参
|
||
ASSERT_NE(gridRows, nullptr);
|
||
gridRows->abort();
|
||
}
|
||
|
||
// 未知 loaderKey 抛 std::runtime_error。
|
||
TEST(AsyncRepoDispatch, UnknownKeyThrows) {
|
||
ensureApp();
|
||
geopro::net::ApiClient api(QStringLiteral("http://tenant.geomative.cn/pop-api"));
|
||
ApiDatasetRepository repo(api);
|
||
EXPECT_THROW(repo.loadAsync("bogus", "ds1"), std::runtime_error);
|
||
}
|
||
|
||
// payload 经 QVariant 类型擦除往返还原(不触网)。
|
||
TEST(AsyncRepoDispatch, ScatterPayloadRoundTrips) {
|
||
ScatterPayload in;
|
||
in.scatter.x = {1.0, 2.0, 3.0};
|
||
in.scatter.v = {10.0, 20.0, 30.0};
|
||
QVariant v = QVariant::fromValue(in);
|
||
ASSERT_TRUE(v.canConvert<ScatterPayload>());
|
||
ScatterPayload out = v.value<ScatterPayload>();
|
||
ASSERT_EQ(out.scatter.x.size(), 3u);
|
||
EXPECT_DOUBLE_EQ(out.scatter.v[2], 30.0);
|
||
}
|
||
|
||
TEST(AsyncRepoDispatch, ContourPayloadRoundTrips) {
|
||
ContourPayload in; // Grid{1,1} 占位 + 空异常
|
||
in.grid.vmin = 1.5;
|
||
in.grid.vmax = 9.5;
|
||
QVariant v = QVariant::fromValue(in);
|
||
ASSERT_TRUE(v.canConvert<ContourPayload>());
|
||
ContourPayload out = v.value<ContourPayload>();
|
||
EXPECT_DOUBLE_EQ(out.grid.vmin, 1.5);
|
||
EXPECT_DOUBLE_EQ(out.grid.vmax, 9.5);
|
||
}
|