-
-
Notifications
You must be signed in to change notification settings - Fork 477
/
Copy pathtest-flex-indexes.cpp
346 lines (289 loc) · 10.4 KB
/
test-flex-indexes.cpp
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
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
/**
* 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 "flex-lua-index.hpp"
#include "flex-table.hpp"
#include "pgsql-capabilities-int.hpp"
#include <lua.hpp>
class test_framework
{
public:
test_framework()
: m_lua_state(luaL_newstate(), [](lua_State *state) { lua_close(state); })
{
auto &c = database_capabilities_for_testing();
c.settings = {};
c.extensions = {"postgis"};
c.schemas = {"testschema"};
c.tablespaces = {"somewhereelse"};
c.index_methods = {"gist", "btree"};
c.database_version = 110000;
}
lua_State *lua_state() const noexcept { return m_lua_state.get(); }
bool run_lua(char const *code) const
{
return luaL_dostring(lua_state(), code) == 0;
}
private:
std::shared_ptr<lua_State> m_lua_state;
};
TEST_CASE("check index with single column", "[NoDB]")
{
test_framework const tf;
flex_table_t table{"public", "test_table", 0};
table.add_column("geom", "geometry", "");
REQUIRE(table.indexes().empty());
REQUIRE(tf.run_lua("return { method = 'gist', column = 'geom' }"));
flex_lua_setup_index(tf.lua_state(), &table);
REQUIRE(table.indexes().size() == 1);
flex_index_t const &idx = table.indexes().front();
REQUIRE(idx.method() == "gist");
REQUIRE(idx.columns() == R"(("geom"))");
REQUIRE_FALSE(idx.is_unique());
REQUIRE(idx.tablespace().empty());
REQUIRE(idx.expression().empty());
REQUIRE(idx.where_condition().empty());
REQUIRE(idx.include_columns().empty());
}
TEST_CASE("check index with multiple columns", "[NoDB]")
{
test_framework const tf;
flex_table_t table{"public", "test_table", 0};
table.add_column("a", "int", "");
table.add_column("b", "int", "");
REQUIRE(tf.run_lua("return { method = 'btree', column = {'a', 'b'} }"));
flex_lua_setup_index(tf.lua_state(), &table);
REQUIRE(table.indexes().size() == 1);
auto const &idx = table.indexes()[0];
REQUIRE(idx.method() == "btree");
REQUIRE(idx.columns() == R"(("a","b"))");
REQUIRE_FALSE(idx.is_unique());
REQUIRE(idx.tablespace().empty());
REQUIRE(idx.expression().empty());
REQUIRE(idx.where_condition().empty());
REQUIRE(idx.include_columns().empty());
}
TEST_CASE("check unique index", "[NoDB]")
{
test_framework const tf;
flex_table_t table{"public", "test_table", 0};
table.add_column("col", "int", "");
REQUIRE(tf.run_lua(
"return { method = 'btree', column = 'col', unique = true }"));
flex_lua_setup_index(tf.lua_state(), &table);
REQUIRE(table.indexes().size() == 1);
auto const &idx = table.indexes()[0];
REQUIRE(idx.method() == "btree");
REQUIRE(idx.columns() == R"(("col"))");
REQUIRE(idx.is_unique());
REQUIRE(idx.tablespace().empty());
REQUIRE(idx.expression().empty());
REQUIRE(idx.where_condition().empty());
REQUIRE(idx.include_columns().empty());
}
TEST_CASE("check index with tablespace from table", "[NoDB]")
{
test_framework const tf;
flex_table_t table{"public", "test_table", 0};
table.set_index_tablespace("foo");
table.add_column("col", "int", "");
REQUIRE(tf.run_lua("return { method = 'btree', column = 'col' }"));
flex_lua_setup_index(tf.lua_state(), &table);
REQUIRE(table.indexes().size() == 1);
auto const &idx = table.indexes()[0];
REQUIRE(idx.method() == "btree");
REQUIRE(idx.columns() == R"(("col"))");
REQUIRE_FALSE(idx.is_unique());
REQUIRE(idx.tablespace() == "foo");
REQUIRE(idx.expression().empty());
REQUIRE(idx.where_condition().empty());
REQUIRE(idx.include_columns().empty());
}
TEST_CASE("check index with tablespace", "[NoDB]")
{
test_framework const tf;
flex_table_t table{"public", "test_table", 0};
table.add_column("col", "int", "");
REQUIRE(tf.run_lua("return { method = 'btree', column = 'col', tablespace "
"= 'somewhereelse' }"));
flex_lua_setup_index(tf.lua_state(), &table);
REQUIRE(table.indexes().size() == 1);
auto const &idx = table.indexes()[0];
REQUIRE(idx.method() == "btree");
REQUIRE(idx.columns() == R"(("col"))");
REQUIRE_FALSE(idx.is_unique());
REQUIRE(idx.tablespace() == "somewhereelse");
REQUIRE(idx.expression().empty());
REQUIRE(idx.where_condition().empty());
REQUIRE(idx.include_columns().empty());
}
TEST_CASE("check index with expression and where clause", "[NoDB]")
{
test_framework const tf;
flex_table_t table{"public", "test_table", 0};
table.add_column("col", "text", "");
REQUIRE(tf.run_lua("return { method = 'btree', expression = 'lower(col)',"
" where = 'length(col) > 1' }"));
flex_lua_setup_index(tf.lua_state(), &table);
REQUIRE(table.indexes().size() == 1);
auto const &idx = table.indexes()[0];
REQUIRE(idx.method() == "btree");
REQUIRE(idx.columns().empty());
REQUIRE_FALSE(idx.is_unique());
REQUIRE(idx.tablespace().empty());
REQUIRE(idx.expression() == "lower(col)");
REQUIRE(idx.where_condition() == "length(col) > 1");
REQUIRE(idx.include_columns().empty());
}
TEST_CASE("check index with include", "[NoDB]")
{
test_framework const tf;
flex_table_t table{"public", "test_table", 0};
table.add_column("col", "int", "");
table.add_column("extra", "int", "");
REQUIRE(tf.run_lua(
"return { method = 'btree', column = 'col', include = 'extra' }"));
flex_lua_setup_index(tf.lua_state(), &table);
REQUIRE(table.indexes().size() == 1);
auto const &idx = table.indexes()[0];
REQUIRE(idx.method() == "btree");
REQUIRE(idx.columns() == R"(("col"))");
REQUIRE_FALSE(idx.is_unique());
REQUIRE(idx.tablespace().empty());
REQUIRE(idx.expression().empty());
REQUIRE(idx.where_condition().empty());
REQUIRE(idx.include_columns() == R"(("extra"))");
}
TEST_CASE("check index with include as array", "[NoDB]")
{
test_framework const tf;
flex_table_t table{"public", "test_table", 0};
table.add_column("col", "int", "");
table.add_column("extra", "int", "");
REQUIRE(tf.run_lua(
"return { method = 'btree', column = 'col', include = { 'extra' } }"));
flex_lua_setup_index(tf.lua_state(), &table);
REQUIRE(table.indexes().size() == 1);
auto const &idx = table.indexes()[0];
REQUIRE(idx.method() == "btree");
REQUIRE(idx.columns() == R"(("col"))");
REQUIRE_FALSE(idx.is_unique());
REQUIRE(idx.tablespace().empty());
REQUIRE(idx.expression().empty());
REQUIRE(idx.where_condition().empty());
REQUIRE(idx.include_columns() == R"(("extra"))");
}
TEST_CASE("check index with empty include array", "[NoDB]")
{
test_framework const tf;
flex_table_t table{"public", "test_table", 0};
table.add_column("col", "int", "");
table.add_column("extra", "int", "");
REQUIRE(tf.run_lua(
"return { method = 'btree', column = 'col', include = {} }"));
flex_lua_setup_index(tf.lua_state(), &table);
REQUIRE(table.indexes().size() == 1);
auto const &idx = table.indexes()[0];
REQUIRE(idx.method() == "btree");
REQUIRE(idx.columns() == R"(("col"))");
REQUIRE_FALSE(idx.is_unique());
REQUIRE(idx.tablespace().empty());
REQUIRE(idx.expression().empty());
REQUIRE(idx.where_condition().empty());
REQUIRE(idx.include_columns().empty());
}
TEST_CASE("check multiple indexes", "[NoDB]")
{
test_framework const tf;
flex_table_t table{"public", "test_table", 0};
table.add_column("a", "int", "");
table.add_column("b", "int", "");
REQUIRE(tf.run_lua("return { method = 'btree', column = {'a'} }"));
flex_lua_setup_index(tf.lua_state(), &table);
REQUIRE(tf.run_lua("return { method = 'gist', column = 'b' }"));
flex_lua_setup_index(tf.lua_state(), &table);
REQUIRE(table.indexes().size() == 2);
auto const &idx0 = table.indexes()[0];
REQUIRE(idx0.method() == "btree");
REQUIRE(idx0.columns() == R"(("a"))");
auto const &idx1 = table.indexes()[1];
REQUIRE(idx1.method() == "gist");
REQUIRE(idx1.columns() == R"(("b"))");
}
TEST_CASE("check various broken index configs", "[NoDB]")
{
test_framework const tf;
flex_table_t table{"public", "test_table", 0};
table.add_column("col", "text", "");
SECTION("empty index description") { REQUIRE(tf.run_lua("return {}")); }
SECTION("missing method")
{
REQUIRE(tf.run_lua("return { column = 'col' }"));
}
SECTION("non-existent method")
{
REQUIRE(tf.run_lua("return { method = 'abc', column = 'col' }"));
}
SECTION("wrong type for method")
{
REQUIRE(tf.run_lua("return { method = 123, column = 'col' }"));
}
SECTION("non-existent column")
{
REQUIRE(tf.run_lua("return { method = 'btree', column = 'x' }"));
}
SECTION("wrong type for column")
{
REQUIRE(tf.run_lua("return { method = 'btree', column = true }"));
}
SECTION("empty array for column")
{
REQUIRE(tf.run_lua("return { method = 'btree', column = {} }"));
}
SECTION("wrong type for expression")
{
REQUIRE(tf.run_lua("return { method = 'btree', expression = true }"));
}
SECTION("column and expression")
{
REQUIRE(tf.run_lua("return { method = 'btree', column = 'col', "
"expression = 'lower(col)' }"));
}
SECTION("non-existent tablespace")
{
REQUIRE(tf.run_lua(
"return { method = 'btree', column = 'col', tablespace = 'not' }"));
}
SECTION("wrong type for tablespace")
{
REQUIRE(tf.run_lua(
"return { method = 'btree', column = 'col', tablespace = 1.3 }"));
}
SECTION("wrong type for unique")
{
REQUIRE(tf.run_lua(
"return { method = 'btree', column = 'col', unique = 1 }"));
}
SECTION("wrong type for where condition")
{
REQUIRE(tf.run_lua(
"return { method = 'btree', column = 'col', where = {} }"));
}
SECTION("wrong type for include")
{
REQUIRE(tf.run_lua(
"return { btree = 'btree', column = 'col', include = 1.2 }"));
}
SECTION("unknown column for include")
{
REQUIRE(tf.run_lua(
"return { btree = 'btree', column = 'col', include = 'foo' }"));
}
REQUIRE_THROWS(flex_lua_setup_index(tf.lua_state(), &table));
}