| 1 |
# -*- coding: utf-8 -*- |
| 2 |
|
| 3 |
import pytest |
| 4 |
|
| 5 |
from freezegun import freeze_time |
| 6 |
from jinja2 import Environment, exceptions |
| 7 |
|
| 8 |
|
| 9 |
@pytest.fixture |
| 10 |
def environment(): |
| 11 |
return Environment(extensions=['jinja2_time.TimeExtension']) |
| 12 |
|
| 13 |
|
| 14 |
@pytest.yield_fixture(autouse=True) |
| 15 |
def freeze(): |
| 16 |
freezer = freeze_time("2015-12-09 23:33:01") |
| 17 |
freezer.start() |
| 18 |
yield |
| 19 |
freezer.stop() |
| 20 |
|
| 21 |
|
| 22 |
def test_tz_is_required(environment): |
| 23 |
with pytest.raises(exceptions.TemplateSyntaxError): |
| 24 |
environment.from_string('{% now %}') |
| 25 |
|
| 26 |
|
| 27 |
def test_utc_default_datetime_format(environment): |
| 28 |
template = environment.from_string("{% now 'utc' %}") |
| 29 |
|
| 30 |
assert template.render() == "2015-12-09" |
| 31 |
|
| 32 |
|
| 33 |
@pytest.fixture(params=['utc', 'local', 'Europe/Berlin']) |
| 34 |
def valid_tz(request): |
| 35 |
return request.param |
| 36 |
|
| 37 |
|
| 38 |
def test_accept_valid_timezones(environment, valid_tz): |
| 39 |
template = environment.from_string( |
| 40 |
"{% now '" + valid_tz + "', '%Y-%m' %}" |
| 41 |
) |
| 42 |
|
| 43 |
assert template.render() == '2015-12' |
| 44 |
|
| 45 |
|
| 46 |
def test_environment_datetime_format(environment): |
| 47 |
environment.datetime_format = '%a, %d %b %Y %H:%M:%S' |
| 48 |
|
| 49 |
template = environment.from_string("{% now 'utc' %}") |
| 50 |
|
| 51 |
assert template.render() == "Wed, 09 Dec 2015 23:33:01" |
| 52 |
|
| 53 |
|
| 54 |
def test_add_time(environment): |
| 55 |
environment.datetime_format = '%a, %d %b %Y %H:%M:%S' |
| 56 |
|
| 57 |
template = environment.from_string( |
| 58 |
"{% now 'utc' + 'hours=2,seconds=30' %}" |
| 59 |
) |
| 60 |
|
| 61 |
assert template.render() == "Thu, 10 Dec 2015 01:33:31" |
| 62 |
|
| 63 |
|
| 64 |
def test_substract_time(environment): |
| 65 |
environment.datetime_format = '%a, %d %b %Y %H:%M:%S' |
| 66 |
|
| 67 |
template = environment.from_string( |
| 68 |
"{% now 'utc' - 'minutes=11' %}" |
| 69 |
) |
| 70 |
|
| 71 |
assert template.render() == "Wed, 09 Dec 2015 23:22:01" |
| 72 |
|
| 73 |
|
| 74 |
def test_offset_with_format(environment): |
| 75 |
environment.datetime_format = '%d %b %Y %H:%M:%S' |
| 76 |
|
| 77 |
template = environment.from_string( |
| 78 |
"{% now 'utc' - 'days=2, minutes=33,seconds=1', '%d %b %Y %H:%M:%S' %}" |
| 79 |
) |
| 80 |
|
| 81 |
assert template.render() == "07 Dec 2015 23:00:00" |