Skip to content

Commit fd61965

Browse files
authored
Merge pull request #1249 from joto/tests-way
Add more way tests for flex output
2 parents 593405c + a98df36 commit fd61965

9 files changed

+1390
-4
lines changed

.travis.yml

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,18 @@ matrix:
1818
- os: linux
1919
dist: bionic
2020
compiler: gcc-8
21-
env: T="bionic_gcc8_all_luajit_release"
22-
PG_VERSIONS="9.3 9.4 9.5 9.6 10 11 12"
21+
env: T="bionic_gcc8_all_9_luajit_release"
22+
PG_VERSIONS="9.3 9.4 9.5 9.6"
23+
LUA_VERSION=5.3
24+
BUILD_TYPE="Release" LUAJIT_OPTION="ON"
25+
CXXFLAGS="-pedantic -Wextra -Werror"
26+
CC=gcc-8 CXX=g++-8 CPPVERSION=14
27+
28+
- os: linux
29+
dist: bionic
30+
compiler: gcc-8
31+
env: T="bionic_gcc8_all_10+_luajit_release"
32+
PG_VERSIONS="10 11 12"
2333
LUA_VERSION=5.3
2434
BUILD_TYPE="Release" LUAJIT_OPTION="ON"
2535
CXXFLAGS="-pedantic -Wextra -Werror"
@@ -149,10 +159,10 @@ script:
149159
- make -j2
150160
- echo "Running tests ... "
151161
- if [[ $TEST_NODB ]]; then
152-
ctest -VV -L NoDB;
162+
ctest --output-on-failure -L NoDB;
153163
else
154164
for PG_VERSION in $PG_VERSIONS; do
155-
pg_virtualenv -v $PG_VERSION ctest -VV;
165+
pg_virtualenv -v $PG_VERSION ctest --output-on-failure;
156166
if [[ $? -ne "0" ]]; then exit 2; fi
157167
done
158168
fi

tests/CMakeLists.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,11 @@ if (HAVE_LUA)
8989
set_test(test-output-flex-types)
9090
set_test(test-output-flex-update)
9191
set_test(test-output-flex-validgeom)
92+
set_test(test-output-flex-way-add)
93+
set_test(test-output-flex-way-change)
94+
set_test(test-output-flex-way-del)
95+
set_test(test-output-flex-way-relation-add)
96+
set_test(test-output-flex-way-relation-del)
9297

9398
set_test(test-output-flex-example-configs)
9499
set(FLEX_EXAMPLE_CONFIGS "compatible,data-types,default-config,geometries,route-relations,simple,unitable")

tests/common-import.hpp

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#include <osmium/io/any_input.hpp>
66
#include <osmium/io/file.hpp>
77
#include <osmium/io/reader.hpp>
8+
#include <osmium/osm/types_from_string.hpp>
89
#include <osmium/visitor.hpp>
910

1011
#include "dependency-manager.hpp"
@@ -18,6 +19,11 @@
1819

1920
#include "common-pg.hpp"
2021

22+
#include <algorithm>
23+
#include <iterator>
24+
#include <string>
25+
#include <vector>
26+
2127
namespace testing {
2228

2329
inline void parse_file(options_t const &options,
@@ -42,6 +48,69 @@ inline void parse_file(options_t const &options,
4248
osmdata.stop();
4349
}
4450

51+
/**
52+
* This is used as a helper to assemble OSM objects into an OPL file which
53+
* can later be used as input for testing.
54+
*/
55+
class data_t
56+
{
57+
public:
58+
data_t() = default;
59+
60+
template <typename CONTAINER>
61+
data_t(CONTAINER const &objects)
62+
{
63+
std::copy(std::begin(objects), std::end(objects),
64+
std::back_inserter(m_objects));
65+
}
66+
67+
void add(char const *object) { m_objects.emplace_back(object); }
68+
69+
template <typename CONTAINER>
70+
void add(CONTAINER const &objects)
71+
{
72+
std::copy(std::begin(objects), std::end(objects),
73+
std::back_inserter(m_objects));
74+
}
75+
76+
void add(std::initializer_list<const char *> const &objects)
77+
{
78+
std::copy(std::begin(objects), std::end(objects),
79+
std::back_inserter(m_objects));
80+
}
81+
82+
const char *operator()()
83+
{
84+
std::sort(m_objects.begin(), m_objects.end(),
85+
[](std::string const &a, std::string const &b) {
86+
return get_type_id(a) < get_type_id(b);
87+
});
88+
89+
m_result.clear();
90+
for (auto const &obj : m_objects) {
91+
assert(!obj.empty());
92+
m_result.append(obj);
93+
if (m_result.back() != '\n') {
94+
m_result += '\n';
95+
}
96+
}
97+
98+
return m_result.c_str();
99+
}
100+
101+
private:
102+
static std::pair<osmium::item_type, osmium::object_id_type>
103+
get_type_id(std::string const &str)
104+
{
105+
std::string ti(str, 0, str.find(' '));
106+
return osmium::string_to_object_id(ti.c_str(),
107+
osmium::osm_entity_bits::nwr);
108+
}
109+
110+
std::vector<std::string> m_objects;
111+
std::string m_result;
112+
};
113+
45114
namespace db {
46115

47116
/**
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
2+
local tables = {}
3+
4+
tables.t1 = osm2pgsql.define_way_table('osm2pgsql_test_t1', {
5+
{ column = 'tags', type = 'hstore' },
6+
{ column = 'geom', type = 'linestring' },
7+
})
8+
9+
tables.t2 = osm2pgsql.define_way_table('osm2pgsql_test_t2', {
10+
{ column = 'tags', type = 'hstore' },
11+
{ column = 'rel_ids', type = 'text' },
12+
{ column = 'geom', type = 'linestring' },
13+
})
14+
15+
tables.tboth = osm2pgsql.define_way_table('osm2pgsql_test_tboth', {
16+
{ column = 'tags', type = 'hstore' },
17+
{ column = 'rel_ids', type = 'text' },
18+
{ column = 'geom', type = 'linestring' },
19+
})
20+
21+
local w2r = {}
22+
23+
function get_ids(data)
24+
if data then
25+
local ids = {}
26+
for rel_id, _ in pairs(data) do
27+
ids[#ids + 1] = rel_id
28+
end
29+
table.sort(ids)
30+
return '{' .. table.concat(ids, ',') .. '}'
31+
end
32+
end
33+
34+
function osm2pgsql.process_way(object)
35+
if object.tags.t1 then
36+
tables.t1:add_row{
37+
tags = object.tags,
38+
geom = { create = 'line' }
39+
}
40+
end
41+
42+
if osm2pgsql.stage == 2 and object.tags.t2 then
43+
local ids = get_ids(w2r[object.id])
44+
if ids then
45+
tables.t2:add_row{
46+
rel_ids = ids,
47+
geom = { create = 'line' }
48+
}
49+
end
50+
end
51+
52+
if object.tags.tboth then
53+
local ids = get_ids(w2r[object.id])
54+
tables.tboth:add_row{
55+
tags = object.tags,
56+
rel_ids = ids,
57+
geom = { create = 'line' }
58+
}
59+
end
60+
end
61+
62+
function way_member_ids(relation)
63+
local ids = {}
64+
for _, member in ipairs(relation.members) do
65+
if member.type == 'w' and member.role == 'mark' then
66+
ids[#ids + 1] = member.ref
67+
end
68+
end
69+
return ids
70+
end
71+
72+
function osm2pgsql.select_relation_members(relation)
73+
return { ways = way_member_ids(relation) }
74+
end
75+
76+
function osm2pgsql.process_relation(object)
77+
for _, member in ipairs(object.members) do
78+
if member.type == 'w' and member.role == 'mark' then
79+
if not w2r[member.ref] then
80+
w2r[member.ref] = {}
81+
end
82+
w2r[member.ref][object.id] = true
83+
end
84+
end
85+
end
86+

tests/test-output-flex-way-add.cpp

Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
#include <catch.hpp>
2+
3+
#include "common-import.hpp"
4+
#include "common-options.hpp"
5+
6+
static testing::db::import_t db;
7+
8+
static char const *const conf_file = "test_output_flex_way.lua";
9+
10+
static const char *const tdata[] = {
11+
"n10 v1 dV x10.0 y10.0",
12+
"n11 v1 dV x10.0 y10.1",
13+
"n12 v1 dV x10.1 y10.0",
14+
"n13 v1 dV x10.1 y10.1",
15+
"n14 v1 dV x10.2 y10.0",
16+
"n15 v1 dV x10.2 y10.1",
17+
"n16 v1 dV x10.3 y10.0",
18+
"n17 v1 dV x10.3 y10.1",
19+
"n18 v1 dV x10.4 y10.0",
20+
"n19 v1 dV x10.4 y10.1",
21+
"w11 v1 dV Tt1=yes Nn12,n13",
22+
"w12 v1 dV Tt2=yes Nn14,n15",
23+
"w13 v1 dV Ttboth=yes Nn16,n17",
24+
"w14 v1 dV Ttboth=yes Nn18,n19",
25+
"r30 v1 dV Tt=ag Mw11@,w12@mark,w13@,w14@mark"};
26+
27+
TEST_CASE("add way")
28+
{
29+
options_t options = testing::opt_t().slim().flex(conf_file);
30+
31+
testing::data_t data{tdata};
32+
33+
REQUIRE_NOTHROW(db.run_import(options, data()));
34+
35+
auto conn = db.db().connect();
36+
37+
CHECK(1 == conn.get_count("osm2pgsql_test_t1"));
38+
CHECK(1 == conn.get_count("osm2pgsql_test_t2"));
39+
CHECK(2 == conn.get_count("osm2pgsql_test_tboth"));
40+
41+
options.append = true;
42+
43+
SECTION("way is not relevant")
44+
{
45+
REQUIRE_NOTHROW(db.run_import(
46+
options, "w10 v1 dV Tt=ag Nn10,n11\n"
47+
"r30 v2 dV Tt=ag Mw10@,w11@,w12@mark,w13@,w14@mark\n"));
48+
49+
CHECK(1 == conn.get_count("osm2pgsql_test_t1"));
50+
CHECK(1 == conn.get_count("osm2pgsql_test_t2"));
51+
CHECK(2 == conn.get_count("osm2pgsql_test_tboth"));
52+
}
53+
54+
SECTION("add to t1")
55+
{
56+
REQUIRE_NOTHROW(db.run_import(
57+
options, "w10 v1 dV Tt1=yes Nn10,n11\n"
58+
"r30 v2 dV Tt=ag Mw10@,w11@,w12@mark,w13@,w14@mark\n"));
59+
60+
CHECK(2 == conn.get_count("osm2pgsql_test_t1"));
61+
CHECK(1 == conn.get_count("osm2pgsql_test_t1", "way_id = 10"));
62+
CHECK(1 == conn.get_count("osm2pgsql_test_t2"));
63+
CHECK(2 == conn.get_count("osm2pgsql_test_tboth"));
64+
}
65+
66+
SECTION("add to t2")
67+
{
68+
REQUIRE_NOTHROW(db.run_import(
69+
options,
70+
"w10 v1 dV Tt2=yes Nn10,n11\n"
71+
"r30 v2 dV Tt=ag Mw10@mark,w11@,w12@mark,w13@,w14@mark\n"));
72+
73+
CHECK(1 == conn.get_count("osm2pgsql_test_t1"));
74+
CHECK(2 == conn.get_count("osm2pgsql_test_t2"));
75+
CHECK(1 == conn.get_count("osm2pgsql_test_t2",
76+
"way_id = 10 AND rel_ids = '{30}'"));
77+
CHECK(2 == conn.get_count("osm2pgsql_test_tboth"));
78+
}
79+
80+
SECTION("add to t1 and t2")
81+
{
82+
REQUIRE_NOTHROW(db.run_import(
83+
options,
84+
"w10 v1 dV Tt1=yes,t2=yes Nn10,n11\n"
85+
"r30 v2 dV Tt=ag Mw10@mark,w11@,w12@mark,w13@,w14@mark\n"));
86+
87+
CHECK(2 == conn.get_count("osm2pgsql_test_t1"));
88+
CHECK(1 == conn.get_count("osm2pgsql_test_t1", "way_id = 10"));
89+
CHECK(2 == conn.get_count("osm2pgsql_test_t2"));
90+
CHECK(1 == conn.get_count("osm2pgsql_test_t2",
91+
"way_id = 10 AND rel_ids = '{30}'"));
92+
CHECK(2 == conn.get_count("osm2pgsql_test_tboth"));
93+
}
94+
95+
SECTION("add to tboth (only stage1)")
96+
{
97+
REQUIRE_NOTHROW(db.run_import(
98+
options, "w10 v1 dV Ttboth=yes Nn10,n11\n"
99+
"r30 v2 dV Tt=ag Mw10@,w11@,w12@mark,w13@,w14@mark\n"));
100+
101+
CHECK(1 == conn.get_count("osm2pgsql_test_t1"));
102+
CHECK(1 == conn.get_count("osm2pgsql_test_t2"));
103+
CHECK(3 == conn.get_count("osm2pgsql_test_tboth"));
104+
CHECK(1 == conn.get_count("osm2pgsql_test_tboth",
105+
"way_id = 10 AND rel_ids IS NULL"));
106+
}
107+
108+
SECTION("add to tboth (stage1 and stage2)")
109+
{
110+
REQUIRE_NOTHROW(db.run_import(
111+
options,
112+
"w10 v1 dV Ttboth=yes Nn10,n11\n"
113+
"r30 v2 dV Tt=ag Mw10@mark,w11@,w12@mark,w13@,w14@mark\n"));
114+
115+
CHECK(1 == conn.get_count("osm2pgsql_test_t1"));
116+
CHECK(1 == conn.get_count("osm2pgsql_test_t2"));
117+
CHECK(3 == conn.get_count("osm2pgsql_test_tboth"));
118+
CHECK(1 == conn.get_count("osm2pgsql_test_tboth",
119+
"way_id = 10 AND rel_ids = '{30}'"));
120+
}
121+
}

0 commit comments

Comments
 (0)