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_relations.lua";
18 
19 TEST_CASE("add relations")
20 {
21     options_t options = testing::opt_t().slim().flex(conf_file);
22 
23     REQUIRE_NOTHROW(db.run_import(options, "r30 v1 dV\n"
24                                            "r31 v1 dV Tt1=yes\n"
25                                            "r32 v1 dV Tt2=yes\n"
26                                            "r33 v1 dV Tt1=yes,t2=yes\n"));
27 
28     auto conn = db.db().connect();
29 
30     CHECK(2 == conn.get_count("osm2pgsql_test_t1"));
31     CHECK(2 == conn.get_count("osm2pgsql_test_t2"));
32     CHECK(1 == conn.get_count("osm2pgsql_test_t1", "relation_id = 31"));
33     CHECK(1 == conn.get_count("osm2pgsql_test_t1", "relation_id = 33"));
34 
35     options.append = true;
36 
37     REQUIRE_NOTHROW(db.run_import(options, "r34 v1 dV\n"
38                                            "r35 v1 dV Tt1=yes\n"
39                                            "r36 v1 dV Tt2=yes\n"
40                                            "r37 v1 dV Tt1=yes,t2=yes\n"));
41 
42     CHECK(4 == conn.get_count("osm2pgsql_test_t1"));
43     CHECK(4 == conn.get_count("osm2pgsql_test_t2"));
44     CHECK(1 == conn.get_count("osm2pgsql_test_t1", "relation_id = 31"));
45     CHECK(1 == conn.get_count("osm2pgsql_test_t1", "relation_id = 33"));
46     CHECK(1 == conn.get_count("osm2pgsql_test_t1", "relation_id = 35"));
47     CHECK(1 == conn.get_count("osm2pgsql_test_t1", "relation_id = 37"));
48 }
49 
50 TEST_CASE("change relations")
51 {
52     options_t options = testing::opt_t().slim().flex(conf_file);
53 
54     REQUIRE_NOTHROW(db.run_import(options, "r30 v1 dV\n"
55                                            "r31 v1 dV Tt1=yes\n"
56                                            "r32 v1 dV Tt2=yes\n"
57                                            "r33 v1 dV Tt1=yes,t2=yes\n"
58 
59                                            "r34 v1 dV\n"
60                                            "r35 v1 dV Tt1=yes\n"
61                                            "r36 v1 dV Tt1=yes,t2=yes\n"));
62 
63     options.append = true;
64 
65     auto conn = db.db().connect();
66 
67     CHECK(4 == conn.get_count("osm2pgsql_test_t1"));
68     CHECK(3 == conn.get_count("osm2pgsql_test_t2"));
69     CHECK(1 == conn.get_count("osm2pgsql_test_t1", "relation_id = 31"));
70     CHECK(1 == conn.get_count("osm2pgsql_test_t1", "relation_id = 33"));
71     CHECK(1 == conn.get_count("osm2pgsql_test_t1", "relation_id = 35"));
72     CHECK(1 == conn.get_count("osm2pgsql_test_t1", "relation_id = 36"));
73 
74     SECTION("no tag, add tag t1")
75     {
76         REQUIRE_NOTHROW(db.run_import(options, "r34 v2 dV Tt1=yes\n"));
77         CHECK(5 == conn.get_count("osm2pgsql_test_t1"));
78         CHECK(3 == conn.get_count("osm2pgsql_test_t2"));
79     }
80 
81     SECTION("no tag, add tag t1, t2")
82     {
83         REQUIRE_NOTHROW(db.run_import(options, "r34 v2 dV Tt1=yes,t2=yes\n"));
84         CHECK(5 == conn.get_count("osm2pgsql_test_t1"));
85         CHECK(4 == conn.get_count("osm2pgsql_test_t2"));
86     }
87 
88     SECTION("one tag, remove tag t1")
89     {
90         REQUIRE_NOTHROW(db.run_import(options, "r35 v2 dV\n"));
91         CHECK(3 == conn.get_count("osm2pgsql_test_t1"));
92         CHECK(3 == conn.get_count("osm2pgsql_test_t2"));
93     }
94 
95     SECTION("one tag, change tag t1 to t2")
96     {
97         REQUIRE_NOTHROW(db.run_import(options, "r35 v2 dV Tt2=yes\n"));
98         CHECK(3 == conn.get_count("osm2pgsql_test_t1"));
99         CHECK(4 == conn.get_count("osm2pgsql_test_t2"));
100     }
101 
102     SECTION("one tag, add tag t2")
103     {
104         REQUIRE_NOTHROW(db.run_import(options, "r35 v2 dV Tt1=yes,t2=yes\n"));
105         CHECK(4 == conn.get_count("osm2pgsql_test_t1"));
106         CHECK(4 == conn.get_count("osm2pgsql_test_t2"));
107     }
108 
109     SECTION("two tags, remove tag t1 and t2")
110     {
111         REQUIRE_NOTHROW(db.run_import(options, "r36 v2 dV\n"));
112         CHECK(3 == conn.get_count("osm2pgsql_test_t1"));
113         CHECK(2 == conn.get_count("osm2pgsql_test_t2"));
114     }
115 
116     SECTION("two tags, remove only tag t1 not t2")
117     {
118         REQUIRE_NOTHROW(db.run_import(options, "r36 v2 dV Tt2=yes\n"));
119         CHECK(3 == conn.get_count("osm2pgsql_test_t1"));
120         CHECK(3 == conn.get_count("osm2pgsql_test_t2"));
121     }
122 }
123 
124 TEST_CASE("delete relation")
125 {
126     options_t options = testing::opt_t().slim().flex(conf_file);
127 
128     REQUIRE_NOTHROW(db.run_import(options, "r30 v1 dV\n"
129                                            "r31 v1 dV Tt1=yes\n"
130                                            "r32 v1 dV Tt2=yes\n"
131                                            "r33 v1 dV Tt1=yes,t2=yes\n"
132 
133                                            "r34 v1 dV\n"
134                                            "r35 v1 dV Tt1=yes\n"
135                                            "r36 v1 dV Tt1=yes,t2=yes\n"));
136 
137     options.append = true;
138 
139     auto conn = db.db().connect();
140 
141     CHECK(4 == conn.get_count("osm2pgsql_test_t1"));
142     CHECK(3 == conn.get_count("osm2pgsql_test_t2"));
143     CHECK(1 == conn.get_count("osm2pgsql_test_t1", "relation_id = 31"));
144     CHECK(1 == conn.get_count("osm2pgsql_test_t1", "relation_id = 33"));
145     CHECK(1 == conn.get_count("osm2pgsql_test_t1", "relation_id = 35"));
146     CHECK(1 == conn.get_count("osm2pgsql_test_t1", "relation_id = 36"));
147 
148     REQUIRE_NOTHROW(db.run_import(options, "r34 v2 dD\n"
149                                            "r35 v2 dD\n"
150                                            "r36 v2 dD\n"));
151 
152     CHECK(2 == conn.get_count("osm2pgsql_test_t1"));
153     CHECK(2 == conn.get_count("osm2pgsql_test_t2"));
154 }
155