Add tests for loading the catalog
This commit is contained in:
parent
591573b6b8
commit
a1ac545086
13 changed files with 203 additions and 32 deletions
|
|
@ -1,10 +1,13 @@
|
|||
add_executable(IntegrationTests
|
||||
main.cpp
|
||||
tst_Smoke.cpp
|
||||
tst_CatalogLoad.cpp
|
||||
tst_DatabaseSizes.cpp
|
||||
)
|
||||
|
||||
target_link_libraries(IntegrationTests PRIVATE
|
||||
pgintegration
|
||||
pglablib
|
||||
GTest::gtest
|
||||
Qt6::Core
|
||||
)
|
||||
|
|
|
|||
36
tests/integration/tst_CatalogLoad.cpp
Normal file
36
tests/integration/tst_CatalogLoad.cpp
Normal file
|
|
@ -0,0 +1,36 @@
|
|||
#include "DbTest.h"
|
||||
#include "PgTestEnvironment.h"
|
||||
#include "catalog/PgClassContainer.h"
|
||||
#include "catalog/PgDatabaseCatalog.h"
|
||||
#include "Pgsql_Result.h"
|
||||
|
||||
using namespace pgtest;
|
||||
|
||||
class CatalogLoad : public DbTest {};
|
||||
|
||||
TEST_F(CatalogLoad, SuperuserLoadsCatalog)
|
||||
{
|
||||
PgDatabaseCatalog catalog;
|
||||
catalog.loadAll(conn(), {});
|
||||
|
||||
EXPECT_EQ(catalog.serverVersion(), serverVersionNum());
|
||||
EXPECT_EQ(catalog.getDBName(), dbName());
|
||||
ASSERT_NE(catalog.classes(), nullptr);
|
||||
EXPECT_NE(catalog.classes()->getByObjectNsAndName("public", "fixture_item"), nullptr);
|
||||
}
|
||||
|
||||
TEST_F(CatalogLoad, PlainRoleLoadsCatalog)
|
||||
{
|
||||
auto plain = plainConn();
|
||||
Pgsql::Result r = plain->query("SELECT rolsuper FROM pg_roles WHERE rolname = current_user");
|
||||
ASSERT_EQ(r.rows(), 1);
|
||||
EXPECT_EQ(QString::fromUtf8(r.val(0, 0)), QString("f"));
|
||||
|
||||
PgDatabaseCatalog catalog;
|
||||
catalog.loadAll(*plain, {});
|
||||
|
||||
EXPECT_EQ(catalog.serverVersion(), serverVersionNum());
|
||||
EXPECT_EQ(catalog.getDBName(), dbName());
|
||||
ASSERT_NE(catalog.classes(), nullptr);
|
||||
EXPECT_NE(catalog.classes()->getByObjectNsAndName("public", "fixture_item"), nullptr);
|
||||
}
|
||||
68
tests/integration/tst_DatabaseSizes.cpp
Normal file
68
tests/integration/tst_DatabaseSizes.cpp
Normal file
|
|
@ -0,0 +1,68 @@
|
|||
#include "DbTest.h"
|
||||
#include "PgTestEnvironment.h"
|
||||
#include "catalog/PgDatabaseSizes.h"
|
||||
#include "Pgsql_Result.h"
|
||||
|
||||
using namespace pgtest;
|
||||
|
||||
namespace {
|
||||
|
||||
Oid oidOfDatabase(Pgsql::Connection &c, const QString &db)
|
||||
{
|
||||
Pgsql::Result r = c.query(QString("SELECT oid FROM pg_database WHERE datname = '%1'").arg(db));
|
||||
if (!r || r.rows() != 1)
|
||||
return InvalidOid;
|
||||
return QString::fromUtf8(r.val(0, 0)).toUInt();
|
||||
}
|
||||
|
||||
const PgDatabaseSize *findSize(const PgDatabaseSizes &sizes, Oid oid)
|
||||
{
|
||||
for (const auto &s : sizes) {
|
||||
if (s.oid == oid)
|
||||
return &s;
|
||||
}
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
class DatabaseSizes : public DbTest {};
|
||||
|
||||
TEST_F(DatabaseSizes, ConnectableDatabaseReturnsSize)
|
||||
{
|
||||
auto plain = plainConn();
|
||||
Oid oid = oidOfDatabase(*plain, dbName());
|
||||
ASSERT_NE(oid, InvalidOid);
|
||||
|
||||
PgDatabaseSizes sizes = loadDatabaseSizes(*plain);
|
||||
const PgDatabaseSize *s = findSize(sizes, oid);
|
||||
ASSERT_NE(s, nullptr);
|
||||
EXPECT_GE(s->totalBytes, 0);
|
||||
}
|
||||
|
||||
TEST_F(DatabaseSizes, RevokedConnectReturnsMinusOne)
|
||||
{
|
||||
Oid oid = oidOfDatabase(conn(), dbName());
|
||||
ASSERT_NE(oid, InvalidOid);
|
||||
conn().query(QString("REVOKE CONNECT ON DATABASE %1 FROM PUBLIC").arg(quoteIdent(dbName())));
|
||||
|
||||
auto plain = std::make_unique<Pgsql::Connection>();
|
||||
plain->connect(urlForDbAsPlainRole("postgres"));
|
||||
|
||||
PgDatabaseSizes sizes = loadDatabaseSizes(*plain);
|
||||
const PgDatabaseSize *s = findSize(sizes, oid);
|
||||
ASSERT_NE(s, nullptr);
|
||||
EXPECT_EQ(s->totalBytes, -1);
|
||||
}
|
||||
|
||||
TEST_F(DatabaseSizes, SuperuserSeesAllDatabases)
|
||||
{
|
||||
PgDatabaseSizes sizes = loadDatabaseSizes(conn());
|
||||
|
||||
Pgsql::Result r = conn().query("SELECT count(*) FROM pg_database");
|
||||
ASSERT_EQ(r.rows(), 1);
|
||||
EXPECT_EQ(static_cast<size_t>(QString::fromUtf8(r.val(0, 0)).toULongLong()), sizes.size());
|
||||
|
||||
for (const auto &s : sizes)
|
||||
EXPECT_GE(s.totalBytes, 0) << "database oid " << s.oid;
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue