-
Notifications
You must be signed in to change notification settings - Fork 6
/
tests.py
167 lines (139 loc) · 7.43 KB
/
tests.py
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
from dateutils import DateUtils
import datetime
import unittest
CURRENT_YEAR = datetime.datetime.now().year
class TestDateUtils(unittest.TestCase):
def test_last_day_of_month(self):
self.assertEqual(DateUtils.last_day_of_month(2019, 3), 31)
self.assertEqual(DateUtils.last_day_of_month(2018, 7), 31)
self.assertEqual(DateUtils.last_day_of_month(2016, 2), 29)
self.assertEqual(DateUtils.last_day_of_month(2017, 2), 28)
def test_date_from_string_exact(self):
dates = DateUtils.date_from_string("01/01/2018")
self.assertEqual(dates[0], datetime.date(2018, 1, 1))
self.assertEqual(dates[1], datetime.date(2018, 1, 1))
dates = DateUtils.date_from_string("31/01/2018")
self.assertEqual(dates[0], datetime.date(2018, 1, 31))
self.assertEqual(dates[1], datetime.date(2018, 1, 31))
with self.assertRaises(ValueError):
dates = DateUtils.date_from_string("01/0/2018")
with self.assertRaises(ValueError):
dates = DateUtils.date_from_string("01/13/2018")
with self.assertRaises(ValueError):
dates = DateUtils.date_from_string("01/99/2018")
with self.assertRaises(ValueError):
dates = DateUtils.date_from_string("32/1/2018")
with self.assertRaises(ValueError):
dates = DateUtils.date_from_string("0/1/2018")
dates = DateUtils.date_from_string("12/06/2323")
self.assertEqual(dates[0], datetime.date(2323, 6, 12))
self.assertEqual(dates[1], datetime.date(2323, 6, 12))
def test_date_from_string_year(self):
dates = DateUtils.date_from_string("2018")
self.assertEqual(dates[0], datetime.date(2018, 1, 1))
self.assertEqual(dates[1], datetime.date(2018, 12, 31))
dates = DateUtils.date_from_string("3000")
self.assertEqual(dates[0], datetime.date(3000, 1, 1))
self.assertEqual(dates[1], datetime.date(3000, 12, 31))
dates = DateUtils.date_from_string("1950")
self.assertEqual(dates[0], datetime.date(1950, 1, 1))
self.assertEqual(dates[1], datetime.date(1950, 12, 31))
# We don't support years that don't have four digits.
with self.assertRaises(ValueError):
dates = DateUtils.date_from_string("659")
with self.assertRaises(ValueError):
dates = DateUtils.date_from_string("23")
with self.assertRaises(ValueError):
dates = DateUtils.date_from_string("1")
with self.assertRaises(ValueError):
dates = DateUtils.date_from_string("65900")
with self.assertRaises(ValueError):
dates = DateUtils.date_from_string("100000000000")
def test_date_from_string_month(self):
dates = DateUtils.date_from_string("12/2018")
self.assertEqual(dates[0], datetime.date(2018, 12, 1))
self.assertEqual(dates[1], datetime.date(2018, 12, 31))
dates = DateUtils.date_from_string("2/2016")
self.assertEqual(dates[0], datetime.date(2016, 2, 1))
self.assertEqual(dates[1], datetime.date(2016, 2, 29))
dates = DateUtils.date_from_string("02/2016")
self.assertEqual(dates[0], datetime.date(2016, 2, 1))
self.assertEqual(dates[1], datetime.date(2016, 2, 29))
# We don't support years that don't have four digits.
with self.assertRaises(ValueError):
dates = DateUtils.date_from_string("02/232")
with self.assertRaises(ValueError):
dates = DateUtils.date_from_string("111/2012")
with self.assertRaises(ValueError):
dates = DateUtils.date_from_string("0/2012")
def test_date_from_string_week(self):
dates = DateUtils.date_from_string("w1/2019")
self.assertEqual(dates[0], datetime.date(2018, 12, 31))
self.assertEqual(dates[1], datetime.date(2019, 1, 6))
dates = DateUtils.date_from_string("w01/2019")
self.assertEqual(dates[0], datetime.date(2018, 12, 31))
self.assertEqual(dates[1], datetime.date(2019, 1, 6))
dates = DateUtils.date_from_string("w01")
self.assertEqual(dates[0], DateUtils.from_week_number(CURRENT_YEAR, 1))
self.assertEqual(dates[1], DateUtils.from_week_number(CURRENT_YEAR, 1, end=True))
dates = DateUtils.date_from_string("w52/2016")
self.assertEqual(dates[0], datetime.date(2016, 12, 26))
self.assertEqual(dates[1], datetime.date(2017, 1, 1))
dates = DateUtils.date_from_string("w1/2017")
self.assertEqual(dates[0], datetime.date(2017, 1, 2))
self.assertEqual(dates[1], datetime.date(2017, 1, 8))
with self.assertRaises(ValueError):
dates = DateUtils.date_from_string("w02/232")
with self.assertRaises(ValueError):
dates = DateUtils.date_from_string("w111/2012")
with self.assertRaises(ValueError):
dates = DateUtils.date_from_string("w0/2012")
def test_date_from_string_quarter(self):
dates = DateUtils.date_from_string("q1/2019")
self.assertEqual(dates[0], datetime.date(2019, 1, 1))
self.assertEqual(dates[1], datetime.date(2019, 3, 31))
dates = DateUtils.date_from_string("Q1/2019")
self.assertEqual(dates[0], datetime.date(2019, 1, 1))
self.assertEqual(dates[1], datetime.date(2019, 3, 31))
dates = DateUtils.date_from_string("Q2")
self.assertEqual(dates[0], datetime.date(CURRENT_YEAR, 4, 1))
self.assertEqual(dates[1], datetime.date(CURRENT_YEAR, 6, 30))
with self.assertRaises(ValueError):
dates = DateUtils.date_from_string("Q2/232")
with self.assertRaises(ValueError):
dates = DateUtils.date_from_string("Q2/00232")
with self.assertRaises(ValueError):
dates = DateUtils.date_from_string("Q5/2012")
with self.assertRaises(ValueError):
dates = DateUtils.date_from_string("Q0/2012")
with self.assertRaises(ValueError):
dates = DateUtils.date_from_string("Q0234/2012")
def test_date_from_string_half(self):
dates = DateUtils.date_from_string("h1/2019")
self.assertEqual(dates[0], datetime.date(2019, 1, 1))
self.assertEqual(dates[1], datetime.date(2019, 6, 30))
dates = DateUtils.date_from_string("H1/2019")
self.assertEqual(dates[0], datetime.date(2019, 1, 1))
self.assertEqual(dates[1], datetime.date(2019, 6, 30))
dates = DateUtils.date_from_string("h2/2019")
self.assertEqual(dates[0], datetime.date(2019, 7, 1))
self.assertEqual(dates[1], datetime.date(2019, 12, 31))
dates = DateUtils.date_from_string("H2/2019")
self.assertEqual(dates[0], datetime.date(2019, 7, 1))
self.assertEqual(dates[1], datetime.date(2019, 12, 31))
with self.assertRaises(ValueError):
dates = DateUtils.date_from_string("H2/232")
with self.assertRaises(ValueError):
dates = DateUtils.date_from_string("H2/00232")
with self.assertRaises(ValueError):
dates = DateUtils.date_from_string("H5/2012")
with self.assertRaises(ValueError):
dates = DateUtils.date_from_string("H0/2012")
with self.assertRaises(ValueError):
dates = DateUtils.date_from_string("H0234/2012")
def test_date_range(self):
dates = DateUtils.date_range_from_string("w11-")
self.assertEqual(dates[0], DateUtils.date_from_string("w11")[0])
self.assertEqual(dates[1], datetime.date.today())
if __name__ == '__main__':
unittest.main()