Skip to content

Commit 9d7d585

Browse files
authored
Merge pull request #295 from lonvia/flake-fix-tests
Add linting for tests
2 parents b81b9cd + 8f82f48 commit 9d7d585

30 files changed

+554
-432
lines changed

.github/workflows/ci.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ jobs:
1818
pipx install flake8
1919
2020
- name: Lint package
21-
run: flake8 src examples
21+
run: flake8 src examples test
2222

2323
- name: Typecheck package
2424
run: mypy src

test/conftest.py

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,36 @@
1-
# SPDX-License-Identifier: BSD
1+
# SPDX-License-Identifier: BSD-2-Clause
22
#
3-
# This file is part of Pyosmium.
3+
# This file is part of pyosmium. (https://osmcode.org/pyosmium/)
44
#
5-
# Copyright (C) 2022 Sarah Hoffmann.
6-
from io import StringIO
5+
# Copyright (C) 2025 Sarah Hoffmann <[email protected]> and others.
6+
# For a full list of authors see the git log.
77
from pathlib import Path
88
import sys
99
import sysconfig
1010
import uuid
1111
from textwrap import dedent
1212

13+
import pytest
14+
15+
1316
SRC_DIR = (Path(__file__) / '..' / '..').resolve()
1417

18+
1519
BUILD_DIR = "build/lib.{}-{}.{}".format(sysconfig.get_platform(),
1620
sys.version_info[0], sys.version_info[1])
21+
22+
1723
if not (SRC_DIR / BUILD_DIR).exists():
1824
BUILD_DIR = "build/lib.{}-{}".format(sysconfig.get_platform(),
19-
sys.implementation.cache_tag)
25+
sys.implementation.cache_tag)
2026

2127
if (SRC_DIR / BUILD_DIR).exists():
2228
sys.path.insert(0, str(SRC_DIR))
2329
sys.path.insert(0, str(SRC_DIR / BUILD_DIR))
2430

25-
import pytest
2631

27-
import osmium as o
32+
import osmium # noqa
33+
2834

2935
@pytest.fixture
3036
def test_data_dir():
@@ -52,27 +58,30 @@ def _mkfile(data):
5258

5359
return _mkfile
5460

61+
5562
@pytest.fixture
5663
def opl_buffer(to_opl):
5764

5865
def _mkbuffer(data):
59-
return o.io.FileBuffer(to_opl(data).encode('utf-8'), 'opl')
66+
return osmium.io.FileBuffer(to_opl(data).encode('utf-8'), 'opl')
6067

6168
return _mkbuffer
6269

70+
6371
@pytest.fixture
6472
def opl_reader(opl_buffer):
6573

6674
def _mkbuffer(data):
67-
return o.io.Reader(opl_buffer(data))
75+
return osmium.io.Reader(opl_buffer(data))
6876

6977
return _mkbuffer
7078

79+
7180
@pytest.fixture
7281
def simple_handler(to_opl):
7382

7483
def _run(data, node=None, way=None, relation=None, area=None, locations=False):
75-
handler = o.make_simple_handler(node=node, way=way, relation=relation, area=area)
84+
handler = osmium.make_simple_handler(node=node, way=way, relation=relation, area=area)
7685
handler.apply_buffer(to_opl(data).encode('utf-8'), 'opl', locations=locations)
7786

7887
return _run

test/helpers.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,20 @@
1+
# SPDX-License-Identifier: BSD-2-Clause
2+
#
3+
# This file is part of pyosmium. (https://osmcode.org/pyosmium/)
4+
#
5+
# Copyright (C) 2025 Sarah Hoffmann <[email protected]> and others.
6+
# For a full list of authors see the git log.
17
""" Provides some helper functions for test.
28
"""
39
from datetime import datetime, timezone
410

511
import osmium
612

13+
714
def mkdate(*args):
815
return datetime(*args, tzinfo=timezone.utc)
916

17+
1018
class CountingHandler(osmium.SimpleHandler):
1119

1220
def __init__(self):

test/test_area.py

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,48 @@
1-
# SPDX-License-Identifier: BSD
1+
# SPDX-License-Identifier: BSD-2-Clause
22
#
3-
# This file is part of Pyosmium.
3+
# This file is part of pyosmium. (https://osmcode.org/pyosmium/)
44
#
5-
# Copyright (C) 2024 Sarah Hoffmann.
5+
# Copyright (C) 2025 Sarah Hoffmann <[email protected]> and others.
6+
# For a full list of authors see the git log.
67
from pathlib import Path
78

8-
import osmium as o
9+
import osmium
910

1011
from helpers import CountingHandler
1112

13+
1214
TEST_FILE = str((Path(__file__) / '..' / 'example-test.pbf').resolve())
1315

16+
1417
def test_area_handler():
15-
area = o.area.AreaManager()
18+
area = osmium.area.AreaManager()
1619

17-
o.apply(o.io.Reader(TEST_FILE), area.first_pass_handler())
20+
osmium.apply(osmium.io.Reader(TEST_FILE), area.first_pass_handler())
1821

1922
ch_area = CountingHandler()
2023
ch_others = CountingHandler()
2124

22-
lh = o.NodeLocationsForWays(o.index.create_map("flex_mem"))
25+
lh = osmium.NodeLocationsForWays(osmium.index.create_map("flex_mem"))
2326
lh.ignore_errors()
2427

25-
o.apply(o.io.Reader(TEST_FILE), lh,
26-
ch_others, area.second_pass_handler(ch_area))
28+
osmium.apply(osmium.io.Reader(TEST_FILE), lh,
29+
ch_others, area.second_pass_handler(ch_area))
2730

2831
assert ch_area.counts == [0, 0, 0, 5239]
2932
assert ch_others.counts == [211100, 10315, 244, 0]
3033

3134

3235
def test_area_buffer_handler():
33-
area = o.area.AreaManager()
36+
area = osmium.area.AreaManager()
3437

35-
o.apply(o.io.Reader(TEST_FILE), area.first_pass_handler())
38+
osmium.apply(osmium.io.Reader(TEST_FILE), area.first_pass_handler())
3639

37-
lh = o.NodeLocationsForWays(o.index.create_map("flex_mem"))
40+
lh = osmium.NodeLocationsForWays(osmium.index.create_map("flex_mem"))
3841
lh.ignore_errors()
3942

40-
buf = o.BufferIterator()
43+
buf = osmium.BufferIterator()
4144

42-
o.apply(o.io.Reader(TEST_FILE), lh, area.second_pass_to_buffer(buf))
45+
osmium.apply(osmium.io.Reader(TEST_FILE), lh, area.second_pass_to_buffer(buf))
4346

4447
counts = 0
4548
for obj in buf:

test/test_back_reference_writer.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,15 @@
22
#
33
# This file is part of pyosmium. (https://osmcode.org/pyosmium/)
44
#
5-
# Copyright (C) 2024 Sarah Hoffmann <[email protected]> and others.
5+
# Copyright (C) 2025 Sarah Hoffmann <[email protected]> and others.
66
# For a full list of authors see the git log.
77
import pytest
88

9-
import osmium as o
9+
import osmium
1010

1111
from helpers import IDCollector
1212

13+
1314
def test_simple_way(test_data, tmp_path):
1415
ref_file = test_data('\n'.join((f"n{i} x2 y3" for i in range(10))))
1516

@@ -19,11 +20,11 @@ class TestWay:
1920

2021
outfile = str(tmp_path / 'test.osm')
2122

22-
with o.BackReferenceWriter(outfile, ref_file) as writer:
23+
with osmium.BackReferenceWriter(outfile, ref_file) as writer:
2324
writer.add_way(TestWay())
2425

2526
ids = IDCollector()
26-
o.apply(outfile, ids)
27+
osmium.apply(outfile, ids)
2728

2829
assert ids.nodes == [3, 5, 6]
2930
assert ids.ways == [34]
@@ -35,7 +36,7 @@ def test_do_not_write_on_exception(test_data, tmp_path):
3536
outfile = tmp_path / 'test.osm'
3637

3738
with pytest.raises(RuntimeError, match="inner error"):
38-
with o.BackReferenceWriter(str(outfile), ref_file) as writer:
39+
with osmium.BackReferenceWriter(str(outfile), ref_file):
3940
raise RuntimeError("inner error")
4041

4142
assert not outfile.exists()

test/test_dangling_references.py

Lines changed: 27 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
1-
# SPDX-License-Identifier: BSD
1+
# SPDX-License-Identifier: BSD-2-Clause
22
#
3-
# This file is part of Pyosmium.
3+
# This file is part of pyosmium. (https://osmcode.org/pyosmium/)
44
#
5-
# Copyright (C) 2022 Sarah Hoffmann.
6-
5+
# Copyright (C) 2025 Sarah Hoffmann <[email protected]> and others.
6+
# For a full list of authors see the git log.
77
from pathlib import Path
88

99
import pytest
10+
import osmium
1011

1112
TEST_DIR = (Path(__file__) / '..').resolve()
1213

13-
import osmium as o
1414

1515
class DanglingReferenceBase:
1616
""" Base class for tests that try to keep a reference to the object
@@ -27,8 +27,8 @@ def keep(self, obj, func):
2727
self.refkeeper.append((obj, func))
2828

2929
def test_keep_reference(self):
30-
h = o.make_simple_handler(node=self.node, way=self.way,
31-
relation=self.relation, area=self.area)
30+
h = osmium.make_simple_handler(node=self.node, way=self.way,
31+
relation=self.relation, area=self.area)
3232
h.apply_file(TEST_DIR / 'example-test.osc')
3333
assert self.refkeeper
3434

@@ -40,7 +40,7 @@ def test_keep_reference(self):
4040
repr(obj)
4141

4242
def test_keep_reference_generator(self):
43-
for obj in o.FileProcessor(TEST_DIR / 'example-test.osc').with_areas():
43+
for obj in osmium.FileProcessor(TEST_DIR / 'example-test.osc').with_areas():
4444
if obj.type_str() == 'n' and self.node is not None:
4545
self.node(obj)
4646
elif obj.type_str() == 'w' and self.way is not None:
@@ -65,77 +65,89 @@ class TestKeepNodeRef(DanglingReferenceBase):
6565
def node(self, n):
6666
self.keep(n, lambda n: n.id)
6767

68+
6869
class TestKeepWayRef(DanglingReferenceBase):
6970

7071
def way(self, w):
7172
self.keep(w, lambda n: n.id)
7273

74+
7375
class TestKeepRelationRef(DanglingReferenceBase):
7476

7577
def relation(self, r):
7678
self.keep(r, lambda n: n.id)
7779

80+
7881
class TestKeepAreaRef(DanglingReferenceBase):
7982

8083
def area(self, a):
8184
self.keep(a, lambda n: n.id)
8285

86+
8387
class TestKeepNodeTagsRef(DanglingReferenceBase):
8488

8589
def node(self, n):
8690
self.keep(n.tags, lambda t: 'foo' in t)
8791

92+
8893
class TestKeepWayTagsRef(DanglingReferenceBase):
8994

9095
def way(self, w):
9196
self.keep(w.tags, lambda t: 'foo' in t)
9297

98+
9399
class TestKeepRelationTagsRef(DanglingReferenceBase):
94100

95101
def relation(self, r):
96102
self.keep(r.tags, lambda t: 'foo' in t)
97103

104+
98105
class TestKeepAreaTagsRef(DanglingReferenceBase):
99106

100107
def area(self, a):
101108
self.keep(a.tags, lambda t: 'foo' in t)
102109

110+
103111
class TestKeepTagListIterator(DanglingReferenceBase):
104112

105113
def node(self, n):
106114
self.keep(n.tags.__iter__(), lambda t: next(t))
107115

116+
108117
class TestKeepOuterRingIterator(DanglingReferenceBase):
109118

110119
def area(self, r):
111120
self.keep(r.outer_rings(), lambda t: next(t))
112121

122+
113123
class TestKeepOuterRing(DanglingReferenceBase):
114124

115125
def area(self, r):
116126
for ring in r.outer_rings():
117127
self.keep(ring, lambda t: len(t))
118128

129+
119130
class TestKeepInnerRingIterator(DanglingReferenceBase):
120131

121132
def area(self, r):
122133
for ring in r.outer_rings():
123134
self.keep(r.inner_rings(ring), lambda t: next(t))
124135

136+
125137
class TestKeepInnerRing(DanglingReferenceBase):
126138

127139
def area(self, r):
128140
for outer in r.outer_rings():
129141
for inner in r.inner_rings(outer):
130142
self.keep(inner, lambda t: len(t))
131143

144+
132145
class TestKeepRelationMemberIterator(DanglingReferenceBase):
133146

134147
def relation(self, r):
135148
self.keep(r.members, lambda t: next(t))
136149

137150

138-
139151
class NotADanglingReferenceBase:
140152
""" Base class for tests that ensure that the callback does not
141153
bail out because of dangling references when POD types are
@@ -152,16 +164,16 @@ def keep(self, obj, func):
152164
self.refkeeper.append((obj, func))
153165

154166
def test_keep_reference(self):
155-
h = o.make_simple_handler(node=self.node, way=self.way,
156-
relation=self.relation, area=self.area)
167+
h = osmium.make_simple_handler(node=self.node, way=self.way,
168+
relation=self.relation, area=self.area)
157169
h.apply_file(TEST_DIR / 'example-test.pbf')
158170
assert self.refkeeper
159171

160172
for obj, func in self.refkeeper:
161173
func(obj)
162174

163175
def test_keep_reference_generator(self):
164-
for obj in o.FileProcessor(TEST_DIR / 'example-test.pbf').with_areas():
176+
for obj in osmium.FileProcessor(TEST_DIR / 'example-test.pbf').with_areas():
165177
if obj.is_node() and self.node is not None:
166178
self.node(obj)
167179
elif obj.is_way() and self.way is not None:
@@ -180,14 +192,16 @@ def test_keep_reference_generator(self):
180192
class TestKeepLocation(NotADanglingReferenceBase):
181193

182194
def node(self, n):
183-
self.keep(n.location, lambda l: l.x)
195+
self.keep(n.location, lambda loc: loc.x)
196+
184197

185198
class TestKeepNode(NotADanglingReferenceBase):
186199

187200
def node(self, n):
188201
for t in n.tags:
189202
self.keep(t, lambda t: t.k)
190203

204+
191205
class TestKeepMember(NotADanglingReferenceBase):
192206

193207
def relation(self, r):

0 commit comments

Comments
 (0)