Skip to content

Commit 7fb9bc7

Browse files
committed
Add unit tests
1 parent b26afa5 commit 7fb9bc7

File tree

6 files changed

+109
-2
lines changed

6 files changed

+109
-2
lines changed

.github/workflows/ci.yml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,11 @@ jobs:
88
- name: Install lua
99
run: |
1010
sudo apt-get update -qq &&
11-
sudo apt-get install -qq --no-install-recommends lua5.3 lua-check
11+
sudo apt-get install -qq --no-install-recommends lua5.3 lua-busted lua-check
1212
- name: Check lua files
1313
run: bin/check-lua-scripts.sh
14+
- name: Run tests
15+
env:
16+
LUA_PATH: "lua/?.lua;;"
17+
run: bin/run-tests.sh
1418

.luacheckrc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,5 +30,5 @@ stds.osm2pgsql = {
3030
}
3131
}
3232

33-
std = 'min+osm2pgsql'
33+
std = 'min+osm2pgsql+busted'
3434

README.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,18 @@ command provided with newer osm2pgsql versions.
160160

161161
[More...](themes/shortbread_v1_gen/README.md)
162162

163+
## Testing
164+
165+
Some unit tests are provided in the `tests` directory. You'll nee the ["busted"
166+
testing framework](https://lunarmodules.github.io/busted/) installed (`luarocks
167+
install busted` or install the `lua-busted` apt package on Debian).
168+
169+
Run all tests:
170+
171+
```
172+
bin/run-tests.sh
173+
```
174+
163175
## License
164176

165177
Copyright 2024 Jochen Topf <[email protected]>

bin/check-lua-scripts.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ luacheck \
99
lua/themepark.lua \
1010
lua/themepark/*.lua \
1111
lua/themepark/*/*.lua \
12+
tests/*.lua \
1213
config/*.lua \
1314
themes/*/*.lua \
1415
themes/*/topics/*.lua

bin/run-tests.sh

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#!/bin/sh
2+
3+
for test in tests/test-*.lua; do
4+
lua "$test"
5+
done
6+

tests/test-utils.lua

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
-- ---------------------------------------------------------------------------
2+
--
3+
-- Osm2pgsql Themepark
4+
--
5+
-- A framework for pluggable osm2pgsql config files.
6+
--
7+
-- ---------------------------------------------------------------------------
8+
--
9+
-- tests/test-utils.lua
10+
--
11+
-- ---------------------------------------------------------------------------
12+
--
13+
-- Copyright 2024 Jochen Topf <[email protected]>
14+
--
15+
-- Licensed under the Apache License, Version 2.0 (the "License");
16+
-- you may not use this file except in compliance with the License.
17+
-- You may obtain a copy of the License at
18+
--
19+
-- https://www.apache.org/licenses/LICENSE-2.0
20+
--
21+
-- Unless required by applicable law or agreed to in writing, software
22+
-- distributed under the License is distributed on an "AS IS" BASIS,
23+
-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
24+
-- See the License for the specific language governing permissions and
25+
-- limitations under the License.
26+
--
27+
-- ---------------------------------------------------------------------------
28+
29+
require 'busted.runner'()
30+
31+
local utils = require 'themepark/utils'
32+
33+
describe('test build_select_query', function()
34+
35+
it('creates simple selects', function()
36+
assert.is_equal([[SELECT first,second FROM "sometable"]],
37+
utils.build_select_query({'first', 'second'}, 'sometable'))
38+
end)
39+
40+
it('handles tables which schemas', function()
41+
assert.is_equal([[SELECT first,second FROM "inschema"."sometable"]],
42+
utils.build_select_query({'first', 'second'}, 'inschema.sometable'))
43+
end)
44+
45+
it('handles WHERE condition', function()
46+
assert.is_equal([[SELECT * FROM "sometable" WHERE a=b]],
47+
utils.build_select_query({'*'}, 'sometable', {'a=b'}))
48+
49+
assert.is_equal([[SELECT * FROM "sometable" WHERE a=b AND c=1]],
50+
utils.build_select_query({'*'}, 'sometable', {'a=b', 'c=1'}))
51+
52+
assert.is_equal([[SELECT * FROM "sometable"]],
53+
utils.build_select_query({'*'}, 'sometable', {}))
54+
end)
55+
56+
it('handles ORDER BY sorting', function()
57+
assert.is_equal([[SELECT a,b FROM "sometable" ORDER BY "b"]],
58+
utils.build_select_query({'a', 'b'}, 'sometable', {}, 'b'))
59+
60+
assert.is_equal([[SELECT a,b FROM "sometable" ORDER BY "b" DESC]],
61+
utils.build_select_query({'a', 'b'}, 'sometable', {}, 'b', 'DESC'))
62+
end)
63+
64+
end)
65+
66+
describe('test find_name_in_array', function()
67+
68+
local a = {
69+
{ name = 'some' },
70+
{ name = 'named' },
71+
{ name = 'things' },
72+
}
73+
74+
it('finds name in array', function()
75+
assert.is_same({ name = 'named' }, utils.find_name_in_array(a, 'named'))
76+
end)
77+
78+
it('does not find name not in array', function()
79+
assert.is_nil(utils.find_name_in_array(a, 'unnamed'))
80+
end)
81+
82+
end)
83+
84+
-- ---------------------------------------------------------------------------

0 commit comments

Comments
 (0)