|
1
|
|
|
from datetime import datetime, date |
|
|
|
|
|
|
2
|
|
|
|
|
3
|
|
|
import pytz |
|
|
|
|
|
|
4
|
|
|
import unittest |
|
5
|
|
|
|
|
6
|
|
|
from foil.dates import parse_date, DateTimeParser, _datetime_to_tuple |
|
7
|
|
|
|
|
8
|
|
|
|
|
9
|
|
|
test_dt_data = [ |
|
|
|
|
|
|
10
|
|
|
('2015-04-03', datetime(2015, 4, 3)), |
|
11
|
|
|
('2015-04-03T06:34:22', datetime(2015, 4, 3, 6, 34, 22)), |
|
12
|
|
|
('2015-04-03T06:34:22.234', datetime(2015, 4, 3, 6, 34, 22, 234000)), |
|
13
|
|
|
('2014-06-29T02:25:20 EST', datetime(2014, 6, 29, 6, 25, 20, tzinfo=pytz.utc)), |
|
14
|
|
|
('2013-02-07T09:34:22 EDT', datetime(2013, 2, 7, 14, 34, 22, tzinfo=pytz.utc))] |
|
15
|
|
|
|
|
16
|
|
|
|
|
17
|
|
|
def dt_dict(): |
|
18
|
|
|
return dict(year='2014', month='07', day='03', hour='13', |
|
19
|
|
|
minute='02', second='45', microsecond='321') |
|
20
|
|
|
|
|
21
|
|
|
|
|
22
|
|
|
class TestToolsDates(unittest.TestCase): |
|
23
|
|
|
|
|
24
|
|
|
def setUp(self): |
|
25
|
|
|
self.dt_dict = dt_dict() |
|
26
|
|
|
|
|
27
|
|
|
def test_factset_time_pattern(self): |
|
28
|
|
|
date_parser = DateTimeParser() |
|
29
|
|
|
|
|
30
|
|
|
for t in test_dt_data: |
|
31
|
|
|
data = t[0] |
|
32
|
|
|
expected = t[1] |
|
33
|
|
|
result = date_parser.parse(data) |
|
34
|
|
|
with self.subTest(data=data): |
|
35
|
|
|
self.assertEqual(expected, result) |
|
36
|
|
|
|
|
37
|
|
|
def test_parse_date(self): |
|
38
|
|
|
expected = date(2015, 4, 3) |
|
39
|
|
|
result = parse_date('2015-04-03') |
|
40
|
|
|
|
|
41
|
|
|
self.assertEqual(expected, result) |
|
42
|
|
|
|
|
43
|
|
|
def test_all_string_components(self): |
|
44
|
|
|
expected = (2014, 7, 3, 13, 2, 45, 321) |
|
45
|
|
|
result = _datetime_to_tuple(self.dt_dict) |
|
46
|
|
|
|
|
47
|
|
|
self.assertEqual(expected, result) |
|
48
|
|
|
|
|
49
|
|
|
def test_null_microsecond(self): |
|
50
|
|
|
self.dt_dict['microsecond'] = None |
|
51
|
|
|
|
|
52
|
|
|
expected = (2014, 7, 3, 13, 2, 45, 0) |
|
53
|
|
|
result = _datetime_to_tuple(self.dt_dict) |
|
54
|
|
|
|
|
55
|
|
|
self.assertEqual(expected, result) |
|
56
|
|
|
|
|
57
|
|
|
def test_no_microsecond(self): |
|
58
|
|
|
self.dt_dict['hour'] = None |
|
59
|
|
|
|
|
60
|
|
|
expected = (2014, 7, 3, 0, 2, 45, 321) |
|
61
|
|
|
result = _datetime_to_tuple(self.dt_dict) |
|
62
|
|
|
|
|
63
|
|
|
self.assertEqual(expected, result) |
|
64
|
|
|
|
|
65
|
|
|
def test_fail_no_month(self): |
|
66
|
|
|
self.dt_dict['month'] = None |
|
67
|
|
|
|
|
68
|
|
|
with self.assertRaises(TypeError): |
|
69
|
|
|
_datetime_to_tuple(dt_dict) |
|
70
|
|
|
|
The coding style of this project requires that you add a docstring to this code element. Below, you find an example for methods:
If you would like to know more about docstrings, we recommend to read PEP-257: Docstring Conventions.