1 /**
2  * SPDX-License-Identifier: GPL-2.0-or-later
3  *
4  * This file is part of osm2pgsql (https://osm2pgsql.org/).
5  *
6  * Copyright (C) 2006-2021 by the osm2pgsql developer community.
7  * For a full list of authors see the git log.
8  */
9 
10 #include <catch.hpp>
11 
12 #include "common-import.hpp"
13 #include "common-options.hpp"
14 
15 static testing::db::import_t db;
16 
17 static char const *const conf_file = "test_output_flex_way.lua";
18 
19 static char const *const tdata[] = {
20     "n10 v1 dV x10.0 y10.0",
21     "n11 v1 dV x10.0 y10.1",
22     "n12 v1 dV x10.1 y10.0",
23     "n13 v1 dV x10.1 y10.1",
24     "n14 v1 dV x10.2 y10.0",
25     "n15 v1 dV x10.2 y10.1",
26     "n16 v1 dV x10.3 y10.0",
27     "n17 v1 dV x10.3 y10.1",
28     "n18 v1 dV x10.4 y10.0",
29     "n19 v1 dV x10.4 y10.1",
30     "w11 v1 dV Tt1=yes Nn12,n13",
31     "w12 v1 dV Tt2=yes Nn14,n15",
32     "w13 v1 dV Ttboth=yes Nn16,n17",
33     "w14 v1 dV Ttboth=yes Nn18,n19",
34     "w15 v1 dV Tt=ag Nn17,n19",
35     "r30 v1 dV Tt=ag Mw10@,w11@,w12@mark,w13@,w14@mark"};
36 
37 // adding relation (marked), also check case where relation changed to contain
38 // the way under test
39 
40 TEST_CASE("test way: add relation with way in t1 (marked)")
41 {
42     options_t options = testing::opt_t().slim().flex(conf_file);
43 
44     testing::db::data_t data{tdata};
45 
46     data.add("w10 v1 dV Tt1=yes,t2=yes Nn10,n11");
47 
48     SECTION("no relation") {}
49 
50     SECTION("relation without w10")
51     {
52         data.add("r32 v1 dV Tt=ag Mw11@,w12@,w13@,w14@,w15@");
53     }
54 
55     REQUIRE_NOTHROW(db.run_import(options, data()));
56 
57     auto conn = db.db().connect();
58 
59     CHECK(2 == conn.get_count("osm2pgsql_test_t1"));
60     CHECK(1 == conn.get_count("osm2pgsql_test_t1", "way_id = 10"));
61     CHECK(1 == conn.get_count("osm2pgsql_test_t2"));
62     CHECK(0 == conn.get_count("osm2pgsql_test_t2", "way_id = 10"));
63     CHECK(2 == conn.get_count("osm2pgsql_test_tboth"));
64     CHECK(0 == conn.get_count("osm2pgsql_test_tboth", "way_id = 10"));
65 
66     options.append = true;
67 
68     REQUIRE_NOTHROW(db.run_import(
69         options, "r32 v2 dV Tt=ag Mw10@mark,w11@,w12@,w13@,w14@,w15@\n"));
70 
71     CHECK(2 == conn.get_count("osm2pgsql_test_t1"));
72     CHECK(1 == conn.get_count("osm2pgsql_test_t1", "way_id = 10"));
73     CHECK(2 == conn.get_count("osm2pgsql_test_t2"));
74     CHECK(1 == conn.get_count("osm2pgsql_test_t2",
75                               "way_id = 10 AND rel_ids = '{32}'"));
76     CHECK(2 == conn.get_count("osm2pgsql_test_tboth"));
77     CHECK(0 == conn.get_count("osm2pgsql_test_tboth", "way_id = 10"));
78 }
79 
80 TEST_CASE("test way: add relation with way in t2 (marked)")
81 {
82     options_t options = testing::opt_t().slim().flex(conf_file);
83 
84     testing::db::data_t data{tdata};
85 
86     data.add("w10 v1 dV Tt2=yes Nn10,n11");
87     data.add("r31 v1 dV Tt=ag Mw10@mark,w11@,w12@,w13@,w14@");
88 
89     REQUIRE_NOTHROW(db.run_import(options, data()));
90 
91     auto conn = db.db().connect();
92 
93     CHECK(1 == conn.get_count("osm2pgsql_test_t1"));
94     CHECK(0 == conn.get_count("osm2pgsql_test_t1", "way_id = 10"));
95     CHECK(2 == conn.get_count("osm2pgsql_test_t2"));
96     CHECK(1 == conn.get_count("osm2pgsql_test_t2",
97                               "way_id = 10 AND rel_ids = '{31}'"));
98     CHECK(2 == conn.get_count("osm2pgsql_test_tboth"));
99     CHECK(0 == conn.get_count("osm2pgsql_test_tboth", "way_id = 10"));
100 
101     options.append = true;
102 
103     REQUIRE_NOTHROW(db.run_import(
104         options, "r32 v2 dV Tt=ag Mw10@mark,w11@,w12@,w13@,w14@,w15@\n"));
105 
106     CHECK(1 == conn.get_count("osm2pgsql_test_t1"));
107     CHECK(0 == conn.get_count("osm2pgsql_test_t1", "way_id = 10"));
108     CHECK(2 == conn.get_count("osm2pgsql_test_t2"));
109     CHECK(1 == conn.get_count("osm2pgsql_test_t2",
110                               "way_id = 10 AND rel_ids = '{31,32}'"));
111     CHECK(2 == conn.get_count("osm2pgsql_test_tboth"));
112     CHECK(0 == conn.get_count("osm2pgsql_test_tboth", "way_id = 10"));
113 }
114 
115 TEST_CASE("test way: add relation with way in t1 and t2 (marked)")
116 {
117     options_t options = testing::opt_t().slim().flex(conf_file);
118 
119     testing::db::data_t data{tdata};
120 
121     data.add("w10 v1 dV Tt1=yes,t2=yes Nn10,n11");
122     data.add("r31 v1 dV Tt=ag Mw10@mark,w11@,w12@,w13@,w14@");
123 
124     REQUIRE_NOTHROW(db.run_import(options, data()));
125 
126     auto conn = db.db().connect();
127 
128     CHECK(2 == conn.get_count("osm2pgsql_test_t1"));
129     CHECK(1 == conn.get_count("osm2pgsql_test_t1", "way_id = 10"));
130     CHECK(2 == conn.get_count("osm2pgsql_test_t2"));
131     CHECK(1 == conn.get_count("osm2pgsql_test_t2",
132                               "way_id = 10 AND rel_ids = '{31}'"));
133     CHECK(2 == conn.get_count("osm2pgsql_test_tboth"));
134     CHECK(0 == conn.get_count("osm2pgsql_test_tboth", "way_id = 10"));
135 
136     options.append = true;
137 
138     REQUIRE_NOTHROW(db.run_import(
139         options, "r32 v2 dV Tt=ag Mw10@mark,w11@,w12@,w13@,w14@,w15@\n"));
140 
141     CHECK(2 == conn.get_count("osm2pgsql_test_t1"));
142     CHECK(1 == conn.get_count("osm2pgsql_test_t1", "way_id = 10"));
143     CHECK(2 == conn.get_count("osm2pgsql_test_t2"));
144     CHECK(1 == conn.get_count("osm2pgsql_test_t2",
145                               "way_id = 10 AND rel_ids = '{31,32}'"));
146     CHECK(2 == conn.get_count("osm2pgsql_test_tboth"));
147     CHECK(0 == conn.get_count("osm2pgsql_test_tboth", "way_id = 10"));
148 }
149 
150 TEST_CASE("test way: add (to) relation with way in tboth stage 1 (marked)")
151 {
152     options_t options = testing::opt_t().slim().flex(conf_file);
153 
154     testing::db::data_t data{tdata};
155 
156     data.add("w10 v1 dV Ttboth=yes Nn10,n11");
157 
158     SECTION("adding relation")
159     {
160         data.add("r31 v1 dV Tt=ag Mw10@,w11@,w12@,w13@,w14@");
161     }
162 
163     SECTION("changing relation")
164     {
165         data.add("r32 v1 dV Tt=ag Mw10@,w11@,w12@,w13@,w14@,w15@");
166     }
167 
168     REQUIRE_NOTHROW(db.run_import(options, data()));
169 
170     auto conn = db.db().connect();
171 
172     CHECK(1 == conn.get_count("osm2pgsql_test_t1"));
173     CHECK(0 == conn.get_count("osm2pgsql_test_t1", "way_id = 10"));
174     CHECK(1 == conn.get_count("osm2pgsql_test_t2"));
175     CHECK(0 == conn.get_count("osm2pgsql_test_t2", "way_id = 10"));
176     CHECK(3 == conn.get_count("osm2pgsql_test_tboth"));
177     CHECK(1 == conn.get_count("osm2pgsql_test_tboth", "way_id = 10"));
178     CHECK(1 == conn.get_count("osm2pgsql_test_tboth",
179                               "way_id = 10 AND rel_ids IS NULL"));
180 
181     options.append = true;
182 
183     REQUIRE_NOTHROW(db.run_import(
184         options, "r32 v2 dV Tt=ag Mw10@mark,w11@,w12@,w13@,w14@,w15@\n"));
185 
186     CHECK(1 == conn.get_count("osm2pgsql_test_t1"));
187     CHECK(0 == conn.get_count("osm2pgsql_test_t1", "way_id = 10"));
188     CHECK(1 == conn.get_count("osm2pgsql_test_t2"));
189     CHECK(0 == conn.get_count("osm2pgsql_test_t2", "way_id = 10"));
190     CHECK(3 == conn.get_count("osm2pgsql_test_tboth"));
191     CHECK(1 == conn.get_count("osm2pgsql_test_tboth", "way_id = 10"));
192     CHECK(1 == conn.get_count("osm2pgsql_test_tboth",
193                               "way_id = 10 AND rel_ids = '{32}'"));
194 }
195 
196 TEST_CASE("test way: add relation with way in tboth stage 2 (marked)")
197 {
198     options_t options = testing::opt_t().slim().flex(conf_file);
199 
200     testing::db::data_t data{tdata};
201 
202     data.add("w10 v1 dV Ttboth=yes Nn10,n11");
203     data.add("r31 v1 dV Tt=ag Mw10@mark,w11@,w12@,w13@,w14@");
204 
205     REQUIRE_NOTHROW(db.run_import(options, data()));
206 
207     auto conn = db.db().connect();
208 
209     CHECK(1 == conn.get_count("osm2pgsql_test_t1"));
210     CHECK(0 == conn.get_count("osm2pgsql_test_t1", "way_id = 10"));
211     CHECK(1 == conn.get_count("osm2pgsql_test_t2"));
212     CHECK(0 == conn.get_count("osm2pgsql_test_t2", "way_id = 10"));
213     CHECK(3 == conn.get_count("osm2pgsql_test_tboth"));
214     CHECK(1 == conn.get_count("osm2pgsql_test_tboth", "way_id = 10"));
215     CHECK(1 == conn.get_count("osm2pgsql_test_tboth",
216                               "way_id = 10 AND rel_ids = '{31}'"));
217 
218     options.append = true;
219 
220     REQUIRE_NOTHROW(db.run_import(
221         options, "r32 v2 dV Tt=ag Mw10@mark,w11@,w12@,w13@,w14@,w15@\n"));
222 
223     CHECK(1 == conn.get_count("osm2pgsql_test_t1"));
224     CHECK(0 == conn.get_count("osm2pgsql_test_t1", "way_id = 10"));
225     CHECK(1 == conn.get_count("osm2pgsql_test_t2"));
226     CHECK(0 == conn.get_count("osm2pgsql_test_t2", "way_id = 10"));
227     CHECK(3 == conn.get_count("osm2pgsql_test_tboth"));
228     CHECK(1 == conn.get_count("osm2pgsql_test_tboth", "way_id = 10"));
229     CHECK(1 == conn.get_count("osm2pgsql_test_tboth",
230                               "way_id = 10 AND rel_ids = '{31,32}'"));
231 }
232 
233 // adding relation (not marked), also check case where relation changed to
234 // contain the way under test
235 
236 TEST_CASE("test way: add relation with way in t1 (not marked)")
237 {
238     options_t options = testing::opt_t().slim().flex(conf_file);
239 
240     testing::db::data_t data{tdata};
241 
242     data.add("w10 v1 dV Tt1=yes,t2=yes Nn10,n11");
243 
244     SECTION("no relation") {}
245 
246     SECTION("relation without w10")
247     {
248         data.add("r32 v1 dV Tt=ag Mw11@,w12@,w13@,w14@,w15@");
249     }
250 
251     REQUIRE_NOTHROW(db.run_import(options, data()));
252 
253     auto conn = db.db().connect();
254 
255     CHECK(2 == conn.get_count("osm2pgsql_test_t1"));
256     CHECK(1 == conn.get_count("osm2pgsql_test_t1", "way_id = 10"));
257     CHECK(1 == conn.get_count("osm2pgsql_test_t2"));
258     CHECK(0 == conn.get_count("osm2pgsql_test_t2", "way_id = 10"));
259     CHECK(2 == conn.get_count("osm2pgsql_test_tboth"));
260     CHECK(0 == conn.get_count("osm2pgsql_test_tboth", "way_id = 10"));
261 
262     options.append = true;
263 
264     REQUIRE_NOTHROW(db.run_import(
265         options, "r32 v2 dV Tt=ag Mw10@,w11@,w12@,w13@,w14@,w15@\n"));
266 
267     CHECK(2 == conn.get_count("osm2pgsql_test_t1"));
268     CHECK(1 == conn.get_count("osm2pgsql_test_t1", "way_id = 10"));
269     CHECK(1 == conn.get_count("osm2pgsql_test_t2"));
270     CHECK(0 == conn.get_count("osm2pgsql_test_t2", "way_id = 10"));
271     CHECK(2 == conn.get_count("osm2pgsql_test_tboth"));
272     CHECK(0 == conn.get_count("osm2pgsql_test_tboth", "way_id = 10"));
273 }
274 
275 TEST_CASE("test way: add relation with way in t2 (not marked)")
276 {
277     options_t options = testing::opt_t().slim().flex(conf_file);
278 
279     testing::db::data_t data{tdata};
280 
281     data.add("w10 v1 dV Tt2=yes Nn10,n11");
282     data.add("r31 v1 dV Tt=ag Mw10@mark,w11@,w12@,w13@,w14@");
283 
284     REQUIRE_NOTHROW(db.run_import(options, data()));
285 
286     auto conn = db.db().connect();
287 
288     CHECK(1 == conn.get_count("osm2pgsql_test_t1"));
289     CHECK(0 == conn.get_count("osm2pgsql_test_t1", "way_id = 10"));
290     CHECK(2 == conn.get_count("osm2pgsql_test_t2"));
291     CHECK(1 == conn.get_count("osm2pgsql_test_t2",
292                               "way_id = 10 AND rel_ids = '{31}'"));
293     CHECK(2 == conn.get_count("osm2pgsql_test_tboth"));
294     CHECK(0 == conn.get_count("osm2pgsql_test_tboth", "way_id = 10"));
295 
296     options.append = true;
297 
298     REQUIRE_NOTHROW(db.run_import(
299         options, "r32 v2 dV Tt=ag Mw10@,w11@,w12@,w13@,w14@,w15@\n"));
300 
301     CHECK(1 == conn.get_count("osm2pgsql_test_t1"));
302     CHECK(0 == conn.get_count("osm2pgsql_test_t1", "way_id = 10"));
303     CHECK(2 == conn.get_count("osm2pgsql_test_t2"));
304     CHECK(1 == conn.get_count("osm2pgsql_test_t2",
305                               "way_id = 10 AND rel_ids = '{31}'"));
306     CHECK(2 == conn.get_count("osm2pgsql_test_tboth"));
307     CHECK(0 == conn.get_count("osm2pgsql_test_tboth", "way_id = 10"));
308 }
309 
310 TEST_CASE("test way: add relation with way in t1 and t2 (not marked)")
311 {
312     options_t options = testing::opt_t().slim().flex(conf_file);
313 
314     testing::db::data_t data{tdata};
315 
316     data.add("w10 v1 dV Tt1=yes,t2=yes Nn10,n11");
317     data.add("r31 v1 dV Tt=ag Mw10@mark,w11@,w12@,w13@,w14@");
318 
319     REQUIRE_NOTHROW(db.run_import(options, data()));
320 
321     auto conn = db.db().connect();
322 
323     CHECK(2 == conn.get_count("osm2pgsql_test_t1"));
324     CHECK(1 == conn.get_count("osm2pgsql_test_t1", "way_id = 10"));
325     CHECK(2 == conn.get_count("osm2pgsql_test_t2"));
326     CHECK(1 == conn.get_count("osm2pgsql_test_t2",
327                               "way_id = 10 AND rel_ids = '{31}'"));
328     CHECK(2 == conn.get_count("osm2pgsql_test_tboth"));
329     CHECK(0 == conn.get_count("osm2pgsql_test_tboth", "way_id = 10"));
330 
331     options.append = true;
332 
333     REQUIRE_NOTHROW(db.run_import(
334         options, "r32 v2 dV Tt=ag Mw10@,w11@,w12@,w13@,w14@,w15@\n"));
335 
336     CHECK(2 == conn.get_count("osm2pgsql_test_t1"));
337     CHECK(1 == conn.get_count("osm2pgsql_test_t1", "way_id = 10"));
338     CHECK(2 == conn.get_count("osm2pgsql_test_t2"));
339     CHECK(1 == conn.get_count("osm2pgsql_test_t2",
340                               "way_id = 10 AND rel_ids = '{31}'"));
341     CHECK(2 == conn.get_count("osm2pgsql_test_tboth"));
342     CHECK(0 == conn.get_count("osm2pgsql_test_tboth", "way_id = 10"));
343 }
344 
345 TEST_CASE("test way: add relation with way in tboth stage 1 (not marked)")
346 {
347     options_t options = testing::opt_t().slim().flex(conf_file);
348 
349     testing::db::data_t data{tdata};
350 
351     data.add("w10 v1 dV Ttboth=yes Nn10,n11");
352     data.add("r31 v1 dV Tt=ag Mw10@,w11@,w12@,w13@,w14@");
353 
354     REQUIRE_NOTHROW(db.run_import(options, data()));
355 
356     auto conn = db.db().connect();
357 
358     CHECK(1 == conn.get_count("osm2pgsql_test_t1"));
359     CHECK(0 == conn.get_count("osm2pgsql_test_t1", "way_id = 10"));
360     CHECK(1 == conn.get_count("osm2pgsql_test_t2"));
361     CHECK(0 == conn.get_count("osm2pgsql_test_t2", "way_id = 10"));
362     CHECK(3 == conn.get_count("osm2pgsql_test_tboth"));
363     CHECK(1 == conn.get_count("osm2pgsql_test_tboth", "way_id = 10"));
364     CHECK(1 == conn.get_count("osm2pgsql_test_tboth",
365                               "way_id = 10 AND rel_ids IS NULL"));
366 
367     options.append = true;
368 
369     REQUIRE_NOTHROW(db.run_import(
370         options, "r32 v2 dV Tt=ag Mw10@,w11@,w12@,w13@,w14@,w15@\n"));
371 
372     CHECK(1 == conn.get_count("osm2pgsql_test_t1"));
373     CHECK(0 == conn.get_count("osm2pgsql_test_t1", "way_id = 10"));
374     CHECK(1 == conn.get_count("osm2pgsql_test_t2"));
375     CHECK(0 == conn.get_count("osm2pgsql_test_t2", "way_id = 10"));
376     CHECK(3 == conn.get_count("osm2pgsql_test_tboth"));
377     CHECK(1 == conn.get_count("osm2pgsql_test_tboth", "way_id = 10"));
378     CHECK(1 == conn.get_count("osm2pgsql_test_tboth",
379                               "way_id = 10 AND rel_ids IS NULL"));
380 }
381 
382 TEST_CASE("test way: add relation with way in tboth stage 2 (not marked)")
383 {
384     options_t options = testing::opt_t().slim().flex(conf_file);
385 
386     testing::db::data_t data{tdata};
387 
388     data.add("w10 v1 dV Ttboth=yes Nn10,n11");
389     data.add("r31 v1 dV Tt=ag Mw10@mark,w11@,w12@,w13@,w14@");
390 
391     REQUIRE_NOTHROW(db.run_import(options, data()));
392 
393     auto conn = db.db().connect();
394 
395     CHECK(1 == conn.get_count("osm2pgsql_test_t1"));
396     CHECK(0 == conn.get_count("osm2pgsql_test_t1", "way_id = 10"));
397     CHECK(1 == conn.get_count("osm2pgsql_test_t2"));
398     CHECK(0 == conn.get_count("osm2pgsql_test_t2", "way_id = 10"));
399     CHECK(3 == conn.get_count("osm2pgsql_test_tboth"));
400     CHECK(1 == conn.get_count("osm2pgsql_test_tboth", "way_id = 10"));
401     CHECK(1 == conn.get_count("osm2pgsql_test_tboth",
402                               "way_id = 10 AND rel_ids = '{31}'"));
403 
404     options.append = true;
405 
406     REQUIRE_NOTHROW(db.run_import(
407         options, "r32 v2 dV Tt=ag Mw10@,w11@,w12@,w13@,w14@,w15@\n"));
408 
409     CHECK(1 == conn.get_count("osm2pgsql_test_t1"));
410     CHECK(0 == conn.get_count("osm2pgsql_test_t1", "way_id = 10"));
411     CHECK(1 == conn.get_count("osm2pgsql_test_t2"));
412     CHECK(0 == conn.get_count("osm2pgsql_test_t2", "way_id = 10"));
413     CHECK(3 == conn.get_count("osm2pgsql_test_tboth"));
414     CHECK(1 == conn.get_count("osm2pgsql_test_tboth", "way_id = 10"));
415     CHECK(1 == conn.get_count("osm2pgsql_test_tboth",
416                               "way_id = 10 AND rel_ids = '{31}'"));
417 }
418