aboutsummaryrefslogtreecommitdiff
path: root/tests/test_api.py
blob: bb79677dec8832d67cf82bafe8a18c175c5d08c6 (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
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
# -*- coding: utf-8 -*-
import datetime
import json
import unittest

from bvggrabber.api import QueryApi, Departure, Response, compute_remaining
from bvggrabber.utils.format import fullformat


class TestFunctions(unittest.TestCase):

    def test_compute_remaining(self):
        dates = [datetime.datetime(2013, 1, 2, 3, 4, 0),
                 datetime.datetime(2013, 1, 2, 3, 4, 1),
                 datetime.datetime(2013, 1, 2, 3, 4, 59),
                 datetime.datetime(2013, 1, 2, 3, 5, 0),
                 datetime.datetime(2013, 1, 2, 3, 5, 1),
                 datetime.datetime(2013, 1, 2, 3, 5, 59)]

        target = [[0, 0, 0, 60, 60, 60],
                  [-60, 0, 0, 0, 60, 60],
                  [-60, -60, 0, 0, 0, 60],
                  [-60, -60, -60, 0, 0, 0],
                  [-120, -60, -60, -60, 0, 0],
                  [-120, -120, -60, -60, -60, 0]]
        for start in range(len(dates)):
            for end in range(len(dates)):
                s = dates[start]
                e = dates[end]
                t = target[start][end]
                self.assertEqual(compute_remaining(s, e),
                                 target[start][end],
                                 "Start: %s; End: %s; target: %d" % (
                                     fullformat(s), fullformat(e), t))

    def test_compute_remaining_error(self):
        self.assertRaises(TypeError, compute_remaining,
                          1357092240, datetime.datetime(2013, 1, 2, 3, 4, 1))
        self.assertRaises(TypeError, compute_remaining,
                          datetime.datetime(2013, 1, 2, 3, 4, 0), 1357092241)
        self.assertRaises(TypeError, compute_remaining,
                          1357092240, 1357092241)


class TestResponse(unittest.TestCase):

    def test_init_ambiguous(self):
        response = Response(False, ['s1', 's2'])
        self.assertFalse(response.state)
        self.assertEqual(str(response.error), "Station is ambiguous: s1, s2")

    def test_init_not_found(self):
        response = Response(False)
        self.assertFalse(response.state)
        self.assertEqual(str(response.error), "Station does not exist")

    def test_init_error_string(self):
        response = Response(False, error="An error occurred")
        self.assertFalse(response.state)
        self.assertEqual(str(response.error), "An error occurred")

    def test_merge(self):
        departures = [Departure("ERP", "HBF",
                                datetime.datetime(2013, 1, 2, 3, 4, 1), "U2"),
                      Departure("HBF", "ERP",
                                datetime.datetime(2013, 2, 1, 3, 4, 1), "U55")]
        departures2 = [Departure("ERP", "HBF",
                                 datetime.datetime(2013, 1, 2, 3, 4, 1), "U6"),
                       Departure("HBF", "ERP",
                                 datetime.datetime(2013, 2, 1, 3, 4, 1), "U9")]
        allDepartures = [("Marchbrücke", departures),
                         ("S Tiergarten", departures2)]
        r1 = Response(True, "Marchbrücke", departures)
        r2 = Response(True, "S Tiergarten", departures2)
        r3 = Response(False, [])
        self.assertRaises(ValueError, r1.merge, r3)
        self.assertRaises(ValueError, r3.merge, r2)
        self.assertRaises(TypeError, r1.merge, departures)
        r1.merge(r2)
        self.assertEqual(r1.departures, allDepartures)

    def test_json_success(self):
        since = datetime.datetime(2013, 1, 2, 3, 0, 0)
        departures = [Departure("ZOO", "HBF",
                                datetime.datetime(2013, 1, 2, 3, 4, 0), "S5",
                                since=since),
                      Departure("ZOO", "ERP",
                                datetime.datetime(2013, 1, 2, 3, 5, 0), "U2",
                                since=since)]
        json1 = [['ZOO', [{
                'start': 'ZOO',
                'line': 'S5',
                'end': 'HBF',
                'remaining': 240, 
            }, {
                'start': 'ZOO',
                'line': 'U2',
                'end': 'ERP',
                'remaining': 300, 
            },
        ]]]
        response = Response(True, "ZOO", departures)
        self.assertEqual(json.loads(response.json), json1)

    def test_json_failure(self):
        response = Response(False, ['ERP', 'ZOO'])
        self.assertEqual(response.json, json.dumps("Station is ambiguous: ERP, ZOO"))

    def test_departures_failure(self):
        response = Response(False, ['ERP', 'ZOO'])
        self.assertEqual(response.departures, "Station is ambiguous: ERP, ZOO")


class TestQueryApi(unittest.TestCase):

    def test_call(self):
        q = QueryApi()
        self.assertRaises(NotImplementedError, q.call)


class BaseTestDeparture(unittest.TestCase):

    def setUp(self):
        self.since = datetime.datetime(2013, 1, 2, 3, 4, 0)
        self.delta1 = datetime.timedelta(seconds=45)
        self.delta2 = datetime.timedelta(seconds=80)
        self.delta3 = datetime.timedelta(seconds=150)


class TestDepartureTimestamp(BaseTestDeparture):

    def setUp(self):
        super(TestDepartureTimestamp, self).setUp()
        self.sincets = 1357092240
        self.since = datetime.datetime.fromtimestamp(self.sincets)
        self.delta1 = self.delta1.total_seconds()
        self.delta2 = self.delta2.total_seconds()
        self.delta3 = self.delta3.total_seconds()

    def test_timestamp_futur(self):
        when = self.sincets + self.delta1
        dep = Departure("from", "to", when, "line", since=self.since)
        self.assertEqual(dep.remaining, 0)

        when = self.sincets + self.delta2
        dep = Departure("from", "to", when, "line", since=self.since)
        self.assertEqual(dep.remaining, 60)

        when = self.sincets + self.delta3
        dep = Departure("from", "to", when, "line", since=self.since)
        self.assertEqual(dep.remaining, 120)

    def test_timestamp_now(self):
        when = self.sincets
        dep = Departure("from", "to", when, "line", since=self.since)
        self.assertEqual(dep.remaining, 0)

    def test_timestamp_past(self):
        when = self.sincets - self.delta1
        dep = Departure("from", "to", when, "line", since=self.since)
        self.assertEqual(dep.remaining, -60)

        when = self.sincets - self.delta2
        dep = Departure("from", "to", when, "line", since=self.since)
        self.assertEqual(dep.remaining, -120)

        when = self.sincets - self.delta3
        dep = Departure("from", "to", when, "line", since=self.since)
        self.assertEqual(dep.remaining, -180)


class TestDepartureString(BaseTestDeparture):

    def test_string_futur(self):
        when = fullformat(self.since + self.delta1)
        dep = Departure("from", "to", when, "line", since=self.since)
        self.assertEqual(dep.remaining, 0)

        when = fullformat(self.since + self.delta2)
        dep = Departure("from", "to", when, "line", since=self.since)
        self.assertEqual(dep.remaining, 60)

        when = fullformat(self.since + self.delta3)
        dep = Departure("from", "to", when, "line", since=self.since)
        self.assertEqual(dep.remaining, 120)

    def test_string_now(self):
        when = fullformat(self.since)
        dep = Departure("from", "to", when, "line", since=self.since)
        self.assertEqual(dep.remaining, 0)

    def test_string_past(self):
        when = fullformat(self.since - self.delta1)
        dep = Departure("from", "to", when, "line", since=self.since)
        self.assertEqual(dep.remaining, -60)

        when = fullformat(self.since - self.delta2)
        dep = Departure("from", "to", when, "line", since=self.since)
        self.assertEqual(dep.remaining, -120)

        when = fullformat(self.since - self.delta3)
        dep = Departure("from", "to", when, "line", since=self.since)
        self.assertEqual(dep.remaining, -180)


class TestDepartureDatetime(BaseTestDeparture):

    def test_datetime_futur(self):
        when = self.since + self.delta1
        dep = Departure("from", "to", when, "line", since=self.since)
        self.assertEqual(dep.remaining, 0)

        when = self.since + self.delta2
        dep = Departure("from", "to", when, "line", since=self.since)
        self.assertEqual(dep.remaining, 60)

        when = self.since + self.delta3
        dep = Departure("from", "to", when, "line", since=self.since)
        self.assertEqual(dep.remaining, 120)

    def test_datetime_now(self):
        when = self.since
        dep = Departure("from", "to", when, "line", since=self.since)
        self.assertEqual(dep.remaining, 0)

    def test_datetime_past(self):
        when = self.since - self.delta1
        dep = Departure("from", "to", when, "line", since=self.since)
        self.assertEqual(dep.remaining, -60)

        when = self.since - self.delta2
        dep = Departure("from", "to", when, "line", since=self.since)
        self.assertEqual(dep.remaining, -120)

        when = self.since - self.delta3
        dep = Departure("from", "to", when, "line", since=self.since)
        self.assertEqual(dep.remaining, -180)


class TestDeparture(BaseTestDeparture):

    def test_error(self):
        self.assertRaises(TypeError, Departure, "from", "to", ["when"], "line")
        self.assertIsInstance(Departure("from", "to", "16:15\n \t*", "line"),
                              Departure)

    def test_regression_new_day(self):
        now = datetime.datetime(2013, 1, 2, 23, 59, 0)

        dep = datetime.datetime(2013, 1, 2, 0, 1, 2)
        departure = Departure("From My Station", "To Your Station", dep,
                              "A Line", since=now)
        self.assertEqual(departure.remaining, 120)
        departure = Departure("From My Station", "To Your Station", dep,
                              "A Line", since=now, no_add_day=True)
        self.assertEqual(departure.remaining, -86280)

        mitday_current_day = datetime.datetime(2013, 1, 2, 11, 59, 0)
        departure = Departure("From My Station", "To Your Station",
                              mitday_current_day, "A Line", since=now)
        self.assertEqual(departure.remaining, -43200)
        departure = Departure("From My Station", "To Your Station",
                              mitday_current_day, "A Line", since=now,
                              no_add_day=True)

        mitday_next_day = datetime.datetime(2013, 1, 2, 11, 58, 59)
        departure = Departure("From My Station", "To Your Station",
                              mitday_next_day, "A Line", since=now)
        self.assertEqual(departure.remaining, 43140)
        departure = Departure("From My Station", "To Your Station",
                              mitday_next_day, "A Line", since=now,
                              no_add_day=True)
        self.assertEqual(departure.remaining, -43260)

    def test_str(self):
        now = datetime.datetime(2013, 1, 2, 12, 13, 0)
        dep = datetime.datetime(2013, 1, 2, 12, 15, 0)
        departure = Departure("From My Station", "To Your Station", dep,
                              "A Line", since=now)
        string = 'Start: From My Station, End: To Your Station, when: 12:15, now: 12:13, line: A Line'
        self.assertEqual(str(departure), string)
        self.assertEqual(repr(departure), string)


class TestDepartureTotalOrder(unittest.TestCase):

    def setUp(self):
        self.td = datetime.timedelta(minutes=1)

        self.dt_now1 = datetime.datetime(2013, 1, 2, 3, 4, 1)
        self.dt_now2 = datetime.datetime(2013, 1, 2, 3, 4, 1)
        self.dt_earlier = datetime.datetime(2013, 1, 2, 3, 4, 1) - self.td
        self.dt_later = datetime.datetime(2013, 1, 2, 3, 4, 1) + self.td

    def test_equal(self):
        self.assertEqual(self.dt_now1, self.dt_now2)

    def test_less(self):
        self.assertLess(self.dt_earlier, self.dt_now1)
        self.assertLess(self.dt_earlier, self.dt_later)
        self.assertLess(self.dt_now1, self.dt_later)

    def test_greater(self):
        self.assertGreater(self.dt_now1, self.dt_earlier)
        self.assertGreater(self.dt_later, self.dt_earlier)
        self.assertGreater(self.dt_later, self.dt_now1)

    def test_not_equal(self):
        self.assertNotEqual(self.dt_now1, self.dt_earlier)
        self.assertNotEqual(self.dt_now1, self.dt_later)
        self.assertNotEqual(self.dt_now2, self.dt_earlier)
        self.assertNotEqual(self.dt_now2, self.dt_later)
        self.assertNotEqual(self.dt_earlier, self.dt_later)

    def test_sorting_single(self):
        l = [self.dt_now1, self.dt_later, self.dt_earlier]
        self.assertEqual([self.dt_earlier, self.dt_now1, self.dt_later],
                         sorted(l))
        self.assertEqual([self.dt_later, self.dt_now1, self.dt_earlier],
                         sorted(l, reverse=True))

    def test_sorting_multiple(self):
        l = [self.dt_now1, self.dt_earlier, self.dt_later,
             self.dt_now2, self.dt_later, self.dt_earlier,
             self.dt_earlier, self.dt_later, self.dt_now2]
        self.assertEqual([self.dt_earlier, self.dt_earlier, self.dt_earlier,
                          self.dt_now1, self.dt_now2, self.dt_now2,
                          self.dt_later, self.dt_later, self.dt_later],
                         sorted(l))
        self.assertEqual([self.dt_later, self.dt_later, self.dt_later,
                          self.dt_now1, self.dt_now2, self.dt_now2,
                          self.dt_earlier, self.dt_earlier, self.dt_earlier],
                         sorted(l, reverse=True))