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 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301
|
/**
* SPDX-License-Identifier: GPL-2.0-or-later
*
* This file is part of osm2pgsql (https://osm2pgsql.org/).
*
* Copyright (C) 2006-2025 by the osm2pgsql developer community.
* For a full list of authors see the git log.
*/
#include <catch.hpp>
#include "common-import.hpp"
#include "common-options.hpp"
#include "common-pg.hpp"
namespace {
testing::db::import_t db;
char const *const conf_file = "test_output_flex.lua";
} // anonymous namespace
struct options_slim_default
{
static options_t options()
{
return testing::opt_t().slim().flex(conf_file);
}
};
struct options_slim_expire
{
static options_t options()
{
options_t o = options_slim_default::options();
o.expire_tiles_zoom = 10;
return o;
}
};
struct options_slim_schema
{
static options_t options()
{
auto conn = db.db().connect();
// Create limited user (if it doesn't exist yet),
// which we need to test that the public schema won't be touched.
// If the public schema is tried to be modified at any point, this user won't have the
// necessary permissions, and hence the test will fail.
conn.exec(R"(
DO
$$
BEGIN
IF NOT EXISTS (SELECT FROM pg_catalog.pg_roles WHERE rolname = 'limited') THEN
CREATE ROLE limited LOGIN PASSWORD 'password_limited';
END IF;
END
$$;
)");
conn.exec("REVOKE ALL PRIVILEGES ON ALL TABLES IN SCHEMA public FROM "
"PUBLIC, limited;");
conn.exec("REVOKE CREATE ON SCHEMA public FROM PUBLIC, limited;");
conn.exec(
"CREATE SCHEMA IF NOT EXISTS myschema AUTHORIZATION limited;");
conn.close();
return testing::opt_t()
.slim()
.flex(conf_file)
.schema("myschema")
.user("limited", "password_limited");
}
};
// Return a string with the schema name prepended to the table name.
std::string with_schema(char const *table_name, options_t const &options)
{
if (options.dbschema.empty()) {
return {table_name};
}
return options.dbschema + "." + table_name;
}
TEMPLATE_TEST_CASE("updating a node", "", options_slim_default,
options_slim_expire, options_slim_schema)
{
options_t options = TestType::options();
// import a node...
REQUIRE_NOTHROW(db.run_import(options, "n10 v1 dV x10 y10\n"));
auto conn = db.db().connect();
REQUIRE(0 == conn.get_count(with_schema("osm2pgsql_test_point", options)));
// give the node a tag...
options.append = true;
REQUIRE_NOTHROW(
db.run_import(options, "n10 v2 dV x10 y10 Tamenity=restaurant\n"));
REQUIRE(1 == conn.get_count(with_schema("osm2pgsql_test_point", options)));
REQUIRE(1 ==
conn.get_count(with_schema("osm2pgsql_test_point", options),
"node_id = 10 AND tags->'amenity' = 'restaurant'"));
SECTION("remove the tag from node")
{
REQUIRE_NOTHROW(db.run_import(options, "n10 v3 dV x10 y10\n"));
}
SECTION("delete the node")
{
REQUIRE_NOTHROW(db.run_import(options, "n10 v3 dD\n"));
}
REQUIRE(0 == conn.get_count(with_schema("osm2pgsql_test_point", options)));
}
TEMPLATE_TEST_CASE("updating a way", "", options_slim_default,
options_slim_expire, options_slim_schema)
{
options_t options = TestType::options();
// import a simple way...
REQUIRE_NOTHROW(db.run_import(options,
"n10 v1 dV x10.0 y10.1\n"
"n11 v1 dV x10.1 y10.2\n"
"w20 v1 dV Thighway=primary Nn10,n11\n"));
auto conn = db.db().connect();
REQUIRE(0 == conn.get_count(with_schema("osm2pgsql_test_point", options)));
REQUIRE(1 == conn.get_count(with_schema("osm2pgsql_test_line", options)));
REQUIRE(1 == conn.get_count(with_schema("osm2pgsql_test_line", options),
"osm_id = 20 AND tags->'highway' = 'primary' "
"AND ST_NumPoints(geom) = 2"));
// now change the way itself...
options.append = true;
REQUIRE_NOTHROW(
db.run_import(options, "w20 v2 dV Thighway=secondary Nn10,n11\n"));
REQUIRE(0 == conn.get_count(with_schema("osm2pgsql_test_point", options)));
REQUIRE(1 == conn.get_count(with_schema("osm2pgsql_test_line", options)));
REQUIRE(1 == conn.get_count(with_schema("osm2pgsql_test_line", options),
"osm_id = 20 AND tags->'highway' = "
"'secondary' AND ST_NumPoints(geom) = 2"));
// now change a node in the way...
REQUIRE_NOTHROW(db.run_import(options, "n10 v2 dV x10.0 y10.3\n"));
REQUIRE(0 == conn.get_count(with_schema("osm2pgsql_test_point", options)));
REQUIRE(1 == conn.get_count(with_schema("osm2pgsql_test_line", options)));
REQUIRE(1 == conn.get_count(with_schema("osm2pgsql_test_line", options),
"osm_id = 20 AND tags->'highway' = "
"'secondary' AND ST_NumPoints(geom) = 2"));
// now add a node to the way...
REQUIRE_NOTHROW(db.run_import(
options, "n12 v1 dV x10.2 y10.1\n"
"w20 v3 dV Thighway=residential Nn10,n11,n12\n"));
REQUIRE(0 == conn.get_count(with_schema("osm2pgsql_test_point", options)));
REQUIRE(1 == conn.get_count(with_schema("osm2pgsql_test_line", options)));
REQUIRE(1 == conn.get_count(with_schema("osm2pgsql_test_line", options),
"osm_id = 20 AND tags->'highway' = "
"'residential' AND ST_NumPoints(geom) = 3"));
// now delete the way...
REQUIRE_NOTHROW(db.run_import(options, "w20 v4 dD\n"));
REQUIRE(0 == conn.get_count(with_schema("osm2pgsql_test_point", options)));
REQUIRE(0 == conn.get_count(with_schema("osm2pgsql_test_line", options)));
}
TEMPLATE_TEST_CASE("ways as linestrings and polygons", "", options_slim_default,
options_slim_expire, options_slim_schema)
{
options_t options = TestType::options();
// import a simple way...
REQUIRE_NOTHROW(db.run_import(
options, "n10 v1 dV x10.0 y10.0\n"
"n11 v1 dV x10.0 y10.2\n"
"n12 v1 dV x10.2 y10.2\n"
"n13 v1 dV x10.2 y10.0\n"
"w20 v1 dV Tbuilding=yes Nn10,n11,n12,n13,n10\n"));
auto conn = db.db().connect();
REQUIRE(0 == conn.get_count(with_schema("osm2pgsql_test_point", options)));
REQUIRE(0 == conn.get_count(with_schema("osm2pgsql_test_line", options)));
REQUIRE(1 ==
conn.get_count(with_schema("osm2pgsql_test_polygon", options)));
REQUIRE(1 == conn.get_count(with_schema("osm2pgsql_test_polygon", options),
"osm_id = 20 AND tags->'building' = 'yes' AND "
"ST_GeometryType(geom) = 'ST_Polygon'"));
// now change the way tags...
options.append = true;
REQUIRE_NOTHROW(db.run_import(
options, "w20 v2 dV Thighway=secondary Nn10,n11,n12,n13,n10\n"));
REQUIRE(0 == conn.get_count(with_schema("osm2pgsql_test_point", options)));
REQUIRE(1 == conn.get_count(with_schema("osm2pgsql_test_line", options)));
REQUIRE(1 ==
conn.get_count(with_schema("osm2pgsql_test_line", options),
"osm_id = 20 AND tags->'highway' = 'secondary' AND "
"ST_GeometryType(geom) = 'ST_LineString'"));
REQUIRE(0 ==
conn.get_count(with_schema("osm2pgsql_test_polygon", options)));
// now remove a node from the way...
REQUIRE_NOTHROW(db.run_import(
options, "w20 v3 dV Thighway=secondary Nn10,n11,n12,n13\n"));
REQUIRE(0 == conn.get_count(with_schema("osm2pgsql_test_point", options)));
REQUIRE(1 == conn.get_count(with_schema("osm2pgsql_test_line", options)));
REQUIRE(1 ==
conn.get_count(with_schema("osm2pgsql_test_line", options),
"osm_id = 20 AND tags->'highway' = 'secondary' AND "
"ST_GeometryType(geom) = 'ST_LineString'"));
REQUIRE(0 ==
conn.get_count(with_schema("osm2pgsql_test_polygon", options)));
// now change the tag back to an area tag (but the way is not closed)...
REQUIRE_NOTHROW(
db.run_import(options, "w20 v4 dV Tbuilding=yes Nn10,n11,n12,n13\n"));
REQUIRE(0 == conn.get_count(with_schema("osm2pgsql_test_point", options)));
REQUIRE(0 == conn.get_count(with_schema("osm2pgsql_test_line", options)));
REQUIRE(0 ==
conn.get_count(with_schema("osm2pgsql_test_polygon", options)));
// now close the way again
REQUIRE_NOTHROW(db.run_import(
options, "w20 v5 dV Tbuilding=yes Nn10,n11,n12,n13,n10\n"));
REQUIRE(0 == conn.get_count(with_schema("osm2pgsql_test_point", options)));
REQUIRE(0 == conn.get_count(with_schema("osm2pgsql_test_line", options)));
REQUIRE(1 ==
conn.get_count(with_schema("osm2pgsql_test_polygon", options)));
REQUIRE(1 == conn.get_count(with_schema("osm2pgsql_test_polygon", options),
"osm_id = 20 AND tags->'building' = 'yes' AND "
"ST_GeometryType(geom) = 'ST_Polygon'"));
}
TEMPLATE_TEST_CASE("multipolygons", "", options_slim_default,
options_slim_expire, options_slim_schema)
{
options_t options = TestType::options();
// import a simple multipolygon relation...
REQUIRE_NOTHROW(db.run_import(
options, "n10 v1 dV x10.0 y10.0\n"
"n11 v1 dV x10.0 y10.2\n"
"n12 v1 dV x10.2 y10.2\n"
"n13 v1 dV x10.2 y10.0\n"
"w20 v1 dV Nn10,n11,n12,n13,n10\n"
"r30 v1 dV Ttype=multipolygon,building=yes Mw20@\n"));
auto conn = db.db().connect();
REQUIRE(0 == conn.get_count(with_schema("osm2pgsql_test_point", options)));
REQUIRE(0 == conn.get_count(with_schema("osm2pgsql_test_line", options)));
REQUIRE(1 ==
conn.get_count(with_schema("osm2pgsql_test_polygon", options)));
REQUIRE(1 == conn.get_count(with_schema("osm2pgsql_test_polygon", options),
"osm_id = -30 AND tags->'building' = 'yes' AND "
"ST_GeometryType(geom) = 'ST_Polygon'"));
// change tags on that relation...
options.append = true;
REQUIRE_NOTHROW(db.run_import(
options,
"r30 v2 dV Ttype=multipolygon,building=yes,name=Shed Mw20@\n"));
REQUIRE(0 == conn.get_count(with_schema("osm2pgsql_test_point", options)));
REQUIRE(0 == conn.get_count(with_schema("osm2pgsql_test_line", options)));
REQUIRE(1 ==
conn.get_count(with_schema("osm2pgsql_test_polygon", options)));
REQUIRE(1 == conn.get_count(with_schema("osm2pgsql_test_polygon", options),
"osm_id = -30 AND tags->'building' = 'yes' AND "
"ST_GeometryType(geom) = 'ST_Polygon'"));
SECTION("remove relation")
{
REQUIRE_NOTHROW(db.run_import(options, "r30 v3 dD\n"));
}
SECTION("remove multipolygon tag")
{
REQUIRE_NOTHROW(db.run_import(
options, "r30 v3 dV Tbuilding=yes,name=Shed Mw20@\n"));
}
REQUIRE(0 == conn.get_count(with_schema("osm2pgsql_test_point", options)));
REQUIRE(0 == conn.get_count(with_schema("osm2pgsql_test_line", options)));
REQUIRE(0 ==
conn.get_count(with_schema("osm2pgsql_test_polygon", options)));
}
|