summaryrefslogtreecommitdiff
path: root/iss/tests/test_utils.py
blob: f51c10ea7b71d487e6730e8224001b01cd6ba27c (plain)
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
import pytest

from ..utils import (
    deg_to_cardinal,
    seconds_to_minutes,
    normalize_lat_lng,
    display_lat_lng,
)


def test_seconds_to_minutes():
    cases = [
        (0, "0:00"),
        (1, "0:01"),
        (60, "1:00"),
        (61, "1:01"),
        (1439, "23:59"),
        (1440, "24:00"),
    ]
    for secs, s in cases:
        assert seconds_to_minutes(secs) == s


def test_normalize_lat_lng():
    cases = [
        ("N12.345", "E67.890", 12.345, 67.890),
        ("S12.345", "W67.890", -12.345, -67.890),
        ("S11", "W22", -11, -22),
        ("N0.987", "W0.123", 0.987, -0.123),
        ("S88.765", "E177.654", -88.765, 177.654),
    ]
    for lat, lng, nlat, nlng in cases:
        assert normalize_lat_lng(lat, lng) == (nlat, nlng)


def test_fail_normalize_lat_lng():
    failures = [
        ("12", "E45"),
        ("B12", "E45"),
        ("12", "X45"),
    ]
    for lat, lng in failures:
        with pytest.raises(Exception):
            normalize_lat_lng(lat, lng)


def test_display_lat_lng():
    cases = [
        (12.345, 67.890, "12.345°N", "67.89°E"),
        (12.345, -67.890, "12.345°N", "67.89°W"),
        (-12.345, -67.890, "12.345°S", "67.89°W"),
        (-12.345, 67.890, "12.345°S", "67.89°E"),
    ]
    for lat, lng, dlat, dlng in cases:
        assert display_lat_lng(lat, lng) == (dlat, dlng)


def test_deg_to_cardinal():
    cases = [
        (0, "N"),
        (360, "N"),
        (90, "E"),
        (91.1, "E"),
        (78, "ENE"),
        (182, "S"),
        (255, "WSW"),
        (324.99999, "NW"),
        (345, "NNW"),
        (350, "N"),
    ]
    for deg, cardinal in cases:
        assert deg_to_cardinal(deg) == cardinal