Add tests for loading the catalog
This commit is contained in:
parent
591573b6b8
commit
a1ac545086
13 changed files with 203 additions and 32 deletions
|
|
@ -15,6 +15,13 @@ QString DbTest::dbUrl() const
|
|||
return urlForDb(dbName_);
|
||||
}
|
||||
|
||||
std::unique_ptr<Pgsql::Connection> DbTest::plainConn() const
|
||||
{
|
||||
auto c = std::make_unique<Pgsql::Connection>();
|
||||
c->connect(urlForDbAsPlainRole(dbName_));
|
||||
return c;
|
||||
}
|
||||
|
||||
void DbTest::SetUp()
|
||||
{
|
||||
if (!available())
|
||||
|
|
|
|||
|
|
@ -17,6 +17,8 @@ protected:
|
|||
Pgsql::Connection& conn() { return *conn_; }
|
||||
QString dbName() const { return dbName_; }
|
||||
QString dbUrl() const;
|
||||
/// Opens a new connection to this test's database as the plain (non-superuser) role.
|
||||
std::unique_ptr<Pgsql::Connection> plainConn() const;
|
||||
|
||||
private:
|
||||
QString dbName_;
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -24,6 +24,12 @@ qint64 processId();
|
|||
/// Name of the template database for this process.
|
||||
QString templateDbName();
|
||||
|
||||
/// Name of the login role without superuser rights, unique per process.
|
||||
QString plainRoleName();
|
||||
|
||||
/// Same as urlForDb() but with the plain role as user.
|
||||
QString urlForDbAsPlainRole(const QString &db);
|
||||
|
||||
/// Quotes an identifier for use in SQL.
|
||||
QString quoteIdent(const QString &name);
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue