126 lines
3.4 KiB
C++
126 lines
3.4 KiB
C++
|
|
#include "app.hpp"
|
|||
|
|
|
|||
|
|
#include <cassert>
|
|||
|
|
#include <cstdio>
|
|||
|
|
#include <iostream>
|
|||
|
|
|
|||
|
|
/**
|
|||
|
|
* @brief 测试创建任务和列表查询。
|
|||
|
|
*/
|
|||
|
|
static void test_create_and_list() {
|
|||
|
|
TodoService service("test_todos.json");
|
|||
|
|
assert(service.get_all().empty());
|
|||
|
|
|
|||
|
|
auto item = service.create("\u6D4B\u8BD5\u4EFB\u52A1", "\u6D4B\u8BD5\u63CF\u8FF0");
|
|||
|
|
assert(item.id == 1);
|
|||
|
|
assert(item.title == "\u6D4B\u8BD5\u4EFB\u52A1");
|
|||
|
|
assert(item.description == "\u6D4B\u8BD5\u63CF\u8FF0");
|
|||
|
|
assert(item.completed == false);
|
|||
|
|
|
|||
|
|
assert(service.get_all().size() == 1);
|
|||
|
|
std::cout << "\u2713 test_create_and_list passed\n";
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/**
|
|||
|
|
* @brief 测试按 ID 查询任务。
|
|||
|
|
*/
|
|||
|
|
static void test_get_by_id() {
|
|||
|
|
TodoService service("test_todos.json");
|
|||
|
|
service.create("\u4EFB\u52A1A");
|
|||
|
|
service.create("\u4EFB\u52A1B");
|
|||
|
|
|
|||
|
|
auto found = service.get_by_id(1);
|
|||
|
|
assert(found.has_value());
|
|||
|
|
assert(found->title == "\u4EFB\u52A1A");
|
|||
|
|
|
|||
|
|
auto not_found = service.get_by_id(999);
|
|||
|
|
assert(!not_found.has_value());
|
|||
|
|
std::cout << "\u2713 test_get_by_id passed\n";
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/**
|
|||
|
|
* @brief 测试更新任务(全字段更新和部分更新)。
|
|||
|
|
*/
|
|||
|
|
static void test_update() {
|
|||
|
|
TodoService service("test_todos.json");
|
|||
|
|
auto item = service.create("\u539F\u6807\u9898", "\u539F\u63CF\u8FF0");
|
|||
|
|
|
|||
|
|
// 全字段更新
|
|||
|
|
auto updated = service.update(item.id, "\u65B0\u6807\u9898",
|
|||
|
|
"\u65B0\u63CF\u8FF0", true);
|
|||
|
|
assert(updated.has_value());
|
|||
|
|
assert(updated->title == "\u65B0\u6807\u9898");
|
|||
|
|
assert(updated->description == "\u65B0\u63CF\u8FF0");
|
|||
|
|
assert(updated->completed == true);
|
|||
|
|
|
|||
|
|
// 部分更新(仅修改完成状态)
|
|||
|
|
auto partial = service.update(item.id, std::nullopt, std::nullopt, false);
|
|||
|
|
assert(partial.has_value());
|
|||
|
|
assert(partial->title == "\u65B0\u6807\u9898");
|
|||
|
|
assert(partial->completed == false);
|
|||
|
|
std::cout << "\u2713 test_update passed\n";
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/**
|
|||
|
|
* @brief 测试删除任务。
|
|||
|
|
*/
|
|||
|
|
static void test_delete() {
|
|||
|
|
TodoService service("test_todos.json");
|
|||
|
|
service.create("\u5F85\u5220\u9664");
|
|||
|
|
assert(service.get_all().size() == 1);
|
|||
|
|
|
|||
|
|
bool result = service.delete_item(1);
|
|||
|
|
assert(result == true);
|
|||
|
|
assert(service.get_all().empty());
|
|||
|
|
|
|||
|
|
// 删除不存在的 ID
|
|||
|
|
result = service.delete_item(999);
|
|||
|
|
assert(result == false);
|
|||
|
|
std::cout << "\u2713 test_delete passed\n";
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/**
|
|||
|
|
* @brief 测试 JSON 文件持久化:构造→写入→重新加载→验证。
|
|||
|
|
*/
|
|||
|
|
static void test_persistence() {
|
|||
|
|
const char* persist_file = "test_persist.json";
|
|||
|
|
|
|||
|
|
// 创建数据并写入文件
|
|||
|
|
{
|
|||
|
|
TodoService service(persist_file);
|
|||
|
|
service.create("\u6301\u4E45\u5316\u6D4B\u8BD5");
|
|||
|
|
service.create("\u7B2C\u4E8C\u4E2A\u4EFB\u52A1");
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// 重新加载并验证
|
|||
|
|
{
|
|||
|
|
TodoService service(persist_file);
|
|||
|
|
assert(service.get_all().size() == 2);
|
|||
|
|
assert(service.get_by_id(1).has_value());
|
|||
|
|
assert(service.get_by_id(2).has_value());
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
std::remove(persist_file);
|
|||
|
|
std::cout << "\u2713 test_persistence passed\n";
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/**
|
|||
|
|
* @brief 测试入口。
|
|||
|
|
*/
|
|||
|
|
int main() {
|
|||
|
|
// 清理可能残留的测试文件
|
|||
|
|
std::remove("test_todos.json");
|
|||
|
|
|
|||
|
|
test_create_and_list();
|
|||
|
|
test_get_by_id();
|
|||
|
|
test_update();
|
|||
|
|
test_delete();
|
|||
|
|
test_persistence();
|
|||
|
|
|
|||
|
|
// 最终清理
|
|||
|
|
std::remove("test_todos.json");
|
|||
|
|
|
|||
|
|
std::cout << "\n\u2713 \u2713 \u2713 \u6240\u6709\u6D4B\u8BD5\u901A\u8FC7\uFF01\n";
|
|||
|
|
return 0;
|
|||
|
|
}
|