File: test_now.py

package info (click to toggle)
jinja2-time 0.2.0-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, sid
  • size: 132 kB
  • sloc: python: 143; makefile: 3
file content (81 lines) | stat: -rw-r--r-- 1,978 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# -*- coding: utf-8 -*-

import pytest

from freezegun import freeze_time
from jinja2 import Environment, exceptions


@pytest.fixture
def environment():
    return Environment(extensions=['jinja2_time.TimeExtension'])


@pytest.yield_fixture(autouse=True)
def freeze():
    freezer = freeze_time("2015-12-09 23:33:01")
    freezer.start()
    yield
    freezer.stop()


def test_tz_is_required(environment):
    with pytest.raises(exceptions.TemplateSyntaxError):
        environment.from_string('{% now %}')


def test_utc_default_datetime_format(environment):
    template = environment.from_string("{% now 'utc' %}")

    assert template.render() == "2015-12-09"


@pytest.fixture(params=['utc', 'local', 'Europe/Berlin'])
def valid_tz(request):
    return request.param


def test_accept_valid_timezones(environment, valid_tz):
    template = environment.from_string(
        "{% now '" + valid_tz + "', '%Y-%m' %}"
    )

    assert template.render() == '2015-12'


def test_environment_datetime_format(environment):
    environment.datetime_format = '%a, %d %b %Y %H:%M:%S'

    template = environment.from_string("{% now 'utc' %}")

    assert template.render() == "Wed, 09 Dec 2015 23:33:01"


def test_add_time(environment):
    environment.datetime_format = '%a, %d %b %Y %H:%M:%S'

    template = environment.from_string(
        "{% now 'utc' + 'hours=2,seconds=30' %}"
    )

    assert template.render() == "Thu, 10 Dec 2015 01:33:31"


def test_substract_time(environment):
    environment.datetime_format = '%a, %d %b %Y %H:%M:%S'

    template = environment.from_string(
        "{% now 'utc' - 'minutes=11' %}"
    )

    assert template.render() == "Wed, 09 Dec 2015 23:22:01"


def test_offset_with_format(environment):
    environment.datetime_format = '%d %b %Y %H:%M:%S'

    template = environment.from_string(
        "{% now 'utc' - 'days=2, minutes=33,seconds=1', '%d %b %Y %H:%M:%S' %}"
    )

    assert template.render() == "07 Dec 2015 23:00:00"