File: test_datetime_utils.py

package info (click to toggle)
python-webob 1%3A1.8.5-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 1,664 kB
  • sloc: python: 21,344; makefile: 171
file content (126 lines) | stat: -rw-r--r-- 4,920 bytes parent folder | download | duplicates (4)
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
import pytest

import datetime
import calendar
from email.utils import formatdate
from webob import datetime_utils

def test_UTC():
    """Test missing function in _UTC"""
    x = datetime_utils.UTC
    assert x.tzname(datetime.datetime.now()) == 'UTC'
    assert x.dst(datetime.datetime.now()) == datetime.timedelta(0)
    assert x.utcoffset(datetime.datetime.now()) == datetime.timedelta(0)
    assert repr(x) == 'UTC'

def test_parse_date():
    """Testing datetime_utils.parse_date.
    We need to verify the following scenarios:
        * a nil submitted value
        * a submitted value that cannot be parse into a date
        * a valid RFC2822 date with and without timezone
    """

    ret = datetime_utils.parse_date(None)
    assert ret is None, ("We passed a None value to parse_date. We should get"
                         " None but instead we got %s" % ret)

    ret = datetime_utils.parse_date('Hi There')
    assert ret is None, ("We passed an invalid value to parse_date. We should"
                         " get None but instead we got %s" % ret)

    ret = datetime_utils.parse_date(1)
    assert ret is None, ("We passed an invalid value to parse_date. We should"
                         " get None but instead we got %s" % ret)

    ret = datetime_utils.parse_date('\xc3')
    assert ret is None, ("We passed an invalid value to parse_date. We should"
                         " get None but instead we got %s" % ret)

    ret = datetime_utils.parse_date('Mon, 20 Nov 1995 19:12:08 -0500')
    assert ret == datetime.datetime(
        1995, 11, 21, 0, 12, 8, tzinfo=datetime_utils.UTC)

    ret = datetime_utils.parse_date('Mon, 20 Nov 1995 19:12:08')

    assert ret == datetime.datetime(
        1995, 11, 20, 19, 12, 8, tzinfo=datetime_utils.UTC)

    ret = datetime_utils.parse_date(Uncooperative())
    assert ret is None

class Uncooperative(object):
    def __str__(self):
        raise NotImplementedError

def test_serialize_date():
    """Testing datetime_utils.serialize_date
    We need to verify the following scenarios:
        * on py3, passing an binary date, return the same date but str
        * on py2, passing an unicode date, return the same date but str
        * passing a timedelta, return now plus the delta
        * passing an invalid object, should raise ValueError
    """
    from webob.compat import text_
    ret = datetime_utils.serialize_date('Mon, 20 Nov 1995 19:12:08 GMT')
    assert isinstance(ret, str)
    assert ret == 'Mon, 20 Nov 1995 19:12:08 GMT'
    ret = datetime_utils.serialize_date(text_('Mon, 20 Nov 1995 19:12:08 GMT'))
    assert isinstance(ret, str)
    assert ret == 'Mon, 20 Nov 1995 19:12:08 GMT'
    dt = formatdate(
        calendar.timegm(
            (datetime.datetime.now() + datetime.timedelta(1)).timetuple()),
        usegmt=True)
    assert dt == datetime_utils.serialize_date(datetime.timedelta(1))
    with pytest.raises(ValueError):
        datetime_utils.serialize_date(None)

def test_parse_date_delta():
    """Testing datetime_utils.parse_date_delta
    We need to verify the following scenarios:
        * passing a nil value, should return nil
        * passing a value that fails the conversion to int, should call
          parse_date
    """
    assert datetime_utils.parse_date_delta(None) is None, ('Passing none value,'
                                                           'should return None')
    ret = datetime_utils.parse_date_delta('Mon, 20 Nov 1995 19:12:08 -0500')
    assert ret == datetime.datetime(
        1995, 11, 21, 0, 12, 8, tzinfo=datetime_utils.UTC)
    WHEN = datetime.datetime(2011, 3, 16, 10, 10, 37, tzinfo=datetime_utils.UTC)
    with _NowRestorer(WHEN):
        ret = datetime_utils.parse_date_delta(1)
        assert ret == WHEN + datetime.timedelta(0, 1)

def test_serialize_date_delta():
    """Testing datetime_utils.serialize_date_delta
    We need to verify the following scenarios:
        * if we pass something that's not an int or float, it should delegate
          the task to serialize_date
    """
    assert datetime_utils.serialize_date_delta(1) == '1'
    assert datetime_utils.serialize_date_delta(1.5) == '1'
    ret = datetime_utils.serialize_date_delta('Mon, 20 Nov 1995 19:12:08 GMT')
    assert type(ret) is (str)
    assert ret == 'Mon, 20 Nov 1995 19:12:08 GMT'

def test_timedelta_to_seconds():
    val = datetime.timedelta(86400)
    result = datetime_utils.timedelta_to_seconds(val)
    assert result == 7464960000


class _NowRestorer(object):
    def __init__(self, new_now):
        self._new_now = new_now
        self._old_now = None

    def __enter__(self):
        import webob.datetime_utils
        self._old_now = webob.datetime_utils._now
        webob.datetime_utils._now = lambda: self._new_now

    def __exit__(self, exc_type, exc_value, traceback):
        import webob.datetime_utils
        webob.datetime_utils._now = self._old_now