X-Git-Url: https://plomlompom.com/repos/feed.xml?a=blobdiff_plain;f=tests%2Fdays.py;h=36d0285296b7b191e4217475a08fb94646ed5207;hb=c021152e6566c8374170de916c69d6b5c816cd54;hp=c1e1343c63c7cb24e6e7133f09538a06f7104e11;hpb=f92de64d072009c8c4bf96b9eeb9fa245045662b;p=plomtask diff --git a/tests/days.py b/tests/days.py index c1e1343..36d0285 100644 --- a/tests/days.py +++ b/tests/days.py @@ -1,8 +1,9 @@ """Test Days module.""" from unittest import TestCase from datetime import datetime +from json import loads as json_loads from tests.utils import TestCaseWithDB, TestCaseWithServer -from plomtask.dating import todays_date +from plomtask.dating import date_in_n_days from plomtask.days import Day from plomtask.exceptions import BadFormatException @@ -43,42 +44,26 @@ class TestsWithDB(TestCaseWithDB): checked_class = Day default_ids = ('2024-01-01', '2024-01-02', '2024-01-03') - def test_saving_and_caching(self) -> None: - """Test storage of instances. - - We don't use the parent class's method here because the checked class - has too different a handling of IDs. - """ - 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.""" + def test_Day_by_date_range_filled(self) -> None: + """Test Day.by_date_range_filled.""" date1, date2, date3 = self.default_ids - day1, day2, day3 = self.check_all() - self.assertEqual(Day.all(self.db_conn, ('', '')), - [day1, day2, day3]) + day1 = Day(date1) + day2 = Day(date2) + day3 = Day(date3) + day1.save(self.db_conn) + day2.save(self.db_conn) + day3.save(self.db_conn) # check date range is a closed interval - self.assertEqual(Day.all(self.db_conn, (date1, date3)), + self.assertEqual(Day.by_date_range_filled(self.db_conn, date1, date3), [day1, day2, day3]) # check first date range value excludes what's earlier - self.assertEqual(Day.all(self.db_conn, (date2, date3)), + self.assertEqual(Day.by_date_range_filled(self.db_conn, date2, date3), [day2, day3]) - self.assertEqual(Day.all(self.db_conn, (date3, '')), - [day3]) # check second date range value excludes what's later - self.assertEqual(Day.all(self.db_conn, ('', date2)), + self.assertEqual(Day.by_date_range_filled(self.db_conn, date1, date2), [day1, day2]) # check swapped (impossible) date range returns emptiness - self.assertEqual(Day.all(self.db_conn, (date3, date1)), + self.assertEqual(Day.by_date_range_filled(self.db_conn, date3, date1), []) # check fill_gaps= instantiates unsaved dates within date range # (but does not store them) @@ -86,27 +71,38 @@ class TestsWithDB(TestCaseWithDB): day6 = Day('2024-01-06') day6.save(self.db_conn) day7 = Day('2024-01-07') - self.assertEqual(Day.all(self.db_conn, (day5.date, day7.date), - fill_gaps=True), + self.assertEqual(Day.by_date_range_filled(self.db_conn, + day5.date, day7.date), [day5, day6, day7]) - self.check_storage([day1, day2, day3, day6]) + self.check_identity_with_cache_and_db([day1, day2, day3, day6]) # check 'today' is interpreted as today's date - today = Day(todays_date()) + today = Day(date_in_n_days(0)) today.save(self.db_conn) - self.assertEqual(Day.all(self.db_conn, ('today', 'today')), [today]) - - 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('day_comment', 'boo') + self.assertEqual(Day.by_date_range_filled(self.db_conn, + 'today', 'today'), + [today]) class TestsWithServer(TestCaseWithServer): """Tests against our HTTP server/handler (and database).""" + def test_get_json(self) -> None: + """Test /day for JSON response.""" + self.conn.request('GET', '/day?date=2024-01-01') + response = self.conn.getresponse() + self.assertEqual(response.status, 200) + expected = {'day': {'id': '2024-01-01', + 'comment': '', + 'todos': []}, + 'top_nodes': [], + 'make_type': '', + 'enablers_for': {}, + 'disablers_for': {}, + 'conditions_present': [], + 'processes': []} + retrieved = json_loads(response.read().decode()) + self.assertEqual(expected, retrieved) + def test_do_GET(self) -> None: """Test /day and /calendar response codes, and / redirect.""" self.check_get('/day', 200) @@ -120,8 +116,8 @@ class TestsWithServer(TestCaseWithServer): def test_do_POST_day(self) -> None: """Test POST /day.""" - form_data = {'day_comment': ''} + form_data = {'day_comment': '', 'make_type': 'full'} 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(form_data, '/day?date=2024-01-01&make_type=full', 302) self.check_post({'foo': ''}, '/day?date=2024-01-01', 400)