Completed
Push — master ( 9ba889...005d8d )
by Bart
59s
created

TestNSApi.test_trip_stop_without()   A

Complexity

Conditions 1

Size

Total Lines 7

Duplication

Lines 0
Ratio 0 %
Metric Value
cc 1
dl 0
loc 7
rs 9.4285
1
import sys
2
import os
3
import shutil
4
import unittest
5
import hashlib
6
7
import numpy
8
import matplotlib as mpl
9
mpl.use('Agg')  # create plots without running X-server
10
import matplotlib.pyplot as plt
11
import matplotlib.mlab as mlab
12
13
import ns_api
14
15
import nsmaps
16
from nsmaps.local_settings import USERNAME, APIKEY
17
18
19
class TestNSApi(unittest.TestCase):
20
    """ Test case of basic communication with the NS API via the nsapi package. """
21
22
    def setUp(self):
23
        self.nsapi = ns_api.NSAPI(USERNAME, APIKEY)
24
25
    def test_get_station_info(self):
26
        stations = self.nsapi.get_stations()
27
        self.assertEqual(len(stations), 620)
28
29
    def test_trip_stop_without(self):
30
        """ Tests https://github.com/aquatix/ns-api/issues/14 """
31
        timestamp = "04-02-2016 08:00"
32
        start = "Rotterdam Blaak"
33
        via = ""
34
        destination = "Amsterdam Centraal"
35
        trips = self.nsapi.get_trips(timestamp, start, via, destination)
36
37
    def test_no_trips_found(self):
38
        """ Tests https://github.com/aquatix/ns-api/issues/12 """
39
        timestamp = "12-01-2016 08:00"
40
        start = "Utrecht Centraal"
41
        via = ""
42
        destination = "Amsterdam Van der Madeweg"
43
        trips = self.nsapi.get_trips(timestamp, start, via, destination)
44
        self.assertEqual(trips, None)
45
46
47
class TestStationData(unittest.TestCase):
48
    """ Test case for nsmaps station data """
49
50
    def test_update_stations(self):
51
        fileout = 'test_stations.json'
52
        data_dir = '.'
53
        stations = nsmaps.station.Stations(data_dir)
54
        stations.update_station_data(fileout)
55
        for station in stations:
56
            str(station)
57
        self.assertTrue(os.path.exists(fileout))
58
        os.remove(fileout)
59
60
61
class TestStations(unittest.TestCase):
62
63
    @classmethod
64
    def setUpClass(cls):
65
        cls.testdir = './test/'
66
        os.mkdir(cls.testdir)
67
        cls.stations = nsmaps.station.Stations(cls.testdir, test=True)
68
        utrecht = cls.stations.find_station("Utrecht Centraal")
69
        if os.path.exists(utrecht.get_travel_time_filepath()):
70
            os.remove(utrecht.get_travel_time_filepath())
71
72
    @classmethod
73
    def tearDownClass(cls):
74
        shutil.rmtree(cls.testdir)
75
76
    def test_create_stations(self):
77
        stations = nsmaps.station.Stations('.')
78
        self.assertTrue(len(stations.stations) > 0)
79
80
    def test_iterate_stations(self):
81
        for station in self.stations:
82
            station.has_travel_time_data()
83
            str(station)
84
85
    def test_find_station(self):
86
        for station in self.stations:
87
            station_found = self.stations.find_station(station.get_name())
88
            self.assertTrue(station_found)
89
            self.assertEqual(station.get_code(), station_found.get_code())
90
91
    def test_get_station_for_types(self):
92
        types = (
93
            nsmaps.station.StationType.intercitystation,
94
            nsmaps.station.StationType.sneltreinstation,
95
            nsmaps.station.StationType.stoptreinstation,
96
        )
97
        stations_of_type = self.stations.get_stations_for_types(types)
98
        self.assertTrue(stations_of_type)
99
100
    def test_create_travel_times_data(self):
101
        utrecht = self.stations.find_station("Utrecht Centraal")
102
        self.assertTrue(utrecht)
103
        self.stations.create_traveltimes_data([utrecht])
104
        self.assertTrue(os.path.exists(utrecht.get_travel_time_filepath()))
105
        self.assertTrue(utrecht.has_travel_time_data())
106
        self.stations.travel_times_from_json(utrecht.get_travel_time_filepath())
107
        for station in self.stations:
108
            self.assertNotEqual(station.travel_time_min, None)
109
            if station.get_code() != "UT":
110
                self.assertTrue(station.travel_time_min > 0)
111
        self.stations.recreate_missing_destinations()
112
        os.remove(utrecht.get_travel_time_filepath())
113
        self.assertFalse(utrecht.has_travel_time_data())
114
115
116
class TestContourMap(unittest.TestCase):
117
    """ Test case for writing a contour to JSON. """
118
    filename_out = 'test_contour.json'
119
    checksum = '39d2ff2f5cbc9a768e816109f41b3288'
120
    data_dir = './test/'
121
122
    @classmethod
123
    def setUpClass(cls):
124
        if os.path.exists(cls.filename_out):
125
            os.remove(cls.filename_out)  # remove file from any previous tests
126
        # taken from http://matplotlib.org/examples/pylab_examples/contour_demo.html
127
        figure = plt.figure()
128
        ax = figure.add_subplot(111)
129
        delta = 0.025
130
        x = numpy.arange(-3.0, 3.0, delta)
131
        y = numpy.arange(-2.0, 2.0, delta)
132
        X, Y = numpy.meshgrid(x, y)
133
        Z1 = mlab.bivariate_normal(X, Y, 1.0, 1.0, 0.0, 0.0)
134
        Z2 = mlab.bivariate_normal(X, Y, 1.5, 0.5, 1, 1)
135
        Z = 10.0 * (Z2 - Z1)
136
        cls.levels = numpy.linspace(0, 100, num=10)
137
        cls.contour_plot = ax.contour(X, Y, Z, levels=cls.levels, cmap=plt.cm.jet)
138
139
    @classmethod
140
    def tearDownClass(cls):
141
        if os.path.exists(cls.filename_out):
142
            os.remove(cls.filename_out)  # remove file from any previous tests
143
144
    def test_create_json(self):
145
        min_angle = 10
146
        ndigits = 5
147
        nsmaps.contourmap.contour_to_json(self.contour_plot, self.filename_out, self.levels, min_angle, ndigits)
148
        self.assertTrue(os.path.exists(self.filename_out))
149
        with open(self.filename_out, 'rb') as jsonfile:
150
            checksum = hashlib.md5(jsonfile.read()).hexdigest()
151
            self.assertEqual(checksum, self.checksum)
152
153
    def test_contour(self):
154
        os.mkdir(self.data_dir)
155
        stations = nsmaps.station.Stations(self.data_dir, test=True)
156
        utrecht = stations.find_station('Utrecht Centraal')
157
        stations.create_traveltimes_data([utrecht])
158
        config = nsmaps.contourmap.ContourPlotConfig()
159
        config = nsmaps.contourmap.TestConfig()
160
        contour = nsmaps.contourmap.Contour(utrecht, stations, config, self.data_dir)
161
        contour_filepath = os.path.join(self.data_dir, self.filename_out)
162
        contour.create_contour_data(contour_filepath)
163
        self.assertTrue(os.path.exists(contour_filepath))
164
        shutil.rmtree(self.data_dir)
165
166
167
class TestUtilGeo(unittest.TestCase):
168
    """ Test case for utilgeo functions. """
169
170
    def test_lla2ecef_and_ecef2lla(self):
171
        gps = nsmaps.utilgeo.GPS()
172
173
        lla = (0, 0, 0)
174
        ecef = gps.lla2ecef(lla)
175
        self.assertAlmostEqual(ecef[0], 6378137.0)
176
        self.assertAlmostEqual(ecef[1], 0.0)
177
        self.assertAlmostEqual(ecef[2], 0.0)
178
        lla_out = gps.ecef2lla(ecef)
179
        self.assertAlmostEqual(lla, lla_out, 6)
180
181
        lla = (5.1, 52.0, 0)
182
        ecef = gps.lla2ecef(lla)
183
        self.assertAlmostEqual(ecef[0], 3911330.8535259487)
184
        self.assertAlmostEqual(ecef[1], 5006275.196709151)
185
        self.assertAlmostEqual(ecef[2], 563199.3212360762)
186
        lla_out = gps.ecef2lla(ecef)
187
        self.assertAlmostEqual(lla[0], lla_out[0], 6)
188
        self.assertAlmostEqual(lla[1], lla_out[1], 6)
189
        self.assertAlmostEqual(lla[2], lla_out[2], 6)
190
191
192
if __name__ == '__main__':
193
    unittest.main()
194