From d6f0b39753f1b55f86a0eba9a822c367a26c45dc Mon Sep 17 00:00:00 2001 From: Markus Holtermann Date: Thu, 24 Jan 2013 21:32:04 +0100 Subject: Add base QueryApi and Departure classes including simple tests --- bvggrabber/api/__init__.py | 47 ++++++++++++++++++++++++++++++++++++++++++++++ tests/test_api.py | 42 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 89 insertions(+) create mode 100644 tests/test_api.py diff --git a/bvggrabber/api/__init__.py b/bvggrabber/api/__init__.py index 40a96af..063e7e2 100644 --- a/bvggrabber/api/__init__.py +++ b/bvggrabber/api/__init__.py @@ -1 +1,48 @@ # -*- coding: utf-8 -*- + + +from datetime import datetime +from dateutil.parser import parse + + +fullformat = lambda dt: dt.strftime('%Y-%m-%d %H:%M') + + +class QueryApi(): + + def __init__(self): + pass + + def call(self): + raise NotImplementedError("The inheriting class needs to implement " + "the call() method!") + + +class Departure(): + + def __init__(self, start, end, when, line): + self.start = start + self.end = end + if isinstance(when, (int, float)): + # We assume to get a UNIX timestamp + self.when = datetime.fromtimestamp(when) + elif isinstance(when, str): + self.when = parse(when) + now = datetime.now() + if (self.when - now).total_seconds() < 0: + self.when = self.when.replace(day=self.when.day + 1) + elif isinstance(when, datetime): + self.when = when + else: + ValueError("when must be a valid datetime, timestamp or string!") + self.line = line + + def __str__(self): + return "Start: %s, End: %s, when: %s, line: %s" % ( + self.start, self.end, fullformat(self.when), self.line) + + def remaining(self): + return self.when - datetime.now() + + def to_json(self): + pass \ No newline at end of file diff --git a/tests/test_api.py b/tests/test_api.py new file mode 100644 index 0000000..dafe598 --- /dev/null +++ b/tests/test_api.py @@ -0,0 +1,42 @@ +# -*- coding: utf-8 -*- + +import time +import unittest + +from datetime import datetime + +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 test_timestamp_futur(self): + when = time.time() + 10 * 60 + dep = Departure("from", "to", when, "line") + self.assertLessEqual(dep.remaining().total_seconds(), 600) + self.assertGreaterEqual(dep.remaining().total_seconds(), 590) + + def test_string_futur(self): + when = datetime.now() + when = when.replace(minute=when.minute + 10) + when = when.strftime('%Y-%m-%d %H:%M:%S') + dep = Departure("from", "to", when, "line") + self.assertLessEqual(dep.remaining().total_seconds(), 600) + self.assertGreaterEqual(dep.remaining().total_seconds(), 590) + + def test_datetime_futur(self): + when = datetime.now() + when = when.replace(minute=when.minute + 10) + dep = Departure("from", "to", when, "line") + self.assertLessEqual(dep.remaining().total_seconds(), 600) + self.assertGreaterEqual(dep.remaining().total_seconds(), 590) + + def test_error(self): + self.assertRaises(ValueError, Departure, "from", "to", "foo", "line") -- cgit v1.2.3