File: test_geo_rss_distance_helper.py

package info (click to toggle)
python-georss-client 0.18-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 320 kB
  • sloc: python: 1,582; xml: 430; makefile: 8; sh: 5
file content (76 lines) | stat: -rw-r--r-- 2,433 bytes parent folder | download
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
"""Tests for georss distance helper."""

from unittest.mock import MagicMock

import pytest

from georss_client.geo_rss_distance_helper import GeoRssDistanceHelper
from georss_client.xml_parser.geometry import Point, Polygon


def test_extract_coordinates_from_point():
    """Test extracting coordinates from point."""
    mock_point = Point(-30.0, 151.0)
    latitude, longitude = GeoRssDistanceHelper.extract_coordinates(mock_point)
    assert latitude == -30.0
    assert longitude == 151.0


def test_extract_coordinates_from_polygon():
    """Test extracting coordinates from polygon."""
    mock_polygon = Polygon(
        [
            Point(-30.0, 151.0),
            Point(-30.0, 151.5),
            Point(-30.5, 151.5),
            Point(-30.5, 151.0),
            Point(-30.0, 151.0),
        ]
    )
    latitude, longitude = GeoRssDistanceHelper.extract_coordinates(mock_polygon)
    assert latitude == pytest.approx(-30.2, 0.1)
    assert longitude == pytest.approx(151.2, 0.1)


def test_extract_coordinates_from_unsupported_geometry():
    """Test extracting coordinates from unsupported geometry."""
    mock_unsupported_geometry = MagicMock()
    latitude, longitude = GeoRssDistanceHelper.extract_coordinates(
        mock_unsupported_geometry
    )
    assert latitude is None
    assert longitude is None


def test_distance_to_point():
    """Test calculating distance to point."""
    home_coordinates = [-31.0, 150.0]
    mock_point = Point(-30.0, 151.0)
    distance = GeoRssDistanceHelper.distance_to_geometry(home_coordinates, mock_point)
    assert distance == pytest.approx(146.8, 0.1)


def test_distance_to_polygon():
    """Test calculating distance to point."""
    home_coordinates = [-31.0, 150.0]
    mock_polygon = Polygon(
        [
            Point(-30.0, 151.0),
            Point(-30.0, 151.5),
            Point(-30.5, 151.5),
            Point(-30.5, 151.0),
            Point(-30.0, 151.0),
        ]
    )
    distance = GeoRssDistanceHelper.distance_to_geometry(home_coordinates, mock_polygon)
    assert distance == pytest.approx(110.6, 0.1)


def test_distance_to_unsupported_geometry():
    """Test calculating distance to unsupported geometry."""
    home_coordinates = [-31.0, 150.0]
    mock_unsupported_geometry = MagicMock()
    distance = GeoRssDistanceHelper.distance_to_geometry(
        home_coordinates, mock_unsupported_geometry
    )
    assert distance == float("inf")