文件
csp/backend/tests/problem_workspace_service_test.cc

63 行
2.1 KiB
C++

#include <catch2/catch_test_macros.hpp>
#include "csp/db/sqlite_db.h"
#include "csp/services/problem_workspace_service.h"
#include <sqlite3.h>
TEST_CASE("problem workspace service drafts and solution jobs") {
auto db = csp::db::SqliteDb::OpenMemory();
csp::db::ApplyMigrations(db);
csp::db::SeedDemoData(db);
REQUIRE(sqlite3_exec(
db.raw(),
"INSERT INTO users(username,password_salt,password_hash,created_at)"
" VALUES('tester','salt','hash',0)",
nullptr,
nullptr,
nullptr) == SQLITE_OK);
sqlite3_stmt* stmt = nullptr;
REQUIRE(sqlite3_prepare_v2(db.raw(), "SELECT id FROM problems ORDER BY id LIMIT 1",
-1, &stmt, nullptr) == SQLITE_OK);
REQUIRE(sqlite3_step(stmt) == SQLITE_ROW);
const int64_t pid = sqlite3_column_int64(stmt, 0);
sqlite3_finalize(stmt);
REQUIRE(pid > 0);
csp::services::ProblemWorkspaceService svc(db);
REQUIRE(svc.ProblemExists(pid));
svc.SaveDraft(1, pid, "cpp", "int main(){return 0;}", "1 2\n");
const auto draft = svc.GetDraft(1, pid);
REQUIRE(draft.has_value());
REQUIRE(draft->language == "cpp");
REQUIRE(draft->code.find("main") != std::string::npos);
const auto job_id = svc.CreateSolutionJob(pid, 1, 3);
REQUIRE(job_id > 0);
const auto latest = svc.GetLatestSolutionJob(pid);
REQUIRE(latest.has_value());
REQUIRE(latest->id == job_id);
REQUIRE(latest->status == "queued");
REQUIRE(latest->max_solutions == 3);
REQUIRE(latest->problem_title.empty());
const auto recent = svc.ListRecentSolutionJobs(10);
REQUIRE(recent.size() == 1);
REQUIRE(recent.front().id == job_id);
REQUIRE(recent.front().problem_id == pid);
REQUIRE(!recent.front().problem_title.empty());
const auto solutions = svc.ListSolutions(pid);
REQUIRE(solutions.empty());
REQUIRE(svc.CountProblemsWithoutSolutions() >= 1);
const auto missing_all = svc.ListProblemIdsWithoutSolutions(10, false);
REQUIRE(!missing_all.empty());
const auto missing_skip_busy = svc.ListProblemIdsWithoutSolutions(10, true);
REQUIRE(!missing_skip_busy.empty());
REQUIRE(missing_skip_busy.size() < missing_all.size());
}