Completed
Push — master ( 7e5a08...0b8668 )
by Charles
01:53
created

test_datetime_from_iso8601()   A

Complexity

Conditions 2

Size

Total Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
dl 0
loc 14
rs 9.4285
c 1
b 0
f 0
1
# -*- coding: utf-8 -*-
2
3
from datetime import datetime
4
5
import pytest
6
import pytz
7
from mock import patch
8
9
import git_app_version.helper.date as date_helper
10
11
12
@patch('git_app_version.helper.date.datetime')
13
@pytest.mark.parametrize("mockInput,expected", [
14
    (datetime(2015, 12, 21, 11, 33, 45), datetime(2015, 12, 21, 11, 33, 45)),
15
])
16
def test_utcnow(mock_dt, mockInput, expected):
17
    tz = pytz.utc
18
19
    expected_date = tz.localize(expected)
20
    mock_dt.now.return_value = tz.localize(mockInput)
21
22
    assert expected_date == date_helper.utcnow()
23
24
25
@pytest.mark.parametrize("isodate,utc,expected,expected_tz", [
26
    ("2015-12-21T11:33:45+0100", False,
27
     datetime(2015, 12, 21, 11, 33, 45), 'Europe/Paris'),
28
    ("2015-12-21T11:33:45+0100", True,
29
     datetime(2015, 12, 21, 10, 33, 45), 'UTC'),
30
    ("2015-12-21T09:33:45+0000", False,
31
     datetime(2015, 12, 21, 9, 33, 45), 'UTC'),
32
    ("2015-12-21T09:33:45Z", False, datetime(2015, 12, 21, 9, 33, 45), 'UTC'),
33
])
34
def test_datetime_from_iso8601(isodate, utc, expected, expected_tz):
35
    tz = pytz.timezone(expected_tz)
36
    expected_date = tz.localize(expected)
37
38
    assert expected_date == date_helper.datetime_from_iso8601(isodate, utc)
39
40
41
def test_datetime_from_iso8601_empty():
42
    assert date_helper.datetime_from_iso8601('', True) is None
43
44
45
@pytest.mark.parametrize("input,input_tz,expected", [
46
    (datetime(2015, 12, 21, 11, 33, 45), 'Europe/Paris',
47
     "2015-12-21T11:33:45+0100"),
48
    (datetime(2015, 12, 21, 10, 33, 45), 'UTC', "2015-12-21T10:33:45+0000"),
49
    (None, None, ''),
50
])
51
def test_iso8601_from_datetime(input, input_tz, expected):
52
    if isinstance(input, datetime):
53
        tz = pytz.timezone(input_tz)
54
        input = tz.localize(input)
55
56
    assert expected == date_helper.iso8601_from_datetime(input)
57
58
59
@pytest.mark.parametrize("dt,input_tz,expected", [
60
    (datetime(2015, 12, 21, 10, 33, 45), 'UTC', '1450694025'),
61
    (datetime(2015, 12, 21, 11, 33, 45), 'Europe/Paris', '1450694025'),
62
    (None, None, ''),
63
])
64
def test_timestamp_from_datetime(dt, input_tz, expected):
65
    if isinstance(dt, datetime):
66
        tz = pytz.timezone(input_tz)
67
        dt = tz.localize(dt)
68
69
    assert expected == date_helper.timestamp_from_datetime(dt)
70