Skip to content

Commit 82460e9

Browse files
committed
Add tests to make sure no schema except specified one is touched
1 parent a3a21a2 commit 82460e9

File tree

4 files changed

+127
-54
lines changed

4 files changed

+127
-54
lines changed

src/pgsql-params.hpp

+7
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,13 @@ class connection_params_t
3434

3535
auto end() const noexcept { return m_params.end(); }
3636

37+
void merge_with(connection_params_t const &other)
38+
{
39+
for (auto const &p : other.m_params) {
40+
m_params[p.first] = p.second;
41+
}
42+
}
43+
3744
private:
3845
std::map<std::string, std::string> m_params;
3946

tests/common-import.hpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ class import_t
131131
std::initializer_list<std::string> input_data,
132132
std::string const &format = "opl")
133133
{
134-
options.connection_params = m_db.connection_params();
134+
options.connection_params.merge_with(m_db.connection_params());
135135

136136
properties_t const properties{options.connection_params,
137137
options.middle_dbschema};
@@ -165,7 +165,7 @@ class import_t
165165

166166
void run_file(options_t options, char const *file = nullptr)
167167
{
168-
options.connection_params = m_db.connection_params();
168+
options.connection_params.merge_with(m_db.connection_params());
169169

170170
properties_t const properties{options.connection_params,
171171
options.middle_dbschema};

tests/common-options.hpp

+14
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,20 @@ class opt_t
8989
return *this;
9090
}
9191

92+
opt_t &schema(char const *schema_name)
93+
{
94+
m_opt.dbschema = schema_name;
95+
m_opt.middle_dbschema = schema_name;
96+
m_opt.output_dbschema = schema_name;
97+
return *this;
98+
}
99+
100+
opt_t &user(char const *user)
101+
{
102+
m_opt.connection_params.set("user", user);
103+
return *this;
104+
}
105+
92106
private:
93107
options_t m_opt;
94108
};

tests/test-output-flex-update.cpp

+104-52
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
#include "common-import.hpp"
1313
#include "common-options.hpp"
14+
#include "common-pg.hpp"
1415

1516
namespace {
1617

@@ -38,8 +39,51 @@ struct options_slim_expire
3839
}
3940
};
4041

42+
struct options_slim_schema
43+
{
44+
static options_t options()
45+
{
46+
auto conn = db.db().connect();
47+
// Create limited user (if it doesn't exist yet),
48+
// which we need to test that the public schema won't be touched.
49+
// If the public schema is tried to be modified at any point, this user won't have the
50+
// necessary permissions, and hence the test will fail.
51+
conn.exec(R"(
52+
DO
53+
$$
54+
BEGIN
55+
IF NOT EXISTS (SELECT FROM pg_catalog.pg_roles WHERE rolname = 'limited') THEN
56+
CREATE ROLE limited LOGIN;
57+
END IF;
58+
END
59+
$$;
60+
)");
61+
conn.exec("REVOKE ALL PRIVILEGES ON ALL TABLES IN SCHEMA public FROM "
62+
"PUBLIC, limited;");
63+
conn.exec("REVOKE CREATE ON SCHEMA public FROM PUBLIC, limited;");
64+
conn.exec(
65+
"CREATE SCHEMA IF NOT EXISTS myschema AUTHORIZATION limited;");
66+
conn.close();
67+
return testing::opt_t()
68+
.slim()
69+
.flex(conf_file)
70+
.schema("myschema")
71+
.user("limited");
72+
}
73+
};
74+
75+
// Return a string with the schema name prepended to the table name.
76+
std::string with_schema(char const *name, options_t const *options)
77+
{
78+
if (options->dbschema.empty()) {
79+
return std::string(name);
80+
}
81+
log_info(options->dbschema.c_str());
82+
return options->dbschema + "." + name;
83+
}
84+
4185
TEMPLATE_TEST_CASE("updating a node", "", options_slim_default,
42-
options_slim_expire)
86+
options_slim_expire, options_slim_schema)
4387
{
4488
options_t options = TestType::options();
4589

@@ -48,16 +92,16 @@ TEMPLATE_TEST_CASE("updating a node", "", options_slim_default,
4892

4993
auto conn = db.db().connect();
5094

51-
REQUIRE(0 == conn.get_count("osm2pgsql_test_point"));
95+
REQUIRE(0 == conn.get_count(with_schema("osm2pgsql_test_point", &options)));
5296

5397
// give the node a tag...
5498
options.append = true;
5599
REQUIRE_NOTHROW(
56100
db.run_import(options, "n10 v2 dV x10 y10 Tamenity=restaurant\n"));
57101

58-
REQUIRE(1 == conn.get_count("osm2pgsql_test_point"));
102+
REQUIRE(1 == conn.get_count(with_schema("osm2pgsql_test_point", &options)));
59103
REQUIRE(1 ==
60-
conn.get_count("osm2pgsql_test_point",
104+
conn.get_count(with_schema("osm2pgsql_test_point", &options),
61105
"node_id = 10 AND tags->'amenity' = 'restaurant'"));
62106

63107
SECTION("remove the tag from node")
@@ -70,11 +114,11 @@ TEMPLATE_TEST_CASE("updating a node", "", options_slim_default,
70114
REQUIRE_NOTHROW(db.run_import(options, "n10 v3 dD\n"));
71115
}
72116

73-
REQUIRE(0 == conn.get_count("osm2pgsql_test_point"));
117+
REQUIRE(0 == conn.get_count(with_schema("osm2pgsql_test_point", &options)));
74118
}
75119

76120
TEMPLATE_TEST_CASE("updating a way", "", options_slim_default,
77-
options_slim_expire)
121+
options_slim_expire, options_slim_schema)
78122
{
79123
options_t options = TestType::options();
80124

@@ -86,9 +130,9 @@ TEMPLATE_TEST_CASE("updating a way", "", options_slim_default,
86130

87131
auto conn = db.db().connect();
88132

89-
REQUIRE(0 == conn.get_count("osm2pgsql_test_point"));
90-
REQUIRE(1 == conn.get_count("osm2pgsql_test_line"));
91-
REQUIRE(1 == conn.get_count("osm2pgsql_test_line",
133+
REQUIRE(0 == conn.get_count(with_schema("osm2pgsql_test_point", &options)));
134+
REQUIRE(1 == conn.get_count(with_schema("osm2pgsql_test_line", &options)));
135+
REQUIRE(1 == conn.get_count(with_schema("osm2pgsql_test_line", &options),
92136
"osm_id = 20 AND tags->'highway' = 'primary' "
93137
"AND ST_NumPoints(geom) = 2"));
94138

@@ -97,18 +141,18 @@ TEMPLATE_TEST_CASE("updating a way", "", options_slim_default,
97141
REQUIRE_NOTHROW(
98142
db.run_import(options, "w20 v2 dV Thighway=secondary Nn10,n11\n"));
99143

100-
REQUIRE(0 == conn.get_count("osm2pgsql_test_point"));
101-
REQUIRE(1 == conn.get_count("osm2pgsql_test_line"));
102-
REQUIRE(1 == conn.get_count("osm2pgsql_test_line",
144+
REQUIRE(0 == conn.get_count(with_schema("osm2pgsql_test_point", &options)));
145+
REQUIRE(1 == conn.get_count(with_schema("osm2pgsql_test_line", &options)));
146+
REQUIRE(1 == conn.get_count(with_schema("osm2pgsql_test_line", &options),
103147
"osm_id = 20 AND tags->'highway' = "
104148
"'secondary' AND ST_NumPoints(geom) = 2"));
105149

106150
// now change a node in the way...
107151
REQUIRE_NOTHROW(db.run_import(options, "n10 v2 dV x10.0 y10.3\n"));
108152

109-
REQUIRE(0 == conn.get_count("osm2pgsql_test_point"));
110-
REQUIRE(1 == conn.get_count("osm2pgsql_test_line"));
111-
REQUIRE(1 == conn.get_count("osm2pgsql_test_line",
153+
REQUIRE(0 == conn.get_count(with_schema("osm2pgsql_test_point", &options)));
154+
REQUIRE(1 == conn.get_count(with_schema("osm2pgsql_test_line", &options)));
155+
REQUIRE(1 == conn.get_count(with_schema("osm2pgsql_test_line", &options),
112156
"osm_id = 20 AND tags->'highway' = "
113157
"'secondary' AND ST_NumPoints(geom) = 2"));
114158

@@ -117,21 +161,21 @@ TEMPLATE_TEST_CASE("updating a way", "", options_slim_default,
117161
options, "n12 v1 dV x10.2 y10.1\n"
118162
"w20 v3 dV Thighway=residential Nn10,n11,n12\n"));
119163

120-
REQUIRE(0 == conn.get_count("osm2pgsql_test_point"));
121-
REQUIRE(1 == conn.get_count("osm2pgsql_test_line"));
122-
REQUIRE(1 == conn.get_count("osm2pgsql_test_line",
164+
REQUIRE(0 == conn.get_count(with_schema("osm2pgsql_test_point", &options)));
165+
REQUIRE(1 == conn.get_count(with_schema("osm2pgsql_test_line", &options)));
166+
REQUIRE(1 == conn.get_count(with_schema("osm2pgsql_test_line", &options),
123167
"osm_id = 20 AND tags->'highway' = "
124168
"'residential' AND ST_NumPoints(geom) = 3"));
125169

126170
// now delete the way...
127171
REQUIRE_NOTHROW(db.run_import(options, "w20 v4 dD\n"));
128172

129-
REQUIRE(0 == conn.get_count("osm2pgsql_test_point"));
130-
REQUIRE(0 == conn.get_count("osm2pgsql_test_line"));
173+
REQUIRE(0 == conn.get_count(with_schema("osm2pgsql_test_point", &options)));
174+
REQUIRE(0 == conn.get_count(with_schema("osm2pgsql_test_line", &options)));
131175
}
132176

133177
TEMPLATE_TEST_CASE("ways as linestrings and polygons", "", options_slim_default,
134-
options_slim_expire)
178+
options_slim_expire, options_slim_schema)
135179
{
136180
options_t options = TestType::options();
137181

@@ -145,10 +189,11 @@ TEMPLATE_TEST_CASE("ways as linestrings and polygons", "", options_slim_default,
145189

146190
auto conn = db.db().connect();
147191

148-
REQUIRE(0 == conn.get_count("osm2pgsql_test_point"));
149-
REQUIRE(0 == conn.get_count("osm2pgsql_test_line"));
150-
REQUIRE(1 == conn.get_count("osm2pgsql_test_polygon"));
151-
REQUIRE(1 == conn.get_count("osm2pgsql_test_polygon",
192+
REQUIRE(0 == conn.get_count(with_schema("osm2pgsql_test_point", &options)));
193+
REQUIRE(0 == conn.get_count(with_schema("osm2pgsql_test_line", &options)));
194+
REQUIRE(1 ==
195+
conn.get_count(with_schema("osm2pgsql_test_polygon", &options)));
196+
REQUIRE(1 == conn.get_count(with_schema("osm2pgsql_test_polygon", &options),
152197
"osm_id = 20 AND tags->'building' = 'yes' AND "
153198
"ST_GeometryType(geom) = 'ST_Polygon'"));
154199

@@ -157,48 +202,52 @@ TEMPLATE_TEST_CASE("ways as linestrings and polygons", "", options_slim_default,
157202
REQUIRE_NOTHROW(db.run_import(
158203
options, "w20 v2 dV Thighway=secondary Nn10,n11,n12,n13,n10\n"));
159204

160-
REQUIRE(0 == conn.get_count("osm2pgsql_test_point"));
161-
REQUIRE(1 == conn.get_count("osm2pgsql_test_line"));
205+
REQUIRE(0 == conn.get_count(with_schema("osm2pgsql_test_point", &options)));
206+
REQUIRE(1 == conn.get_count(with_schema("osm2pgsql_test_line", &options)));
162207
REQUIRE(1 ==
163-
conn.get_count("osm2pgsql_test_line",
208+
conn.get_count(with_schema("osm2pgsql_test_line", &options),
164209
"osm_id = 20 AND tags->'highway' = 'secondary' AND "
165210
"ST_GeometryType(geom) = 'ST_LineString'"));
166-
REQUIRE(0 == conn.get_count("osm2pgsql_test_polygon"));
211+
REQUIRE(0 ==
212+
conn.get_count(with_schema("osm2pgsql_test_polygon", &options)));
167213

168214
// now remove a node from the way...
169215
REQUIRE_NOTHROW(db.run_import(
170216
options, "w20 v3 dV Thighway=secondary Nn10,n11,n12,n13\n"));
171217

172-
REQUIRE(0 == conn.get_count("osm2pgsql_test_point"));
173-
REQUIRE(1 == conn.get_count("osm2pgsql_test_line"));
218+
REQUIRE(0 == conn.get_count(with_schema("osm2pgsql_test_point", &options)));
219+
REQUIRE(1 == conn.get_count(with_schema("osm2pgsql_test_line", &options)));
174220
REQUIRE(1 ==
175-
conn.get_count("osm2pgsql_test_line",
221+
conn.get_count(with_schema("osm2pgsql_test_line", &options),
176222
"osm_id = 20 AND tags->'highway' = 'secondary' AND "
177223
"ST_GeometryType(geom) = 'ST_LineString'"));
178-
REQUIRE(0 == conn.get_count("osm2pgsql_test_polygon"));
224+
REQUIRE(0 ==
225+
conn.get_count(with_schema("osm2pgsql_test_polygon", &options)));
179226

180227
// now change the tag back to an area tag (but the way is not closed)...
181228
REQUIRE_NOTHROW(
182229
db.run_import(options, "w20 v4 dV Tbuilding=yes Nn10,n11,n12,n13\n"));
183230

184-
REQUIRE(0 == conn.get_count("osm2pgsql_test_point"));
185-
REQUIRE(0 == conn.get_count("osm2pgsql_test_line"));
186-
REQUIRE(0 == conn.get_count("osm2pgsql_test_polygon"));
231+
REQUIRE(0 == conn.get_count(with_schema("osm2pgsql_test_point", &options)));
232+
REQUIRE(0 == conn.get_count(with_schema("osm2pgsql_test_line", &options)));
233+
REQUIRE(0 ==
234+
conn.get_count(with_schema("osm2pgsql_test_polygon", &options)));
187235

188236
// now close the way again
189237
REQUIRE_NOTHROW(db.run_import(
190238
options, "w20 v5 dV Tbuilding=yes Nn10,n11,n12,n13,n10\n"));
191239

192-
REQUIRE(0 == conn.get_count("osm2pgsql_test_point"));
193-
REQUIRE(0 == conn.get_count("osm2pgsql_test_line"));
194-
REQUIRE(1 == conn.get_count("osm2pgsql_test_polygon"));
195-
REQUIRE(1 == conn.get_count("osm2pgsql_test_polygon",
240+
REQUIRE(0 == conn.get_count(with_schema("osm2pgsql_test_point", &options)));
241+
REQUIRE(0 == conn.get_count(with_schema("osm2pgsql_test_line", &options)));
242+
REQUIRE(1 ==
243+
conn.get_count(with_schema("osm2pgsql_test_polygon", &options)));
244+
REQUIRE(1 == conn.get_count(with_schema("osm2pgsql_test_polygon", &options),
196245
"osm_id = 20 AND tags->'building' = 'yes' AND "
197246
"ST_GeometryType(geom) = 'ST_Polygon'"));
198247
}
199248

200249
TEMPLATE_TEST_CASE("multipolygons", "", options_slim_default,
201-
options_slim_expire)
250+
options_slim_expire, options_slim_schema)
202251
{
203252
options_t options = TestType::options();
204253

@@ -213,10 +262,11 @@ TEMPLATE_TEST_CASE("multipolygons", "", options_slim_default,
213262

214263
auto conn = db.db().connect();
215264

216-
REQUIRE(0 == conn.get_count("osm2pgsql_test_point"));
217-
REQUIRE(0 == conn.get_count("osm2pgsql_test_line"));
218-
REQUIRE(1 == conn.get_count("osm2pgsql_test_polygon"));
219-
REQUIRE(1 == conn.get_count("osm2pgsql_test_polygon",
265+
REQUIRE(0 == conn.get_count(with_schema("osm2pgsql_test_point", &options)));
266+
REQUIRE(0 == conn.get_count(with_schema("osm2pgsql_test_line", &options)));
267+
REQUIRE(1 ==
268+
conn.get_count(with_schema("osm2pgsql_test_polygon", &options)));
269+
REQUIRE(1 == conn.get_count(with_schema("osm2pgsql_test_polygon", &options),
220270
"osm_id = -30 AND tags->'building' = 'yes' AND "
221271
"ST_GeometryType(geom) = 'ST_Polygon'"));
222272

@@ -226,10 +276,11 @@ TEMPLATE_TEST_CASE("multipolygons", "", options_slim_default,
226276
options,
227277
"r30 v2 dV Ttype=multipolygon,building=yes,name=Shed Mw20@\n"));
228278

229-
REQUIRE(0 == conn.get_count("osm2pgsql_test_point"));
230-
REQUIRE(0 == conn.get_count("osm2pgsql_test_line"));
231-
REQUIRE(1 == conn.get_count("osm2pgsql_test_polygon"));
232-
REQUIRE(1 == conn.get_count("osm2pgsql_test_polygon",
279+
REQUIRE(0 == conn.get_count(with_schema("osm2pgsql_test_point", &options)));
280+
REQUIRE(0 == conn.get_count(with_schema("osm2pgsql_test_line", &options)));
281+
REQUIRE(1 ==
282+
conn.get_count(with_schema("osm2pgsql_test_polygon", &options)));
283+
REQUIRE(1 == conn.get_count(with_schema("osm2pgsql_test_polygon", &options),
233284
"osm_id = -30 AND tags->'building' = 'yes' AND "
234285
"ST_GeometryType(geom) = 'ST_Polygon'"));
235286

@@ -244,7 +295,8 @@ TEMPLATE_TEST_CASE("multipolygons", "", options_slim_default,
244295
options, "r30 v3 dV Tbuilding=yes,name=Shed Mw20@\n"));
245296
}
246297

247-
REQUIRE(0 == conn.get_count("osm2pgsql_test_point"));
248-
REQUIRE(0 == conn.get_count("osm2pgsql_test_line"));
249-
REQUIRE(0 == conn.get_count("osm2pgsql_test_polygon"));
298+
REQUIRE(0 == conn.get_count(with_schema("osm2pgsql_test_point", &options)));
299+
REQUIRE(0 == conn.get_count(with_schema("osm2pgsql_test_line", &options)));
300+
REQUIRE(0 ==
301+
conn.get_count(with_schema("osm2pgsql_test_polygon", &options)));
250302
}

0 commit comments

Comments
 (0)