File: test_urls.py

package info (click to toggle)
python-buienradar 1.0.6-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,380 kB
  • sloc: xml: 9,450; python: 3,432; makefile: 5
file content (127 lines) | stat: -rw-r--r-- 3,629 bytes parent folder | download | duplicates (2)
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
"""Test for URL utilities."""
from urllib.parse import parse_qs, urlparse

from buienradar.urls import (
    json_precipitation_forecast_url,
    radar_url,
    xml_precipitation_forecast_url
)


def parse_qs_dict(url):
    """
    Parse a url and get a dictionary of query string elements in a dictionary.

    Raises ValueError when an element is repeated (to safely return a dict of
    strings instead of an dict of lists of strings.
    """
    token_dict = parse_qs(urlparse(url).query)
    res = {}

    for k, val in token_dict.items():
        if len(val) > 1:
            raise ValueError('Key {} has {} values'.format(k, len(val)))

        res[k] = val[0]

    return res


def test_base_urls():
    """Test the url functions."""
    json_url = json_precipitation_forecast_url(1.23, 4.56)
    xml_url = xml_precipitation_forecast_url(1.23, 4.56)

    assert 'https://gpsgadget.buienradar.nl/data/raintext?' in json_url
    assert 'http://gadgets.buienradar.nl/data/raintext/?' in xml_url

    assert 'https://api.buienradar.nl/image/1.0/RadarMapNL?' in radar_url()


def test_util():
    """Test the utility function for dictionaries."""
    try:
        res = parse_qs_dict("http://example.org/?foo=1&bar=2")
        assert res['foo'] == '1'
        assert res['bar'] == '2'

        parse_qs_dict("http://example.org/?foo=1&foo=2")
        # Unreachable
        assert False
    except ValueError:
        assert True


def test_precipitation_forecast_url():
    """Test both variations of the precipation forcecast URL-builder."""
    for func in (xml_precipitation_forecast_url,
                 json_precipitation_forecast_url):
        # should return an url including identical 2-decimal lat/lon
        url = func(1.23, 4.56)
        tokens = parse_qs_dict(url)

        assert tokens['lat'] == '1.23' and tokens['lon'] == '4.56'

        # should return an url with truncated values
        url = func(1.23456, 7.890123)
        tokens = parse_qs_dict(url)

        assert tokens['lat'] == '1.23' and tokens['lon'] == '7.89'

        # that are rounded (up)
        url = func(1.235, 7.890123)
        tokens = parse_qs_dict(url)

        assert tokens['lat'] == '1.24'


def test_radar_url():
    """
    Test the generated radar url's.

      * Check that it echoes the correct values in the result.
      * Check that it excepts the extreme values of it's domain.

    Pairs used are valid size and some minimum/maximum sizes.
    """
    # test default value:
    tokens = parse_qs_dict(radar_url())
    default_w = int(tokens['w'])
    default_h = int(tokens['h'])

    assert (default_w >= 120 and default_w <= 700 and
            default_h >= 120 and default_h <= 700)

    # test pairs:
    for width, height in [(120, 120),
                          (300, 300),
                          (700, 700),
                          (700, 765)]:
        tokens = parse_qs_dict(radar_url(width, height))
        assert tokens['w'] == str(width) and tokens['h'] == str(height)


def test_radar_url_bounds():
    """Test illegal values for radar image url."""
    # outside of minimum/maximum width
    for width in [119, 701]:
        try:
            radar_url(width, 125)
            assert False
        except ValueError:
            assert True

    # outside of minimum/maximum height
    for height in [119, 766]:
        try:
            radar_url(125, height)
            assert False
        except ValueError:
            assert True

    # Check that it also raises when both are out of range
    try:
        radar_url(1234, 4567)
        assert False
    except ValueError:
        assert True