243 lines
10 KiB
Python
243 lines
10 KiB
Python
# -*- coding: utf-8 -*-
|
|
import pytest
|
|
import json
|
|
import os
|
|
|
|
from django.utils.safestring import mark_safe
|
|
|
|
from combo_plugin_gnm.templatetags.gnm import as_opening_hours_badge
|
|
|
|
|
|
BASE_DIR = os.path.dirname(os.path.dirname(__file__))
|
|
GEOJSON = json.load(open(os.path.join(
|
|
BASE_DIR, 'tests/data/mairie-geojson.json')))['features']
|
|
MDR_GEOJSON = json.load(open(os.path.join(
|
|
BASE_DIR, 'tests/data/mdr-geojson.json')))['features']
|
|
TZOFFSETS = {"Europe/Paris": 3600}
|
|
|
|
|
|
@pytest.mark.freeze_time("2018-03-04 23:00:00",)
|
|
def test_every_mairie_closed():
|
|
"""every mairie is closed at mignight"""
|
|
opening_hours = [as_opening_hours_badge(x) for x in GEOJSON]
|
|
assert len([x for x in opening_hours if 'open' in x]) == 0
|
|
|
|
|
|
def test_all_mairie_data_parsed_correct():
|
|
"""everything got parsed correctly"""
|
|
opening_hours = [as_opening_hours_badge(x) for x in GEOJSON
|
|
if x['properties'].get('openinghours') or x['properties'].get('openinghoursspecification')]
|
|
assert opening_hours.count('') == 0
|
|
|
|
|
|
@pytest.mark.freeze_time("2018-03-05 14:59:00")
|
|
def test_empty_data():
|
|
""""no data return the empty html"""
|
|
test_html = as_opening_hours_badge(GEOJSON[0]['properties'])
|
|
assert test_html == ''
|
|
|
|
|
|
@pytest.mark.freeze_time("2018-08-13 14:59:00")
|
|
def test_mairie_bron_monday():
|
|
"""S1326"""
|
|
test_html = [as_opening_hours_badge(x) for x in GEOJSON if x['properties']['identifiant'] == 'S1326'][0]
|
|
klass, label = 'open', u"Ouvert jusqu'à 17h15"
|
|
assert test_html == mark_safe(u'<div class="badge %s"><span>%s</span></div>' % (klass, label))
|
|
|
|
|
|
@pytest.mark.freeze_time("2018-08-12 14:59:00")
|
|
def test_mairie_bron_sunday():
|
|
"""S1326"""
|
|
test_html = [as_opening_hours_badge(x) for x in GEOJSON if x['properties']['identifiant'] == 'S1326'][0]
|
|
klass, label = 'closed', u"Réouvre demain à 8h00"
|
|
assert test_html == mark_safe(u'<div class="badge %s"><span>%s</span></div>' % (klass, label))
|
|
|
|
|
|
@pytest.mark.freeze_time("2018-08-09 13:30:01")
|
|
def test_mairie_sathonay_thursday_afternoon():
|
|
"""S1326"""
|
|
test_html = [as_opening_hours_badge(x) for x in GEOJSON if x['properties']['identifiant'] == 'S1415'][0]
|
|
klass, label = 'open', u"Ouvert jusqu'à 17h00"
|
|
assert test_html == mark_safe(u'<div class="badge %s"><span>%s</span></div>' % (klass, label))
|
|
|
|
|
|
@pytest.mark.freeze_time("2018-08-11 11:30:01")
|
|
def test_mairie_saint_priest_closed():
|
|
"""S1326"""
|
|
test_html = [as_opening_hours_badge(x) for x in GEOJSON if x['properties']['identifiant'] == 'S1406'][0]
|
|
klass, label = 'closed', u"Réouvre lundi à 8h15"
|
|
assert test_html == mark_safe(u'<div class="badge %s"><span>%s</span></div>' % (klass, label))
|
|
|
|
|
|
@pytest.mark.freeze_time("2018-08-22 12:13:01")
|
|
def test_mairie_saint_priest_open():
|
|
"""S1326"""
|
|
test_html = [as_opening_hours_badge(x) for x in GEOJSON if x['properties']['identifiant'] == 'S1406'][0]
|
|
klass, label = 'soon-to-be-closed', u"Ouvert jusqu'à 12h15"
|
|
assert test_html == mark_safe(u'<div class="badge %s"><span>%s</span></div>' % (klass, label))
|
|
|
|
|
|
@pytest.mark.freeze_time("2018-08-11 14:59:00")
|
|
def test_mairie_bron_saturday():
|
|
"""S1326"""
|
|
test_html = [as_opening_hours_badge(x) for x in GEOJSON if x['properties']['identifiant'] == 'S1326'][0]
|
|
klass, label = 'closed', u"Réouvre lundi à 8h00"
|
|
assert test_html == mark_safe(u'<div class="badge %s"><span>%s</span></div>' % (klass, label))
|
|
|
|
|
|
@pytest.mark.freeze_time("2018-03-05 09:59:13")
|
|
def test_jonage_open():
|
|
"Jonage is defined only by openinghoursspecification data"
|
|
test_html = [as_opening_hours_badge(x) for x in GEOJSON if x['properties']['nom'] == 'Mairie de Jonage'][0]
|
|
klass, label = 'open', u"Ouvert jusqu'à 12h30"
|
|
assert test_html == mark_safe(u'<div class="badge %s"><span>%s</span></div>' % (klass, label))
|
|
|
|
|
|
@pytest.mark.freeze_time("2018-03-05 11:32:00")
|
|
def test_jonage_soon_to_be_closed():
|
|
"Jonage is defined only by openinghoursspecification data"
|
|
test_html = [as_opening_hours_badge(x) for x in GEOJSON if x['properties']['nom'] == 'Mairie de Jonage'][0]
|
|
klass, label = 'soon-to-be-closed', u"Ouvert jusqu'à 12h30"
|
|
assert test_html == mark_safe(u'<div class="badge %s"><span>%s</span></div>' % (klass, label))
|
|
|
|
|
|
@pytest.mark.freeze_time("2018-03-10 17:30:00")
|
|
def test_jonage_closed():
|
|
"Jonage is defined only by openinghoursspecification data"
|
|
test_html = [as_opening_hours_badge(x) for x in GEOJSON if x['properties']['nom'] == 'Mairie de Jonage'][0]
|
|
klass, label = 'closed', u"Réouvre lundi à 8h30"
|
|
assert test_html == mark_safe(u'<div class="badge %s"><span>%s</span></div>' % (klass, label))
|
|
|
|
|
|
@pytest.mark.freeze_time("2018-03-05 15:55:00")
|
|
def test_data_input_compatibility():
|
|
"""as_opening_hours with a fixed datetime"""
|
|
klass = 'open'
|
|
label = u"Ouvert jusqu'à 17h00"
|
|
test_html = as_opening_hours_badge(GEOJSON[1]['properties'])
|
|
assert test_html == mark_safe(u'<div class="badge %s"><span>%s</span></div>' % (klass, label))
|
|
test_html = as_opening_hours_badge(GEOJSON[1])
|
|
assert test_html == mark_safe(u'<div class="badge %s"><span>%s</span></div>' % (klass, label))
|
|
|
|
|
|
@pytest.mark.freeze_time("2018-03-05 00:00:00")
|
|
def test_all_mdr_closed():
|
|
"""every mdr is closed at mignight"""
|
|
opening_hours = [as_opening_hours_badge(x) for x in MDR_GEOJSON]
|
|
assert len([x for x in opening_hours if 'open' in x]) == 0
|
|
|
|
|
|
def test_all_mdr_data_parsed_correct():
|
|
"""everything got parsed correctly"""
|
|
opening_hours = [as_opening_hours_badge(x) for x in MDR_GEOJSON]
|
|
assert opening_hours.count('') == 0
|
|
|
|
|
|
@pytest.mark.freeze_time("2018-03-05 14:59:00")
|
|
def test_mdr_open():
|
|
test_html = as_opening_hours_badge(MDR_GEOJSON[0])
|
|
klass, label = 'open', u"Ouvert jusqu'à 16h45"
|
|
assert test_html == mark_safe(u'<div class="badge %s"><span>%s</span></div>' % (klass, label))
|
|
|
|
|
|
@pytest.mark.freeze_time("2018-03-05 15:46:00")
|
|
def test_mdr_soon_to_be_closed():
|
|
test_html = as_opening_hours_badge(MDR_GEOJSON[0])
|
|
klass, label = 'soon-to-be-closed', u"Ouvert jusqu'à 16h45"
|
|
assert test_html == mark_safe(u'<div class="badge %s"><span>%s</span></div>' % (klass, label))
|
|
|
|
|
|
@pytest.mark.freeze_time("2018-03-10 17:30:00")
|
|
def test_mdr_just_closed():
|
|
test_html = as_opening_hours_badge(MDR_GEOJSON[0])
|
|
klass, label = 'closed', u"Réouvre lundi à 8h30"
|
|
assert test_html == mark_safe(u'<div class="badge %s"><span>%s</span></div>' % (klass, label))
|
|
|
|
|
|
@pytest.mark.freeze_time("2018-01-01 14:59:00")
|
|
def test_mairie_holiday():
|
|
# Ecully, using datetimes
|
|
test_html = [as_opening_hours_badge(x) for x in GEOJSON if x['properties']['identifiant'] == 'S1361'][0]
|
|
klass, label = 'closed', u"Réouvre demain à 8h30"
|
|
assert test_html == mark_safe(u'<div class="badge %s"><span>%s</span></div>' % (klass, label))
|
|
|
|
# Feyzin, using dates
|
|
test_html = [as_opening_hours_badge(x) for x in GEOJSON if x['properties']['identifiant'] == 'S1365'][0]
|
|
klass, label = 'closed', u"Réouvre demain à 8h30"
|
|
assert test_html == mark_safe(u'<div class="badge %s"><span>%s</span></div>' % (klass, label))
|
|
|
|
|
|
@pytest.mark.freeze_time("2018-03-05 00:30:00")
|
|
@pytest.mark.parametrize('day, hours, badge, text', [
|
|
('mercredi_pm', '14h30-17h45', 'closed', 'Réouvre mercredi à 14h30'),
|
|
('mardi_pm', '14h30-17h45', 'closed', 'Réouvre demain à 14h30'),
|
|
('mardi_pm', '14h30-00h15', 'closed', 'Réouvre demain à 14h30'),
|
|
('lundi_pm', '14h30-17h45', 'closed', 'Réouvre à 14h30'),
|
|
('lundi_am', '08h30-11h45', 'closed', 'Ouvre à 8h30'),
|
|
('lundi_am', '00h00-00h45', 'soon-to-be-closed', "Ouvert jusqu'à 0h45"),
|
|
('dimanche_pm', '20h30-00h45', 'soon-to-be-closed', "Ouvert jusqu'à 0h45"),
|
|
('lundi_am', '00h15-24h00', 'open', "Ouvert jusqu'à minuit"),
|
|
])
|
|
def test_mdr_format(day, hours, badge, text):
|
|
geojson = """
|
|
{
|
|
"properties": {
|
|
"%s" : "%s"
|
|
}
|
|
}
|
|
""" % (day, hours)
|
|
html = as_opening_hours_badge(json.loads(geojson))
|
|
assert html == '<div class="badge %s"><span>%s</span></div>' % (badge, text)
|
|
|
|
|
|
@pytest.mark.freeze_time("2018-03-05 00:30:00")
|
|
@pytest.mark.parametrize('openinghour, badge, text', [
|
|
('We 14:30-17:45', 'closed', 'Réouvre mercredi à 14h30'),
|
|
('Tu 14:30-17:45', 'closed', 'Réouvre demain à 14h30'),
|
|
('Tu 14:30-00:15', 'closed', 'Réouvre demain à 14h30'),
|
|
('Mo 14:30-17:45', 'closed', 'Réouvre à 14h30'),
|
|
('Mo 08:30-11:45', 'closed', 'Ouvre à 8h30'),
|
|
('Mo 00:00-00:45', 'soon-to-be-closed', "Ouvert jusqu'à 0h45"),
|
|
('Su 20:30-00:45', 'soon-to-be-closed', "Ouvert jusqu'à 0h45"),
|
|
('Mo 00:15-24:00', 'open', "Ouvert jusqu'à minuit"),
|
|
])
|
|
def test_mairie_format_openinghours(openinghour, badge, text):
|
|
geojson = """
|
|
{
|
|
"properties": {
|
|
"openinghours": ["%s"]
|
|
}
|
|
}
|
|
""" % openinghour
|
|
html = as_opening_hours_badge(json.loads(geojson))
|
|
assert html == '<div class="badge %s"><span>%s</span></div>' % (badge, text)
|
|
|
|
|
|
@pytest.mark.freeze_time("2018-03-05 00:30:00")
|
|
@pytest.mark.parametrize('day, opens, closes, badge, text', [
|
|
('Wednesday', '14:30', '17:45', 'closed', 'Réouvre mercredi à 14h30'),
|
|
('Tuesday', '14:30', '17:45', 'closed', 'Réouvre demain à 14h30'),
|
|
('Tuesday', '14:30', '00:15', 'closed', 'Réouvre demain à 14h30'),
|
|
('Monday', '14:30', '17:45', 'closed', 'Réouvre à 14h30'),
|
|
('Monday', '08:30', '11:45', 'closed', 'Ouvre à 8h30'),
|
|
('Monday', '00:00', '00:45', 'soon-to-be-closed', "Ouvert jusqu'à 0h45"),
|
|
('Sunday', '20:30', '00:45', 'soon-to-be-closed', "Ouvert jusqu'à 0h45"),
|
|
('Monday', '00:15', '24:00', 'open', "Ouvert jusqu'à minuit"),
|
|
])
|
|
def test_mairie_format_openinghoursspecification(day, opens, closes, badge, text):
|
|
geojson = """
|
|
{
|
|
"properties": {
|
|
"openinghoursspecification": [{
|
|
"opens": "%s",
|
|
"closes": "%s",
|
|
"dayOfWeek": "http:\/\/schema.org\/%s",
|
|
"validFrom": "2018-01-01T00:00:00+01:00",
|
|
"validThrough": "2018-06-30T23:59:59+01:00"
|
|
}]
|
|
}
|
|
}
|
|
""" % (opens, closes, day)
|
|
html = as_opening_hours_badge(json.loads(geojson))
|
|
assert html == '<div class="badge %s"><span>%s</span></div>' % (badge, text)
|