diff --git a/.gitignore b/.gitignore index daa64ac35..3c34b037a 100644 --- a/.gitignore +++ b/.gitignore @@ -57,6 +57,7 @@ tests/test-output-multi-poly-trivial tests/test-output-pgsql tests/test-output-pgsql-tablespace tests/test-output-pgsql-z_order +tests/test-output-pgsql-schema tests/test-expire-tiles tests/test-hstore-match-only tests/*.log diff --git a/Makefile.am b/Makefile.am index fb3fa054f..e5eb4071c 100644 --- a/Makefile.am +++ b/Makefile.am @@ -58,6 +58,7 @@ check_PROGRAMS = \ tests/test-output-pgsql \ tests/test-output-pgsql-z_order \ tests/test-output-pgsql-tablespace \ + tests/test-output-pgsql-schema \ tests/test-pgsql-escape \ tests/test-parse-options \ tests/test-expire-tiles \ @@ -93,6 +94,8 @@ tests_test_output_pgsql_tablespace_SOURCES = tests/test-output-pgsql-tablespace. tests_test_output_pgsql_tablespace_LDADD = libosm2pgsql.la tests_test_output_pgsql_z_order_SOURCES = tests/test-output-pgsql-z_order.cpp tests/common-pg.cpp tests_test_output_pgsql_z_order_LDADD = libosm2pgsql.la +tests_test_output_pgsql_schema_SOURCES = tests/test-output-pgsql-schema.cpp tests/common-pg.cpp +tests_test_output_pgsql_schema_LDADD = libosm2pgsql.la tests_test_pgsql_escape_SOURCES = tests/test-pgsql-escape.cpp tests_test_pgsql_escape_LDADD = libosm2pgsql.la tests_test_parse_options_SOURCES = tests/test-parse-options.cpp @@ -152,6 +155,7 @@ tests_test_output_multi_poly_trivial_LDADD += $(GLOBAL_LDFLAGS) tests_test_output_pgsql_LDADD += $(GLOBAL_LDFLAGS) tests_test_output_pgsql_tablespace_LDADD += $(GLOBAL_LDFLAGS) tests_test_output_pgsql_z_order_LDADD += $(GLOBAL_LDFLAGS) +tests_test_output_pgsql_schema_LDADD += $(GLOBAL_LDFLAGS) tests_test_pgsql_escape_LDADD += $(GLOBAL_LDFLAGS) tests_test_parse_options_LDADD += $(GLOBAL_LDFLAGS) tests_test_expire_tiles_LDADD += $(GLOBAL_LDFLAGS) diff --git a/tests/common-pg.cpp b/tests/common-pg.cpp index 3a7adb3ea..ad2b6b2e4 100644 --- a/tests/common-pg.cpp +++ b/tests/common-pg.cpp @@ -9,6 +9,7 @@ #include #include #include +#include #ifdef _MSC_VER #include @@ -76,10 +77,11 @@ result::~result() { } tempdb::tempdb() - : m_conn(conn::connect("dbname=postgres")) { + : m_postgres_conn(conn::connect("dbname=postgres")) +{ result_ptr res = nullptr; database_options.db = (boost::format("osm2pgsql-test-%1%-%2%") % getpid() % time(nullptr)).str(); - m_conn->exec(boost::format("DROP DATABASE IF EXISTS \"%1%\"") % database_options.db); + m_postgres_conn->exec(boost::format("DROP DATABASE IF EXISTS \"%1%\"") % database_options.db); //tests can be run concurrently which means that this query can collide with other similar ones //so we implement a simple retry here to get around the case that they do collide if we dont //we often fail due to both trying to access template1 at the same time @@ -88,7 +90,7 @@ tempdb::tempdb() while(status != PGRES_COMMAND_OK && retries++ < 20) { sleep(1); - res = m_conn->exec(boost::format("CREATE DATABASE \"%1%\" WITH ENCODING 'UTF8'") % database_options.db); + res = m_postgres_conn->exec(boost::format("CREATE DATABASE \"%1%\" WITH ENCODING 'UTF8'") % database_options.db); status = PQresultStatus(res->get()); } if (PQresultStatus(res->get()) != PGRES_COMMAND_OK) { @@ -96,10 +98,10 @@ tempdb::tempdb() % PQresultErrorMessage(res->get())).str()); } - conn_ptr db = conn::connect(database_options.conninfo()); + m_conn = conn::connect(database_options); - setup_extension(db, "postgis", {"postgis-1.5/postgis.sql", "postgis-1.5/spatial_ref_sys.sql"}); - setup_extension(db, "hstore"); + setup_extension("postgis", {"postgis-1.5/postgis.sql", "postgis-1.5/spatial_ref_sys.sql"}); + setup_extension("hstore"); } void tempdb::check_tblspc() { @@ -120,23 +122,98 @@ void tempdb::check_tblspc() { } +void tempdb::check_count(int expected, const std::string &query) { + pg::result_ptr res = m_conn->exec(query); + if (PQresultStatus(res->get()) != PGRES_TUPLES_OK) { + throw std::runtime_error((boost::format("Expected PGRES_TUPLES_OK but " + "got %1%. %2% Query was %3%.") + % PQresStatus(PQresultStatus(res->get())) + % PQresultErrorMessage(res->get()) + % query).str()); + } + int ntuples = PQntuples(res->get()); + if (ntuples != 1) { + throw std::runtime_error((boost::format("Expected one tuple from a query to check " + "COUNT(*), but got %1%. Query was: %2%.") + % ntuples % query).str()); + } + + std::string numstr = PQgetvalue(res->get(), 0, 0); + int count = boost::lexical_cast(numstr); + + if (count != expected) { + throw std::runtime_error((boost::format("Expected %1%, but got %2%, when running " + "query: %3%.") + % expected % numstr % query).str()); + } +} + +void tempdb::check_number(double expected, const std::string &query) { + pg::result_ptr res = m_conn->exec(query); + + int ntuples = PQntuples(res->get()); + if (ntuples != 1) { + throw std::runtime_error((boost::format("Expected only one tuple from a query, " + " but got %1%. Query was: %2%.") + % ntuples % query).str()); + } + + std::string numstr = PQgetvalue(res->get(), 0, 0); + double num = boost::lexical_cast(numstr); + + // floating point isn't exact, so allow a 0.01% difference + if ((num > 1.0001*expected) || (num < 0.9999*expected)) { + throw std::runtime_error((boost::format("Expected %1%, but got %2%, when running " + "query: %3%.") + % expected % num % query).str()); + } +} + +void tempdb::check_string(const std::string &expected, const std::string &query) { + pg::result_ptr res = m_conn->exec(query); + + int ntuples = PQntuples(res->get()); + if (ntuples != 1) { + throw std::runtime_error((boost::format("Expected only one tuple from a query, " + " but got %1%. Query was: %2%.") + % ntuples % query).str()); + } + + std::string actual = PQgetvalue(res->get(), 0, 0); + + if (actual != expected) { + throw std::runtime_error((boost::format("Expected %1%, but got %2%, when running " + "query: %3%.") + % expected % actual % query).str()); + } +} + +void tempdb::assert_has_table(const std::string &table_name) { + std::string query = (boost::format("select count(*) from pg_catalog.pg_class " + "where oid = '%1%'::regclass") + % table_name).str(); + + check_count(1, query); +} + tempdb::~tempdb() { - if (m_conn) { - m_conn->exec(boost::format("DROP DATABASE IF EXISTS \"%1%\"") % database_options.db); + m_conn.reset(); // close the connection to the db + if (m_postgres_conn) { + m_postgres_conn->exec(boost::format("DROP DATABASE IF EXISTS \"%1%\"") % database_options.db); } } -void tempdb::setup_extension(conn_ptr db, const std::string &extension, +void tempdb::setup_extension(const std::string &extension, const std::vector &extension_files) { // first, try the new way of setting up extensions - result_ptr res = db->exec(boost::format("CREATE EXTENSION %1%") % extension); + result_ptr res = m_conn->exec(boost::format("CREATE EXTENSION %1%") % extension); if (PQresultStatus(res->get()) != PGRES_COMMAND_OK) { if (extension_files.size() == 0) { throw std::runtime_error((boost::format("Unable to load extension %1% and no files specified") % extension).str()); } // if that fails, then fall back to trying to find the files on // the filesystem to load to create the extension. - res = db->exec("select regexp_replace(split_part(version(),' ',2),'\\.[0-9]*$','');"); + res = m_conn->exec("select regexp_replace(split_part(version(),' ',2),'\\.[0-9]*$','');"); if ((PQresultStatus(res->get()) != PGRES_TUPLES_OK) || (PQntuples(res->get()) != 1)) { @@ -162,7 +239,7 @@ void tempdb::setup_extension(conn_ptr db, const std::string &extension, "load extension \"%2%\".") % sql_file % extension).str()); } - res = db->exec(sql); + res = m_conn->exec(sql); if (PQresultStatus(res->get()) != PGRES_COMMAND_OK) { throw std::runtime_error((boost::format("Could not load extension \"%1%\": %2%") % extension diff --git a/tests/common-pg.hpp b/tests/common-pg.hpp index 67bd353f5..32f727a3e 100644 --- a/tests/common-pg.hpp +++ b/tests/common-pg.hpp @@ -59,12 +59,50 @@ struct tempdb database_options_t database_options; void check_tblspc(); + /** + * Checks the result of a query with COUNT(*). + * It will work with any integer-returning query. + * \param[in] expected expected result + * \param[in] query SQL query to run. Must return one tuple. + * \throws std::runtime_error if query result is not expected + */ + void check_count(int expected, const std::string &query); + + /** + * Checks a floating point number. + * It allows a small variance around the expected result to allow for + * floating point differences. + * The query must only return one tuple + * \param[in] expected expected result + * \param[in] query SQL query to run. Must return one tuple. + * \throws std::runtime_error if query result is not expected + */ + void check_number(double expected, const std::string &query); + + /** + * Check the string a query returns. + * \param[in] expected expected result + * \param[in] query SQL query to run. Must return one tuple. + * \throws std::runtime_error if query result is not expected + */ + void check_string(const std::string &expected, const std::string &query); + /** + * Assert that the database has a certain table_name + * \param[in] table_name Name of the table to check, optionally schema-qualified + * \throws std::runtime_error if missing the table + */ + void assert_has_table(const std::string &table_name); private: - // Sets up an extension, trying first with 9.1 CREATE EXTENSION, and falling back to trying to find extension_files - void setup_extension(conn_ptr db, const std::string &extension, const std::vector &extension_files = std::vector()); - - conn_ptr m_conn; + /** + * Sets up an extension, trying first with 9.1 CREATE EXTENSION, and falling + * back to trying to find extension_files. The fallback is not likely to + * work on systems not based on Debian. + */ + void setup_extension(const std::string &extension, const std::vector &extension_files = std::vector()); + + conn_ptr m_conn; ///< connection to the test DB + conn_ptr m_postgres_conn; ///< Connection to the "postgres" db, used to create and drop test DBs }; } // namespace pg diff --git a/tests/test-hstore-match-only.cpp b/tests/test-hstore-match-only.cpp index acd37d74d..351a77ab6 100644 --- a/tests/test-hstore-match-only.cpp +++ b/tests/test-hstore-match-only.cpp @@ -24,7 +24,6 @@ The tags of inteest are specified in hstore-match-only.style #include "taginfo_impl.hpp" #include "parse.hpp" -#include #include #include @@ -32,28 +31,6 @@ The tags of inteest are specified in hstore-match-only.style #include "tests/common-pg.hpp" - -void check_count(pg::conn_ptr &conn, int expected, const std::string &query) { - pg::result_ptr res = conn->exec(query); - - int ntuples = PQntuples(res->get()); - if (ntuples != 1) { - throw std::runtime_error((boost::format("Expected only one tuple from a query " - "to check COUNT(*), but got %1%. Query " - "was: %2%.") - % ntuples % query).str()); - } - - std::string numstr = PQgetvalue(res->get(), 0, 0); - int count = boost::lexical_cast(numstr); - - if (count != expected) { - throw std::runtime_error((boost::format("Expected %1%, but got %2%, when running " - "query: %3%.") - % expected % count % query).str()); - } -} - int main(int argc, char *argv[]) { std::unique_ptr db; @@ -89,14 +66,11 @@ int main(int argc, char *argv[]) { osmdata.stop(); - // start a new connection to run tests on - pg::conn_ptr test_conn = pg::conn::connect(db->database_options); - // tables should not contain any tag columns - check_count(test_conn, 4, "select count(column_name) from information_schema.columns where table_name='osm2pgsql_test_point'"); - check_count(test_conn, 5, "select count(column_name) from information_schema.columns where table_name='osm2pgsql_test_polygon'"); - check_count(test_conn, 5, "select count(column_name) from information_schema.columns where table_name='osm2pgsql_test_line'"); - check_count(test_conn, 5, "select count(column_name) from information_schema.columns where table_name='osm2pgsql_test_roads'"); + db->check_count(4, "select count(column_name) from information_schema.columns where table_name='osm2pgsql_test_point'"); + db->check_count(5, "select count(column_name) from information_schema.columns where table_name='osm2pgsql_test_polygon'"); + db->check_count(5, "select count(column_name) from information_schema.columns where table_name='osm2pgsql_test_line'"); + db->check_count(5, "select count(column_name) from information_schema.columns where table_name='osm2pgsql_test_roads'"); // the testfile contains 19 tagged ways and 7 tagged nodes // out of them 18 ways and 6 nodes are interesting as specified by hstore-match-only.style @@ -106,10 +80,10 @@ int main(int argc, char *argv[]) { // 12 objects in osm2pgsql_test_line // 3 objects in osm2pgsql_test_roads - check_count(test_conn, 6, "select count(*) from osm2pgsql_test_point"); - check_count(test_conn, 7, "select count(*) from osm2pgsql_test_polygon"); - check_count(test_conn, 12, "select count(*) from osm2pgsql_test_line"); - check_count(test_conn, 3, "select count(*) from osm2pgsql_test_roads"); + db->check_count(6, "select count(*) from osm2pgsql_test_point"); + db->check_count(7, "select count(*) from osm2pgsql_test_polygon"); + db->check_count(12, "select count(*) from osm2pgsql_test_line"); + db->check_count(3, "select count(*) from osm2pgsql_test_roads"); return 0; diff --git a/tests/test-middle-flat.cpp b/tests/test-middle-flat.cpp index a0cbca81e..eda362aeb 100644 --- a/tests/test-middle-flat.cpp +++ b/tests/test-middle-flat.cpp @@ -12,7 +12,6 @@ #include "options.hpp" #include "middle-pgsql.hpp" -#include #include #include diff --git a/tests/test-middle-pgsql.cpp b/tests/test-middle-pgsql.cpp index 30ab0ea07..a22af1a44 100644 --- a/tests/test-middle-pgsql.cpp +++ b/tests/test-middle-pgsql.cpp @@ -12,7 +12,6 @@ #include "options.hpp" #include "middle-pgsql.hpp" -#include #include #include diff --git a/tests/test-output-multi-line-storage.cpp b/tests/test-output-multi-line-storage.cpp index 6bdfdf7d9..a53f0bd8a 100644 --- a/tests/test-output-multi-line-storage.cpp +++ b/tests/test-output-multi-line-storage.cpp @@ -15,7 +15,6 @@ #include "taginfo_impl.hpp" #include "parse.hpp" -#include #include #include @@ -24,27 +23,6 @@ #include "tests/middle-tests.hpp" #include "tests/common-pg.hpp" -void check_count(pg::conn_ptr &conn, int expected, const std::string &query) { - pg::result_ptr res = conn->exec(query); - - int ntuples = PQntuples(res->get()); - if (ntuples != 1) { - throw std::runtime_error((boost::format("Expected only one tuple from a query " - "to check COUNT(*), but got %1%. Query " - "was: %2%.") - % ntuples % query).str()); - } - - std::string numstr = PQgetvalue(res->get(), 0, 0); - int count = boost::lexical_cast(numstr); - - if (count != expected) { - throw std::runtime_error((boost::format("Expected %1%, but got %2%, when running " - "query: %3%.") - % expected % count % query).str()); - } -} - int main(int argc, char *argv[]) { std::unique_ptr db; @@ -84,18 +62,15 @@ int main(int argc, char *argv[]) { osmdata.stop(); - // start a new connection to run tests on - pg::conn_ptr test_conn = pg::conn::connect(db->database_options); - - check_count(test_conn, 1, "select count(*) from pg_catalog.pg_class where relname = 'test_line'"); - check_count(test_conn, 3, "select count(*) from test_line"); + db->check_count(1, "select count(*) from pg_catalog.pg_class where relname = 'test_line'"); + db->check_count(3, "select count(*) from test_line"); //check that we have the number of vertexes in each linestring - check_count(test_conn, 3, "SELECT ST_NumPoints(way) FROM test_line WHERE osm_id = 1"); - check_count(test_conn, 2, "SELECT ST_NumPoints(way) FROM test_line WHERE osm_id = 2"); - check_count(test_conn, 2, "SELECT ST_NumPoints(way) FROM test_line WHERE osm_id = 3"); + db->check_count(3, "SELECT ST_NumPoints(way) FROM test_line WHERE osm_id = 1"); + db->check_count(2, "SELECT ST_NumPoints(way) FROM test_line WHERE osm_id = 2"); + db->check_count(2, "SELECT ST_NumPoints(way) FROM test_line WHERE osm_id = 3"); - check_count(test_conn, 3, "SELECT COUNT(*) FROM test_line WHERE foo = 'bar'"); + db->check_count(3, "SELECT COUNT(*) FROM test_line WHERE foo = 'bar'"); return 0; } catch (const std::exception &e) { diff --git a/tests/test-output-multi-line.cpp b/tests/test-output-multi-line.cpp index 486bde0eb..71938e45b 100644 --- a/tests/test-output-multi-line.cpp +++ b/tests/test-output-multi-line.cpp @@ -15,7 +15,7 @@ #include "taginfo_impl.hpp" #include "parse.hpp" -#include + #include #include @@ -24,27 +24,6 @@ #include "tests/middle-tests.hpp" #include "tests/common-pg.hpp" -void check_count(pg::conn_ptr &conn, int expected, const std::string &query) { - pg::result_ptr res = conn->exec(query); - - int ntuples = PQntuples(res->get()); - if (ntuples != 1) { - throw std::runtime_error((boost::format("Expected only one tuple from a query " - "to check COUNT(*), but got %1%. Query " - "was: %2%.") - % ntuples % query).str()); - } - - std::string numstr = PQgetvalue(res->get(), 0, 0); - int count = boost::lexical_cast(numstr); - - if (count != expected) { - throw std::runtime_error((boost::format("Expected %1%, but got %2%, when running " - "query: %3%.") - % expected % count % query).str()); - } -} - int main(int argc, char *argv[]) { std::unique_ptr db; @@ -83,29 +62,27 @@ int main(int argc, char *argv[]) { osmdata.stop(); // start a new connection to run tests on - pg::conn_ptr test_conn = pg::conn::connect(db->database_options); - - check_count(test_conn, 1, "select count(*) from pg_catalog.pg_class where relname = 'foobar_highways'"); - check_count(test_conn, 2753, "select count(*) from foobar_highways"); + db->check_count(1, "select count(*) from pg_catalog.pg_class where relname = 'foobar_highways'"); + db->check_count(2753, "select count(*) from foobar_highways"); //check that we have the right spread - check_count(test_conn, 13, "select count(*) from foobar_highways where highway='bridleway'"); - check_count(test_conn, 3, "select count(*) from foobar_highways where highway='construction'"); - check_count(test_conn, 96, "select count(*) from foobar_highways where highway='cycleway'"); - check_count(test_conn, 249, "select count(*) from foobar_highways where highway='footway'"); - check_count(test_conn, 18, "select count(*) from foobar_highways where highway='living_street'"); - check_count(test_conn, 171, "select count(*) from foobar_highways where highway='path'"); - check_count(test_conn, 6, "select count(*) from foobar_highways where highway='pedestrian'"); - check_count(test_conn, 81, "select count(*) from foobar_highways where highway='primary'"); - check_count(test_conn, 842, "select count(*) from foobar_highways where highway='residential'"); - check_count(test_conn, 3, "select count(*) from foobar_highways where highway='road'"); - check_count(test_conn, 90, "select count(*) from foobar_highways where highway='secondary'"); - check_count(test_conn, 1, "select count(*) from foobar_highways where highway='secondary_link'"); - check_count(test_conn, 352, "select count(*) from foobar_highways where highway='service'"); - check_count(test_conn, 34, "select count(*) from foobar_highways where highway='steps'"); - check_count(test_conn, 33, "select count(*) from foobar_highways where highway='tertiary'"); - check_count(test_conn, 597, "select count(*) from foobar_highways where highway='track'"); - check_count(test_conn, 164, "select count(*) from foobar_highways where highway='unclassified'"); + db->check_count(13, "select count(*) from foobar_highways where highway='bridleway'"); + db->check_count(3, "select count(*) from foobar_highways where highway='construction'"); + db->check_count(96, "select count(*) from foobar_highways where highway='cycleway'"); + db->check_count(249, "select count(*) from foobar_highways where highway='footway'"); + db->check_count(18, "select count(*) from foobar_highways where highway='living_street'"); + db->check_count(171, "select count(*) from foobar_highways where highway='path'"); + db->check_count(6, "select count(*) from foobar_highways where highway='pedestrian'"); + db->check_count(81, "select count(*) from foobar_highways where highway='primary'"); + db->check_count(842, "select count(*) from foobar_highways where highway='residential'"); + db->check_count(3, "select count(*) from foobar_highways where highway='road'"); + db->check_count(90, "select count(*) from foobar_highways where highway='secondary'"); + db->check_count(1, "select count(*) from foobar_highways where highway='secondary_link'"); + db->check_count(352, "select count(*) from foobar_highways where highway='service'"); + db->check_count(34, "select count(*) from foobar_highways where highway='steps'"); + db->check_count(33, "select count(*) from foobar_highways where highway='tertiary'"); + db->check_count(597, "select count(*) from foobar_highways where highway='track'"); + db->check_count(164, "select count(*) from foobar_highways where highway='unclassified'"); return 0; } catch (const std::exception &e) { diff --git a/tests/test-output-multi-point-multi-table.cpp b/tests/test-output-multi-point-multi-table.cpp index 22236a1ad..de90a37d4 100644 --- a/tests/test-output-multi-point-multi-table.cpp +++ b/tests/test-output-multi-point-multi-table.cpp @@ -15,7 +15,6 @@ #include "taginfo_impl.hpp" #include "parse.hpp" -#include #include #include @@ -24,32 +23,6 @@ #include "tests/middle-tests.hpp" #include "tests/common-pg.hpp" -void check_count(pg::conn_ptr &conn, int expected, const std::string &query) { - pg::result_ptr res = conn->exec(query); - - if (PQresultStatus(res->get()) != PGRES_TUPLES_OK) { - throw std::runtime_error((boost::format("Query ERROR running %1%: %2%") - % query % PQresultErrorMessage(res->get())).str()); - } - - int ntuples = PQntuples(res->get()); - if (ntuples != 1) { - throw std::runtime_error((boost::format("Expected only one tuple from a query " - "to check COUNT(*), but got %1%. Query " - "was: %2%.") - % ntuples % query).str()); - } - - std::string numstr = PQgetvalue(res->get(), 0, 0); - int count = boost::lexical_cast(numstr); - - if (count != expected) { - throw std::runtime_error((boost::format("Expected %1%, but got %2%, when running " - "query: %3%.") - % expected % count % query).str()); - } -} - int main(int argc, char *argv[]) { std::unique_ptr db; @@ -97,32 +70,29 @@ int main(int argc, char *argv[]) { osmdata.stop(); - // start a new connection to run tests on - pg::conn_ptr test_conn = pg::conn::connect(db->database_options); - for (int i = 0; i < 10; ++i) { std::string name = (boost::format("foobar_%d") % i).str(); - check_count(test_conn, 1, + db->check_count(1, (boost::format("select count(*) from pg_catalog.pg_class " "where relname = 'foobar_%d'") % i).str()); - check_count(test_conn, 244, + db->check_count(244, (boost::format("select count(*) from foobar_%d") % i).str()); - check_count(test_conn, 36, + db->check_count(36, (boost::format("select count(*) from foobar_%d " "where amenity='parking'") % i).str()); - check_count(test_conn, 34, + db->check_count(34, (boost::format("select count(*) from foobar_%d " "where amenity='bench'") % i).str()); - check_count(test_conn, 1, + db->check_count(1, (boost::format("select count(*) from foobar_%d " "where amenity='vending_machine'") % i).str()); diff --git a/tests/test-output-multi-point.cpp b/tests/test-output-multi-point.cpp index 4e064f4cc..7f3ff05ea 100644 --- a/tests/test-output-multi-point.cpp +++ b/tests/test-output-multi-point.cpp @@ -15,7 +15,6 @@ #include "taginfo_impl.hpp" #include "parse.hpp" -#include #include #include @@ -24,27 +23,6 @@ #include "tests/middle-tests.hpp" #include "tests/common-pg.hpp" -void check_count(pg::conn_ptr &conn, int expected, const std::string &query) { - pg::result_ptr res = conn->exec(query); - - int ntuples = PQntuples(res->get()); - if (ntuples != 1) { - throw std::runtime_error((boost::format("Expected only one tuple from a query " - "to check COUNT(*), but got %1%. Query " - "was: %2%.") - % ntuples % query).str()); - } - - std::string numstr = PQgetvalue(res->get(), 0, 0); - int count = boost::lexical_cast(numstr); - - if (count != expected) { - throw std::runtime_error((boost::format("Expected %1%, but got %2%, when running " - "query: %3%.") - % expected % count % query).str()); - } -} - int main(int argc, char *argv[]) { std::unique_ptr db; @@ -86,20 +64,20 @@ int main(int argc, char *argv[]) { // start a new connection to run tests on pg::conn_ptr test_conn = pg::conn::connect(db->database_options); - check_count(test_conn, 1, + db->check_count(1, "select count(*) from pg_catalog.pg_class " "where relname = 'foobar_amenities'"); - check_count(test_conn, 244, + db->check_count(244, "select count(*) from foobar_amenities"); - check_count(test_conn, 36, + db->check_count(36, "select count(*) from foobar_amenities where amenity='parking'"); - check_count(test_conn, 34, + db->check_count(34, "select count(*) from foobar_amenities where amenity='bench'"); - check_count(test_conn, 1, + db->check_count(1, "select count(*) from foobar_amenities where amenity='vending_machine'"); return 0; diff --git a/tests/test-output-multi-poly-trivial.cpp b/tests/test-output-multi-poly-trivial.cpp index 1a0538aad..0ee2877fb 100644 --- a/tests/test-output-multi-poly-trivial.cpp +++ b/tests/test-output-multi-poly-trivial.cpp @@ -15,7 +15,6 @@ #include "taginfo_impl.hpp" #include "parse.hpp" -#include #include #include @@ -24,27 +23,6 @@ #include "tests/middle-tests.hpp" #include "tests/common-pg.hpp" -void check_count(pg::conn_ptr &conn, int expected, const std::string &query) { - pg::result_ptr res = conn->exec(query); - - int ntuples = PQntuples(res->get()); - if (ntuples != 1) { - throw std::runtime_error((boost::format("Expected only one tuple from a query " - "to check COUNT(*), but got %1%. Query " - "was: %2%.") - % ntuples % query).str()); - } - - std::string numstr = PQgetvalue(res->get(), 0, 0); - int count = boost::lexical_cast(numstr); - - if (count != expected) { - throw std::runtime_error((boost::format("Expected %1%, but got %2%, when running " - "query: %3%.") - % expected % count % query).str()); - } -} - void run_osm2pgsql(options_t &options) { //setup the front (input) parse_delegate_t parser(options.extra_attributes, options.bbox, options.projection, options.append); @@ -65,9 +43,9 @@ void run_osm2pgsql(options_t &options) { osmdata.stop(); } -void check_output_poly_trivial(bool enable_multi, database_options_t &database_options) { +void check_output_poly_trivial(bool enable_multi, std::shared_ptr db) { options_t options; - options.database_options = database_options; + options.database_options = db->database_options; options.num_procs = 1; options.slim = 1; options.enable_multi = enable_multi; @@ -79,37 +57,34 @@ void check_output_poly_trivial(bool enable_multi, database_options_t &database_o run_osm2pgsql(options); - // start a new connection to run tests on - pg::conn_ptr test_conn = pg::conn::connect(database_options); - // expect that the table exists - check_count(test_conn, 1, "select count(*) from pg_catalog.pg_class where relname = 'test_poly'"); + db->check_count(1, "select count(*) from pg_catalog.pg_class where relname = 'test_poly'"); // expect 2 polygons if not in multi(geometry) mode, or 1 if multi(geometry) // mode is enabled. if (enable_multi) { - check_count(test_conn, 1, "select count(*) from test_poly"); - check_count(test_conn, 1, "select count(*) from test_poly where foo='bar'"); - check_count(test_conn, 1, "select count(*) from test_poly where bar='baz'"); + db->check_count(1, "select count(*) from test_poly"); + db->check_count(1, "select count(*) from test_poly where foo='bar'"); + db->check_count(1, "select count(*) from test_poly where bar='baz'"); // there should be two 5-pointed polygons in the multipolygon (note that // it's 5 points including the duplicated first/last point) - check_count(test_conn, 2, "select count(*) from (select (st_dump(way)).geom as way from test_poly) x"); - check_count(test_conn, 5, "select distinct st_numpoints(st_exteriorring(way)) from (select (st_dump(way)).geom as way from test_poly) x"); + db->check_count(2, "select count(*) from (select (st_dump(way)).geom as way from test_poly) x"); + db->check_count(5, "select distinct st_numpoints(st_exteriorring(way)) from (select (st_dump(way)).geom as way from test_poly) x"); } else { - check_count(test_conn, 2, "select count(*) from test_poly"); - check_count(test_conn, 2, "select count(*) from test_poly where foo='bar'"); - check_count(test_conn, 2, "select count(*) from test_poly where bar='baz'"); + db->check_count(2, "select count(*) from test_poly"); + db->check_count(2, "select count(*) from test_poly where foo='bar'"); + db->check_count(2, "select count(*) from test_poly where bar='baz'"); // although there are 2 rows, they should both be 5-pointed polygons (note // that it's 5 points including the duplicated first/last point) - check_count(test_conn, 5, "select distinct st_numpoints(st_exteriorring(way)) from test_poly"); + db->check_count(5, "select distinct st_numpoints(st_exteriorring(way)) from test_poly"); } } int main(int argc, char *argv[]) { - std::unique_ptr db; + std::shared_ptr db; try { db.reset(new pg::tempdb); @@ -119,8 +94,8 @@ int main(int argc, char *argv[]) { } try { - check_output_poly_trivial(0, db->database_options); - check_output_poly_trivial(1, db->database_options); + check_output_poly_trivial(0, db); + check_output_poly_trivial(1, db); return 0; } catch (const std::exception &e) { diff --git a/tests/test-output-multi-polygon.cpp b/tests/test-output-multi-polygon.cpp index cadd14527..e6c1c0ea4 100644 --- a/tests/test-output-multi-polygon.cpp +++ b/tests/test-output-multi-polygon.cpp @@ -15,7 +15,6 @@ #include "taginfo_impl.hpp" #include "parse.hpp" -#include #include #include @@ -24,27 +23,6 @@ #include "tests/middle-tests.hpp" #include "tests/common-pg.hpp" -void check_count(pg::conn_ptr &conn, int expected, const std::string &query) { - pg::result_ptr res = conn->exec(query); - - int ntuples = PQntuples(res->get()); - if (ntuples != 1) { - throw std::runtime_error((boost::format("Expected only one tuple from a query " - "to check COUNT(*), but got %1%. Query " - "was: %2%.") - % ntuples % query).str()); - } - - std::string numstr = PQgetvalue(res->get(), 0, 0); - int count = boost::lexical_cast(numstr); - - if (count != expected) { - throw std::runtime_error((boost::format("Expected %1%, but got %2%, when running " - "query: %3%.") - % expected % count % query).str()); - } -} - int main(int argc, char *argv[]) { std::unique_ptr db; @@ -82,31 +60,28 @@ int main(int argc, char *argv[]) { osmdata.stop(); - // start a new connection to run tests on - pg::conn_ptr test_conn = pg::conn::connect(db->database_options); - - check_count(test_conn, 1, "select count(*) from pg_catalog.pg_class where relname = 'foobar_buildings'"); - check_count(test_conn, 0, "select count(*) from foobar_buildings where building is null"); - check_count(test_conn, 3723, "select count(*) from foobar_buildings"); + db->check_count(1, "select count(*) from pg_catalog.pg_class where relname = 'foobar_buildings'"); + db->check_count(0, "select count(*) from foobar_buildings where building is null"); + db->check_count(3723, "select count(*) from foobar_buildings"); //check that we have the right spread - check_count(test_conn, 1, "select count(*) from foobar_buildings where building='barn'"); - check_count(test_conn, 1, "select count(*) from foobar_buildings where building='chapel'"); - check_count(test_conn, 5, "select count(*) from foobar_buildings where building='church'"); - check_count(test_conn, 3, "select count(*) from foobar_buildings where building='commercial'"); - check_count(test_conn, 6, "select count(*) from foobar_buildings where building='farm'"); - check_count(test_conn, 1, "select count(*) from foobar_buildings where building='garage'"); - check_count(test_conn, 2, "select count(*) from foobar_buildings where building='glasshouse'"); - check_count(test_conn, 1, "select count(*) from foobar_buildings where building='greenhouse'"); - check_count(test_conn, 153, "select count(*) from foobar_buildings where building='house'"); - check_count(test_conn, 4, "select count(*) from foobar_buildings where building='hut'"); - check_count(test_conn, 8, "select count(*) from foobar_buildings where building='industrial'"); - check_count(test_conn, 200, "select count(*) from foobar_buildings where building='residential'"); - check_count(test_conn, 6, "select count(*) from foobar_buildings where building='roof'"); - check_count(test_conn, 4, "select count(*) from foobar_buildings where building='school'"); - check_count(test_conn, 2, "select count(*) from foobar_buildings where building='station'"); - check_count(test_conn, 3, "select count(*) from foobar_buildings where building='warehouse'"); - check_count(test_conn, 3323, "select count(*) from foobar_buildings where building='yes'"); + db->check_count(1, "select count(*) from foobar_buildings where building='barn'"); + db->check_count(1, "select count(*) from foobar_buildings where building='chapel'"); + db->check_count(5, "select count(*) from foobar_buildings where building='church'"); + db->check_count(3, "select count(*) from foobar_buildings where building='commercial'"); + db->check_count(6, "select count(*) from foobar_buildings where building='farm'"); + db->check_count(1, "select count(*) from foobar_buildings where building='garage'"); + db->check_count(2, "select count(*) from foobar_buildings where building='glasshouse'"); + db->check_count(1, "select count(*) from foobar_buildings where building='greenhouse'"); + db->check_count(153, "select count(*) from foobar_buildings where building='house'"); + db->check_count(4, "select count(*) from foobar_buildings where building='hut'"); + db->check_count(8, "select count(*) from foobar_buildings where building='industrial'"); + db->check_count(200, "select count(*) from foobar_buildings where building='residential'"); + db->check_count(6, "select count(*) from foobar_buildings where building='roof'"); + db->check_count(4, "select count(*) from foobar_buildings where building='school'"); + db->check_count(2, "select count(*) from foobar_buildings where building='station'"); + db->check_count(3, "select count(*) from foobar_buildings where building='warehouse'"); + db->check_count(3323, "select count(*) from foobar_buildings where building='yes'"); return 0; } catch (const std::exception &e) { diff --git a/tests/test-output-multi-tags.cpp b/tests/test-output-multi-tags.cpp index 80d570034..3f8077ad9 100644 --- a/tests/test-output-multi-tags.cpp +++ b/tests/test-output-multi-tags.cpp @@ -15,7 +15,6 @@ #include "taginfo_impl.hpp" #include "parse.hpp" -#include #include #include @@ -24,27 +23,6 @@ #include "tests/middle-tests.hpp" #include "tests/common-pg.hpp" -void check_count(pg::conn_ptr &conn, int expected, const std::string &query) { - pg::result_ptr res = conn->exec(query); - - int ntuples = PQntuples(res->get()); - if (ntuples != 1) { - throw std::runtime_error((boost::format("Expected only one tuple from a query " - "to check COUNT(*), but got %1%. Query " - "was: %2%.") - % ntuples % query).str()); - } - - std::string numstr = PQgetvalue(res->get(), 0, 0); - int count = boost::lexical_cast(numstr); - - if (count != expected) { - throw std::runtime_error((boost::format("Expected %1%, but got %2%, when running " - "query: %3%.") - % expected % count % query).str()); - } -} - int main(int argc, char *argv[]) { std::unique_ptr db; @@ -84,35 +62,32 @@ int main(int argc, char *argv[]) { osmdata.stop(); - // start a new connection to run tests on - pg::conn_ptr test_conn = pg::conn::connect(db->database_options); - // Check we got the right tables - check_count(test_conn, 1, "select count(*) from pg_catalog.pg_class where relname = 'test_points_1'"); - check_count(test_conn, 1, "select count(*) from pg_catalog.pg_class where relname = 'test_points_2'"); - check_count(test_conn, 1, "select count(*) from pg_catalog.pg_class where relname = 'test_line_1'"); - check_count(test_conn, 1, "select count(*) from pg_catalog.pg_class where relname = 'test_polygon_1'"); - check_count(test_conn, 1, "select count(*) from pg_catalog.pg_class where relname = 'test_polygon_2'"); + db->check_count(1, "select count(*) from pg_catalog.pg_class where relname = 'test_points_1'"); + db->check_count(1, "select count(*) from pg_catalog.pg_class where relname = 'test_points_2'"); + db->check_count(1, "select count(*) from pg_catalog.pg_class where relname = 'test_line_1'"); + db->check_count(1, "select count(*) from pg_catalog.pg_class where relname = 'test_polygon_1'"); + db->check_count(1, "select count(*) from pg_catalog.pg_class where relname = 'test_polygon_2'"); // Check we didn't get any extra in the tables - check_count(test_conn, 2, "select count(*) from test_points_1"); - check_count(test_conn, 2, "select count(*) from test_points_2"); - check_count(test_conn, 1, "select count(*) from test_line_1"); - check_count(test_conn, 1, "select count(*) from test_line_2"); - check_count(test_conn, 1, "select count(*) from test_polygon_1"); - check_count(test_conn, 1, "select count(*) from test_polygon_2"); + db->check_count(2, "select count(*) from test_points_1"); + db->check_count(2, "select count(*) from test_points_2"); + db->check_count(1, "select count(*) from test_line_1"); + db->check_count(1, "select count(*) from test_line_2"); + db->check_count(1, "select count(*) from test_polygon_1"); + db->check_count(1, "select count(*) from test_polygon_2"); // Check that the first table for each type got the right transform - check_count(test_conn, 1, "SELECT COUNT(*) FROM test_points_1 WHERE foo IS NULL and bar = 'n1' AND baz IS NULL"); - check_count(test_conn, 1, "SELECT COUNT(*) FROM test_points_1 WHERE foo IS NULL and bar = 'n2' AND baz IS NULL"); - check_count(test_conn, 1, "SELECT COUNT(*) FROM test_line_1 WHERE foo IS NULL and bar = 'w1' AND baz IS NULL"); - check_count(test_conn, 1, "SELECT COUNT(*) FROM test_polygon_1 WHERE foo IS NULL and bar = 'w2' AND baz IS NULL"); + db->check_count(1, "SELECT COUNT(*) FROM test_points_1 WHERE foo IS NULL and bar = 'n1' AND baz IS NULL"); + db->check_count(1, "SELECT COUNT(*) FROM test_points_1 WHERE foo IS NULL and bar = 'n2' AND baz IS NULL"); + db->check_count(1, "SELECT COUNT(*) FROM test_line_1 WHERE foo IS NULL and bar = 'w1' AND baz IS NULL"); + db->check_count(1, "SELECT COUNT(*) FROM test_polygon_1 WHERE foo IS NULL and bar = 'w2' AND baz IS NULL"); // Check that the second table also got the right transform - check_count(test_conn, 1, "SELECT COUNT(*) FROM test_points_2 WHERE foo IS NULL and bar IS NULL AND baz = 'n1'"); - check_count(test_conn, 1, "SELECT COUNT(*) FROM test_points_2 WHERE foo IS NULL and bar IS NULL AND baz = 'n2'"); - check_count(test_conn, 1, "SELECT COUNT(*) FROM test_line_2 WHERE foo IS NULL and bar IS NULL AND baz = 'w1'"); - check_count(test_conn, 1, "SELECT COUNT(*) FROM test_polygon_2 WHERE foo IS NULL and bar IS NULL AND baz = 'w2'"); + db->check_count(1, "SELECT COUNT(*) FROM test_points_2 WHERE foo IS NULL and bar IS NULL AND baz = 'n1'"); + db->check_count(1, "SELECT COUNT(*) FROM test_points_2 WHERE foo IS NULL and bar IS NULL AND baz = 'n2'"); + db->check_count(1, "SELECT COUNT(*) FROM test_line_2 WHERE foo IS NULL and bar IS NULL AND baz = 'w1'"); + db->check_count(1, "SELECT COUNT(*) FROM test_polygon_2 WHERE foo IS NULL and bar IS NULL AND baz = 'w2'"); return 0; diff --git a/tests/test-output-pgsql-schema.cpp b/tests/test-output-pgsql-schema.cpp new file mode 100644 index 000000000..8dc31a5ca --- /dev/null +++ b/tests/test-output-pgsql-schema.cpp @@ -0,0 +1,118 @@ +#include +#include +#include +#include +#include +#include +#include +#include + +#include "osmtypes.hpp" +#include "osmdata.hpp" +#include "output-pgsql.hpp" +#include "options.hpp" +#include "middle-pgsql.hpp" +#include "middle-ram.hpp" +#include "taginfo_impl.hpp" +#include "parse.hpp" + +#include +#include + +#include + +#include "tests/common-pg.hpp" + +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)) + +void test_other_output_schema() { + std::unique_ptr db; + + try { + db.reset(new pg::tempdb); + } catch (const std::exception &e) { + std::cerr << "Unable to setup database: " << e.what() << "\n"; + throw skip_test(); + } + + pg::conn_ptr schema_conn = pg::conn::connect(db->database_options); + + schema_conn->exec("CREATE SCHEMA myschema;" + "CREATE TABLE myschema.osm2pgsql_test_point (id bigint);" + "CREATE TABLE myschema.osm2pgsql_test_line (id bigint);" + "CREATE TABLE myschema.osm2pgsql_test_polygon (id bigint);" + "CREATE TABLE myschema.osm2pgsql_test_roads (id bigint)"); + + std::string proc_name("test-output-pgsql-schema"), input_file("-"); + char *argv[] = { &proc_name[0], &input_file[0], nullptr }; + + std::shared_ptr mid_pgsql(new middle_pgsql_t()); + options_t options = options_t(2, argv); + options.database_options = db->database_options; + options.num_procs = 1; + options.prefix = "osm2pgsql_test"; + options.style = "default.style"; + + auto out_test = std::make_shared(mid_pgsql.get(), options); + + osmdata_t osmdata(mid_pgsql, out_test); + + std::unique_ptr parser(new parse_delegate_t(options.extra_attributes, options.bbox, options.projection, false)); + + osmdata.start(); + + parser->stream_file("libxml2", "tests/test_output_pgsql_z_order.osm", &osmdata); + + parser.reset(nullptr); + + osmdata.stop(); + + + db->assert_has_table("public.osm2pgsql_test_point"); + db->assert_has_table("public.osm2pgsql_test_line"); + db->assert_has_table("public.osm2pgsql_test_polygon"); + db->assert_has_table("public.osm2pgsql_test_roads"); + db->assert_has_table("public.osm2pgsql_test_point"); + db->assert_has_table("public.osm2pgsql_test_line"); + db->assert_has_table("public.osm2pgsql_test_polygon"); + db->assert_has_table("public.osm2pgsql_test_roads"); + + db->check_count( 2, "SELECT COUNT(*) FROM osm2pgsql_test_point"); + db->check_count( 11, "SELECT COUNT(*) FROM osm2pgsql_test_line"); + db->check_count( 1, "SELECT COUNT(*) FROM osm2pgsql_test_polygon"); + db->check_count( 8, "SELECT COUNT(*) FROM osm2pgsql_test_roads"); + db->check_count( 0, "SELECT COUNT(*) FROM myschema.osm2pgsql_test_point"); + db->check_count( 0, "SELECT COUNT(*) FROM myschema.osm2pgsql_test_line"); + db->check_count( 0, "SELECT COUNT(*) FROM myschema.osm2pgsql_test_polygon"); + db->check_count( 0, "SELECT COUNT(*) FROM myschema.osm2pgsql_test_roads"); +} + +} // anonymous namespace + +int main(int argc, char *argv[]) { + RUN_TEST(test_other_output_schema); + + return 0; +} diff --git a/tests/test-output-pgsql-tablespace.cpp b/tests/test-output-pgsql-tablespace.cpp index 3b8abef8e..382dbca91 100644 --- a/tests/test-output-pgsql-tablespace.cpp +++ b/tests/test-output-pgsql-tablespace.cpp @@ -16,7 +16,6 @@ #include "taginfo_impl.hpp" #include "parse.hpp" -#include #include #include @@ -49,35 +48,6 @@ void run_test(const char* test_name, void (*testfunc)()) { } #define RUN_TEST(x) run_test(#x, &(x)) -void check_count(pg::conn_ptr &conn, int expected, const std::string &query) { - pg::result_ptr res = conn->exec(query); - - int ntuples = PQntuples(res->get()); - if (ntuples != 1) { - throw std::runtime_error((boost::format("Expected only one tuple from a query " - "to check COUNT(*), but got %1%. Query " - "was: %2%.") - % ntuples % query).str()); - } - - std::string numstr = PQgetvalue(res->get(), 0, 0); - int count = boost::lexical_cast(numstr); - - if (count != expected) { - throw std::runtime_error((boost::format("Expected %1%, but got %2%, when running " - "query: %3%.") - % expected % count % query).str()); - } -} - -void assert_has_table(pg::conn_ptr &test_conn, const std::string &table_name) { - std::string query = (boost::format("select count(*) from pg_catalog.pg_class " - "where relname = '%1%'") - % table_name).str(); - - check_count(test_conn, 1, query); -} - // "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. @@ -120,18 +90,15 @@ void test_regression_simple() { osmdata.stop(); - // start a new connection to run tests on - pg::conn_ptr test_conn = pg::conn::connect(db->database_options); - - assert_has_table(test_conn, "osm2pgsql_test_point"); - assert_has_table(test_conn, "osm2pgsql_test_line"); - assert_has_table(test_conn, "osm2pgsql_test_polygon"); - assert_has_table(test_conn, "osm2pgsql_test_roads"); + 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"); - check_count(test_conn, 1342, "SELECT count(*) FROM osm2pgsql_test_point"); - check_count(test_conn, 3300, "SELECT count(*) FROM osm2pgsql_test_line"); - check_count(test_conn, 375, "SELECT count(*) FROM osm2pgsql_test_roads"); - check_count(test_conn, 4128, "SELECT count(*) FROM osm2pgsql_test_polygon"); + db->check_count(1342, "SELECT count(*) FROM osm2pgsql_test_point"); + db->check_count(3300, "SELECT count(*) FROM osm2pgsql_test_line"); + db->check_count( 375, "SELECT count(*) FROM osm2pgsql_test_roads"); + db->check_count(4128, "SELECT count(*) FROM osm2pgsql_test_polygon"); } } // anonymous namespace diff --git a/tests/test-output-pgsql-z_order.cpp b/tests/test-output-pgsql-z_order.cpp index bea56a442..68ec098cc 100644 --- a/tests/test-output-pgsql-z_order.cpp +++ b/tests/test-output-pgsql-z_order.cpp @@ -16,7 +16,6 @@ #include "taginfo_impl.hpp" #include "parse.hpp" -#include #include #include @@ -49,56 +48,6 @@ void run_test(const char* test_name, void (*testfunc)()) { } #define RUN_TEST(x) run_test(#x, &(x)) -void check_string(pg::conn_ptr &conn, std::string expected, const std::string &query) { - pg::result_ptr res = conn->exec(query); - - int ntuples = PQntuples(res->get()); - if (ntuples != 1) { - throw std::runtime_error((boost::format("Expected only one tuple from a query " - "to check a string, but got %1%. Query " - "was: %2%.") - % ntuples % query).str()); - } - - std::string actual = PQgetvalue(res->get(), 0, 0); - - if (actual != expected) { - throw std::runtime_error((boost::format("Expected %1%, but got %2%, when running " - "query: %3%.") - % expected % actual % query).str()); - } -} - - -void check_count(pg::conn_ptr &conn, int expected, const std::string &query) { - pg::result_ptr res = conn->exec(query); - - int ntuples = PQntuples(res->get()); - if (ntuples != 1) { - throw std::runtime_error((boost::format("Expected only one tuple from a query " - "to check COUNT(*), but got %1%. Query " - "was: %2%.") - % ntuples % query).str()); - } - - std::string numstr = PQgetvalue(res->get(), 0, 0); - int count = boost::lexical_cast(numstr); - - if (count != expected) { - throw std::runtime_error((boost::format("Expected %1%, but got %2%, when running " - "query: %3%.") - % expected % count % query).str()); - } -} - -void assert_has_table(pg::conn_ptr &test_conn, const std::string &table_name) { - std::string query = (boost::format("select count(*) from pg_catalog.pg_class " - "where relname = '%1%'") - % table_name).str(); - - check_count(test_conn, 1, query); -} - // "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. @@ -112,7 +61,7 @@ void test_z_order() { throw skip_test(); } - std::string proc_name("test-output-pgsql"), input_file("-"); + std::string proc_name("test-output-pgsql-z_order"), input_file("-"); char *argv[] = { &proc_name[0], &input_file[0], nullptr }; std::shared_ptr mid_pgsql(new middle_pgsql_t()); @@ -136,21 +85,18 @@ void test_z_order() { osmdata.stop(); - // start a new connection to run tests on - pg::conn_ptr test_conn = pg::conn::connect(db->database_options); - - assert_has_table(test_conn, "osm2pgsql_test_point"); - assert_has_table(test_conn, "osm2pgsql_test_line"); - assert_has_table(test_conn, "osm2pgsql_test_polygon"); - assert_has_table(test_conn, "osm2pgsql_test_roads"); + 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"); - check_string(test_conn, "motorway", "SELECT highway FROM osm2pgsql_test_line WHERE layer IS NULL ORDER BY z_order DESC LIMIT 1 OFFSET 0"); - check_string(test_conn, "trunk", "SELECT highway FROM osm2pgsql_test_line WHERE layer IS NULL ORDER BY z_order DESC LIMIT 1 OFFSET 1"); - check_string(test_conn, "primary", "SELECT highway FROM osm2pgsql_test_line WHERE layer IS NULL ORDER BY z_order DESC LIMIT 1 OFFSET 2"); - check_string(test_conn, "secondary", "SELECT highway FROM osm2pgsql_test_line WHERE layer IS NULL ORDER BY z_order DESC LIMIT 1 OFFSET 3"); - check_string(test_conn, "tertiary", "SELECT highway FROM osm2pgsql_test_line WHERE layer IS NULL ORDER BY z_order DESC LIMIT 1 OFFSET 4"); + db->check_string("motorway", "SELECT highway FROM osm2pgsql_test_line WHERE layer IS NULL ORDER BY z_order DESC LIMIT 1 OFFSET 0"); + db->check_string("trunk", "SELECT highway FROM osm2pgsql_test_line WHERE layer IS NULL ORDER BY z_order DESC LIMIT 1 OFFSET 1"); + db->check_string("primary", "SELECT highway FROM osm2pgsql_test_line WHERE layer IS NULL ORDER BY z_order DESC LIMIT 1 OFFSET 2"); + db->check_string("secondary", "SELECT highway FROM osm2pgsql_test_line WHERE layer IS NULL ORDER BY z_order DESC LIMIT 1 OFFSET 3"); + db->check_string("tertiary", "SELECT highway FROM osm2pgsql_test_line WHERE layer IS NULL ORDER BY z_order DESC LIMIT 1 OFFSET 4"); - check_string(test_conn, "residential", "SELECT highway FROM osm2pgsql_test_line ORDER BY z_order DESC LIMIT 1 OFFSET 0"); + db->check_string("residential", "SELECT highway FROM osm2pgsql_test_line ORDER BY z_order DESC LIMIT 1 OFFSET 0"); } } // anonymous namespace diff --git a/tests/test-output-pgsql.cpp b/tests/test-output-pgsql.cpp index 1ee7ffbbb..769fa7135 100644 --- a/tests/test-output-pgsql.cpp +++ b/tests/test-output-pgsql.cpp @@ -16,7 +16,6 @@ #include "taginfo_impl.hpp" #include "parse.hpp" -#include #include #include @@ -49,56 +48,6 @@ void run_test(const char* test_name, void (*testfunc)()) { } #define RUN_TEST(x) run_test(#x, &(x)) -void check_count(pg::conn_ptr &conn, int expected, const std::string &query) { - pg::result_ptr res = conn->exec(query); - - int ntuples = PQntuples(res->get()); - if (ntuples != 1) { - throw std::runtime_error((boost::format("Expected only one tuple from a query " - "to check COUNT(*), but got %1%. Query " - "was: %2%.") - % ntuples % query).str()); - } - - std::string numstr = PQgetvalue(res->get(), 0, 0); - int count = boost::lexical_cast(numstr); - - if (count != expected) { - throw std::runtime_error((boost::format("Expected %1%, but got %2%, when running " - "query: %3%.") - % expected % count % query).str()); - } -} - -void check_number(pg::conn_ptr &conn, double expected, const std::string &query) { - pg::result_ptr res = conn->exec(query); - - int ntuples = PQntuples(res->get()); - if (ntuples != 1) { - throw std::runtime_error((boost::format("Expected only one tuple from a query, " - " but got %1%. Query was: %2%.") - % ntuples % query).str()); - } - - std::string numstr = PQgetvalue(res->get(), 0, 0); - double num = boost::lexical_cast(numstr); - - // floating point isn't exact, so allow a 0.01% difference - if ((num > 1.0001*expected) || (num < 0.9999*expected)) { - throw std::runtime_error((boost::format("Expected %1%, but got %2%, when running " - "query: %3%.") - % expected % num % query).str()); - } -} - -void assert_has_table(pg::conn_ptr &test_conn, const std::string &table_name) { - std::string query = (boost::format("select count(*) from pg_catalog.pg_class " - "where relname = '%1%'") - % table_name).str(); - - check_count(test_conn, 1, query); -} - // "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. @@ -137,29 +86,26 @@ void test_regression_simple() { osmdata.stop(); - // start a new connection to run tests on - pg::conn_ptr test_conn = pg::conn::connect(db->database_options); + 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"); - assert_has_table(test_conn, "osm2pgsql_test_point"); - assert_has_table(test_conn, "osm2pgsql_test_line"); - assert_has_table(test_conn, "osm2pgsql_test_polygon"); - assert_has_table(test_conn, "osm2pgsql_test_roads"); - - check_count(test_conn, 1342, "SELECT count(*) FROM osm2pgsql_test_point"); - check_count(test_conn, 3300, "SELECT count(*) FROM osm2pgsql_test_line"); - check_count(test_conn, 375, "SELECT count(*) FROM osm2pgsql_test_roads"); - check_count(test_conn, 4128, "SELECT count(*) FROM osm2pgsql_test_polygon"); + db->check_count(1342, "SELECT count(*) FROM osm2pgsql_test_point"); + db->check_count(3300, "SELECT count(*) FROM osm2pgsql_test_line"); + db->check_count( 375, "SELECT count(*) FROM osm2pgsql_test_roads"); + db->check_count(4128, "SELECT count(*) FROM osm2pgsql_test_polygon"); // Check size of lines - check_number(test_conn, 1696.04, "SELECT ST_Length(way) FROM osm2pgsql_test_line WHERE osm_id = 44822682"); - check_number(test_conn, 1151.26, "SELECT ST_Length(ST_Transform(way,4326)::geography) FROM osm2pgsql_test_line WHERE osm_id = 44822682"); + db->check_number(1696.04, "SELECT ST_Length(way) FROM osm2pgsql_test_line WHERE osm_id = 44822682"); + db->check_number(1151.26, "SELECT ST_Length(ST_Transform(way,4326)::geography) FROM osm2pgsql_test_line WHERE osm_id = 44822682"); - check_number(test_conn, 311.21, "SELECT way_area FROM osm2pgsql_test_polygon WHERE osm_id = 157261342"); - check_number(test_conn, 311.21, "SELECT ST_Area(way) FROM osm2pgsql_test_polygon WHERE osm_id = 157261342"); - check_number(test_conn, 143.81, "SELECT ST_Area(ST_Transform(way,4326)::geography) FROM osm2pgsql_test_polygon WHERE osm_id = 157261342"); + db->check_number(311.21, "SELECT way_area FROM osm2pgsql_test_polygon WHERE osm_id = 157261342"); + db->check_number(311.21, "SELECT ST_Area(way) FROM osm2pgsql_test_polygon WHERE osm_id = 157261342"); + db->check_number(143.81, "SELECT ST_Area(ST_Transform(way,4326)::geography) FROM osm2pgsql_test_polygon WHERE osm_id = 157261342"); // Check a point's location - check_count(test_conn, 1, "SELECT count(*) FROM osm2pgsql_test_point WHERE ST_DWithin(way, 'SRID=900913;POINT(1062645.12 5972593.4)'::geometry, 0.1)"); + db->check_count(1, "SELECT count(*) FROM osm2pgsql_test_point WHERE ST_DWithin(way, 'SRID=900913;POINT(1062645.12 5972593.4)'::geometry, 0.1)"); } void test_latlong() { @@ -200,29 +146,26 @@ void test_latlong() { osmdata.stop(); - // start a new connection to run tests on - pg::conn_ptr test_conn = pg::conn::connect(db->database_options); - - assert_has_table(test_conn, "osm2pgsql_test_point"); - assert_has_table(test_conn, "osm2pgsql_test_line"); - assert_has_table(test_conn, "osm2pgsql_test_polygon"); - assert_has_table(test_conn, "osm2pgsql_test_roads"); + 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"); - check_count(test_conn, 1342, "SELECT count(*) FROM osm2pgsql_test_point"); - check_count(test_conn, 3298, "SELECT count(*) FROM osm2pgsql_test_line"); - check_count(test_conn, 374, "SELECT count(*) FROM osm2pgsql_test_roads"); - check_count(test_conn, 4128, "SELECT count(*) FROM osm2pgsql_test_polygon"); + db->check_count(1342, "SELECT count(*) FROM osm2pgsql_test_point"); + db->check_count(3298, "SELECT count(*) FROM osm2pgsql_test_line"); + db->check_count(374, "SELECT count(*) FROM osm2pgsql_test_roads"); + db->check_count(4128, "SELECT count(*) FROM osm2pgsql_test_polygon"); // Check size of lines - check_number(test_conn, 0.0105343, "SELECT ST_Length(way) FROM osm2pgsql_test_line WHERE osm_id = 44822682"); - check_number(test_conn, 1151.26, "SELECT ST_Length(ST_Transform(way,4326)::geography) FROM osm2pgsql_test_line WHERE osm_id = 44822682"); + db->check_number(0.0105343, "SELECT ST_Length(way) FROM osm2pgsql_test_line WHERE osm_id = 44822682"); + db->check_number(1151.26, "SELECT ST_Length(ST_Transform(way,4326)::geography) FROM osm2pgsql_test_line WHERE osm_id = 44822682"); - check_number(test_conn, 1.70718e-08, "SELECT way_area FROM osm2pgsql_test_polygon WHERE osm_id = 157261342"); - check_number(test_conn, 1.70718e-08, "SELECT ST_Area(way) FROM osm2pgsql_test_polygon WHERE osm_id = 157261342"); - check_number(test_conn, 143.845, "SELECT ST_Area(ST_Transform(way,4326)::geography) FROM osm2pgsql_test_polygon WHERE osm_id = 157261342"); + db->check_number(1.70718e-08, "SELECT way_area FROM osm2pgsql_test_polygon WHERE osm_id = 157261342"); + db->check_number(1.70718e-08, "SELECT ST_Area(way) FROM osm2pgsql_test_polygon WHERE osm_id = 157261342"); + db->check_number(143.845, "SELECT ST_Area(ST_Transform(way,4326)::geography) FROM osm2pgsql_test_polygon WHERE osm_id = 157261342"); // Check a point's location - check_count(test_conn, 1, "SELECT count(*) FROM osm2pgsql_test_point WHERE ST_DWithin(way, 'SRID=4326;POINT(9.5459035 47.1866494)'::geometry, 0.00001)"); + db->check_count(1, "SELECT count(*) FROM osm2pgsql_test_point WHERE ST_DWithin(way, 'SRID=4326;POINT(9.5459035 47.1866494)'::geometry, 0.00001)"); } @@ -263,18 +206,15 @@ void test_area_way_simple() { osmdata.stop(); - // start a new connection to run tests on - pg::conn_ptr test_conn = pg::conn::connect(db->database_options); - - assert_has_table(test_conn, "osm2pgsql_test_point"); - assert_has_table(test_conn, "osm2pgsql_test_line"); - assert_has_table(test_conn, "osm2pgsql_test_polygon"); - assert_has_table(test_conn, "osm2pgsql_test_roads"); + 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"); - check_count(test_conn, 0, "SELECT count(*) FROM osm2pgsql_test_point"); - check_count(test_conn, 0, "SELECT count(*) FROM osm2pgsql_test_line"); - check_count(test_conn, 0, "SELECT count(*) FROM osm2pgsql_test_roads"); - check_count(test_conn, 1, "SELECT count(*) FROM osm2pgsql_test_polygon"); + 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() { @@ -312,18 +252,15 @@ void test_route_rel() { osmdata.stop(); - // start a new connection to run tests on - pg::conn_ptr test_conn = pg::conn::connect(db->database_options); + 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"); - assert_has_table(test_conn, "osm2pgsql_test_point"); - assert_has_table(test_conn, "osm2pgsql_test_line"); - assert_has_table(test_conn, "osm2pgsql_test_polygon"); - assert_has_table(test_conn, "osm2pgsql_test_roads"); - - check_count(test_conn, 0, "SELECT count(*) FROM osm2pgsql_test_point"); - check_count(test_conn, 2, "SELECT count(*) FROM osm2pgsql_test_line"); - check_count(test_conn, 1, "SELECT count(*) FROM osm2pgsql_test_roads"); - check_count(test_conn, 0, "SELECT count(*) FROM osm2pgsql_test_polygon"); + 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 @@ -367,18 +304,15 @@ void test_clone() { osmdata.stop(); - // start a new connection to run tests on - pg::conn_ptr test_conn = pg::conn::connect(db->database_options); - - assert_has_table(test_conn, "osm2pgsql_test_point"); - assert_has_table(test_conn, "osm2pgsql_test_line"); - assert_has_table(test_conn, "osm2pgsql_test_polygon"); - assert_has_table(test_conn, "osm2pgsql_test_roads"); + 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"); - check_count(test_conn, 1342, "SELECT count(*) FROM osm2pgsql_test_point"); - check_count(test_conn, 3300, "SELECT count(*) FROM osm2pgsql_test_line"); - check_count(test_conn, 375, "SELECT count(*) FROM osm2pgsql_test_roads"); - check_count(test_conn, 4128, "SELECT count(*) FROM osm2pgsql_test_polygon"); + db->check_count(1342, "SELECT count(*) FROM osm2pgsql_test_point"); + db->check_count(3300, "SELECT count(*) FROM osm2pgsql_test_line"); + db->check_count( 375, "SELECT count(*) FROM osm2pgsql_test_roads"); + db->check_count(4128, "SELECT count(*) FROM osm2pgsql_test_polygon"); } } // anonymous namespace