文件
csp/backend/tests/sqlite_db_test.cc

45 行
1.6 KiB
C++

#include <catch2/catch_test_macros.hpp>
#include "csp/db/sqlite_db.h"
#include <sqlite3.h>
namespace {
int CountTable(sqlite3* db, const char* table_name) {
sqlite3_stmt* stmt = nullptr;
const char* sql =
"SELECT COUNT(1) FROM sqlite_master WHERE type='table' AND name=?";
REQUIRE(sqlite3_prepare_v2(db, sql, -1, &stmt, nullptr) == SQLITE_OK);
REQUIRE(sqlite3_bind_text(stmt, 1, table_name, -1, SQLITE_TRANSIENT) ==
SQLITE_OK);
REQUIRE(sqlite3_step(stmt) == SQLITE_ROW);
const int v = sqlite3_column_int(stmt, 0);
sqlite3_finalize(stmt);
return v;
}
} // namespace
TEST_CASE("migrations create core tables") {
auto db = csp::db::SqliteDb::OpenMemory();
csp::db::ApplyMigrations(db);
REQUIRE(CountTable(db.raw(), "users") == 1);
REQUIRE(CountTable(db.raw(), "sessions") == 1);
REQUIRE(CountTable(db.raw(), "problems") == 1);
REQUIRE(CountTable(db.raw(), "problem_tags") == 1);
REQUIRE(CountTable(db.raw(), "submissions") == 1);
REQUIRE(CountTable(db.raw(), "wrong_book") == 1);
REQUIRE(CountTable(db.raw(), "contests") == 1);
REQUIRE(CountTable(db.raw(), "contest_problems") == 1);
REQUIRE(CountTable(db.raw(), "contest_registrations") == 1);
REQUIRE(CountTable(db.raw(), "kb_articles") == 1);
REQUIRE(CountTable(db.raw(), "kb_article_links") == 1);
REQUIRE(CountTable(db.raw(), "import_jobs") == 1);
REQUIRE(CountTable(db.raw(), "import_job_items") == 1);
REQUIRE(CountTable(db.raw(), "problem_drafts") == 1);
REQUIRE(CountTable(db.raw(), "problem_solution_jobs") == 1);
REQUIRE(CountTable(db.raw(), "problem_solutions") == 1);
}