rewrite basic pgsql output test in catch

This commit is contained in:
Sarah Hoffmann
2019-10-16 15:53:38 +02:00
parent f1b19d8f4c
commit aeb845a0cd
5 changed files with 125 additions and 291 deletions

View File

@ -95,6 +95,8 @@ public:
pg::conn_t connect() { return m_db.connect(); }
pg::tempdb_t const &db() const { return m_db; }
private:
pg::tempdb_t m_db;
};

View File

@ -95,6 +95,9 @@ public:
return boost::lexical_cast<T>(str);
}
void assert_double(double expected, std::string const &cmd) const
{ REQUIRE(Approx(expected) == require_scalar<double>(cmd)); }
result_t require_row(std::string const &cmd) const
{
result_t res = query(cmd);
@ -104,6 +107,23 @@ public:
return res;
}
unsigned long get_count(char const *table_name, std::string const &where = "") const
{
auto query = boost::format("select count(*) from %1% %2% %3%")
% table_name
% (where.empty() ? "" : "where")
% where;
return require_scalar<unsigned long>(query.str());
}
void require_has_table(char const *table_name) const
{
auto where = boost::format("oid = '%1%'::regclass") % table_name;
REQUIRE(get_count("pg_catalog.pg_class", where.str()) == 1);
}
private:
PGconn *m_conn = nullptr;
};
@ -134,7 +154,7 @@ public:
}
}
conn_t connect()
conn_t connect() const
{
return conn_t(conninfo().c_str());
}

View File

@ -18,6 +18,7 @@ struct slim_default : options_t
{
database_options = db.db_options();
prefix = "osm2pgsql_test";
style = "default.style";
slim = true;
cache = 1;
num_procs = 1;
@ -58,6 +59,18 @@ struct flat_nodes : options_t
flat_node_cache_enabled = true;
}
};
struct slim_flat_nodes : slim_default
{
slim_flat_nodes(pg::tempdb_t const &db)
: slim_default(db)
{
flat_node_file = boost::optional<std::string>(
"newtests/test_middle_flat.flat.nodes.bin");
flat_node_cache_enabled = true;
}
};
}
}

View File

@ -5,11 +5,94 @@
static testing::db::import_t db;
TEST_CASE("output_pgsql_t simple import")
static void require_tables(pg::conn_t const &conn)
{
SECTION("Regression simple")
{
REQUIRE_NOTHROW(db.run_file(testing::options::slim_default(db),
"liechtenstein-2013-08-03.osm.pbf"));
}
conn.require_has_table("osm2pgsql_test_point");
conn.require_has_table("osm2pgsql_test_line");
conn.require_has_table("osm2pgsql_test_polygon");
conn.require_has_table("osm2pgsql_test_roads");
}
TEST_CASE("liechtenstein slim regression simple")
{
REQUIRE_NOTHROW(db.run_file(testing::options::slim_default(db.db()),
"liechtenstein-2013-08-03.osm.pbf"));
auto conn = db.db().connect();
require_tables(conn);
REQUIRE(1342 == conn.get_count("osm2pgsql_test_point"));
REQUIRE(3231 == conn.get_count("osm2pgsql_test_line"));
REQUIRE(375 == conn.get_count("osm2pgsql_test_roads"));
REQUIRE(4130 == conn.get_count("osm2pgsql_test_polygon"));
// Check size of lines
conn.assert_double(1696.04, "SELECT ST_Length(way) FROM osm2pgsql_test_line WHERE osm_id = 1101");
conn.assert_double(1151.26, "SELECT ST_Length(ST_Transform(way,4326)::geography) FROM osm2pgsql_test_line WHERE osm_id = 1101");
conn.assert_double(311.289, "SELECT way_area FROM osm2pgsql_test_polygon WHERE osm_id = 3265");
conn.assert_double(311.289, "SELECT ST_Area(way) FROM osm2pgsql_test_polygon WHERE osm_id = 3265");
conn.assert_double(143.845, "SELECT ST_Area(ST_Transform(way,4326)::geography) FROM osm2pgsql_test_polygon WHERE osm_id = 3265");
// Check a point's location
REQUIRE(1 == conn.get_count("osm2pgsql_test_point",
"ST_DWithin(way, 'SRID=3857;POINT(1062645.12 5972593.4)'::geometry, 0.1)"));
}
TEST_CASE("liechtenstein slim latlon")
{
testing::options::slim_default options(db.db());
options.projection.reset(reprojection::create_projection(PROJ_LATLONG));
REQUIRE_NOTHROW(db.run_file(options, "liechtenstein-2013-08-03.osm.pbf"));
auto conn = db.db().connect();
require_tables(conn);
REQUIRE(1342 == conn.get_count("osm2pgsql_test_point"));
REQUIRE(3229 == conn.get_count("osm2pgsql_test_line"));
REQUIRE(374 == conn.get_count("osm2pgsql_test_roads"));
REQUIRE(4130 == conn.get_count("osm2pgsql_test_polygon"));
// Check size of lines
conn.assert_double(0.0105343, "SELECT ST_Length(way) FROM osm2pgsql_test_line WHERE osm_id = 1101");
conn.assert_double(1151.26, "SELECT ST_Length(ST_Transform(way,4326)::geography) FROM osm2pgsql_test_line WHERE osm_id = 1101");
conn.assert_double(1.70718e-08, "SELECT way_area FROM osm2pgsql_test_polygon WHERE osm_id = 3265");
conn.assert_double(1.70718e-08, "SELECT ST_Area(way) FROM osm2pgsql_test_polygon WHERE osm_id = 3265");
conn.assert_double(143.845, "SELECT ST_Area(ST_Transform(way,4326)::geography) FROM osm2pgsql_test_polygon WHERE osm_id = 3265");
// Check a point's location
REQUIRE(1 == conn.get_count("osm2pgsql_test_point",
"ST_DWithin(way, 'SRID=4326;POINT(9.5459035 47.1866494)'::geometry, 0.00001)"));
}
TEST_CASE("way area slim flatnode")
{
REQUIRE_NOTHROW(db.run_file(testing::options::slim_flat_nodes(db.db()),
"test_output_pgsql_way_area.osm"));
auto conn = db.db().connect();
require_tables(conn);
REQUIRE(0 == conn.get_count("osm2pgsql_test_point"));
REQUIRE(0 == conn.get_count("osm2pgsql_test_line"));
REQUIRE(0 == conn.get_count("osm2pgsql_test_roads"));
REQUIRE(1 == conn.get_count("osm2pgsql_test_polygon"));
}
TEST_CASE("route relation slim flatnode")
{
REQUIRE_NOTHROW(db.run_file(testing::options::slim_flat_nodes(db.db()),
"test_output_pgsql_route_rel.osm"));
auto conn = db.db().connect();
require_tables(conn);
REQUIRE(0 == conn.get_count("osm2pgsql_test_point"));
REQUIRE(2 == conn.get_count("osm2pgsql_test_line"));
REQUIRE(1 == conn.get_count("osm2pgsql_test_roads"));
REQUIRE(0 == conn.get_count("osm2pgsql_test_polygon"));
}

View File

@ -1,284 +0,0 @@
#include <iostream>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <cassert>
#include <sstream>
#include <stdexcept>
#include <memory>
#include "db-copy.hpp"
#include "middle-pgsql.hpp"
#include "middle-ram.hpp"
#include "options.hpp"
#include "osmdata.hpp"
#include "osmtypes.hpp"
#include "output-pgsql.hpp"
#include "taginfo_impl.hpp"
#include <sys/types.h>
#include <unistd.h>
#include <boost/lexical_cast.hpp>
#include "tests/common-pg.hpp"
#include "tests/common-cleanup.hpp"
#include "tests/common.hpp"
#define FLAT_NODES_FILE_NAME "tests/test_output_pgsql_area_way.flat.nodes.bin"
namespace {
struct skip_test : public std::exception {
const char *what() const noexcept { return "Test skipped."; }
};
void run_test(const char* test_name, void (*testfunc)()) {
try {
fprintf(stderr, "%s\n", test_name);
testfunc();
} catch (const skip_test &) {
exit(77); // <-- code to skip this test.
} catch (const std::exception& e) {
fprintf(stderr, "%s\n", e.what());
fprintf(stderr, "FAIL\n");
exit(EXIT_FAILURE);
}
fprintf(stderr, "PASS\n");
}
#define RUN_TEST(x) run_test(#x, &(x))
// "simple" test modeled on the basic regression test from
// the python script. this is just to check everything is
// working as expected before we start the complex stuff.
void test_regression_simple() {
std::unique_ptr<pg::tempdb> db;
try {
db.reset(new pg::tempdb);
} catch (const std::exception &e) {
std::cerr << "Unable to setup database: " << e.what() << "\n";
throw skip_test();
}
std::string proc_name("test-output-pgsql"), input_file("-");
char *argv[] = { &proc_name[0], &input_file[0], nullptr };
options_t options = options_t(2, argv);
options.database_options = db->database_options;
options.num_procs = 1;
options.prefix = "osm2pgsql_test";
options.slim = true;
options.style = "default.style";
testing::run_osm2pgsql(options, "tests/liechtenstein-2013-08-03.osm.pbf",
"pbf");
db->assert_has_table("osm2pgsql_test_point");
db->assert_has_table("osm2pgsql_test_line");
db->assert_has_table("osm2pgsql_test_polygon");
db->assert_has_table("osm2pgsql_test_roads");
db->check_count(1342, "SELECT count(*) FROM osm2pgsql_test_point");
db->check_count(3231, "SELECT count(*) FROM osm2pgsql_test_line");
db->check_count( 375, "SELECT count(*) FROM osm2pgsql_test_roads");
db->check_count(4130, "SELECT count(*) FROM osm2pgsql_test_polygon");
// Check size of lines
db->check_number(1696.04, "SELECT ST_Length(way) FROM osm2pgsql_test_line WHERE osm_id = 1101");
db->check_number(1151.26, "SELECT ST_Length(ST_Transform(way,4326)::geography) FROM osm2pgsql_test_line WHERE osm_id = 1101");
db->check_number(311.289, "SELECT way_area FROM osm2pgsql_test_polygon WHERE osm_id = 3265");
db->check_number(311.289, "SELECT ST_Area(way) FROM osm2pgsql_test_polygon WHERE osm_id = 3265");
db->check_number(143.845, "SELECT ST_Area(ST_Transform(way,4326)::geography) FROM osm2pgsql_test_polygon WHERE osm_id = 3265");
// Check a point's location
db->check_count(1, "SELECT count(*) FROM osm2pgsql_test_point WHERE ST_DWithin(way, 'SRID=3857;POINT(1062645.12 5972593.4)'::geometry, 0.1)");
}
void test_latlong() {
std::unique_ptr<pg::tempdb> db;
try {
db.reset(new pg::tempdb);
} catch (const std::exception &e) {
std::cerr << "Unable to setup database: " << e.what() << "\n";
throw skip_test();
}
std::string proc_name("test-output-pgsql"), input_file("-");
char *argv[] = { &proc_name[0], &input_file[0], nullptr };
options_t options = options_t(2, argv);
options.database_options = db->database_options;
options.num_procs = 1;
options.prefix = "osm2pgsql_test";
options.slim = true;
options.style = "default.style";
options.projection.reset(reprojection::create_projection(PROJ_LATLONG));
testing::run_osm2pgsql(options, "tests/liechtenstein-2013-08-03.osm.pbf",
"pbf");
db->assert_has_table("osm2pgsql_test_point");
db->assert_has_table("osm2pgsql_test_line");
db->assert_has_table("osm2pgsql_test_polygon");
db->assert_has_table("osm2pgsql_test_roads");
db->check_count(1342, "SELECT count(*) FROM osm2pgsql_test_point");
db->check_count(3229, "SELECT count(*) FROM osm2pgsql_test_line");
db->check_count(374, "SELECT count(*) FROM osm2pgsql_test_roads");
db->check_count(4130, "SELECT count(*) FROM osm2pgsql_test_polygon");
// Check size of lines
db->check_number(0.0105343, "SELECT ST_Length(way) FROM osm2pgsql_test_line WHERE osm_id = 1101");
db->check_number(1151.26, "SELECT ST_Length(ST_Transform(way,4326)::geography) FROM osm2pgsql_test_line WHERE osm_id = 1101");
db->check_number(1.70718e-08, "SELECT way_area FROM osm2pgsql_test_polygon WHERE osm_id = 3265");
db->check_number(1.70718e-08, "SELECT ST_Area(way) FROM osm2pgsql_test_polygon WHERE osm_id = 3265");
db->check_number(143.845, "SELECT ST_Area(ST_Transform(way,4326)::geography) FROM osm2pgsql_test_polygon WHERE osm_id = 3265");
// Check a point's location
db->check_count(1, "SELECT count(*) FROM osm2pgsql_test_point WHERE ST_DWithin(way, 'SRID=4326;POINT(9.5459035 47.1866494)'::geometry, 0.00001)");
}
void test_area_way_simple() {
std::unique_ptr<pg::tempdb> db;
try {
db.reset(new pg::tempdb);
} catch (const std::exception &e) {
std::cerr << "Unable to setup database: " << e.what() << "\n";
throw skip_test();
}
std::string proc_name("test-output-pgsql"), input_file("-");
char *argv[] = { &proc_name[0], &input_file[0], nullptr };
options_t options = options_t(2, argv);
options.database_options = db->database_options;
options.num_procs = 1;
options.prefix = "osm2pgsql_test";
options.slim = true;
options.style = "default.style";
options.flat_node_cache_enabled = true;
options.flat_node_file = boost::optional<std::string>(FLAT_NODES_FILE_NAME);
testing::run_osm2pgsql(options, "tests/test_output_pgsql_way_area.osm",
"xml");
db->assert_has_table("osm2pgsql_test_point");
db->assert_has_table("osm2pgsql_test_line");
db->assert_has_table("osm2pgsql_test_polygon");
db->assert_has_table("osm2pgsql_test_roads");
db->check_count(0, "SELECT count(*) FROM osm2pgsql_test_point");
db->check_count(0, "SELECT count(*) FROM osm2pgsql_test_line");
db->check_count(0, "SELECT count(*) FROM osm2pgsql_test_roads");
db->check_count(1, "SELECT count(*) FROM osm2pgsql_test_polygon");
}
void test_route_rel() {
std::unique_ptr<pg::tempdb> db;
try {
db.reset(new pg::tempdb);
} catch (const std::exception &e) {
std::cerr << "Unable to setup database: " << e.what() << "\n";
throw skip_test();
}
std::string proc_name("test-output-pgsql"), input_file("-");
char *argv[] = { &proc_name[0], &input_file[0], nullptr };
options_t options = options_t(2, argv);
options.database_options = db->database_options;
options.num_procs = 1;
options.prefix = "osm2pgsql_test";
options.slim = false;
options.style = "default.style";
testing::run_osm2pgsql(options, "tests/test_output_pgsql_route_rel.osm",
"xml");
db->assert_has_table("osm2pgsql_test_point");
db->assert_has_table("osm2pgsql_test_line");
db->assert_has_table("osm2pgsql_test_polygon");
db->assert_has_table("osm2pgsql_test_roads");
db->check_count(0, "SELECT count(*) FROM osm2pgsql_test_point");
db->check_count(2, "SELECT count(*) FROM osm2pgsql_test_line");
db->check_count(1, "SELECT count(*) FROM osm2pgsql_test_roads");
db->check_count(0, "SELECT count(*) FROM osm2pgsql_test_polygon");
}
// test the same, but clone the output. it should
// behave the same as the original.
void test_clone() {
std::unique_ptr<pg::tempdb> db;
try {
db.reset(new pg::tempdb);
} catch (const std::exception &e) {
std::cerr << "Unable to setup database: " << e.what() << "\n";
throw skip_test();
}
std::string proc_name("test-output-pgsql"), input_file("-");
char *argv[] = { &proc_name[0], &input_file[0], nullptr };
options_t options = options_t(2, argv);
options.database_options = db->database_options;
options.num_procs = 1;
options.prefix = "osm2pgsql_test";
options.slim = true;
options.style = "default.style";
auto mid_pgsql = std::make_shared<middle_pgsql_t>(&options);
mid_pgsql->start();
auto ct =
std::make_shared<db_copy_thread_t>(db->database_options.conninfo());
output_pgsql_t out_test(mid_pgsql->get_query_instance(mid_pgsql), options,
ct);
std::shared_ptr<output_t> out_clone =
out_test.clone(mid_pgsql->get_query_instance(mid_pgsql), ct);
osmdata_t osmdata(std::static_pointer_cast<middle_t>(mid_pgsql), out_clone);
testing::parse("tests/liechtenstein-2013-08-03.osm.pbf", "pbf",
options, &osmdata);
db->assert_has_table("osm2pgsql_test_point");
db->assert_has_table("osm2pgsql_test_line");
db->assert_has_table("osm2pgsql_test_polygon");
db->assert_has_table("osm2pgsql_test_roads");
db->check_count(1342, "SELECT count(*) FROM osm2pgsql_test_point");
db->check_count(3231, "SELECT count(*) FROM osm2pgsql_test_line");
db->check_count( 375, "SELECT count(*) FROM osm2pgsql_test_roads");
db->check_count(4130, "SELECT count(*) FROM osm2pgsql_test_polygon");
}
} // anonymous namespace
int main(int argc, char *argv[]) {
(void)argc;
(void)argv;
// remove flat nodes file on exit - it's 20GB and bad manners to
// leave that lying around on the filesystem.
cleanup::file flat_nodes_file(FLAT_NODES_FILE_NAME);
RUN_TEST(test_regression_simple);
RUN_TEST(test_latlong);
RUN_TEST(test_clone);
RUN_TEST(test_area_way_simple);
RUN_TEST(test_route_rel);
return 0;
}