aboutsummaryrefslogtreecommitdiff
path: root/tests/test_api.py
blob: 849352488b7a6e1d97c513f276e37f9dbc735be4 (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
# -*- coding: utf-8 -*-
import time
import unittest

from datetime import datetime, timedelta

from bvggrabber.api import QueryApi, Departure


class TestQueryApi(unittest.TestCase):

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


class TestDeparture(unittest.TestCase):

    def setUp(self):
        self.td = timedelta(minutes=10)

    def test_timestamp_futur(self):
        when = time.time() + 10 * 60
        dep = Departure("from", "to", when, "line")
        diff = dep.remaining.total_seconds()
        self.assertLessEqual(diff, 600)
        self.assertGreaterEqual(diff, 590)

    def test_timestamp_now(self):
        when = time.time()
        dep = Departure("from", "to", when, "line")
        diff = dep.remaining.total_seconds()
        self.assertLessEqual(diff, 5)
        self.assertGreaterEqual(diff, -5)

    def test_timestamp_past(self):
        when = time.time() - 10 * 60
        dep = Departure("from", "to", when, "line")
        diff = dep.remaining.total_seconds()
        self.assertLessEqual(diff, -600)
        self.assertGreaterEqual(diff, -610)

    def test_string_futur(self):
        when = datetime.now() + self.td
        when = when.strftime('%Y-%m-%d %H:%M:%S')
        dep = Departure("from", "to", when, "line")
        diff = dep.remaining.total_seconds()
        self.assertLessEqual(diff, 600)
        self.assertGreaterEqual(diff, 590)

    def test_string_now(self):
        when = datetime.now()
        when = when.strftime('%Y-%m-%d %H:%M:%S')
        dep = Departure("from", "to", when, "line")
        diff = dep.remaining.total_seconds()
        self.assertLessEqual(diff, 5)
        self.assertGreaterEqual(diff, -5)

    def test_string_past(self):
        when = datetime.now() - self.td
        when = when.strftime('%Y-%m-%d %H:%M:%S')
        dep = Departure("from", "to", when, "line")
        diff = dep.remaining.total_seconds()
        self.assertLessEqual(diff, -600)
        self.assertGreaterEqual(diff, -610)

    def test_datetime_futur(self):
        when = datetime.now() + self.td
        dep = Departure("from", "to", when, "line")
        diff = dep.remaining.total_seconds()
        self.assertLessEqual(diff, 600)
        self.assertGreaterEqual(diff, 590)

    def test_datetime_now(self):
        when = datetime.now()
        dep = Departure("from", "to", when, "line")
        diff = dep.remaining.total_seconds()
        self.assertLessEqual(diff, 5)
        self.assertGreaterEqual(diff, -5)

    def test_datetime_past(self):
        when = datetime.now() - self.td
        dep = Departure("from", "to", when, "line")
        diff = dep.remaining.total_seconds()
        self.assertLessEqual(diff, -600)
        self.assertGreaterEqual(diff, -610)

    def test_error(self):
        self.assertRaises(ValueError, Departure, "from", "to", "foo", "line")