mono-cpp/tests/functional/test_postgres_live.cpp
2026-03-28 13:11:29 +01:00

82 lines
3.0 KiB
C++

#include <catch2/catch_test_macros.hpp>
#include "postgres/postgres.h"
#include "search/search.h"
#include "json/json.h"
#include "logger/logger.h"
#include <toml++/toml.h>
// Note: This test requires a valid config/postgres.toml pointing to a Supabase instance.
// We test against an arbitrary table 'test_items' or standard table.
// In this case we'll test against `grid_search_runs` since we know it exists,
// using a dummy uuid for testing.
// DO NOT RUN UNLESS CONFIGURED.
TEST_CASE("Postgres Live Operations", "[postgres_live]") {
// Load config
std::string supabase_url;
std::string supabase_key;
try {
auto config = toml::parse_file("config/postgres.toml");
supabase_url = config["supabase"]["url"].value_or("");
supabase_key = config["supabase"]["service_key"].value_or("");
} catch (const std::exception &e) {
WARN("Skipping postgres live tests. Config missing or invalid: " << e.what());
return;
}
if (supabase_url.empty() || supabase_key.empty()) {
WARN("Skipping postgres live tests. Supabase credentials missing.");
return;
}
postgres::Config pg_cfg;
pg_cfg.supabase_url = supabase_url;
pg_cfg.supabase_key = supabase_key;
postgres::init(pg_cfg);
REQUIRE(postgres::ping() == "ok");
std::string test_id = "00000000-0000-0000-0000-0000000000cc";
std::string user_id = "3bb4cfbf-318b-44d3-a9d3-35680e738421";
SECTION("Insert, Query, Update, Upsert, Delete") {
// 1. Clean up first just in case
postgres::del("grid_search_runs", "id=eq." + test_id);
// 2. Insert
std::string insert_body = R"({"id": ")" + test_id + R"(", "user_id": ")" + user_id + R"(", "run_id": "test_run", "status": "searching", "request": {}})";
std::string res1 = postgres::insert("grid_search_runs", insert_body);
// 3. Query
std::string res2 = postgres::query("grid_search_runs", "*", "id=eq." + test_id);
WARN("Insert Result: " << res1);
WARN("Query Result: " << res2);
REQUIRE(json::is_valid(res2));
REQUIRE(res2.find("test_run") != std::string::npos);
// 4. Update
std::string update_body = R"({"status": "enriching"})";
std::string res3 = postgres::update("grid_search_runs", update_body, "id=eq." + test_id);
REQUIRE(json::is_valid(res3));
REQUIRE(res3.find("error") == std::string::npos);
// 5. Upsert
std::string upsert_body = R"({"id": ")" + test_id + R"(", "user_id": ")" + user_id + R"(", "run_id": "upsert_run", "status": "complete", "request": {}})";
std::string res4 = postgres::upsert("grid_search_runs", upsert_body, "id");
REQUIRE(res4.find("error") == std::string::npos);
// Query again to verify upsert
std::string res5 = postgres::query("grid_search_runs", "*", "id=eq." + test_id);
REQUIRE(res5.find("upsert_run") != std::string::npos);
// 6. Delete
std::string res6 = postgres::del("grid_search_runs", "id=eq." + test_id);
REQUIRE(json::is_valid(res6));
// Verify deleted
std::string res7 = postgres::query("grid_search_runs", "*", "id=eq." + test_id);
REQUIRE(res7 == "[]");
}
}