Completed
Push — master ( 419779...7515a9 )
by Charles
02:09
created

test_datetime_from_iso8601()   A

Complexity

Conditions 2

Size

Total Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

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