Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ jobs:
pipx install flake8

- name: Lint package
run: flake8 src examples
run: flake8 src examples test

- name: Typecheck package
run: mypy src
Expand Down
29 changes: 19 additions & 10 deletions test/conftest.py
Original file line number Diff line number Diff line change
@@ -1,30 +1,36 @@
# SPDX-License-Identifier: BSD
# SPDX-License-Identifier: BSD-2-Clause
#
# This file is part of Pyosmium.
# This file is part of pyosmium. (https://osmcode.org/pyosmium/)
#
# Copyright (C) 2022 Sarah Hoffmann.
from io import StringIO
# Copyright (C) 2025 Sarah Hoffmann <[email protected]> and others.
# For a full list of authors see the git log.
from pathlib import Path
import sys
import sysconfig
import uuid
from textwrap import dedent

import pytest


SRC_DIR = (Path(__file__) / '..' / '..').resolve()


BUILD_DIR = "build/lib.{}-{}.{}".format(sysconfig.get_platform(),
sys.version_info[0], sys.version_info[1])


if not (SRC_DIR / BUILD_DIR).exists():
BUILD_DIR = "build/lib.{}-{}".format(sysconfig.get_platform(),
sys.implementation.cache_tag)
sys.implementation.cache_tag)

if (SRC_DIR / BUILD_DIR).exists():
sys.path.insert(0, str(SRC_DIR))
sys.path.insert(0, str(SRC_DIR / BUILD_DIR))

import pytest

import osmium as o
import osmium # noqa


@pytest.fixture
def test_data_dir():
Expand Down Expand Up @@ -52,27 +58,30 @@ def _mkfile(data):

return _mkfile


@pytest.fixture
def opl_buffer(to_opl):

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

return _mkbuffer


@pytest.fixture
def opl_reader(opl_buffer):

def _mkbuffer(data):
return o.io.Reader(opl_buffer(data))
return osmium.io.Reader(opl_buffer(data))

return _mkbuffer


@pytest.fixture
def simple_handler(to_opl):

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

return _run
8 changes: 8 additions & 0 deletions test/helpers.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,20 @@
# SPDX-License-Identifier: BSD-2-Clause
#
# This file is part of pyosmium. (https://osmcode.org/pyosmium/)
#
# Copyright (C) 2025 Sarah Hoffmann <[email protected]> and others.
# For a full list of authors see the git log.
""" Provides some helper functions for test.
"""
from datetime import datetime, timezone

import osmium


def mkdate(*args):
return datetime(*args, tzinfo=timezone.utc)


class CountingHandler(osmium.SimpleHandler):

def __init__(self):
Expand Down
31 changes: 17 additions & 14 deletions test/test_area.py
Original file line number Diff line number Diff line change
@@ -1,45 +1,48 @@
# SPDX-License-Identifier: BSD
# SPDX-License-Identifier: BSD-2-Clause
#
# This file is part of Pyosmium.
# This file is part of pyosmium. (https://osmcode.org/pyosmium/)
#
# Copyright (C) 2024 Sarah Hoffmann.
# Copyright (C) 2025 Sarah Hoffmann <[email protected]> and others.
# For a full list of authors see the git log.
from pathlib import Path

import osmium as o
import osmium

from helpers import CountingHandler


TEST_FILE = str((Path(__file__) / '..' / 'example-test.pbf').resolve())


def test_area_handler():
area = o.area.AreaManager()
area = osmium.area.AreaManager()

o.apply(o.io.Reader(TEST_FILE), area.first_pass_handler())
osmium.apply(osmium.io.Reader(TEST_FILE), area.first_pass_handler())

ch_area = CountingHandler()
ch_others = CountingHandler()

lh = o.NodeLocationsForWays(o.index.create_map("flex_mem"))
lh = osmium.NodeLocationsForWays(osmium.index.create_map("flex_mem"))
lh.ignore_errors()

o.apply(o.io.Reader(TEST_FILE), lh,
ch_others, area.second_pass_handler(ch_area))
osmium.apply(osmium.io.Reader(TEST_FILE), lh,
ch_others, area.second_pass_handler(ch_area))

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


def test_area_buffer_handler():
area = o.area.AreaManager()
area = osmium.area.AreaManager()

o.apply(o.io.Reader(TEST_FILE), area.first_pass_handler())
osmium.apply(osmium.io.Reader(TEST_FILE), area.first_pass_handler())

lh = o.NodeLocationsForWays(o.index.create_map("flex_mem"))
lh = osmium.NodeLocationsForWays(osmium.index.create_map("flex_mem"))
lh.ignore_errors()

buf = o.BufferIterator()
buf = osmium.BufferIterator()

o.apply(o.io.Reader(TEST_FILE), lh, area.second_pass_to_buffer(buf))
osmium.apply(osmium.io.Reader(TEST_FILE), lh, area.second_pass_to_buffer(buf))

counts = 0
for obj in buf:
Expand Down
11 changes: 6 additions & 5 deletions test/test_back_reference_writer.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@
#
# This file is part of pyosmium. (https://osmcode.org/pyosmium/)
#
# Copyright (C) 2024 Sarah Hoffmann <[email protected]> and others.
# Copyright (C) 2025 Sarah Hoffmann <[email protected]> and others.
# For a full list of authors see the git log.
import pytest

import osmium as o
import osmium

from helpers import IDCollector


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

Expand All @@ -19,11 +20,11 @@ class TestWay:

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

with o.BackReferenceWriter(outfile, ref_file) as writer:
with osmium.BackReferenceWriter(outfile, ref_file) as writer:
writer.add_way(TestWay())

ids = IDCollector()
o.apply(outfile, ids)
osmium.apply(outfile, ids)

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

with pytest.raises(RuntimeError, match="inner error"):
with o.BackReferenceWriter(str(outfile), ref_file) as writer:
with osmium.BackReferenceWriter(str(outfile), ref_file):
raise RuntimeError("inner error")

assert not outfile.exists()
40 changes: 27 additions & 13 deletions test/test_dangling_references.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
# SPDX-License-Identifier: BSD
# SPDX-License-Identifier: BSD-2-Clause
#
# This file is part of Pyosmium.
# This file is part of pyosmium. (https://osmcode.org/pyosmium/)
#
# Copyright (C) 2022 Sarah Hoffmann.

# Copyright (C) 2025 Sarah Hoffmann <[email protected]> and others.
# For a full list of authors see the git log.
from pathlib import Path

import pytest
import osmium

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

import osmium as o

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

def test_keep_reference(self):
h = o.make_simple_handler(node=self.node, way=self.way,
relation=self.relation, area=self.area)
h = osmium.make_simple_handler(node=self.node, way=self.way,
relation=self.relation, area=self.area)
h.apply_file(TEST_DIR / 'example-test.osc')
assert self.refkeeper

Expand All @@ -40,7 +40,7 @@ def test_keep_reference(self):
repr(obj)

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


class TestKeepWayRef(DanglingReferenceBase):

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


class TestKeepRelationRef(DanglingReferenceBase):

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


class TestKeepAreaRef(DanglingReferenceBase):

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


class TestKeepNodeTagsRef(DanglingReferenceBase):

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


class TestKeepWayTagsRef(DanglingReferenceBase):

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


class TestKeepRelationTagsRef(DanglingReferenceBase):

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


class TestKeepAreaTagsRef(DanglingReferenceBase):

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


class TestKeepTagListIterator(DanglingReferenceBase):

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


class TestKeepOuterRingIterator(DanglingReferenceBase):

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


class TestKeepOuterRing(DanglingReferenceBase):

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


class TestKeepInnerRingIterator(DanglingReferenceBase):

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


class TestKeepInnerRing(DanglingReferenceBase):

def area(self, r):
for outer in r.outer_rings():
for inner in r.inner_rings(outer):
self.keep(inner, lambda t: len(t))


class TestKeepRelationMemberIterator(DanglingReferenceBase):

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



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

def test_keep_reference(self):
h = o.make_simple_handler(node=self.node, way=self.way,
relation=self.relation, area=self.area)
h = osmium.make_simple_handler(node=self.node, way=self.way,
relation=self.relation, area=self.area)
h.apply_file(TEST_DIR / 'example-test.pbf')
assert self.refkeeper

for obj, func in self.refkeeper:
func(obj)

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

def node(self, n):
self.keep(n.location, lambda l: l.x)
self.keep(n.location, lambda loc: loc.x)


class TestKeepNode(NotADanglingReferenceBase):

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


class TestKeepMember(NotADanglingReferenceBase):

def relation(self, r):
Expand Down
Loading