113 lines
4.9 KiB
Python
113 lines
4.9 KiB
Python
import pytest
|
|
|
|
from django.template import Context, Template
|
|
from django.test.client import RequestFactory
|
|
from django.contrib.auth.models import User, Group, AnonymousUser
|
|
|
|
pytestmark = pytest.mark.django_db
|
|
|
|
|
|
def test_strptime():
|
|
t = Template('{% load combo %}{{ someday|strptime:"%Y-%m-%d"|date:"Y" }}')
|
|
assert t.render(Context({'someday': '2015-04-15'})) == '2015'
|
|
assert t.render(Context({'someday': 'foobar'})) == ''
|
|
assert t.render(Context({'someday': None})) == ''
|
|
|
|
def test_parse_datetime():
|
|
t = Template('{% load combo %}{{ someday|parse_datetime|date:"Y m d H i s T" }}')
|
|
expected = '2015 04 15 13 11 12 UTC'
|
|
assert t.render(Context({'someday': '2015-04-15T13:11:12'})) == expected
|
|
assert t.render(Context({'someday': '2015-04-15 13:11:12'})) == expected
|
|
assert t.render(Context({'someday': '2015-04-15T13:11:12Z'})) == expected
|
|
assert t.render(Context({'someday': '2015-04-15T13:11:12+00:00'})) == expected
|
|
assert t.render(Context({'someday': '2015-04-15T13:11:12.12345'})) == expected
|
|
assert t.render(Context({'someday': '2015-04-15T13:11:12.12345Z'})) == expected
|
|
assert t.render(Context({'someday': '2015-04-15T13:11:12.12345+00:00'})) == expected
|
|
assert t.render(Context({'someday': '2015-04-15T13:11'})) == '2015 04 15 13 11 00 UTC'
|
|
assert t.render(Context({'someday': '2015-04-15T13'})) == ''
|
|
assert t.render(Context({'someday': 'foobar'})) == ''
|
|
assert t.render(Context({'someday': ''})) == ''
|
|
|
|
t = Template('{% load combo %}{{ someday|parse_date|date:"Y m d" }}')
|
|
expected = '2015 04 15'
|
|
assert t.render(Context({'someday': '2015-04-15'})) == expected
|
|
assert t.render(Context({'someday': '2015-04-15T13:11:12Z'})) == ''
|
|
assert t.render(Context({'someday': 'foobar'})) == ''
|
|
assert t.render(Context({'someday': ''})) == ''
|
|
|
|
t = Template('{% load combo %}{{ someday|parse_time|date:"H i s" }}')
|
|
expected = '13 11 12'
|
|
assert t.render(Context({'someday': '13:11:12'})) == expected
|
|
assert t.render(Context({'someday': '13:11:12Z'})) == expected
|
|
assert t.render(Context({'someday': '13:11:12+00:00'})) == expected
|
|
assert t.render(Context({'someday': '13:11:12.12345'})) == expected
|
|
assert t.render(Context({'someday': '13:11:12.12345Z'})) == expected
|
|
assert t.render(Context({'someday': '13:11:12.12345+00:00'})) == expected
|
|
assert t.render(Context({'someday': '13:11'})) == '13 11 00'
|
|
assert t.render(Context({'someday': '13:99'})) == ''
|
|
assert t.render(Context({'someday': '13'})) == ''
|
|
assert t.render(Context({'someday': 'foobar'})) == ''
|
|
assert t.render(Context({'someday': ''})) == ''
|
|
|
|
def test_has_role():
|
|
t = Template('{% load combo %}{{ request.user|has_role:"Role1" }}')
|
|
|
|
request = RequestFactory().get('/')
|
|
user = User(username='foo', email='foo@example.net')
|
|
user.save()
|
|
request.user = user
|
|
context = Context({'request': request})
|
|
assert t.render(context) == 'False'
|
|
|
|
group = Group(name='Role1')
|
|
group.save()
|
|
user.groups.add(group)
|
|
user.save()
|
|
assert t.render(context) == 'True'
|
|
|
|
t = Template('{% load combo %}{{ request.user|has_role:"Role2" }}')
|
|
assert t.render(context) == 'False'
|
|
group = Group(name='Role2')
|
|
group.save()
|
|
user.groups.add(group)
|
|
user.save()
|
|
assert t.render(context) == 'True'
|
|
|
|
# no request, no user, anonymous user
|
|
context = Context()
|
|
assert t.render(context) == 'False'
|
|
request = RequestFactory().get('/')
|
|
context = Context({'request': request})
|
|
assert t.render(context) == 'False'
|
|
request.user = AnonymousUser()
|
|
assert t.render(context) == 'False'
|
|
|
|
def test_get():
|
|
t = Template('{% load combo %}{{ foo|get:"foo-bar" }}')
|
|
context = Context({'foo': {'foo-bar': 'hello'}})
|
|
assert t.render(context) == 'hello'
|
|
context = Context({'foo': {'bar-foo': 'hello'}})
|
|
assert t.render(context) == 'None'
|
|
context = Context({'foo': None})
|
|
assert t.render(context) == 'None'
|
|
|
|
t = Template('{% load combo %}{{ foo|get:"foo-bar"|default:"" }}')
|
|
context = Context({'foo': {'rab': 'hello'}})
|
|
assert t.render(context) == ''
|
|
|
|
t = Template('{% load combo %}{{ foo|get:key }}')
|
|
context = Context({'foo': {'foo-bar': 'hello'}, 'key': 'foo-bar'})
|
|
assert t.render(context) == 'hello'
|
|
|
|
def test_get_group():
|
|
context = Context({'cities': [
|
|
{'name': 'Mumbai', 'population': '19,000,000', 'country': 'India'},
|
|
{'name': 'New York', 'population': '20,000,000', 'country': 'USA'},
|
|
{'name': 'Calcutta', 'population': '15,000,000', 'country': 'India'},
|
|
{'name': 'Chicago', 'population': '7,000,000', 'country': 'USA'},
|
|
{'name': 'Tokyo', 'population': '33,000,000', 'country': 'Japan'},
|
|
]})
|
|
t = Template('{% load combo %}{% regroup cities by country as country_list %}'
|
|
'{% for c in country_list|get_group:"USA" %}{{c.name}},{% endfor %}')
|
|
assert t.render(context) == 'New York,Chicago,'
|