diff options
author | Tomek Mrugalski <tomek@isc.org> | 2021-10-29 13:14:16 +0200 |
---|---|---|
committer | Tomek Mrugalski <tomek@isc.org> | 2021-11-17 15:35:19 +0100 |
commit | 10be36ea4cdb3410a7e32ca7ac015e20591ca0d5 (patch) | |
tree | 4ba567463b882b444313d72703032c935ec6bdd3 /src | |
parent | [#1848] PgSqlConnectionPtr moved to common place (diff) | |
download | kea-10be36ea4cdb3410a7e32ca7ac015e20591ca0d5.tar.xz kea-10be36ea4cdb3410a7e32ca7ac015e20591ca0d5.zip |
[#1848] The tests now manage their own schema
Diffstat (limited to 'src')
-rw-r--r-- | src/hooks/dhcp/pgsql_cb/tests/pgsql_cb_impl_unittest.cc | 77 |
1 files changed, 76 insertions, 1 deletions
diff --git a/src/hooks/dhcp/pgsql_cb/tests/pgsql_cb_impl_unittest.cc b/src/hooks/dhcp/pgsql_cb/tests/pgsql_cb_impl_unittest.cc index 3e49d24a77..b4dff0abeb 100644 --- a/src/hooks/dhcp/pgsql_cb/tests/pgsql_cb_impl_unittest.cc +++ b/src/hooks/dhcp/pgsql_cb/tests/pgsql_cb_impl_unittest.cc @@ -7,6 +7,7 @@ #include <config.h> #include <dhcpsrv/testutils/pgsql_generic_backend_unittest.h> +#include <pgsql/testutils/pgsql_schema.h> #include <gtest/gtest.h> #include <pgsql_cb_impl.h> @@ -28,9 +29,80 @@ public: params["password"] = "keatest"; params["user"] = "keatest"; + createDummySchema(); + cbptr_.reset(new PgSqlConfigBackendImpl(params, 0)); } + ~PgsqlConfigBackendTest() { + destroyDummySchema(); + } + + /// @brief Creates the absolute minimum schema. + /// + /// This is a basic schema that has the absolute minimum necessary to run some + /// tests. This is the schema that should be used, unless you really need the full + /// schema. See @ref createFullSchema() for full schema that is much slower to create. + /// Don't forget to destroy it with @ref destroyDummySchema(). + void createDummySchema() { + + DatabaseConnection::ParameterMap params; + params["name"] = "keatest"; + params["user"] = "keatest"; + params["password"] = "keatest"; + + // Create and open the database connection + conn_.reset(new PgSqlConnection(params)); + conn_->openDatabase(); + + // Dummy schema queries + const char* sql[] = { + "CREATE TABLE schema_version (version INT PRIMARY KEY NOT NULL, minor INT);", + "INSERT INTO schema_version VALUES (7,0);" + }; + + for (auto s : sql) { + PgSqlResult r(PQexec(*conn_, s)); + ASSERT_EQ(PQresultStatus(r), PGRES_COMMAND_OK) + << " create a table for dummy schema failed: " << PQerrorMessage(*conn_); + } + } + + /// @brief Destroys the basic schema. + /// Asserts if the destruction fails + void destroyDummySchema() { + if (conn_) { + + const char* sql[] = { + "DROP TABLE schema_version;" + }; + + + for (auto s : sql) { + PgSqlResult r(PQexec(*conn_, s)); + ASSERT_EQ(PQresultStatus(r), PGRES_COMMAND_OK) + << " dropping a table for dummy schema failed: " << PQerrorMessage(*conn_); + } + } + } + + /// @brief creates full schema (slow!) + /// + /// If possible, use simpler, faster alternative: @ref createDummySchema(); + /// Don't forget to tear it down with @ref destroyFullSchema(); + void createFullSchema() { + // Create the actual full Kea schema. + isc::db::test::createPgSQLSchema(true, true); + } + + /// @brief destroys the full schema (slow!) + /// + /// Don't forget to call this method once you're done, if you used @ref createFullSchema(). + void destroyFullSchema() { + // Clean up after ourselves. + isc::db::test::destroyPgSQLSchema(true, true); + } + /// @brief checks if specified triplet generating function stores the values properly. /// /// @param f function pointer to a function that converts triplet to PgSqlBindArray @@ -63,9 +135,12 @@ public: } boost::shared_ptr<PgSqlConfigBackendImpl> cbptr_; + + /// @brief Database connection + PgSqlConnectionPtr conn_; }; -// Let's start with absolute basics. Is this the right type? +// Let's start with absolute basics. Is this the right config backend type? TEST_F(PgsqlConfigBackendTest, triplet) { EXPECT_EQ("pgsql", cbptr_->getType()); } |