pgLab/tests/pgintegration/PgTestEnvironment.h

53 lines
1.5 KiB
C
Raw Permalink Normal View History

#pragma once
#include <gtest/gtest.h>
#include <optional>
#include <QString>
namespace pgtest {
/// Value of the TEST_DATABASE_URL environment variable, if set and non-empty.
std::optional<QString> baseUrl();
/// Same as baseUrl() but with the database name replaced.
QString urlForDb(const QString &db);
/// True when a server is configured and the template database was created.
bool available();
/// server_version_num of the server under test (0 when unavailable).
int serverVersionNum();
/// Portable process id.
qint64 processId();
/// Name of the template database for this process.
QString templateDbName();
2026-10-18 12:32:16 +02:00
/// 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);
/// Global environment, register it in main():
/// ::testing::AddGlobalTestEnvironment(new pgtest::PgEnv(PGTEST_FIXTURE_DIR));
/// Creates a template database from <fixtureDir>/schema.sql once per process.
class PgEnv : public ::testing::Environment {
public:
explicit PgEnv(QString fixtureDir) : fixtureDir_(std::move(fixtureDir)) {}
void SetUp() override;
void TearDown() override;
private:
QString fixtureDir_;
};
} // namespace pgtest
#define REQUIRE_PG_AT_LEAST(v) \
if (pgtest::serverVersionNum() < (v) * 10000) GTEST_SKIP() << "needs PG " #v