Skip to content

Commit f3edac2

Browse files
committed
Extend tests
1 parent 07edb33 commit f3edac2

File tree

4 files changed

+64
-20
lines changed

4 files changed

+64
-20
lines changed

openinghours/templatetags/openinghours_tags.py

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
from django.template.loader import get_template
33
from django.utils.translation import ugettext_lazy as _
44

5-
from openinghours.models import *
5+
from openinghours.models import WEEKDAYS, OpeningHours
66
from openinghours import utils
77

88

@@ -11,6 +11,9 @@
1111

1212
@register.filter(expects_localtime=True)
1313
def iso_day_to_weekday(d):
14+
"""
15+
Returns the weekday's name given a ISO weekday number; "today" if today is the same weekday
16+
"""
1417
if int(d) == utils.get_now().isoweekday():
1518
return _("today")
1619
for w in WEEKDAYS:
@@ -32,6 +35,9 @@ def to_weekday(date_obj_tpl):
3235

3336
@register.assignment_tag
3437
def is_open(location=None, attr=None):
38+
"""
39+
Returns False if the location is closed, or the OpeningHours object specifying that the location is currently open
40+
"""
3541
obj = utils.is_open(location)
3642
if obj is False:
3743
return False
@@ -42,6 +48,10 @@ def is_open(location=None, attr=None):
4248

4349
@register.assignment_tag
4450
def next_time_open(location):
51+
"""
52+
Returns the next possible OpeningHours object, or False if the location is currently open or if there is no such
53+
object
54+
"""
4555
obj, ts = utils.next_time_open(location)
4656
return obj
4757

@@ -58,7 +68,6 @@ def has_closing_rule_for_now(location, attr=None):
5868

5969
@register.filter(expects_localtime=True)
6070
def get_closing_rule_for_now(location, attr=None):
61-
"""Only accesses the *first* closing rule, because closed means closed."""
6271
obj = utils.get_closing_rule_for_now(location)
6372
if obj is False:
6473
return False
@@ -69,7 +78,9 @@ def get_closing_rule_for_now(location, attr=None):
6978

7079
@register.simple_tag
7180
def opening_hours(location=None, concise=False):
72-
"""Creates a rendered listing of hours."""
81+
"""
82+
Creates a rendered listing of hours.
83+
"""
7384
template_name = 'openinghours/opening_hours_list.html'
7485
days = [] # [{'hours': '9:00am to 5:00pm', 'name': u'Monday'}, {'hours...
7586

openinghours/tests/test_templatetags.py

Lines changed: 42 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
from django.utils.encoding import force_text
22
from freezegun import freeze_time
33

4-
from openinghours.templatetags.openinghours_tags import iso_day_to_weekday
4+
from openinghours.models import OpeningHours, ClosingRules
5+
from openinghours.templatetags.openinghours_tags import iso_day_to_weekday, is_open, next_time_open, \
6+
has_closing_rule_for_now, get_closing_rule_for_now
57
from openinghours.tests.tests import OpeningHoursTestCase
68

79

@@ -22,20 +24,53 @@ def test_iso_day_to_weekday(self):
2224
self.assertEqual(force_text(iso_day_to_weekday(2)), u'today')
2325

2426
def test_to_weekday(self):
25-
pass
27+
pass # TODO: Write test
2628

2729
def test_is_open(self):
28-
pass
30+
with freeze_time("2016-02-22 09:00:00"): # Monday
31+
self.assertEqual(
32+
is_open(self.company),
33+
OpeningHours.objects.filter(company=self.company).first()
34+
)
35+
with freeze_time("2016-02-22 12:15:00"): # Monday
36+
self.assertFalse(is_open(self.company))
37+
with freeze_time("2016-02-22 23:00:30"): # Monday
38+
self.assertFalse(is_open(self.company))
39+
with freeze_time("2016-02-23 17:59:59"): # Tuesday
40+
self.assertTrue(is_open(self.company))
41+
with freeze_time("2016-02-23 18:00:00"): # Tuesday
42+
self.assertTrue(is_open(self.company))
43+
with freeze_time("2016-02-23 18:00:01"): # Tuesday
44+
self.assertFalse(is_open(self.company))
45+
with freeze_time("2016-02-23 18:29:59"): # Tuesday
46+
self.assertFalse(is_open(self.company))
47+
with freeze_time("2016-02-23 18:30:00"): # Tuesday
48+
self.assertTrue(is_open(self.company))
2949

3050
def test_next_time_open(self):
31-
pass
51+
with freeze_time("2016-02-22 08:00:00"): # Monday
52+
self.assertEqual(
53+
next_time_open(self.company),
54+
OpeningHours.objects.filter(company=self.company).first()
55+
)
56+
with freeze_time("2016-02-22 09:00:00"): # Monday
57+
self.assertFalse(next_time_open(self.company))
3258

3359
def test_has_closing_rule_for_now(self):
34-
pass
60+
with freeze_time("2015-12-26 10:00:00"):
61+
self.assertTrue(has_closing_rule_for_now(self.company))
62+
with freeze_time("2016-02-22 10:00:00"):
63+
self.assertFalse(has_closing_rule_for_now(self.company))
3564

3665
def test_get_closing_rule_for_now(self):
37-
pass
66+
with freeze_time("2015-12-26 10:00:00"):
67+
self.assertEqual(
68+
get_closing_rule_for_now(self.company).first(),
69+
ClosingRules.objects.filter(company=self.company).first()
70+
)
71+
with freeze_time("2016-02-22 10:00:00"):
72+
self.assertFalse(get_closing_rule_for_now(self.company))
3873

3974
def opening_hours(self):
40-
pass
75+
pass # TODO: Write test
4176

openinghours/tests/tests.py

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,27 +22,24 @@ def setUp(self):
2222
6: ['10:00 13:00'],
2323
7: None,
2424
}
25-
company, created = Company.objects.get_or_create(name="Company Ltd.",
25+
self.company, created = Company.objects.get_or_create(name="Company Ltd.",
2626
slug="company-ltd")
2727
for day, hours in test_data.items():
2828
if not hours:
2929
continue
3030
for slot in hours:
3131
from_hour, to_hour = slot.split()
3232
OpeningHours.objects.get_or_create(
33-
company=company,
33+
company=self.company,
3434
weekday=day,
3535
from_hour=str_to_time(from_hour),
3636
to_hour=str_to_time(to_hour)
3737
)
3838

39-
def days_ahead(d): return datetime.today() + timedelta(days=d)
40-
41-
# setup some holidays
4239
holiday = ClosingRules.objects.create(
43-
company=company,
44-
start=days_ahead(2),
45-
end=days_ahead(4),
40+
company=self.company,
41+
start=datetime(2015, 12, 25) - timedelta(days=2),
42+
end=datetime(2015, 12, 25) + timedelta(days=4),
4643
reason="Public holiday"
4744
)
4845

openinghours/utils.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ def get_now():
4646

4747
def get_closing_rule_for_now(location):
4848
"""
49-
Access all the closing rules for a company
49+
Returns QuerySet of ClosingRules that are currently valid
5050
"""
5151
now = get_now()
5252

@@ -105,7 +105,8 @@ def is_open(location, now=None):
105105

106106
def next_time_open(location):
107107
"""
108-
Returns the next possible opening hours object.
108+
Returns the next possible opening hours object, or (False, None) if location is currently open or there is no
109+
such object
109110
I.e. when is the company open for the next time?
110111
"""
111112
if not is_open(location):

0 commit comments

Comments
 (0)