TestNSApi.test_no_trips_found()   A
last analyzed

Complexity

Conditions 1

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

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