"""Test Days module."""
from unittest import TestCase
-from http.client import HTTPConnection
from datetime import datetime
from tests.utils import TestCaseWithDB, TestCaseWithServer
from plomtask.days import Day, todays_date
-from plomtask.misc import HandledException
+from plomtask.exceptions import BadFormatException, NotFoundException
class TestsSansDB(TestCase):
"""Days module tests not requiring DB setup."""
- def test_Day_dates(self):
+ def test_Day_dates(self) -> None:
"""Test Day's date format."""
- with self.assertRaises(HandledException):
+ with self.assertRaises(BadFormatException):
Day('foo')
- with self.assertRaises(HandledException):
- Day(None)
- with self.assertRaises(HandledException):
- Day(3)
- with self.assertRaises(HandledException):
+ with self.assertRaises(BadFormatException):
Day('2024-02-30')
- with self.assertRaises(HandledException):
+ with self.assertRaises(BadFormatException):
Day('2024-02-01 23:00:00')
self.assertEqual(datetime(2024, 1, 1), Day('2024-01-01').datetime)
- def test_Day_sorting(self):
+ def test_Day_sorting(self) -> None:
"""Test Day.__lt__."""
day1 = Day('2024-01-01')
day2 = Day('2024-01-02')
days = [day3, day1, day2]
self.assertEqual(sorted(days), [day1, day2, day3])
- def test_Day_weekday(self):
+ def test_Day_weekday(self) -> None:
"""Test Day.weekday."""
self.assertEqual(Day('2024-03-17').weekday, 'Sunday')
class TestsWithDB(TestCaseWithDB):
"""Days module tests not requiring DB setup."""
- def test_Day_by_date(self):
+ def test_Day_by_date(self) -> None:
"""Test Day.by_date()."""
- self.assertEqual(None, Day.by_date(self.db_conn, '2024-01-01'))
+ with self.assertRaises(NotFoundException):
+ Day.by_date(self.db_conn, '2024-01-01')
Day('2024-01-01').save(self.db_conn)
self.assertEqual(Day('2024-01-01'),
Day.by_date(self.db_conn, '2024-01-01'))
- self.assertEqual(None,
- Day.by_date(self.db_conn, '2024-01-02'))
+ with self.assertRaises(NotFoundException):
+ Day.by_date(self.db_conn, '2024-01-02')
self.assertEqual(Day('2024-01-02'),
Day.by_date(self.db_conn, '2024-01-02', create=True))
- def test_Day_all(self):
+ def test_Day_all(self) -> None:
"""Test Day.all(), especially in regards to date range filtering."""
day1 = Day('2024-01-01')
day2 = Day('2024-01-02')
day1.save(self.db_conn)
day2.save(self.db_conn)
day3.save(self.db_conn)
- self.assertEqual(Day.all(self.db_conn),
- [day1, day2, day3])
+ self.assertEqual(Day.all(self.db_conn), [day1, day2, day3])
self.assertEqual(Day.all(self.db_conn, ('', '')),
[day1, day2, day3])
self.assertEqual(Day.all(self.db_conn, ('2024-01-01', '2024-01-03')),
today.save(self.db_conn)
self.assertEqual(Day.all(self.db_conn, ('today', 'today')), [today])
- def test_Day_neighbor_dates(self):
+ def test_Day_neighbor_dates(self) -> None:
"""Test Day.prev_date and Day.next_date."""
self.assertEqual(Day('2024-01-01').prev_date, '2023-12-31')
self.assertEqual(Day('2023-02-28').next_date, '2023-03-01')
class TestsWithServer(TestCaseWithServer):
"""Tests against our HTTP server/handler (and database)."""
- def test_do_GET(self):
- """Test /day and /calendar response codes."""
- http_conn = HTTPConnection(*self.httpd.server_address)
- http_conn.request('GET', '/day')
- self.assertEqual(http_conn.getresponse().status, 200)
- http_conn.request('GET', '/day?date=3000-01-01')
- self.assertEqual(http_conn.getresponse().status, 200)
- http_conn.request('GET', '/day?date=FOO')
- self.assertEqual(http_conn.getresponse().status, 400)
- http_conn.request('GET', '/calendar')
- self.assertEqual(http_conn.getresponse().status, 200)
- http_conn.request('GET', '/calendar?start=&end=')
- self.assertEqual(http_conn.getresponse().status, 200)
- http_conn.request('GET', '/calendar?start=today&end=today')
- self.assertEqual(http_conn.getresponse().status, 200)
- http_conn.request('GET', '/calendar?start=2024-01-01&end=2025-01-01')
- self.assertEqual(http_conn.getresponse().status, 200)
- http_conn.request('GET', '/calendar?start=foo')
- self.assertEqual(http_conn.getresponse().status, 400)
+ def test_do_GET(self) -> None:
+ """Test /day and /calendar response codes, and / redirect."""
+ self.check_get('/day', 200)
+ self.check_get('/day?date=3000-01-01', 200)
+ self.check_get('/day?date=FOO', 400)
+ self.check_get('/calendar', 200)
+ self.check_get('/calendar?start=&end=', 200)
+ self.check_get('/calendar?start=today&end=today', 200)
+ self.check_get('/calendar?start=2024-01-01&end=2025-01-01', 200)
+ self.check_get('/calendar?start=foo', 400)
+
+ def test_do_POST_day(self) -> None:
+ """Test POST /day."""
+ form_data = {'comment': ''}
+ self.check_post(form_data, '/day', 400)
+ self.check_post(form_data, '/day?date=foo', 400)
+ self.check_post(form_data, '/day?date=2024-01-01', 302, '/')
+ self.check_post({'foo': ''}, '/day?date=2024-01-01', 400)