File: test_examples.py

package info (click to toggle)
pyosmium 4.2.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 5,584 kB
  • sloc: python: 4,400; cpp: 2,504; makefile: 20
file content (80 lines) | stat: -rw-r--r-- 2,301 bytes parent folder | download | duplicates (4)
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
# SPDX-License-Identifier: BSD-2-Clause
#
# This file is part of pyosmium. (https://osmcode.org/pyosmium/)
#
# Copyright (C) 2025 Sarah Hoffmann <lonvia@denofr.de> and others.
# For a full list of authors see the git log.
"""
Tests for all examples.
"""
from pathlib import Path

import pytest

TEST_DIR = (Path(__file__) / '..').resolve()
TEST_FILE = TEST_DIR / 'example-test.pbf'
TEST_DIFF = TEST_DIR / 'example-test.osc'


def run_example(name, *args):
    filename = (TEST_DIR / '..' / "examples" / (name + ".py")).resolve()
    globvars = dict()

    with filename.open("rb") as f:
        exec(compile(f.read(), str(filename), 'exec'), globvars)

    return globvars['main'](*args)


def test_amenity_list(capsys):
    pytest.importorskip("shapely")
    assert 0 == run_example("amenity_list", TEST_FILE)

    output = capsys.readouterr().out.splitlines()

    assert 835 == len(output)
    assert '1.535245 42.556681 parking' == output[0].strip()
    assert '1.570729 42.529562 parking         Aparcament Comunal' == output[-1].strip()


def test_road_length(capsys):
    assert 0 == run_example("road_length", TEST_FILE)

    output = capsys.readouterr().out.strip()

    assert output == "Total way length: 1590.82 km"


def test_pub_names(capsys):
    assert 0 == run_example("pub_names", TEST_FILE)

    output = capsys.readouterr().out.splitlines()

    assert output == ['Kyu', 'Havana Club', "Mulligan's", 'Bar Broques',
                      'The Camden - English Pub', 'Aspen', 'el Raval']


def test_osm_diff_stats(capsys):
    assert 0 == run_example("osm_diff_stats", TEST_DIFF)

    output = capsys.readouterr().out.splitlines()

    assert output == ['Nodes added: 305',
                      'Nodes modified: 192',
                      'Nodes deleted: 20',
                      'Ways added: 31',
                      'Ways modified: 93',
                      'Ways deleted: 0',
                      'Relations added: 0',
                      'Relations modified: 0',
                      'Relations deleted: 0']


def test_osm_file_stats(capsys):
    assert 0 == run_example("osm_file_stats", TEST_FILE)

    output = capsys.readouterr().out.splitlines()

    assert output == ['Nodes: 211100',
                      'Ways: 10315',
                      'Relations: 244']