File: usace_swtwc_test.py

package info (click to toggle)
python-ulmo 0.8.8%2Bdfsg1-1.1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 1,064 kB
  • sloc: python: 6,135; makefile: 144; sh: 5
file content (86 lines) | stat: -rw-r--r-- 2,986 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
import pytest

import ulmo
import test_util


def test_get_stations():
    stations_file = 'usace/swtwc/shefids.html'
    with test_util.mocked_urls(stations_file):
        stations = ulmo.usace.swtwc.get_stations()

    test_stations = [
        {'code': u'DSNT2', 'description': u'Lake Texoma, Denison Dam'},
        {'code': u'MYST2', 'description': u'Pat Mayse Lake'},
    ]

    for test_station in test_stations:
        assert stations[test_station['code']] == test_station
    assert 700 <= len(stations) <= 800


def test_get_station_data():
    test_station_data = [
        ('DSNT2', '2017-07-25', {
            'code': 'DSNT2',
            'description': 'Lake Texoma, Denison Dam',
            'station_type': 'Reservoir',
            'timezone': 'US/Central',
            'values': {
                '2017-07-25 01:00:00': {
                    'AIR-TEMP': 80.10,
                    'BAT-LOAD': 11.37,
                    'ELEVATION': 617.73,
                    'INFLOW': 564,
                    'PRECIP': 0.0,
                    'PRECIP(A)': 0.0,
                    'REL-HUMID': 81.98,
                    'RELEASE': 19,
                    'SOLAR-RAD': 0.0,
                    'STORAGE': 2572324.0,
                    'VOLTAGE': 11.77,
                    'WIND-DIR': 135.90,
                    'WIND-SPEED': 1.42,
                    'TW-ELEV': 501.87
                }
            },
        }),
    ]

    for code, date, test_data in test_station_data:
        url_date = date.replace('-', '')
        filename = '%s.%s.html' % (code, url_date)
        data_file = 'usace/swtwc/' + filename
        with test_util.mocked_urls(data_file):
            station_data = ulmo.usace.swtwc.get_station_data(code, date)

        for key, value in test_data.items():
            if key == 'values':
                _compare_values(test_data['values'], station_data['values'])
            else:
                assert station_data[key] == test_data[key]


def test_get_station_data_current():
    # can't easily test current since it is a moving target changes, but mostly
    # just make sure it parses correctl: current will have '---' values where
    # previous days do not
    data_file = 'usace/swtwc/DSNT2.current.html'
    with test_util.mocked_urls(data_file):
        station_data = ulmo.usace.swtwc.get_station_data('DSNT2')
    assert len(station_data.get('values')) > 0


def test_get_station_data_out_of_range():
    # can't easily test current since it is a moving target changes, but mostly
    # just make sure it parses correctl: current will have '---' values where
    # previous days do not
    data_file = 'usace/swtwc/empty.html'
    with test_util.mocked_urls(data_file):
        with pytest.raises(ValueError):
            station_data = ulmo.usace.swtwc.get_station_data('MYST2', '1945-01-01')


def _compare_values(test_values, station_values):
    for key, test_value in test_values.items():
        assert station_values[key] == test_value