X-Git-Url: https://plomlompom.com/repos/?a=blobdiff_plain;f=tests%2Fdays.py;h=1f0e55d871b282881dc0fee2f7e2fc91a591d243;hb=c4ccb784bb3a83c1c614c9bab7fc007ee17f6615;hp=5ff6459f48e34e09b945b5f79f62d0176c3295f8;hpb=f20d686a4972db5e6bc10bdbd48d27d4b035a716;p=plomtask diff --git a/tests/days.py b/tests/days.py index 5ff6459..1f0e55d 100644 --- a/tests/days.py +++ b/tests/days.py @@ -1,116 +1,122 @@ """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 class TestsSansDB(TestCase): """Days module tests not requiring DB setup.""" - def test_Day_dates(self): - """Test Day's date format.""" - with self.assertRaises(HandledException): + def test_Day_valid_date(self) -> None: + """Test Day's date format validation and parsing.""" + 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): - """Test Day.__lt__.""" + def test_Day_sorting(self) -> None: + """Test sorting by .__lt__ and Day.__eq__.""" day1 = Day('2024-01-01') day2 = Day('2024-01-02') day3 = Day('2024-01-03') 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') + 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 TestsWithDB(TestCaseWithDB): - """Days module tests not requiring DB setup.""" + """Tests requiring DB, but not server setup.""" + checked_class = Day + default_ids = ('2024-01-01', '2024-01-02', '2024-01-03') - def test_Day_by_date(self): - """Test Day.by_date().""" - self.assertEqual(None, 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')) - 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_saving_and_caching(self) -> None: + """Test .save/.save_core.""" + kwargs = {'date': self.default_ids[0], 'comment': 'foo'} + self.check_saving_and_caching(**kwargs) + + def test_Day_from_table_row(self) -> None: + """Test .from_table_row() properly reads in class from DB""" + self.check_from_table_row() + + def test_Day_by_id(self) -> None: + """Test .by_id().""" + self.check_by_id() + + 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') - day3 = Day('2024-01-03') - 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]) + date1, date2, date3 = self.default_ids + day1, day2, day3 = self.check_all() self.assertEqual(Day.all(self.db_conn, ('', '')), [day1, day2, day3]) - self.assertEqual(Day.all(self.db_conn, ('2024-01-01', '2024-01-03')), + # check date range is a closed interval + self.assertEqual(Day.all(self.db_conn, (date1, date3)), [day1, day2, day3]) - self.assertEqual(Day.all(self.db_conn, ('2024-01-02', '2024-01-03')), + # check first date range value excludes what's earlier + self.assertEqual(Day.all(self.db_conn, (date2, date3)), [day2, day3]) - self.assertEqual(Day.all(self.db_conn, ('2024-01-03', '')), + self.assertEqual(Day.all(self.db_conn, (date3, '')), [day3]) - self.assertEqual(Day.all(self.db_conn, ('2024-01-01', '')), - [day1, day2, day3]) - self.assertEqual(Day.all(self.db_conn, ('', '2024-01-02')), + # check second date range value excludes what's later + self.assertEqual(Day.all(self.db_conn, ('', date2)), [day1, day2]) - self.assertEqual(Day.all(self.db_conn, ('2024-01-03', '2024-01-01')), + # check swapped (impossible) date range returns emptiness + self.assertEqual(Day.all(self.db_conn, (date3, date1)), []) + # check fill_gaps= instantiates unsaved dates within date range + # (but does not store them) day4 = Day('2024-01-04') day5 = Day('2024-01-05') day6 = Day('2024-01-06') day6.save(self.db_conn) - self.assertEqual(Day.all(self.db_conn, ('2024-01-02', '2024-01-07'), + self.assertEqual(Day.all(self.db_conn, (date2, '2024-01-07'), fill_gaps=True), [day2, day3, day4, day5, day6]) + self.check_storage([day1, day2, day3, day6]) + # check 'today' is interpreted as today's date today = Day(todays_date()) today.save(self.db_conn) self.assertEqual(Day.all(self.db_conn, ('today', 'today')), [today]) - def test_Day_neighbor_dates(self): - """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') + def test_Day_remove(self) -> None: + """Test .remove() effects on DB and cache.""" + self.check_remove() + + def test_Day_singularity(self) -> None: + """Test pointers made for single object keep pointing to it.""" + self.check_singularity('comment', 'boo') 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)