Skip to content

Commit 69f9c31

Browse files
committed
Cleanup flex type conversion tests
1 parent a6b9b5d commit 69f9c31

2 files changed

Lines changed: 72 additions & 82 deletions

File tree

tests/data/test_output_flex_types.lua

Lines changed: 48 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11

2-
local table = osm2pgsql.define_node_table("nodes", {
2+
local test_table = osm2pgsql.define_node_table("nodes", {
33
{ column = 'ttext', type = 'text' },
44
{ column = 'tbool', type = 'boolean' },
55
{ column = 'tint2', type = 'int2' },
@@ -12,32 +12,34 @@ local table = osm2pgsql.define_node_table("nodes", {
1212
})
1313

1414
function osm2pgsql.process_node(object)
15-
if object.tags.type == 'nil' then
16-
table:add_row{}
15+
local test_type = object.tags.type
16+
17+
if test_type == 'nil' then
18+
test_table:add_row{}
1719
return
1820
end
19-
if object.tags.type == 'boolean' then
21+
if test_type == 'boolean' then
2022
local row = { tbool = true, tint2 = true, tint4 = true,
2123
tint8 = true, tdirn = true }
22-
table:add_row(row)
24+
test_table:add_row(row)
2325

2426
row = { tbool = false, tint2 = false, tint4 = false,
2527
tint8 = false, tdirn = false }
26-
table:add_row(row)
28+
test_table:add_row(row)
2729
return
2830
end
29-
if object.tags.type == 'boolean-fail' then
30-
table:add_row{ [object.tags.column] = true }
31+
if test_type == 'boolean-fail' then
32+
test_table:add_row{ [object.tags.column] = true }
3133
return
3234
end
33-
if object.tags.type == 'number' then
35+
if test_type == 'number' then
3436
local numbers = { -2^31 - 1, -2^31, -2^31 + 1,
3537
-2^15 - 1, -2^15, -2^15 + 1,
3638
-2, -1, -0.5, 0, 0.5, 1, 2,
3739
2^15 - 1, 2^15, 2^15 + 1,
3840
2^31 - 1, 2^31, 2^31 + 1 }
3941
for _, n in ipairs(numbers) do
40-
table:add_row{
42+
test_table:add_row{
4143
ttext = n,
4244
tbool = n,
4345
tint2 = n,
@@ -50,47 +52,47 @@ function osm2pgsql.process_node(object)
5052
end
5153
return
5254
end
53-
if object.tags.type == 'number-fail' then
54-
table:add_row{ [object.tags.column] = 1 }
55+
if test_type == 'number-fail' then
56+
test_table:add_row{ [object.tags.column] = 1 }
5557
return
5658
end
57-
if object.tags.type == 'string-bool' then
58-
table:add_row{ tbool = '1', ttext = 'istrue' };
59-
table:add_row{ tbool = 'yes', ttext = 'istrue' };
60-
table:add_row{ tbool = 'true', ttext = 'istrue' };
59+
if test_type == 'string-bool' then
60+
test_table:add_row{ tbool = '1', ttext = 'istrue' };
61+
test_table:add_row{ tbool = 'yes', ttext = 'istrue' };
62+
test_table:add_row{ tbool = 'true', ttext = 'istrue' };
6163

62-
table:add_row{ tbool = '0', ttext = 'isfalse' };
63-
table:add_row{ tbool = 'no', ttext = 'isfalse' };
64-
table:add_row{ tbool = 'false', ttext = 'isfalse' };
64+
test_table:add_row{ tbool = '0', ttext = 'isfalse' };
65+
test_table:add_row{ tbool = 'no', ttext = 'isfalse' };
66+
test_table:add_row{ tbool = 'false', ttext = 'isfalse' };
6567

66-
table:add_row{ tbool = '', ttext = 'isnull' };
67-
table:add_row{ tbool = '2', ttext = 'isnull' };
68-
table:add_row{ tbool = 'YES', ttext = 'isnull' };
68+
test_table:add_row{ tbool = '', ttext = 'isnull' };
69+
test_table:add_row{ tbool = '2', ttext = 'isnull' };
70+
test_table:add_row{ tbool = 'YES', ttext = 'isnull' };
6971
return
7072
end
71-
if object.tags.type == 'string-direction' then
72-
table:add_row{ tdirn = '1', tint2 = 1 };
73-
table:add_row{ tdirn = 'yes', tint2 = 1 };
73+
if test_type == 'string-direction' then
74+
test_table:add_row{ tdirn = '1', tint2 = 1 };
75+
test_table:add_row{ tdirn = 'yes', tint2 = 1 };
7476

75-
table:add_row{ tdirn = '0', tint2 = 0 };
76-
table:add_row{ tdirn = 'no', tint2 = 0 };
77+
test_table:add_row{ tdirn = '0', tint2 = 0 };
78+
test_table:add_row{ tdirn = 'no', tint2 = 0 };
7779

78-
table:add_row{ tdirn = '-1', tint2 = -1 };
80+
test_table:add_row{ tdirn = '-1', tint2 = -1 };
7981

80-
table:add_row{ tdirn = '2', tint2 = nil };
81-
table:add_row{ tdirn = '-2', tint2 = nil };
82-
table:add_row{ tdirn = '', tint2 = nil };
83-
table:add_row{ tdirn = 'FOO', tint2 = nil };
82+
test_table:add_row{ tdirn = '2', tint2 = nil };
83+
test_table:add_row{ tdirn = '-2', tint2 = nil };
84+
test_table:add_row{ tdirn = '', tint2 = nil };
85+
test_table:add_row{ tdirn = 'FOO', tint2 = nil };
8486
return
8587
end
86-
if object.tags.type == 'string-with-number' then
88+
if test_type == 'string-with-number' then
8789
local numbers = { -2^31 - 1, -2^31, -2^31 + 1,
8890
-2^15 - 1, -2^15, -2^15 + 1,
8991
-2, -1, 0, 1, 2,
9092
2^15 - 1, 2^15, 2^15 + 1,
9193
2^31 - 1, 2^31, 2^31 + 1 }
9294
for _, n in ipairs(numbers) do
93-
table:add_row{
95+
test_table:add_row{
9496
ttext = string.format('%d', n),
9597
tint2 = string.format('%d', n),
9698
tint4 = string.format('%d', n),
@@ -99,7 +101,7 @@ function osm2pgsql.process_node(object)
99101
tsqlt = string.format('%d', n),
100102
}
101103
end
102-
table:add_row{
104+
test_table:add_row{
103105
ttext = ' 42',
104106
tint2 = ' 42',
105107
tint4 = ' 42',
@@ -109,10 +111,10 @@ function osm2pgsql.process_node(object)
109111
}
110112
return
111113
end
112-
if object.tags.type == 'string-with-invalid-number' then
114+
if test_type == 'string-with-invalid-number' then
113115
local not_numbers = { '', 'abc', '0a', '0xa', '--1', '1foo', '1.2' }
114116
for _, n in ipairs(not_numbers) do
115-
table:add_row{
117+
test_table:add_row{
116118
ttext = n,
117119
tint2 = n,
118120
tint4 = n,
@@ -121,21 +123,21 @@ function osm2pgsql.process_node(object)
121123
end
122124
return
123125
end
124-
if object.tags.type == 'function-fail' then
125-
table:add_row{ [object.tags.column] = table.insert }
126+
if test_type == 'function-fail' then
127+
test_table:add_row{ [object.tags.column] = table.insert }
126128
return
127129
end
128-
if object.tags.type == 'table' then
129-
table:add_row{ thstr = {} }
130-
table:add_row{ thstr = { a = 'b', c = 'd' } }
130+
if test_type == 'table' then
131+
test_table:add_row{ thstr = {} }
132+
test_table:add_row{ thstr = { a = 'b', c = 'd' } }
131133
return
132134
end
133-
if object.tags.type == 'table-hstore-fail' then
134-
table:add_row{ thstr = { num = 1, bln = true } }
135+
if test_type == 'table-hstore-fail' then
136+
test_table:add_row{ thstr = { num = 1, bln = true } }
135137
return
136138
end
137-
if object.tags.type == 'table-fail' then
138-
table:add_row{ [object.tags.column] = {} }
139+
if test_type == 'table-fail' then
140+
test_table:add_row{ [object.tags.column] = {} }
139141
return
140142
end
141143
end

tests/test-output-flex-types.cpp

Lines changed: 24 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,11 @@
55

66
static testing::db::import_t db;
77

8+
static char const *const conf_file = "test_output_flex_types.lua";
9+
810
TEST_CASE("type nil")
911
{
10-
testing::opt_t const options =
11-
testing::opt_t().flex("test_output_flex_types.lua");
12+
testing::opt_t const options = testing::opt_t().flex(conf_file);
1213

1314
REQUIRE_NOTHROW(
1415
db.run_import(options, "n10 v1 dV x10.0 y10.0 Ttype=nil\n"));
@@ -26,8 +27,7 @@ TEST_CASE("type nil")
2627

2728
TEST_CASE("type boolean")
2829
{
29-
testing::opt_t const options =
30-
testing::opt_t().flex("test_output_flex_types.lua");
30+
testing::opt_t const options = testing::opt_t().flex(conf_file);
3131

3232
REQUIRE_NOTHROW(
3333
db.run_import(options, "n10 v1 dV x10.0 y10.0 Ttype=boolean\n"));
@@ -43,8 +43,7 @@ TEST_CASE("type boolean")
4343

4444
TEST_CASE("type boolean in column where it doesn't belong")
4545
{
46-
testing::opt_t const options =
47-
testing::opt_t().flex("test_output_flex_types.lua");
46+
testing::opt_t const options = testing::opt_t().flex(conf_file);
4847

4948
REQUIRE_THROWS(db.run_import(
5049
options, "n10 v1 dV x10.0 y10.0 Ttype=boolean-fail column=ttext\n"));
@@ -62,8 +61,7 @@ TEST_CASE("type boolean in column where it doesn't belong")
6261

6362
TEST_CASE("type number")
6463
{
65-
testing::opt_t const options =
66-
testing::opt_t().flex("test_output_flex_types.lua");
64+
testing::opt_t const options = testing::opt_t().flex(conf_file);
6765

6866
REQUIRE_NOTHROW(
6967
db.run_import(options, "n10 v1 dV x10.0 y10.0 Ttype=number\n"));
@@ -97,11 +95,10 @@ TEST_CASE("type number")
9795

9896
TEST_CASE("type string (with bool)")
9997
{
100-
testing::opt_t const options =
101-
testing::opt_t().flex("test_output_flex_types.lua");
98+
testing::opt_t const options = testing::opt_t().flex(conf_file);
10299

103-
REQUIRE_NOTHROW(db.run_import(
104-
options, "n10 v1 dV x10.0 y10.0 Ttype=string-bool\n"));
100+
REQUIRE_NOTHROW(
101+
db.run_import(options, "n10 v1 dV x10.0 y10.0 Ttype=string-bool\n"));
105102

106103
auto conn = db.db().connect();
107104

@@ -113,8 +110,7 @@ TEST_CASE("type string (with bool)")
113110

114111
TEST_CASE("type string (with direction)")
115112
{
116-
testing::opt_t const options =
117-
testing::opt_t().flex("test_output_flex_types.lua");
113+
testing::opt_t const options = testing::opt_t().flex(conf_file);
118114

119115
REQUIRE_NOTHROW(db.run_import(
120116
options, "n10 v1 dV x10.0 y10.0 Ttype=string-direction\n"));
@@ -128,8 +124,7 @@ TEST_CASE("type string (with direction)")
128124

129125
TEST_CASE("type string (with number)")
130126
{
131-
testing::opt_t const options =
132-
testing::opt_t().flex("test_output_flex_types.lua");
127+
testing::opt_t const options = testing::opt_t().flex(conf_file);
133128

134129
REQUIRE_NOTHROW(db.run_import(
135130
options, "n10 v1 dV x10.0 y10.0 Ttype=string-with-number\n"));
@@ -162,8 +157,7 @@ TEST_CASE("type string (with number)")
162157

163158
TEST_CASE("type string (with invalid number)")
164159
{
165-
testing::opt_t const options =
166-
testing::opt_t().flex("test_output_flex_types.lua");
160+
testing::opt_t const options = testing::opt_t().flex(conf_file);
167161

168162
REQUIRE_NOTHROW(db.run_import(
169163
options, "n10 v1 dV x10.0 y10.0 Ttype=string-with-invalid-number\n"));
@@ -185,11 +179,10 @@ TEST_CASE("type string (with invalid number)")
185179

186180
TEST_CASE("type number in column where it doesn't belong")
187181
{
188-
testing::opt_t const options =
189-
testing::opt_t().flex("test_output_flex_types.lua");
182+
testing::opt_t const options = testing::opt_t().flex(conf_file);
190183

191-
REQUIRE_THROWS(
192-
db.run_import(options, "n10 v1 dV x10.0 y10.0 Ttype=number-fail column=thstr\n"));
184+
REQUIRE_THROWS(db.run_import(
185+
options, "n10 v1 dV x10.0 y10.0 Ttype=number-fail column=thstr\n"));
193186

194187
auto conn = db.db().connect();
195188

@@ -198,11 +191,10 @@ TEST_CASE("type number in column where it doesn't belong")
198191

199192
TEST_CASE("Adding a function should always fail")
200193
{
201-
testing::opt_t const options =
202-
testing::opt_t().flex("test_output_flex_types.lua");
194+
testing::opt_t const options = testing::opt_t().flex(conf_file);
203195

204-
std::string types[] = {"ttext", "tbool", "tint2", "tint4",
205-
"tint8", "treal", "thstr", "tdirn", "tsqlt"};
196+
std::string const types[] = {"ttext", "tbool", "tint2", "tint4", "tint8",
197+
"treal", "thstr", "tdirn", "tsqlt"};
206198

207199
for (auto const &type : types) {
208200
auto const line =
@@ -217,8 +209,7 @@ TEST_CASE("Adding a function should always fail")
217209

218210
TEST_CASE("type table")
219211
{
220-
testing::opt_t const options =
221-
testing::opt_t().flex("test_output_flex_types.lua");
212+
testing::opt_t const options = testing::opt_t().flex(conf_file);
222213

223214
REQUIRE_NOTHROW(
224215
db.run_import(options, "n10 v1 dV x10.0 y10.0 Ttype=table\n"));
@@ -233,11 +224,9 @@ TEST_CASE("type table")
233224

234225
TEST_CASE("Adding a table with non-strings should fail for hstore")
235226
{
236-
testing::opt_t const options =
237-
testing::opt_t().flex("test_output_flex_types.lua");
227+
testing::opt_t const options = testing::opt_t().flex(conf_file);
238228

239-
char const *const line =
240-
"n10 v1 dV x10.0 y10.0 Ttype=table-hstore-fail\n";
229+
char const *const line = "n10 v1 dV x10.0 y10.0 Ttype=table-hstore-fail\n";
241230
REQUIRE_THROWS(db.run_import(options, line));
242231

243232
auto conn = db.db().connect();
@@ -247,11 +236,10 @@ TEST_CASE("Adding a table with non-strings should fail for hstore")
247236

248237
TEST_CASE("Adding a table should fail except for hstore")
249238
{
250-
testing::opt_t const options =
251-
testing::opt_t().flex("test_output_flex_types.lua");
239+
testing::opt_t const options = testing::opt_t().flex(conf_file);
252240

253-
std::string types[] = {"ttext", "tbool", "tint2", "tint4",
254-
"tint8", "treal", "tdirn", "tsqlt"};
241+
std::string const types[] = {"ttext", "tbool", "tint2", "tint4",
242+
"tint8", "treal", "tdirn", "tsqlt"};
255243

256244
for (auto const &type : types) {
257245
auto const line =

0 commit comments

Comments
 (0)