Fully cover the Gregorian Calendar with tests

Being one of the cornerstones right now, it deserves a lot of tests
This commit is contained in:
Gergely Polonkai 2018-07-24 08:28:04 +02:00
parent 3deaa39256
commit c40e776036
3 changed files with 97 additions and 21 deletions

View File

@ -18,24 +18,12 @@
""" """
from datetime import datetime, timedelta from datetime import datetime, timedelta
from functools import wraps
from flask_babelex import lazy_gettext as _ from flask_babelex import lazy_gettext as _
from . import CalendarSystem 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): class GregorianCalendar(CalendarSystem):
"""Gregorian calendar system for Calendar.social """Gregorian calendar system for Calendar.social
""" """
@ -104,7 +92,6 @@ class GregorianCalendar(CalendarSystem):
return day_list return day_list
@property @property
@to_timestamp
def prev_year(self): def prev_year(self):
"""Returns the timestamp of the same date in the previous year """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 return self.timestamp.replace(year=self.timestamp.year - 1).year
@property @property
@to_timestamp
def prev_month(self): def prev_month(self):
"""Returns the timestamp of the same day in the previous month """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] return self.month_names[timestamp.month - 1]
@property @property
@to_timestamp
def next_month(self): def next_month(self):
"""Returns the timestamp of the same day in the next month """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] return self.month_names[timestamp.month - 1]
@property @property
@to_timestamp
def next_year(self): def next_year(self):
"""Returns the timestamp of the same date in the next year """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) 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 @staticmethod
def day_events(date, user=None): def day_events(date, user=None):

View File

@ -11,10 +11,10 @@
</tr> </tr>
<tr class="month"> <tr class="month">
<td> <td>
<a href="{{ url_for('hello', date=calendar.prev_year) }}">« {{ calendar.prev_year_year }}</a> <a href="{{ url_for('hello', date=calendar.prev_year.timestamp()) }}">« {{ calendar.prev_year_year }}</a>
</td> </td>
<td> <td>
<a href="{{ url_for('hello', date=calendar.prev_month) }}"> {{ calendar.prev_month_name }}</a> <a href="{{ url_for('hello', date=calendar.prev_month.timestamp()) }}"> {{ calendar.prev_month_name }}</a>
</td> </td>
<td colspan="3" class="month-name"> <td colspan="3" class="month-name">
{% if not calendar.has_today %} {% if not calendar.has_today %}
@ -26,10 +26,10 @@
{% endif %} {% endif %}
</td> </td>
<td> <td>
<a href="{{ url_for('hello', date=calendar.next_month) }}">{{ calendar.next_month_name }} </a> <a href="{{ url_for('hello', date=calendar.next_month.timestamp()) }}">{{ calendar.next_month_name }} </a>
</td> </td>
<td> <td>
<a href="{{ url_for('hello', date=calendar.next_year) }}">{{ calendar.next_year_year }} »</a> <a href="{{ url_for('hello', date=calendar.next_year.timestamp()) }}">{{ calendar.next_year_year }} »</a>
</td> </td>
</tr> </tr>
<tr class="days"> <tr class="days">

92
tests/test_gregorian.py Normal file
View File

@ -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'