1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306
|
/**
* SPDX-License-Identifier: GPL-2.0-or-later
*
* This file is part of osm2pgsql (https://osm2pgsql.org/).
*
* Copyright (C) 2006-2025 by the osm2pgsql developer community.
* For a full list of authors see the git log.
*/
#include <catch.hpp>
#include "common-import.hpp"
#include "common-options.hpp"
#include <array>
namespace {
testing::db::import_t db;
char const *const conf_file = "test_output_flex_types.lua";
} // anonymous namespace
TEST_CASE("type nil")
{
testing::opt_t const options = testing::opt_t().flex(conf_file);
REQUIRE_NOTHROW(
db.run_import(options, "n10 v1 dV x10.0 y10.0 Ttype=nil\n"));
auto conn = db.db().connect();
CHECK(1 == conn.get_count("nodes"));
CHECK(1 ==
conn.get_count(
"nodes",
"ttext IS NULL AND tbool IS NULL AND tint2 IS NULL AND "
"tint4 IS NULL AND tint8 IS NULL AND treal IS NULL AND "
"thstr IS NULL AND tjson IS NULL AND tdirn IS NULL AND "
"tsqlt IS NULL"));
}
TEST_CASE("type boolean")
{
testing::opt_t const options = testing::opt_t().flex(conf_file);
REQUIRE_NOTHROW(
db.run_import(options, "n10 v1 dV x10.0 y10.0 Ttype=boolean\n"));
auto conn = db.db().connect();
CHECK(2 == conn.get_count("nodes"));
CHECK(1 == conn.get_count("nodes", "tbool = true AND tint2 = 1 AND "
"tint4 = 1 AND tint8 = 1 AND "
"tjson = 'true'::jsonb AND tdirn = 1"));
CHECK(1 == conn.get_count("nodes", "tbool = false AND tint2 = 0 AND "
"tint4 = 0 AND tint8 = 0 AND "
"tjson = 'false'::jsonb AND tdirn = 0"));
}
TEST_CASE("type boolean in column where it doesn't belong")
{
testing::opt_t const options = testing::opt_t().flex(conf_file);
REQUIRE_THROWS(db.run_import(
options, "n10 v1 dV x10.0 y10.0 Ttype=boolean-fail column=ttext\n"));
REQUIRE_THROWS(db.run_import(
options, "n10 v1 dV x10.0 y10.0 Ttype=boolean-fail column=treal\n"));
REQUIRE_THROWS(db.run_import(
options, "n10 v1 dV x10.0 y10.0 Ttype=boolean-fail column=thstr\n"));
REQUIRE_THROWS(db.run_import(
options, "n10 v1 dV x10.0 y10.0 Ttype=boolean-fail column=tsqlt\n"));
auto conn = db.db().connect();
CHECK(0 == conn.get_count("nodes"));
}
TEST_CASE("type number")
{
testing::opt_t const options = testing::opt_t().flex(conf_file);
REQUIRE_NOTHROW(
db.run_import(options, "n10 v1 dV x10.0 y10.0 Ttype=number\n"));
auto conn = db.db().connect();
CHECK(19 == conn.get_count("nodes"));
// clang-format off
CHECK(1 == conn.get_count("nodes", "split_part(tsqlt, '.', 1) = '-2147483649' AND ttext = tsqlt AND tbool = true AND tint2 IS NULL AND tint4 IS NULL AND tint8 = -2147483649 AND tjson = '-2147483649'::jsonb AND tdirn = -1"));
CHECK(1 == conn.get_count("nodes", "split_part(tsqlt, '.', 1) = '-2147483648' AND ttext = tsqlt AND tbool = true AND tint2 IS NULL AND tint4 = -2147483648 AND tint8 = -2147483648 AND tjson = '-2147483648'::jsonb AND tdirn = -1"));
CHECK(1 == conn.get_count("nodes", "split_part(tsqlt, '.', 1) = '-2147483647' AND ttext = tsqlt AND tbool = true AND tint2 IS NULL AND tint4 = -2147483647 AND tint8 = -2147483647 AND tjson = '-2147483647'::jsonb AND tdirn = -1"));
CHECK(1 == conn.get_count("nodes", "split_part(tsqlt, '.', 1) = '-32769' AND ttext = tsqlt AND tbool = true AND tint2 IS NULL AND tint4 = -32769 AND tint8 = -32769 AND treal = -32769 AND tjson = '-32769'::jsonb AND tdirn = -1"));
CHECK(1 == conn.get_count("nodes", "split_part(tsqlt, '.', 1) = '-32768' AND ttext = tsqlt AND tbool = true AND tint2 = -32768 AND tint4 = -32768 AND tint8 = -32768 AND treal = -32768 AND tjson = '-32768'::jsonb AND tdirn = -1"));
CHECK(1 == conn.get_count("nodes", "split_part(tsqlt, '.', 1) = '-32767' AND ttext = tsqlt AND tbool = true AND tint2 = -32767 AND tint4 = -32767 AND tint8 = -32767 AND treal = -32767 AND tjson = '-32767'::jsonb AND tdirn = -1"));
CHECK(1 == conn.get_count("nodes", "split_part(tsqlt, '.', 1) = '-2' AND ttext = tsqlt AND tbool = true AND tint2 = -2 AND tint4 = -2 AND tint8 = -2 AND treal = -2 AND tjson = '-2'::jsonb AND tdirn = -1"));
CHECK(1 == conn.get_count("nodes", "split_part(tsqlt, '.', 1) = '-1' AND ttext = tsqlt AND tbool = true AND tint2 = -1 AND tint4 = -1 AND tint8 = -1 AND treal = -1 AND tjson = '-1'::jsonb AND tdirn = -1"));
CHECK(1 == conn.get_count("nodes", " tsqlt = '-0.5' AND ttext = tsqlt AND tbool = true AND tint2 = 0 AND tint4 = 0 AND tint8 = 0 AND treal = -0.5 AND tjson = '-0.5'::jsonb AND tdirn = -1"));
CHECK(1 == conn.get_count("nodes", "split_part(tsqlt, '.', 1) = '0' AND ttext = tsqlt AND tbool = false AND tint2 = 0 AND tint4 = 0 AND tint8 = 0 AND treal = 0 AND tjson = '0'::jsonb AND tdirn = 0"));
CHECK(1 == conn.get_count("nodes", " tsqlt = '0.5' AND ttext = tsqlt AND tbool = true AND tint2 = 0 AND tint4 = 0 AND tint8 = 0 AND treal = 0.5 AND tjson = '0.5'::jsonb AND tdirn = 1"));
CHECK(1 == conn.get_count("nodes", "split_part(tsqlt, '.', 1) = '1' AND ttext = tsqlt AND tbool = true AND tint2 = 1 AND tint4 = 1 AND tint8 = 1 AND treal = 1 AND tjson = '1'::jsonb AND tdirn = 1"));
CHECK(1 == conn.get_count("nodes", "split_part(tsqlt, '.', 1) = '2' AND ttext = tsqlt AND tbool = true AND tint2 = 2 AND tint4 = 2 AND tint8 = 2 AND treal = 2 AND tjson = '2'::jsonb AND tdirn = 1"));
CHECK(1 == conn.get_count("nodes", "split_part(tsqlt, '.', 1) = '32767' AND ttext = tsqlt AND tbool = true AND tint2 = 32767 AND tint4 = 32767 AND tint8 = 32767 AND treal = 32767 AND tjson = '32767'::jsonb AND tdirn = 1"));
CHECK(1 == conn.get_count("nodes", "split_part(tsqlt, '.', 1) = '32768' AND ttext = tsqlt AND tbool = true AND tint2 IS NULL AND tint4 = 32768 AND tint8 = 32768 AND treal = 32768 AND tjson = '32768'::jsonb AND tdirn = 1"));
CHECK(1 == conn.get_count("nodes", "split_part(tsqlt, '.', 1) = '32769' AND ttext = tsqlt AND tbool = true AND tint2 IS NULL AND tint4 = 32769 AND tint8 = 32769 AND treal = 32769 AND tjson = '32769'::jsonb AND tdirn = 1"));
CHECK(1 == conn.get_count("nodes", "split_part(tsqlt, '.', 1) = '2147483647' AND ttext = tsqlt AND tbool = true AND tint2 IS NULL AND tint4 = 2147483647 AND tint8 = 2147483647 AND tjson = '2147483647'::jsonb AND tdirn = 1"));
CHECK(1 == conn.get_count("nodes", "split_part(tsqlt, '.', 1) = '2147483648' AND ttext = tsqlt AND tbool = true AND tint2 IS NULL AND tint4 IS NULL AND tint8 = 2147483648 AND tjson = '2147483648'::jsonb AND tdirn = 1"));
CHECK(1 == conn.get_count("nodes", "split_part(tsqlt, '.', 1) = '2147483649' AND ttext = tsqlt AND tbool = true AND tint2 IS NULL AND tint4 IS NULL AND tint8 = 2147483649 AND tjson = '2147483649'::jsonb AND tdirn = 1"));
// clang-format on
}
TEST_CASE("type string (with bool)")
{
testing::opt_t const options = testing::opt_t().flex(conf_file);
REQUIRE_NOTHROW(
db.run_import(options, "n10 v1 dV x10.0 y10.0 Ttype=string-bool\n"));
auto conn = db.db().connect();
CHECK(9 == conn.get_count("nodes"));
CHECK(3 == conn.get_count("nodes", "tbool = true AND ttext = 'istrue'"));
CHECK(3 == conn.get_count("nodes", "tbool = false AND ttext = 'isfalse'"));
CHECK(3 == conn.get_count("nodes", "tbool IS NULL AND ttext = 'isnull'"));
}
TEST_CASE("type string (with direction)")
{
testing::opt_t const options = testing::opt_t().flex(conf_file);
REQUIRE_NOTHROW(db.run_import(
options, "n10 v1 dV x10.0 y10.0 Ttype=string-direction\n"));
auto conn = db.db().connect();
CHECK(9 == conn.get_count("nodes"));
CHECK(5 == conn.get_count("nodes", "tdirn = tint2"));
CHECK(4 == conn.get_count("nodes", "tdirn IS NULL AND tint2 IS NULL"));
}
TEST_CASE("type string (with number)")
{
testing::opt_t const options = testing::opt_t().flex(conf_file);
REQUIRE_NOTHROW(db.run_import(
options, "n10 v1 dV x10.0 y10.0 Ttype=string-with-number\n"));
auto conn = db.db().connect();
CHECK(18 == conn.get_count("nodes"));
// clang-format off
CHECK(1 == conn.get_count("nodes", "tsqlt = '-2147483649' AND ttext = tsqlt AND tint2 IS NULL AND tint4 IS NULL AND tint8 = -2147483649 "));
CHECK(1 == conn.get_count("nodes", "tsqlt = '-2147483648' AND ttext = tsqlt AND tint2 IS NULL AND tint4 = -2147483648 AND tint8 = -2147483648 "));
CHECK(1 == conn.get_count("nodes", "tsqlt = '-2147483647' AND ttext = tsqlt AND tint2 IS NULL AND tint4 = -2147483647 AND tint8 = -2147483647 "));
CHECK(1 == conn.get_count("nodes", "tsqlt = '-32769' AND ttext = tsqlt AND tint2 IS NULL AND tint4 = -32769 AND tint8 = -32769 AND treal = -32769"));
CHECK(1 == conn.get_count("nodes", "tsqlt = '-32768' AND ttext = tsqlt AND tint2 = -32768 AND tint4 = -32768 AND tint8 = -32768 AND treal = -32768"));
CHECK(1 == conn.get_count("nodes", "tsqlt = '-32767' AND ttext = tsqlt AND tint2 = -32767 AND tint4 = -32767 AND tint8 = -32767 AND treal = -32767"));
CHECK(1 == conn.get_count("nodes", "tsqlt = '-2' AND ttext = tsqlt AND tint2 = -2 AND tint4 = -2 AND tint8 = -2 AND treal = -2 "));
CHECK(1 == conn.get_count("nodes", "tsqlt = '-1' AND ttext = tsqlt AND tint2 = -1 AND tint4 = -1 AND tint8 = -1 AND treal = -1 "));
CHECK(1 == conn.get_count("nodes", "tsqlt = '0' AND ttext = tsqlt AND tint2 = 0 AND tint4 = 0 AND tint8 = 0 AND treal = 0 "));
CHECK(1 == conn.get_count("nodes", "tsqlt = '1' AND ttext = tsqlt AND tint2 = 1 AND tint4 = 1 AND tint8 = 1 AND treal = 1 "));
CHECK(1 == conn.get_count("nodes", "tsqlt = '2' AND ttext = tsqlt AND tint2 = 2 AND tint4 = 2 AND tint8 = 2 AND treal = 2 "));
CHECK(1 == conn.get_count("nodes", "tsqlt = '32767' AND ttext = tsqlt AND tint2 = 32767 AND tint4 = 32767 AND tint8 = 32767 AND treal = 32767 "));
CHECK(1 == conn.get_count("nodes", "tsqlt = '32768' AND ttext = tsqlt AND tint2 IS NULL AND tint4 = 32768 AND tint8 = 32768 AND treal = 32768 "));
CHECK(1 == conn.get_count("nodes", "tsqlt = '32769' AND ttext = tsqlt AND tint2 IS NULL AND tint4 = 32769 AND tint8 = 32769 AND treal = 32769 "));
CHECK(1 == conn.get_count("nodes", "tsqlt = '2147483647' AND ttext = tsqlt AND tint2 IS NULL AND tint4 = 2147483647 AND tint8 = 2147483647 "));
CHECK(1 == conn.get_count("nodes", "tsqlt = '2147483648' AND ttext = tsqlt AND tint2 IS NULL AND tint4 IS NULL AND tint8 = 2147483648 "));
CHECK(1 == conn.get_count("nodes", "tsqlt = '2147483649' AND ttext = tsqlt AND tint2 IS NULL AND tint4 IS NULL AND tint8 = 2147483649 "));
CHECK(1 == conn.get_count("nodes", "tsqlt = ' 42' AND ttext = tsqlt AND tint2 = 42 AND tint4 = 42 AND tint8 = 42 AND treal = 42 "));
// clang-format on
}
TEST_CASE("type string (with invalid number)")
{
testing::opt_t const options = testing::opt_t().flex(conf_file);
REQUIRE_NOTHROW(db.run_import(
options, "n10 v1 dV x10.0 y10.0 Ttype=string-with-invalid-number\n"));
auto conn = db.db().connect();
CHECK(7 == conn.get_count("nodes"));
// clang-format off
CHECK(1 == conn.get_count("nodes", "ttext = '' AND tint2 IS NULL AND tint4 IS NULL AND tint8 IS NULL AND treal IS NULL"));
CHECK(1 == conn.get_count("nodes", "ttext = 'abc' AND tint2 IS NULL AND tint4 IS NULL AND tint8 IS NULL AND treal IS NULL"));
CHECK(1 == conn.get_count("nodes", "ttext = '0a' AND tint2 IS NULL AND tint4 IS NULL AND tint8 IS NULL AND treal IS NULL"));
CHECK(1 == conn.get_count("nodes", "ttext = '0xa' AND tint2 IS NULL AND tint4 IS NULL AND tint8 IS NULL AND abs(treal - 10) < 0.0000001"));
CHECK(1 == conn.get_count("nodes", "ttext = '--1' AND tint2 IS NULL AND tint4 IS NULL AND tint8 IS NULL AND treal IS NULL"));
CHECK(1 == conn.get_count("nodes", "ttext = '1foo' AND tint2 IS NULL AND tint4 IS NULL AND tint8 IS NULL AND treal IS NULL"));
CHECK(1 == conn.get_count("nodes", "ttext = '1.2' AND tint2 IS NULL AND tint4 IS NULL AND tint8 IS NULL AND abs(treal - 1.2) < 0.0000001"));
// clang-format on
}
TEST_CASE("type number in column where it doesn't belong")
{
testing::opt_t const options = testing::opt_t().flex(conf_file);
REQUIRE_THROWS(db.run_import(
options, "n10 v1 dV x10.0 y10.0 Ttype=number-fail column=thstr\n"));
auto conn = db.db().connect();
CHECK(0 == conn.get_count("nodes"));
}
TEST_CASE("Adding a function should always fail")
{
testing::opt_t const options = testing::opt_t().flex(conf_file);
std::array<std::string, 9> const types = {"ttext", "tbool", "tint2",
"tint4", "tint8", "treal",
"thstr", "tdirn", "tsqlt"};
for (auto const &type : types) {
auto const line =
"n10 v1 dV x10.0 y10.0 Ttype=function-fail column=" + type + "\n";
REQUIRE_THROWS(db.run_import(options, line.c_str()));
}
auto conn = db.db().connect();
CHECK(0 == conn.get_count("nodes"));
}
TEST_CASE("type table")
{
testing::opt_t const options = testing::opt_t().flex(conf_file);
REQUIRE_NOTHROW(
db.run_import(options, "n10 v1 dV x10.0 y10.0 Ttype=table\n"));
auto conn = db.db().connect();
CHECK(2 == conn.get_count("nodes"));
CHECK(1 == conn.get_count("nodes", "thstr = '' AND tjson = '{}'::jsonb"));
CHECK(1 == conn.get_count("nodes",
"thstr = 'a=>b,c=>d' AND "
"tjson = '{\"a\": \"b\", \"c\": \"d\"}'::jsonb"));
}
TEST_CASE("Adding a table with non-strings should fail for hstore")
{
testing::opt_t const options = testing::opt_t().flex(conf_file);
char const *const line = "n10 v1 dV x10.0 y10.0 Ttype=table-hstore-fail\n";
REQUIRE_THROWS(db.run_import(options, line));
auto conn = db.db().connect();
CHECK(0 == conn.get_count("nodes"));
}
TEST_CASE("Adding a table should fail except for hstore and json/jsonb")
{
testing::opt_t const options = testing::opt_t().flex(conf_file);
std::array<std::string, 8> const types = {
"ttext", "tbool", "tint2", "tint4", "tint8", "treal", "tdirn", "tsqlt"};
for (auto const &type : types) {
auto const line =
"n10 v1 dV x10.0 y10.0 Ttype=table-fail column=" + type + "\n";
REQUIRE_THROWS(db.run_import(options, line.c_str()));
}
auto conn = db.db().connect();
CHECK(0 == conn.get_count("nodes"));
}
TEST_CASE("Adding a complex table in jsonb")
{
testing::opt_t const options = testing::opt_t().flex(conf_file);
char const *const line = "n10 v1 dV x10.0 y10.0 Ttype=json\n";
REQUIRE_NOTHROW(db.run_import(options, line));
auto conn = db.db().connect();
CHECK(1 ==
conn.get_count(
"nodes",
"tjson = '{"
"\"astring\": \"123\", \"aninteger\": 124, \"anumber\": 12.5, "
"\"atrue\": true, \"afalse\": false, "
"\"atable\": {\"a\": \"nested\", \"tab\": \"le\"}, "
"\"anarray\": [4, 3, 7]"
"}'::jsonb"));
}
TEST_CASE("Adding a table with a loop should fail")
{
testing::opt_t const options = testing::opt_t().flex(conf_file);
char const *const line = "n10 v1 dV x10.0 y10.0 Ttype=json-loop\n";
REQUIRE_THROWS(db.run_import(options, line));
auto conn = db.db().connect();
CHECK(0 == conn.get_count("nodes"));
}
|