1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156
|
#ifndef OSM2PGSQL_TESTS_COMMON_PG_HPP
#define OSM2PGSQL_TESTS_COMMON_PG_HPP
/**
* SPDX-License-Identifier: GPL-2.0-or-later
*
* This file is part of osm2pgsql (https://osm2pgsql.org/).
*
* Copyright (C) 2006-2021 by the osm2pgsql developer community.
* For a full list of authors see the git log.
*/
#include <cstdio>
#include <cstdlib>
#include <stdexcept>
#include <string>
#include <boost/lexical_cast.hpp>
#include "format.hpp"
#include "options.hpp"
#include "pgsql.hpp"
#include <catch.hpp>
#ifdef _MSC_VER
#include <process.h>
#include <windows.h>
#define getpid _getpid
#endif
namespace testing {
/// Helper classes for postgres connections
namespace pg {
class conn_t : public pg_conn_t
{
public:
conn_t(std::string const &conninfo) : pg_conn_t(conninfo) {}
template <typename T>
T require_scalar(std::string const &cmd) const
{
pg_result_t const res = query(PGRES_TUPLES_OK, cmd);
REQUIRE(res.num_tuples() == 1);
auto const str = res.get_value_as_string(0, 0);
return boost::lexical_cast<T>(str);
}
void assert_double(double expected, std::string const &cmd) const
{
REQUIRE(Approx(expected).epsilon(0.01) == require_scalar<double>(cmd));
}
void assert_null(std::string const &cmd) const
{
pg_result_t const res = query(PGRES_TUPLES_OK, cmd);
REQUIRE(res.num_tuples() == 1);
REQUIRE(res.is_null(0, 0));
}
pg_result_t require_row(std::string const &cmd) const
{
pg_result_t res = query(PGRES_TUPLES_OK, cmd);
REQUIRE(res.num_tuples() == 1);
return res;
}
unsigned long get_count(char const *table_name,
std::string const &where = "") const
{
auto const query = "SELECT count(*) FROM {} {} {}"_format(
table_name, (where.empty() ? "" : "WHERE"), where);
return require_scalar<unsigned long>(query);
}
void require_has_table(char const *table_name) const
{
auto const where = "oid = '{}'::regclass"_format(table_name);
REQUIRE(get_count("pg_catalog.pg_class", where) == 1);
}
};
class tempdb_t
{
public:
tempdb_t() noexcept
{
try {
conn_t conn{"dbname=postgres"};
m_db_name = "osm2pgsql-test-{}-{}"_format(getpid(), time(nullptr));
conn.exec("DROP DATABASE IF EXISTS \"{}\""_format(m_db_name));
conn.exec("CREATE DATABASE \"{}\" WITH ENCODING 'UTF8'"_format(
m_db_name));
conn_t local = connect();
local.exec("CREATE EXTENSION postgis");
local.exec("CREATE EXTENSION hstore");
} catch (std::runtime_error const &e) {
fmt::print(stderr,
"Test database cannot be created: {}\n"
"Did you mean to run 'pg_virtualenv ctest'?\n",
e.what());
std::exit(1);
}
}
tempdb_t(tempdb_t const &) = delete;
tempdb_t &operator=(tempdb_t const &) = delete;
tempdb_t(tempdb_t &&) = delete;
tempdb_t &operator=(tempdb_t const &&) = delete;
~tempdb_t() noexcept
{
if (!m_db_name.empty()) {
// Disable removal of the test database by setting the environment
// variable OSM2PGSQL_KEEP_TEST_DB to anything. This can be useful
// when debugging tests.
char const *const keep_db = std::getenv("OSM2PGSQL_KEEP_TEST_DB");
if (keep_db != nullptr) {
return;
}
try {
conn_t conn{"dbname=postgres"};
conn.exec("DROP DATABASE IF EXISTS \"{}\""_format(m_db_name));
} catch (...) {
fprintf(stderr, "DROP DATABASE failed. Ignored.\n");
}
}
}
conn_t connect() const { return conn_t{conninfo()}; }
std::string conninfo() const { return "dbname=" + m_db_name; }
database_options_t db_options() const
{
database_options_t opt;
opt.db = m_db_name;
return opt;
}
private:
std::string m_db_name;
};
} // namespace pg
} // namespace testing
#endif // OSM2PGSQL_TESTS_COMMON_PG_HPP
|