Merge pull request #3180 from mapnik/postgis-tests-squashed
1st iteration of PostGIS tests
This commit is contained in:
commit
9c3f78b411
1 changed files with 230 additions and 30 deletions
|
@ -29,24 +29,22 @@
|
|||
#include <mapnik/util/fs.hpp>
|
||||
|
||||
/*
|
||||
|
||||
Compile and run just this test:
|
||||
|
||||
clang++ -o test-postgis -g -I./test/ test/unit/run.cpp test/unit/datasource/postgis.cpp `mapnik-config --all-flags` && ./test-postgis -d yes
|
||||
|
||||
*/
|
||||
|
||||
namespace {
|
||||
#include <boost/optional/optional_io.hpp>
|
||||
|
||||
int run(std::string const& command, bool silent = false)
|
||||
int run(std::string const& command, bool okay_to_fail = false)
|
||||
{
|
||||
std::string cmd;
|
||||
if (std::getenv("DYLD_LIBRARY_PATH") != nullptr)
|
||||
{
|
||||
cmd += std::string("DYLD_LIBRARY_PATH=") + std::getenv("DYLD_LIBRARY_PATH") + " ";
|
||||
cmd += std::string("DYLD_LIBRARY_PATH=") + std::getenv("DYLD_LIBRARY_PATH") + " && ";
|
||||
}
|
||||
cmd += command;
|
||||
if (silent)
|
||||
// silence output unless MAPNIK_TEST_DEBUG is defined
|
||||
if (std::getenv("MAPNIK_TEST_DEBUG") == nullptr)
|
||||
{
|
||||
#ifndef _WINDOWS
|
||||
cmd += " 2>/dev/null";
|
||||
|
@ -54,41 +52,243 @@ int run(std::string const& command, bool silent = false)
|
|||
cmd += " 2> nul";
|
||||
#endif
|
||||
}
|
||||
else
|
||||
{
|
||||
std::clog << "Running " << cmd << "\n";
|
||||
}
|
||||
bool worked = (std::system(cmd.c_str()) == 0);
|
||||
if (silent == true) return true;
|
||||
if (okay_to_fail == true) return true;
|
||||
return worked;
|
||||
}
|
||||
|
||||
std::string dbname("mapnik-tmp-postgis-test-db");
|
||||
|
||||
TEST_CASE("postgis") {
|
||||
|
||||
SECTION("Postgis data initialization")
|
||||
{
|
||||
REQUIRE(run("dropdb mapnik-tmp-postgis-test-db",true));
|
||||
REQUIRE(run("createdb -T template_postgis mapnik-tmp-postgis-test-db"));
|
||||
std::stringstream cmd;
|
||||
cmd << "psql -q mapnik-tmp-postgis-test-db -f ./test/data/sql/table1.sql";
|
||||
REQUIRE(run(cmd.str()));
|
||||
//don't add 'true' here, to get error message, when drop fails. If it works nothing is output
|
||||
REQUIRE(run("dropdb --if-exists " + dbname));
|
||||
REQUIRE(run("createdb -T template_postgis " + dbname));
|
||||
//REQUIRE(run("createdb " + dbname));
|
||||
// Breaks when raster support is missing (unfortunately this is common)
|
||||
//REQUIRE(run("psql -c 'CREATE EXTENSION postgis;' " + dbname, true));
|
||||
REQUIRE(run("psql -q -f ./test/data/sql/postgis-create-db-and-tables.sql " + dbname));
|
||||
}
|
||||
|
||||
std::string datasource_plugin("./plugins/input/postgis.input");
|
||||
if (mapnik::util::exists(datasource_plugin))
|
||||
mapnik::parameters params;
|
||||
params["type"] = "postgis";
|
||||
params["dbname"] = dbname;
|
||||
|
||||
SECTION("Postgis should throw without 'table' parameter")
|
||||
{
|
||||
SECTION("Postgis plugin initialization")
|
||||
{
|
||||
mapnik::parameters params;
|
||||
params["type"] = "postgis";
|
||||
params["dbname"] = "mapnik-tmp-postgis-test-db";
|
||||
params["table"] = "test";
|
||||
auto ds = mapnik::datasource_cache::instance().create(params);
|
||||
REQUIRE(ds != nullptr);
|
||||
CHECK(ds->type() == mapnik::datasource::datasource_t::Vector);
|
||||
auto fields = ds->get_descriptor().get_descriptors();
|
||||
require_field_names(fields, {"gid"});
|
||||
require_field_types(fields, {mapnik::Integer});
|
||||
}
|
||||
CHECK_THROWS(mapnik::datasource_cache::instance().create(params));
|
||||
}
|
||||
}
|
||||
|
||||
SECTION("Postgis should throw with 'max_async_connection' greater than 'max_size'")
|
||||
{
|
||||
params["table"] = "test";
|
||||
params["max_async_connection"] = "2";
|
||||
params["max_size"] = "1";
|
||||
CHECK_THROWS(mapnik::datasource_cache::instance().create(params));
|
||||
}
|
||||
|
||||
}
|
||||
SECTION("Postgis should throw with invalid metadata query")
|
||||
{
|
||||
params["table"] = "does_not_exist";
|
||||
CHECK_THROWS(mapnik::datasource_cache::instance().create(params));
|
||||
}
|
||||
|
||||
SECTION("Postgis should throw with invalid key field")
|
||||
{
|
||||
params["table"] = "test_invalid_id";
|
||||
params["key_field"] = "id";
|
||||
CHECK_THROWS(mapnik::datasource_cache::instance().create(params));
|
||||
}
|
||||
|
||||
SECTION("Postgis should throw with multicolumn primary key")
|
||||
{
|
||||
params["table"] = "test_invalid_multi_col_pk";
|
||||
params["autodetect_key_field"] = "true";
|
||||
CHECK_THROWS(mapnik::datasource_cache::instance().create(params));
|
||||
}
|
||||
|
||||
SECTION("Postgis should throw without geom column")
|
||||
{
|
||||
params["table"] = "test_no_geom_col";
|
||||
auto ds = mapnik::datasource_cache::instance().create(params);
|
||||
REQUIRE(ds != nullptr);
|
||||
CHECK_THROWS(all_features(ds));
|
||||
}
|
||||
|
||||
SECTION("Postgis should throw with invalid credentials")
|
||||
{
|
||||
params["table"] = "test";
|
||||
params["user"] = "not_a_valid_user";
|
||||
params["password"] = "not_a_valid_pwd";
|
||||
CHECK_THROWS(mapnik::datasource_cache::instance().create(params));
|
||||
}
|
||||
|
||||
SECTION("Postgis initialize dataset with persist_connection, schema, extent, geometry field, autodectect key field, simplify_geometries, row_limit")
|
||||
{
|
||||
params["persist_connection"] = "false";
|
||||
params["table"] = "public.test";
|
||||
params["geometry_field"] = "geom";
|
||||
params["autodetect_key_field"] = "true";
|
||||
params["extent"] = "-1 -1, -1 2, 4 3, 3 -1, -1 -1";
|
||||
params["simplify_geometries"] = "true";
|
||||
params["row_limit"] = "1";
|
||||
auto ds = mapnik::datasource_cache::instance().create(params);
|
||||
}
|
||||
|
||||
SECTION("Postgis dataset geometry type")
|
||||
{
|
||||
params["table"] = "(SELECT * FROM test WHERE gid=1) as data";
|
||||
auto ds = mapnik::datasource_cache::instance().create(params);
|
||||
REQUIRE(ds != nullptr);
|
||||
CHECK(ds->get_geometry_type() == mapnik::datasource_geometry_t::Point);
|
||||
}
|
||||
|
||||
SECTION("Postgis query field names")
|
||||
{
|
||||
params["table"] = "test";
|
||||
auto ds = mapnik::datasource_cache::instance().create(params);
|
||||
REQUIRE(ds != nullptr);
|
||||
REQUIRE(ds->type() == mapnik::datasource::datasource_t::Vector);
|
||||
auto fields = ds->get_descriptor().get_descriptors();
|
||||
require_field_names(fields, { "gid", "colbigint", "col_text", "col-char", "col+bool", "colnumeric", "colsmallint", "colfloat4", "colfloat8", "colcharacter" });
|
||||
require_field_types(fields, { mapnik::Integer, mapnik::Integer, mapnik::String, mapnik::String, mapnik::Boolean, mapnik::Double, mapnik::Integer, mapnik::Double, mapnik::Double, mapnik::String });
|
||||
}
|
||||
|
||||
SECTION("Postgis iterate features")
|
||||
{
|
||||
params["table"] = "test";
|
||||
params["key_field"] = "gid";
|
||||
params["max_async_connection"] = "2";
|
||||
//params["cursor_size"] = "2";
|
||||
auto ds = mapnik::datasource_cache::instance().create(params);
|
||||
REQUIRE(ds != nullptr);
|
||||
|
||||
auto featureset = ds->features_at_point(mapnik::coord2d(1, 1));
|
||||
mapnik::feature_ptr feature;
|
||||
while ((bool(feature = featureset->next()))) {
|
||||
REQUIRE(feature->get(2).to_string() == feature->get("col_text").to_string());
|
||||
REQUIRE(feature->get(4).to_bool() == feature->get("col+bool").to_bool());
|
||||
REQUIRE(feature->get(5).to_double() == feature->get("colnumeric").to_double());
|
||||
REQUIRE(feature->get(5).to_string() == feature->get("colnumeric").to_string());
|
||||
}
|
||||
|
||||
featureset = all_features(ds);
|
||||
feature = featureset->next();
|
||||
//deactivate char tests for now: not yet implemented.
|
||||
//add at postgis_datasource.cpp:423
|
||||
//case 18: // char
|
||||
//REQUIRE("A" == feature->get("col-char").to_string());
|
||||
feature = featureset->next();
|
||||
//REQUIRE("B" == feature->get("col-char").to_string());
|
||||
feature = featureset->next();
|
||||
REQUIRE(false == feature->get("col+bool").to_bool());
|
||||
}
|
||||
|
||||
SECTION("Postgis cursorresultest")
|
||||
{
|
||||
params["table"] = "(SELECT * FROM test) as data";
|
||||
params["cursor_size"] = "2";
|
||||
auto ds = mapnik::datasource_cache::instance().create(params);
|
||||
REQUIRE(ds != nullptr);
|
||||
auto featureset = all_features(ds);
|
||||
CHECK(count_features(featureset) == 8);
|
||||
|
||||
featureset = all_features(ds);
|
||||
mapnik::feature_ptr feature;
|
||||
while (bool(feature = featureset->next())) {
|
||||
CHECK(feature->size() == 10);
|
||||
}
|
||||
|
||||
featureset = all_features(ds);
|
||||
require_geometry(featureset->next(), 1, mapnik::geometry::geometry_types::Point);
|
||||
require_geometry(featureset->next(), 1, mapnik::geometry::geometry_types::Point);
|
||||
require_geometry(featureset->next(), 2, mapnik::geometry::geometry_types::MultiPoint);
|
||||
require_geometry(featureset->next(), 1, mapnik::geometry::geometry_types::LineString);
|
||||
require_geometry(featureset->next(), 2, mapnik::geometry::geometry_types::MultiLineString);
|
||||
require_geometry(featureset->next(), 1, mapnik::geometry::geometry_types::Polygon);
|
||||
require_geometry(featureset->next(), 2, mapnik::geometry::geometry_types::MultiPolygon);
|
||||
require_geometry(featureset->next(), 3, mapnik::geometry::geometry_types::GeometryCollection);
|
||||
}
|
||||
|
||||
SECTION("Postgis bbox query")
|
||||
{
|
||||
params["table"] = "(SELECT * FROM public.test) as data WHERE geom && !bbox!";
|
||||
auto ds = mapnik::datasource_cache::instance().create(params);
|
||||
REQUIRE(ds != nullptr);
|
||||
mapnik::box2d<double> ext = ds->envelope();
|
||||
CAPTURE(ext);
|
||||
INFO(std::setprecision(6) << std::fixed << ext.minx() << "/" << ext.miny() << " " << ext.maxx() << "/" << ext.maxy());
|
||||
REQUIRE(ext.minx() == -2);
|
||||
REQUIRE(ext.miny() == -2);
|
||||
REQUIRE(ext.maxx() == 5);
|
||||
REQUIRE(ext.maxy() == 4);
|
||||
}
|
||||
|
||||
SECTION("Postgis query extent: full dataset")
|
||||
{
|
||||
//include schema to increase coverage
|
||||
params["table"] = "(SELECT * FROM public.test) as data";
|
||||
auto ds = mapnik::datasource_cache::instance().create(params);
|
||||
REQUIRE(ds != nullptr);
|
||||
mapnik::box2d<double> ext = ds->envelope();
|
||||
CAPTURE(ext);
|
||||
INFO(std::setprecision(6) << std::fixed << ext.minx() << "/" << ext.miny() << " " << ext.maxx() << "/" << ext.maxy());
|
||||
REQUIRE(ext.minx() == -2);
|
||||
REQUIRE(ext.miny() == -2);
|
||||
REQUIRE(ext.maxx() == 5);
|
||||
REQUIRE(ext.maxy() == 4);
|
||||
}
|
||||
/* deactivated for merging: still investigating a proper fix
|
||||
SECTION("Postgis query extent from subquery")
|
||||
{
|
||||
params["table"] = "(SELECT * FROM test where gid=4) as data";
|
||||
auto ds = mapnik::datasource_cache::instance().create(params);
|
||||
REQUIRE(ds != nullptr);
|
||||
mapnik::box2d<double> ext = ds->envelope();
|
||||
CAPTURE(ext);
|
||||
INFO(std::setprecision(6) << std::fixed << ext.minx() << "/" << ext.miny() << " " << ext.maxx() << "/" << ext.maxy());
|
||||
REQUIRE(ext.minx() == 0);
|
||||
REQUIRE(ext.miny() == 0);
|
||||
REQUIRE(ext.maxx() == 1);
|
||||
REQUIRE(ext.maxy() == 2);
|
||||
}
|
||||
*/
|
||||
SECTION("Postgis query extent: from subquery with 'extent_from_subquery=true'")
|
||||
{
|
||||
params["table"] = "(SELECT * FROM test where gid=4) as data";
|
||||
params["extent_from_subquery"] = "true";
|
||||
auto ds = mapnik::datasource_cache::instance().create(params);
|
||||
REQUIRE(ds != nullptr);
|
||||
mapnik::box2d<double> ext = ds->envelope();
|
||||
CAPTURE(ext);
|
||||
INFO(std::setprecision(6) << std::fixed << ext.minx() << "/" << ext.miny() << " " << ext.maxx() << "/" << ext.maxy());
|
||||
REQUIRE(ext.minx() == 0);
|
||||
REQUIRE(ext.miny() == 0);
|
||||
REQUIRE(ext.maxx() == 1);
|
||||
REQUIRE(ext.maxy() == 2);
|
||||
}
|
||||
/* deactivated for merging: still investigating a proper fix
|
||||
SECTION("Postgis query extent: subset with 'extent_from_subquery=true' and 'scale_denominator'")
|
||||
{
|
||||
// !!!! postgis-vt-util::z() returns 'null' when 'scale_denominator > 600000000'
|
||||
// https://github.com/mapbox/postgis-vt-util/blob/559f073877696a6bfea41baf3e1065f9cf4d18d1/postgis-vt-util.sql#L615-L617
|
||||
params["table"] = "(SELECT * FROM test where gid=4 AND z(!scale_denominator!) BETWEEN 0 AND 22) as data";
|
||||
params["extent_from_subquery"] = "true";
|
||||
auto ds = mapnik::datasource_cache::instance().create(params);
|
||||
REQUIRE(ds != nullptr);
|
||||
mapnik::box2d<double> ext = ds->envelope();
|
||||
CAPTURE(ext);
|
||||
INFO("" << std::setprecision(6) << std::fixed << ext.minx() << "/" << ext.miny() << " " << ext.maxx() << "/" << ext.maxy());
|
||||
REQUIRE(ext.minx() == 0);
|
||||
REQUIRE(ext.miny() == 0);
|
||||
REQUIRE(ext.maxx() == 1);
|
||||
REQUIRE(ext.maxy() == 2);
|
||||
}
|
||||
*/
|
||||
}
|
Loading…
Add table
Reference in a new issue