]> git.openstreetmap.org Git - nominatim.git/blob - test/python/utils/test_centroid.py
enable flake for Python tests
[nominatim.git] / test / python / utils / test_centroid.py
1 # SPDX-License-Identifier: GPL-3.0-or-later
2 #
3 # This file is part of Nominatim. (https://nominatim.org)
4 #
5 # Copyright (C) 2025 by the Nominatim developer community.
6 # For a full list of authors see the git log.
7 """
8 Tests for centroid computation.
9 """
10 import pytest
11
12 from nominatim_db.utils.centroid import PointsCentroid
13
14
15 def test_empty_set():
16     c = PointsCentroid()
17
18     with pytest.raises(ValueError, match='No points'):
19         c.centroid()
20
21
22 @pytest.mark.parametrize("centroid", [(0, 0), (-1, 3), [0.0000032, 88.4938]])
23 def test_one_point_centroid(centroid):
24     c = PointsCentroid()
25
26     c += centroid
27
28     assert len(c.centroid()) == 2
29     assert c.centroid() == (pytest.approx(centroid[0]), pytest.approx(centroid[1]))
30
31
32 def test_multipoint_centroid():
33     c = PointsCentroid()
34
35     c += (20.0, -10.0)
36     assert c.centroid() == (pytest.approx(20.0), pytest.approx(-10.0))
37     c += (20.2, -9.0)
38     assert c.centroid() == (pytest.approx(20.1), pytest.approx(-9.5))
39     c += (20.2, -9.0)
40     assert c.centroid() == (pytest.approx(20.13333), pytest.approx(-9.333333))
41
42
43 def test_manypoint_centroid():
44     c = PointsCentroid()
45
46     for _ in range(10000):
47         c += (4.564732, -0.000034)
48
49     assert c.centroid() == (pytest.approx(4.564732), pytest.approx(-0.000034))
50
51
52 @pytest.mark.parametrize("param", ["aa", None, 5, [1, 2, 3], (3, None), ("a", 3.9)])
53 def test_add_non_tuple(param):
54     c = PointsCentroid()
55
56     with pytest.raises(ValueError, match='2-element tuples'):
57         c += param