plan_execute/tests/test_app.cpp

121 lines
3.4 KiB
C++
Raw Normal View History

2026-05-06 06:16:41 +00:00
#include <gtest/gtest.h>
#include "app.hpp"
#include <fstream>
#include <filesystem>
namespace fs = std::filesystem;
class TodoServiceTest : public ::testing::Test {
protected:
void SetUp() override {
test_file_ = "test_todo.json";
if (fs::exists(test_file_)) fs::remove(test_file_);
}
void TearDown() override {
if (fs::exists(test_file_)) fs::remove(test_file_);
}
std::string test_file_;
};
TEST_F(TodoServiceTest, testLoadValidJsonFile) {
std::ofstream ofs(test_file_);
ofs << R"({
"items": [
{"id": 1, "title": "Task1", "description": "Desc1", "completed": false},
{"id": 2, "title": "Task2", "description": "Desc2", "completed": true}
],
"next_id": 3
})";
ofs.close();
TodoService service(test_file_);
auto items = service.get_all();
ASSERT_EQ(items.size(), 2);
EXPECT_EQ(items[0].id, 1);
EXPECT_EQ(items[1].id, 2);
EXPECT_EQ(items[1].completed, true);
auto new_item = service.create("Task3", "Desc3");
EXPECT_EQ(new_item.id, 3);
}
TEST_F(TodoServiceTest, testLoadEmptyFile) {
std::ofstream ofs(test_file_);
ofs.close();
TodoService service(test_file_);
auto items = service.get_all();
EXPECT_TRUE(items.empty());
auto new_item = service.create("Task", "Desc");
EXPECT_EQ(new_item.id, 1);
}
TEST_F(TodoServiceTest, testLoadNonExistentFile) {
TodoService service("nonexistent.json");
auto items = service.get_all();
EXPECT_TRUE(items.empty());
auto new_item = service.create("Task", "Desc");
EXPECT_EQ(new_item.id, 1);
}
TEST_F(TodoServiceTest, testLoadInvalidJson) {
std::ofstream ofs(test_file_);
ofs << "invalid json content";
ofs.close();
TodoService service(test_file_);
auto items = service.get_all();
EXPECT_TRUE(items.empty());
auto new_item = service.create("Task", "Desc");
EXPECT_EQ(new_item.id, 1);
}
TEST_F(TodoServiceTest, testLoadNextIdAdjustment) {
std::ofstream ofs(test_file_);
ofs << R"({
"items": [
{"id": 5, "title": "Task", "description": "Desc", "completed": false}
],
"next_id": 3
})";
ofs.close();
TodoService service(test_file_);
auto new_item = service.create("NewTask", "NewDesc");
EXPECT_EQ(new_item.id, 6);
}
TEST_F(TodoServiceTest, testSaveValidData) {
TodoService service(test_file_);
service.create("Task1", "Desc1");
service.create("Task2", "Desc2");
std::ifstream ifs(test_file_);
std::string content((std::istreambuf_iterator<char>(ifs)), std::istreambuf_iterator<char>());
EXPECT_NE(content.find("\"id\": 1"), std::string::npos);
EXPECT_NE(content.find("\"id\": 2"), std::string::npos);
EXPECT_NE(content.find("\"next_id\": 3"), std::string::npos);
}
TEST_F(TodoServiceTest, testSaveEmptyItems) {
TodoService service(test_file_);
auto item = service.create("Temp", "Desc");
service.delete_item(item.id);
std::ifstream ifs(test_file_);
std::string content((std::istreambuf_iterator<char>(ifs)), std::istreambuf_iterator<char>());
EXPECT_NE(content.find("\"items\": []"), std::string::npos);
EXPECT_NE(content.find("\"next_id\": 2"), std::string::npos);
}
TEST_F(TodoServiceTest, testSaveInvalidPath) {
TodoService service("/invalid/path/test.json");
EXPECT_NO_THROW(service.create("Task", "Desc"));
EXPECT_FALSE(fs::exists("/invalid/path/test.json"));
}