Add tests for loading the catalog

This commit is contained in:
eelke 2026-10-18 12:32:16 +02:00
parent 591573b6b8
commit a1ac545086
13 changed files with 203 additions and 32 deletions

View file

@ -16,6 +16,8 @@ namespace {
bool g_available = false;
int g_versionNum = 0;
const char *kPlainRolePassword = "pglab_plain_test";
} // namespace
std::optional<QString> baseUrl()
@ -56,6 +58,23 @@ QString templateDbName()
return QString("tmpl_%1").arg(processId());
}
QString plainRoleName()
{
return QString("tst_%1").arg(processId());
}
QString urlForDbAsPlainRole(const QString &db)
{
auto base = baseUrl();
if (!base)
throw std::runtime_error("TEST_DATABASE_URL is not set");
QUrl url(*base);
url.setPath("/" + db);
url.setUserName(plainRoleName());
url.setPassword(QString::fromUtf8(kPlainRolePassword));
return url.toString();
}
QString quoteIdent(const QString &name)
{
QString r = name;
@ -82,6 +101,9 @@ void PgEnv::SetUp()
g_versionNum = QString::fromUtf8(r.val(0, 0)).toInt();
admin.query(QString("DROP DATABASE IF EXISTS %1 WITH (FORCE)").arg(quoteIdent(tmpl)));
admin.query(QString("CREATE DATABASE %1").arg(quoteIdent(tmpl)));
admin.query(QString("DROP ROLE IF EXISTS %1").arg(quoteIdent(plainRoleName())));
admin.query(QString("CREATE ROLE %1 LOGIN NOSUPERUSER NOCREATEDB NOCREATEROLE PASSWORD '%2'")
.arg(quoteIdent(plainRoleName()), QString::fromUtf8(kPlainRolePassword)));
}
{
QFile f(fixtureDir_ + "/schema.sql");
@ -101,6 +123,7 @@ void PgEnv::SetUp()
Pgsql::Connection admin;
admin.connect(*base);
admin.query(QString("DROP DATABASE IF EXISTS %1 WITH (FORCE)").arg(quoteIdent(tmpl)));
admin.query(QString("DROP ROLE IF EXISTS %1").arg(quoteIdent(plainRoleName())));
}
catch (const std::exception &) {
}
@ -115,9 +138,10 @@ void PgEnv::TearDown()
Pgsql::Connection admin;
admin.connect(*baseUrl());
admin.query(QString("DROP DATABASE IF EXISTS %1 WITH (FORCE)").arg(quoteIdent(templateDbName())));
admin.query(QString("DROP ROLE IF EXISTS %1").arg(quoteIdent(plainRoleName())));
}
catch (const std::exception &e) {
ADD_FAILURE() << "Dropping template database failed: " << e.what();
ADD_FAILURE() << "Dropping template database or plain role failed: " << e.what();
}
g_available = false;
}