AI 自动生成测试用例
This commit is contained in:
parent
9ceeaf09c6
commit
9ee9b41448
|
|
@ -0,0 +1,25 @@
|
||||||
|
cmake_minimum_required(VERSION 3.10.0)
|
||||||
|
project(test_plan_execute_t1)
|
||||||
|
include(FetchContent)
|
||||||
|
if (MSVC)
|
||||||
|
add_compile_options(/utf-8)
|
||||||
|
endif()
|
||||||
|
FetchContent_Declare(
|
||||||
|
googletest
|
||||||
|
URL https://github.com/google/googletest/archive/03597a01ee50ed33e9dfd640b249b4be3799d395.zip
|
||||||
|
)
|
||||||
|
# For Windows: Prevent overriding the parent project's compiler/linker settings
|
||||||
|
set(gtest_force_shared_crt ON CACHE BOOL "" FORCE)
|
||||||
|
FetchContent_MakeAvailable(googletest)
|
||||||
|
set(CMAKE_CXX_STANDARD 17)
|
||||||
|
set(CMAKE_CXX_STANDARD_REQUIRED ON)
|
||||||
|
include_directories(${CMAKE_CURRENT_SOURCE_DIR}/../include)
|
||||||
|
|
||||||
|
include(CTest)
|
||||||
|
enable_testing()
|
||||||
|
|
||||||
|
add_executable(test_plan_execute_t1 test_app.cpp ../src/app.cpp)
|
||||||
|
|
||||||
|
target_link_libraries(test_plan_execute_t1 gtest gmock gtest_main)
|
||||||
|
include(GoogleTest)
|
||||||
|
gtest_discover_tests(test_plan_execute_t1)
|
||||||
|
|
@ -0,0 +1,325 @@
|
||||||
|
#include <gtest/gtest.h>
|
||||||
|
#include <string>
|
||||||
|
#include <vector>
|
||||||
|
#include <chrono>
|
||||||
|
#include "app.hpp"
|
||||||
|
|
||||||
|
// ============================================================================
|
||||||
|
// 测试夹具:CmsEngineTest
|
||||||
|
// ============================================================================
|
||||||
|
class CmsEngineTest : public ::testing::Test {
|
||||||
|
protected:
|
||||||
|
void SetUp() override {
|
||||||
|
// 每个测试用例开始前重置引擎
|
||||||
|
engine = new CmsEngine();
|
||||||
|
}
|
||||||
|
|
||||||
|
void TearDown() override {
|
||||||
|
delete engine;
|
||||||
|
}
|
||||||
|
|
||||||
|
CmsEngine* engine;
|
||||||
|
};
|
||||||
|
|
||||||
|
// ============================================================================
|
||||||
|
// ingestEvent 测试用例
|
||||||
|
// ============================================================================
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief 正常输入测试:有效事件接入成功
|
||||||
|
*/
|
||||||
|
TEST_F(CmsEngineTest, testIngestEventValid) {
|
||||||
|
EventRecord event;
|
||||||
|
event.id = "EVT-001";
|
||||||
|
event.priority = 100;
|
||||||
|
event.status = EventStatus::Pending;
|
||||||
|
|
||||||
|
bool result = engine->ingestEvent(event);
|
||||||
|
EXPECT_TRUE(result);
|
||||||
|
|
||||||
|
// 验证事件已存储
|
||||||
|
const EventRecord* found = engine->findEventById("EVT-001");
|
||||||
|
ASSERT_NE(found, nullptr);
|
||||||
|
EXPECT_EQ(found->id, "EVT-001");
|
||||||
|
EXPECT_EQ(found->priority, 100);
|
||||||
|
EXPECT_EQ(found->status, EventStatus::Pending);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief 边界值测试:优先级为 0 的事件接入成功
|
||||||
|
*/
|
||||||
|
TEST_F(CmsEngineTest, testIngestEventPriorityZero) {
|
||||||
|
EventRecord event;
|
||||||
|
event.id = "EVT-002";
|
||||||
|
event.priority = 0;
|
||||||
|
event.status = EventStatus::Pending;
|
||||||
|
|
||||||
|
bool result = engine->ingestEvent(event);
|
||||||
|
EXPECT_TRUE(result);
|
||||||
|
|
||||||
|
const EventRecord* found = engine->findEventById("EVT-002");
|
||||||
|
ASSERT_NE(found, nullptr);
|
||||||
|
EXPECT_EQ(found->priority, 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief 边界值测试:优先级为 255 的事件接入成功
|
||||||
|
*/
|
||||||
|
TEST_F(CmsEngineTest, testIngestEventPriorityMax) {
|
||||||
|
EventRecord event;
|
||||||
|
event.id = "EVT-003";
|
||||||
|
event.priority = 255;
|
||||||
|
event.status = EventStatus::Pending;
|
||||||
|
|
||||||
|
bool result = engine->ingestEvent(event);
|
||||||
|
EXPECT_TRUE(result);
|
||||||
|
|
||||||
|
const EventRecord* found = engine->findEventById("EVT-003");
|
||||||
|
ASSERT_NE(found, nullptr);
|
||||||
|
EXPECT_EQ(found->priority, 255);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief 异常输入测试:空 ID 事件接入失败
|
||||||
|
*/
|
||||||
|
TEST_F(CmsEngineTest, testIngestEventEmptyId) {
|
||||||
|
EventRecord event;
|
||||||
|
event.id = "";
|
||||||
|
event.priority = 100;
|
||||||
|
event.status = EventStatus::Pending;
|
||||||
|
|
||||||
|
bool result = engine->ingestEvent(event);
|
||||||
|
EXPECT_FALSE(result);
|
||||||
|
|
||||||
|
// 验证事件未存储
|
||||||
|
const EventRecord* found = engine->findEventById("");
|
||||||
|
EXPECT_EQ(found, nullptr);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief 异常输入测试:优先级超过 255 的事件接入失败
|
||||||
|
*/
|
||||||
|
TEST_F(CmsEngineTest, testIngestEventPriorityOverflow) {
|
||||||
|
EventRecord event;
|
||||||
|
event.id = "EVT-004";
|
||||||
|
event.priority = 256;
|
||||||
|
event.status = EventStatus::Pending;
|
||||||
|
|
||||||
|
bool result = engine->ingestEvent(event);
|
||||||
|
EXPECT_FALSE(result);
|
||||||
|
|
||||||
|
const EventRecord* found = engine->findEventById("EVT-004");
|
||||||
|
EXPECT_EQ(found, nullptr);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief 边界值测试:优先级为 256 的边界情况
|
||||||
|
*/
|
||||||
|
TEST_F(CmsEngineTest, testIngestEventPriorityJustOver) {
|
||||||
|
EventRecord event;
|
||||||
|
event.id = "EVT-005";
|
||||||
|
event.priority = 256;
|
||||||
|
event.status = EventStatus::Pending;
|
||||||
|
|
||||||
|
bool result = engine->ingestEvent(event);
|
||||||
|
EXPECT_FALSE(result);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief 特殊场景测试:连续接入多个事件
|
||||||
|
*/
|
||||||
|
TEST_F(CmsEngineTest, testIngestEventMultiple) {
|
||||||
|
EventRecord event1;
|
||||||
|
event1.id = "EVT-001";
|
||||||
|
event1.priority = 50;
|
||||||
|
event1.status = EventStatus::Pending;
|
||||||
|
|
||||||
|
EventRecord event2;
|
||||||
|
event2.id = "EVT-002";
|
||||||
|
event2.priority = 150;
|
||||||
|
event2.status = EventStatus::Pending;
|
||||||
|
|
||||||
|
EXPECT_TRUE(engine->ingestEvent(event1));
|
||||||
|
EXPECT_TRUE(engine->ingestEvent(event2));
|
||||||
|
|
||||||
|
const EventRecord* found1 = engine->findEventById("EVT-001");
|
||||||
|
const EventRecord* found2 = engine->findEventById("EVT-002");
|
||||||
|
ASSERT_NE(found1, nullptr);
|
||||||
|
ASSERT_NE(found2, nullptr);
|
||||||
|
EXPECT_EQ(found1->priority, 50);
|
||||||
|
EXPECT_EQ(found2->priority, 150);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief 特殊场景测试:接入事件后状态保持不变
|
||||||
|
*/
|
||||||
|
TEST_F(CmsEngineTest, testIngestEventStatusPreserved) {
|
||||||
|
EventRecord event;
|
||||||
|
event.id = "EVT-010";
|
||||||
|
event.priority = 200;
|
||||||
|
event.status = EventStatus::Generated;
|
||||||
|
|
||||||
|
EXPECT_TRUE(engine->ingestEvent(event));
|
||||||
|
|
||||||
|
const EventRecord* found = engine->findEventById("EVT-010");
|
||||||
|
ASSERT_NE(found, nullptr);
|
||||||
|
EXPECT_EQ(found->status, EventStatus::Generated);
|
||||||
|
}
|
||||||
|
|
||||||
|
// ============================================================================
|
||||||
|
// processPendingEvents 测试用例
|
||||||
|
// ============================================================================
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief 正常输入测试:处理待处理事件
|
||||||
|
*/
|
||||||
|
TEST_F(CmsEngineTest, testProcessPendingEventsValid) {
|
||||||
|
EventRecord event;
|
||||||
|
event.id = "EVT-001";
|
||||||
|
event.priority = 100;
|
||||||
|
event.status = EventStatus::Pending;
|
||||||
|
engine->ingestEvent(event);
|
||||||
|
|
||||||
|
size_t count = engine->processPendingEvents();
|
||||||
|
EXPECT_EQ(count, 1);
|
||||||
|
|
||||||
|
// 验证事件状态已更新
|
||||||
|
const EventRecord* found = engine->findEventById("EVT-001");
|
||||||
|
ASSERT_NE(found, nullptr);
|
||||||
|
EXPECT_EQ(found->status, EventStatus::Generated);
|
||||||
|
|
||||||
|
// 验证生成了对应的 TaskPlan
|
||||||
|
const std::vector<TaskPlan>& plans = engine->getAllPlans();
|
||||||
|
ASSERT_EQ(plans.size(), 1);
|
||||||
|
EXPECT_EQ(plans[0].name, "AutoPlan-EVT-001");
|
||||||
|
EXPECT_EQ(plans[0].type, PlanType::Centralized);
|
||||||
|
EXPECT_EQ(plans[0].status, PlanStatus::Drafting);
|
||||||
|
EXPECT_EQ(plans[0].relatedEventId, "EVT-001");
|
||||||
|
EXPECT_FLOAT_EQ(plans[0].resourceQuota, 0.5f);
|
||||||
|
EXPECT_EQ(plans[0].constraints, "自动生成约束");
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief 边界值测试:无待处理事件
|
||||||
|
*/
|
||||||
|
TEST_F(CmsEngineTest, testProcessPendingEventsNoPending) {
|
||||||
|
EventRecord event;
|
||||||
|
event.id = "EVT-001";
|
||||||
|
event.priority = 100;
|
||||||
|
event.status = EventStatus::Generated; // 非 Pending 状态
|
||||||
|
engine->ingestEvent(event);
|
||||||
|
|
||||||
|
size_t count = engine->processPendingEvents();
|
||||||
|
EXPECT_EQ(count, 0);
|
||||||
|
|
||||||
|
// 验证事件状态未改变
|
||||||
|
const EventRecord* found = engine->findEventById("EVT-001");
|
||||||
|
ASSERT_NE(found, nullptr);
|
||||||
|
EXPECT_EQ(found->status, EventStatus::Generated);
|
||||||
|
|
||||||
|
// 验证未生成 TaskPlan
|
||||||
|
const std::vector<TaskPlan>& plans = engine->getAllPlans();
|
||||||
|
EXPECT_EQ(plans.size(), 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief 边界值测试:空事件列表
|
||||||
|
*/
|
||||||
|
TEST_F(CmsEngineTest, testProcessPendingEventsEmptyList) {
|
||||||
|
size_t count = engine->processPendingEvents();
|
||||||
|
EXPECT_EQ(count, 0);
|
||||||
|
|
||||||
|
const std::vector<TaskPlan>& plans = engine->getAllPlans();
|
||||||
|
EXPECT_EQ(plans.size(), 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief 特殊场景测试:多个待处理事件
|
||||||
|
*/
|
||||||
|
TEST_F(CmsEngineTest, testProcessPendingEventsMultiple) {
|
||||||
|
EventRecord event1;
|
||||||
|
event1.id = "EVT-001";
|
||||||
|
event1.priority = 50;
|
||||||
|
event1.status = EventStatus::Pending;
|
||||||
|
engine->ingestEvent(event1);
|
||||||
|
|
||||||
|
EventRecord event2;
|
||||||
|
event2.id = "EVT-002";
|
||||||
|
event2.priority = 150;
|
||||||
|
event2.status = EventStatus::Pending;
|
||||||
|
engine->ingestEvent(event2);
|
||||||
|
|
||||||
|
EventRecord event3;
|
||||||
|
event3.id = "EVT-003";
|
||||||
|
event3.priority = 200;
|
||||||
|
event3.status = EventStatus::Generated; // 非 Pending
|
||||||
|
engine->ingestEvent(event3);
|
||||||
|
|
||||||
|
size_t count = engine->processPendingEvents();
|
||||||
|
EXPECT_EQ(count, 2);
|
||||||
|
|
||||||
|
// 验证事件状态
|
||||||
|
const EventRecord* found1 = engine->findEventById("EVT-001");
|
||||||
|
const EventRecord* found2 = engine->findEventById("EVT-002");
|
||||||
|
const EventRecord* found3 = engine->findEventById("EVT-003");
|
||||||
|
ASSERT_NE(found1, nullptr);
|
||||||
|
ASSERT_NE(found2, nullptr);
|
||||||
|
ASSERT_NE(found3, nullptr);
|
||||||
|
EXPECT_EQ(found1->status, EventStatus::Generated);
|
||||||
|
EXPECT_EQ(found2->status, EventStatus::Generated);
|
||||||
|
EXPECT_EQ(found3->status, EventStatus::Generated); // 保持不变
|
||||||
|
|
||||||
|
// 验证生成了 2 个 TaskPlan
|
||||||
|
const std::vector<TaskPlan>& plans = engine->getAllPlans();
|
||||||
|
ASSERT_EQ(plans.size(), 2);
|
||||||
|
EXPECT_EQ(plans[0].relatedEventId, "EVT-001");
|
||||||
|
EXPECT_EQ(plans[1].relatedEventId, "EVT-002");
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief 特殊场景测试:重复处理待处理事件
|
||||||
|
*/
|
||||||
|
TEST_F(CmsEngineTest, testProcessPendingEventsIdempotent) {
|
||||||
|
EventRecord event;
|
||||||
|
event.id = "EVT-001";
|
||||||
|
event.priority = 100;
|
||||||
|
event.status = EventStatus::Pending;
|
||||||
|
engine->ingestEvent(event);
|
||||||
|
|
||||||
|
// 第一次处理
|
||||||
|
size_t count1 = engine->processPendingEvents();
|
||||||
|
EXPECT_EQ(count1, 1);
|
||||||
|
|
||||||
|
// 第二次处理(事件状态已变为 Generated,不应再次处理)
|
||||||
|
size_t count2 = engine->processPendingEvents();
|
||||||
|
EXPECT_EQ(count2, 0);
|
||||||
|
|
||||||
|
// 验证只生成了一个 TaskPlan
|
||||||
|
const std::vector<TaskPlan>& plans = engine->getAllPlans();
|
||||||
|
ASSERT_EQ(plans.size(), 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief 特殊场景测试:处理事件后生成唯一 ID
|
||||||
|
*/
|
||||||
|
TEST_F(CmsEngineTest, testProcessPendingEventsUniqueIds) {
|
||||||
|
EventRecord event1;
|
||||||
|
event1.id = "EVT-001";
|
||||||
|
event1.priority = 100;
|
||||||
|
event1.status = EventStatus::Pending;
|
||||||
|
engine->ingestEvent(event1);
|
||||||
|
|
||||||
|
EventRecord event2;
|
||||||
|
event2.id = "EVT-002";
|
||||||
|
event2.priority = 200;
|
||||||
|
event2.status = EventStatus::Pending;
|
||||||
|
engine->ingestEvent(event2);
|
||||||
|
|
||||||
|
engine->processPendingEvents();
|
||||||
|
|
||||||
|
const std::vector<TaskPlan>& plans = engine->getAllPlans();
|
||||||
|
ASSERT_EQ(plans.size(), 2);
|
||||||
|
EXPECT_NE(plans[0].id, plans[1].id); // ID 必须唯一
|
||||||
|
EXPECT_TRUE(plans[0].id.find("ID-") != std::string::npos);
|
||||||
|
EXPECT_TRUE(plans[1].id.find("ID-") != std::string::npos);
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue