polymech pages

This commit is contained in:
lovebird 2026-02-18 12:42:20 +01:00
parent faa8554035
commit 4c5d8ad717
5 changed files with 86 additions and 0 deletions

View File

@ -0,0 +1,9 @@
add_library(polymech STATIC
src/polymech.cpp
)
target_include_directories(polymech
PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/include
)
target_link_libraries(polymech PUBLIC postgres logger)

View File

@ -0,0 +1,16 @@
#pragma once
#include <string>
#include <vector>
namespace polymech {
/// Fetch all rows from the "pages" table.
/// Returns raw JSON array string from Supabase.
std::string fetch_pages();
/// Fetch pages with a specific select clause and optional filter.
std::string fetch_pages(const std::string &select,
const std::string &filter = "", int limit = 0);
} // namespace polymech

View File

@ -0,0 +1,17 @@
#include "polymech/polymech.h"
#include "logger/logger.h"
#include "postgres/postgres.h"
namespace polymech {
std::string fetch_pages() { return fetch_pages("*"); }
std::string fetch_pages(const std::string &select, const std::string &filter,
int limit) {
logger::debug("polymech::fetch_pages → select=" + select +
" filter=" + filter + " limit=" + std::to_string(limit));
return postgres::query("pages", select, filter, limit);
}
} // namespace polymech

View File

@ -0,0 +1,34 @@
#include <catch2/catch_test_macros.hpp>
#include <toml++/toml.hpp>
#include "logger/logger.h"
#include "polymech/polymech.h"
#include "postgres/postgres.h"
#include "json/json.h"
// ── E2E: fetch pages from live Supabase ─────────────────────────────────
TEST_CASE("E2E: fetch all pages", "[e2e]") {
logger::init("e2e-polymech");
// Load config
auto cfg = toml::parse_file("config/postgres.toml");
postgres::Config pg_cfg;
pg_cfg.supabase_url = cfg["supabase"]["url"].value_or(std::string(""));
pg_cfg.supabase_key =
cfg["supabase"]["publishable_key"].value_or(std::string(""));
REQUIRE(!pg_cfg.supabase_url.empty());
REQUIRE(!pg_cfg.supabase_key.empty());
postgres::init(pg_cfg);
auto result = polymech::fetch_pages();
// Should return valid JSON
REQUIRE(json::is_valid(result));
// Should be an array (even if empty)
REQUIRE(result.front() == '[');
REQUIRE(result.back() == ']');
}

View File

@ -0,0 +1,10 @@
#include "polymech/polymech.h"
#include "postgres/postgres.h"
#include <catch2/catch_test_macros.hpp>
// Unit test — no network required
TEST_CASE("polymech::fetch_pages throws without init", "[polymech]") {
// postgres::init has not been called, so fetch_pages should throw
REQUIRE_THROWS(polymech::fetch_pages());
}