From c40e77603674103b3086382dc8530258d6954791 Mon Sep 17 00:00:00 2001 From: Gergely Polonkai Date: Tue, 24 Jul 2018 08:28:04 +0200 Subject: [PATCH] Fully cover the Gregorian Calendar with tests Being one of the cornerstones right now, it deserves a lot of tests --- calsocial/calendar_system/gregorian.py | 18 +---- calsocial/templates/month-view.html | 8 +-- tests/test_gregorian.py | 92 ++++++++++++++++++++++++++ 3 files changed, 97 insertions(+), 21 deletions(-) create mode 100644 tests/test_gregorian.py diff --git a/calsocial/calendar_system/gregorian.py b/calsocial/calendar_system/gregorian.py index 8c76236..807e7a7 100644 --- a/calsocial/calendar_system/gregorian.py +++ b/calsocial/calendar_system/gregorian.py @@ -18,24 +18,12 @@ """ from datetime import datetime, timedelta -from functools import wraps from flask_babelex import lazy_gettext as _ from . import CalendarSystem -def to_timestamp(func): - """Decorator that converts the return value of a function from `datetime` to a UNIX timestamp - """ - - @wraps(func) - def _decorator(*args, **kwargs): - return func(*args, **kwargs).timestamp() - - return _decorator - - class GregorianCalendar(CalendarSystem): """Gregorian calendar system for Calendar.social """ @@ -104,7 +92,6 @@ class GregorianCalendar(CalendarSystem): return day_list @property - @to_timestamp def prev_year(self): """Returns the timestamp of the same date in the previous year """ @@ -119,7 +106,6 @@ class GregorianCalendar(CalendarSystem): return self.timestamp.replace(year=self.timestamp.year - 1).year @property - @to_timestamp def prev_month(self): """Returns the timestamp of the same day in the previous month """ @@ -142,7 +128,6 @@ class GregorianCalendar(CalendarSystem): return self.month_names[timestamp.month - 1] @property - @to_timestamp def next_month(self): """Returns the timestamp of the same day in the next month """ @@ -165,7 +150,6 @@ class GregorianCalendar(CalendarSystem): return self.month_names[timestamp.month - 1] @property - @to_timestamp def next_year(self): """Returns the timestamp of the same date in the next year """ @@ -198,7 +182,7 @@ class GregorianCalendar(CalendarSystem): month_end_timestamp = month_start_timestamp.replace(month=next_month) - return now >= month_start_timestamp and now < month_end_timestamp + return month_start_timestamp <= now < month_end_timestamp @staticmethod def day_events(date, user=None): diff --git a/calsocial/templates/month-view.html b/calsocial/templates/month-view.html index b305937..b820861 100644 --- a/calsocial/templates/month-view.html +++ b/calsocial/templates/month-view.html @@ -11,10 +11,10 @@ - « {{ calendar.prev_year_year }} + « {{ calendar.prev_year_year }} - ‹ {{ calendar.prev_month_name }} + ‹ {{ calendar.prev_month_name }} {% if not calendar.has_today %} @@ -26,10 +26,10 @@ {% endif %} - {{ calendar.next_month_name }} › + {{ calendar.next_month_name }} › - {{ calendar.next_year_year }} » + {{ calendar.next_year_year }} » diff --git a/tests/test_gregorian.py b/tests/test_gregorian.py new file mode 100644 index 0000000..0704454 --- /dev/null +++ b/tests/test_gregorian.py @@ -0,0 +1,92 @@ +from datetime import datetime, date + +from pytz import utc + +from calsocial.calendar_system.gregorian import GregorianCalendar + + +def test_day_list(): + calendar = GregorianCalendar(utc.localize(datetime(2018, 1, 1, 0, 0, 0)).timestamp()) + assert calendar.days[0].date() == date(2018, 1, 1) + assert calendar.days[-1].date() == date(2018, 2, 4) + + calendar = GregorianCalendar(utc.localize(datetime(2018, 12, 1, 0, 0, 0)).timestamp()) + assert calendar.days[0].date() == date(2018, 11, 26) + assert calendar.days[-1].date() == date(2019, 1, 6) + + +def test_prev_year(): + calendar = GregorianCalendar(utc.localize(datetime(2018, 1, 1, 0, 0, 0)).timestamp()) + assert calendar.prev_year == datetime(2017, 1, 1, 0, 0, 0) + + calendar = GregorianCalendar(utc.localize(datetime(2018, 12, 1, 0, 0, 0)).timestamp()) + assert calendar.prev_year == datetime(2017, 12, 1, 0, 0, 0) + + +def test_prev_year_year(): + calendar = GregorianCalendar(utc.localize(datetime(2018, 1, 1, 0, 0, 0)).timestamp()) + assert calendar.prev_year_year == 2017 + + calendar = GregorianCalendar(utc.localize(datetime(2018, 12, 1, 0, 0, 0)).timestamp()) + assert calendar.prev_year_year == 2017 + + +def test_prev_month(): + calendar = GregorianCalendar(utc.localize(datetime(2018, 1, 1, 0, 0, 0)).timestamp()) + assert calendar.prev_month == datetime(2017, 12, 1, 0, 0, 0) + + calendar = GregorianCalendar(utc.localize(datetime(2018, 12, 1, 0, 0, 0)).timestamp()) + assert calendar.prev_month == datetime(2018, 11, 1, 0, 0, 0) + + +def test_prev_month_name(): + calendar = GregorianCalendar(utc.localize(datetime(2018, 1, 1, 0, 0, 0)).timestamp()) + assert calendar.prev_month_name == 'December' + + calendar = GregorianCalendar(utc.localize(datetime(2018, 12, 1, 0, 0, 0)).timestamp()) + assert calendar.prev_month_name == 'November' + + +def test_next_year(): + calendar = GregorianCalendar(utc.localize(datetime(2018, 1, 1, 0, 0, 0)).timestamp()) + assert calendar.next_year == datetime(2019, 1, 1, 0, 0, 0) + + calendar = GregorianCalendar(utc.localize(datetime(2018, 12, 1, 0, 0, 0)).timestamp()) + assert calendar.next_year == datetime(2019, 12, 1, 0, 0, 0) + + +def test_next_year_year(): + calendar = GregorianCalendar(utc.localize(datetime(2018, 1, 1, 0, 0, 0)).timestamp()) + assert calendar.next_year_year == 2019 + + calendar = GregorianCalendar(utc.localize(datetime(2018, 12, 1, 0, 0, 0)).timestamp()) + assert calendar.next_year_year == 2019 + + +def test_next_month(): + calendar = GregorianCalendar(utc.localize(datetime(2018, 1, 1, 0, 0, 0)).timestamp()) + assert calendar.next_month == datetime(2018, 2, 1, 0, 0, 0) + + calendar = GregorianCalendar(utc.localize(datetime(2018, 12, 1, 0, 0, 0)).timestamp()) + assert calendar.next_month == datetime(2019, 1, 1, 0, 0, 0) + + +def test_next_month_name(): + calendar = GregorianCalendar(utc.localize(datetime(2018, 1, 1, 0, 0, 0)).timestamp()) + assert calendar.next_month_name == 'February' + + calendar = GregorianCalendar(utc.localize(datetime(2018, 12, 1, 0, 0, 0)).timestamp()) + assert calendar.next_month_name == 'January' + + +def test_has_today(): + calendar = GregorianCalendar(utc.localize(datetime(1990, 12, 1, 0, 0, 0)).timestamp()) + assert calendar.has_today is False + + calendar = GregorianCalendar(utc.localize(datetime.utcnow()).timestamp()) + assert calendar.has_today is True + + +def test_current_month(): + calendar = GregorianCalendar(utc.localize(datetime(2018, 1, 1, 0, 0, 0)).timestamp()) + assert calendar.month == 'January, 2018'