geopro/tests/data/test_grid_dto.cpp

110 lines
4.3 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 <QJsonDocument>
#include <QJsonObject>
#include "dto/GridDto.hpp"
using namespace geopro::data::dto;
using geopro::core::TableColumnKind;
namespace {
// 取自真实夹具 tests/fixtures/dd/ert-grid-rows.json 的 datarowList 5 行gridHeaderDisplay
// 两列 x/ytotal=62逐字一致端点 dd/ert/grid/rows。内联避免引入 fixture 路径编译定义。
const char* kGridData = R"({
"gridHeaderDisplay": [
{ "columnCode": "x", "columnNameChn": "x", "columnNameEng": "x", "columnWidth": 10, "columnSort": 1 },
{ "columnCode": "y", "columnNameChn": "y", "columnNameEng": "y", "columnWidth": 10, "columnSort": 2 }
],
"functionList": [],
"total": 62,
"rowList": [
{ "x": 2.904, "y": 25.126, "id": "1438944148742144" },
{ "x": 4.897, "y": 25.161, "id": "1438944148742145" },
{ "x": 6.892, "y": 25.247, "id": "1438944148742146" },
{ "x": 8.892, "y": 25.251, "id": "1438944148742147" },
{ "x": 10.891, "y": 25.226, "id": "1438944148742148" }
]
})";
QJsonObject gridData() { return QJsonDocument::fromJson(kGridData).object(); }
} // namespace
TEST(GridDto, FirstPagePrependsSeqColumnAndReadsTotal) {
auto t = parseGridTable(gridData(), /*pageNo*/ 1, /*pageSize*/ 50);
// 三列:序号 / x / y序号前插x/y 来自 gridHeaderDisplay 按 columnSort
ASSERT_EQ(t.columns.size(), 3u);
EXPECT_EQ(t.columns[0].title.toStdString(), std::string("序号"));
EXPECT_EQ(t.columns[0].code.toStdString(), "__seq");
EXPECT_EQ(t.columns[1].title.toStdString(), "x");
EXPECT_EQ(t.columns[2].title.toStdString(), "y");
for (const auto& c : t.columns) EXPECT_EQ(c.kind, TableColumnKind::Text); // 无特殊列
// 5 行;首行 序号=1 / x=2.904 / y=25.126。
ASSERT_EQ(t.rows.size(), 5u);
ASSERT_EQ(t.rows[0].size(), 3u);
EXPECT_EQ(t.rows[0][0].toStdString(), "1");
EXPECT_EQ(t.rows[0][1].toStdString(), "2.904");
EXPECT_EQ(t.rows[0][2].toStdString(), "25.126");
EXPECT_EQ(t.rows[4][0].toStdString(), "5"); // 第 5 行序号=5
// 总数取 data.total=62非本批 5 行);分页态回填。
EXPECT_EQ(t.total, 62);
EXPECT_EQ(t.pageNo, 1);
EXPECT_EQ(t.pageSize, 50);
}
TEST(GridDto, SeqColumnOffsetsByPage) {
// 第 2 页、每页 50本页首行全局序号 = (2-1)*50 + 1 = 51。
auto t = parseGridTable(gridData(), /*pageNo*/ 2, /*pageSize*/ 50);
ASSERT_EQ(t.rows.size(), 5u);
EXPECT_EQ(t.rows[0][0].toStdString(), "51");
EXPECT_EQ(t.rows[4][0].toStdString(), "55");
EXPECT_EQ(t.pageNo, 2);
EXPECT_EQ(t.pageSize, 50);
}
TEST(GridDto, EmptyDataYieldsSeqOnlyColumnNoRows) {
const QJsonObject empty;
auto t = parseGridTable(empty, 1, 50);
// 空数据仅有前插的「序号」列无行total=0。
ASSERT_EQ(t.columns.size(), 1u);
EXPECT_EQ(t.columns[0].code.toStdString(), "__seq");
EXPECT_EQ(t.rows.size(), 0u);
EXPECT_EQ(t.total, 0);
EXPECT_TRUE(t.functionButtons.empty()); // 无 functionList → 无功能按钮
}
TEST(GridDto, EmptyFunctionListYieldsNoButtons) {
// 真实夹具 functionList:[] → functionButtons 空(不渲染工具条)。
auto t = parseGridTable(gridData(), 1, 50);
EXPECT_TRUE(t.functionButtons.empty());
}
TEST(GridDto, ParsesFunctionListIntoButtons) {
// functionList = DDGridFunctionVO[]functionCode/functionNameChn/enable→ functionButtons。
const char* kData = R"({
"gridHeaderDisplay": [
{ "columnCode": "x", "columnNameChn": "x", "columnSort": 1 },
{ "columnCode": "y", "columnNameChn": "y", "columnSort": 2 }
],
"functionList": [
{ "functionCode": "inversion", "functionNameChn": "反演", "enable": true },
{ "functionCode": "other", "functionNameChn": "其它", "enable": false }
],
"total": 1,
"rowList": [ { "x": 1.0, "y": 2.0, "id": "1" } ]
})";
auto t = parseGridTable(QJsonDocument::fromJson(kData).object(), 1, 50);
ASSERT_EQ(t.functionButtons.size(), 2u);
EXPECT_EQ(t.functionButtons[0].code.toStdString(), "inversion");
EXPECT_EQ(t.functionButtons[0].nameChn.toStdString(), "反演");
EXPECT_TRUE(t.functionButtons[0].enable);
EXPECT_EQ(t.functionButtons[1].code.toStdString(), "other");
EXPECT_FALSE(t.functionButtons[1].enable); // enable=false视图按 v-show 不渲染)
}