test_date.py 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  1. # -*- coding: utf-8 -*-
  2. # Copyright (c) 2021, Brandon Nielsen
  3. # All rights reserved.
  4. #
  5. # This software may be modified and distributed under the terms
  6. # of the BSD license. See the LICENSE file for details.
  7. import unittest
  8. import aniso8601
  9. from aniso8601.date import get_date_resolution, parse_date
  10. from aniso8601.exceptions import DayOutOfBoundsError, ISOFormatError
  11. from aniso8601.resolution import DateResolution
  12. from aniso8601.tests.compat import mock
  13. class TestDateResolutionFunctions(unittest.TestCase):
  14. def test_get_date_resolution_year(self):
  15. self.assertEqual(get_date_resolution("2013"), DateResolution.Year)
  16. self.assertEqual(get_date_resolution("0001"), DateResolution.Year)
  17. self.assertEqual(get_date_resolution("19"), DateResolution.Year)
  18. def test_get_date_resolution_month(self):
  19. self.assertEqual(get_date_resolution("1981-04"), DateResolution.Month)
  20. def test_get_date_resolution_week(self):
  21. self.assertEqual(get_date_resolution("2004-W53"), DateResolution.Week)
  22. self.assertEqual(get_date_resolution("2009-W01"), DateResolution.Week)
  23. self.assertEqual(get_date_resolution("2004W53"), DateResolution.Week)
  24. def test_get_date_resolution_day(self):
  25. self.assertEqual(get_date_resolution("2004-04-11"), DateResolution.Day)
  26. self.assertEqual(get_date_resolution("20090121"), DateResolution.Day)
  27. def test_get_date_resolution_year_weekday(self):
  28. self.assertEqual(get_date_resolution("2004-W53-6"), DateResolution.Weekday)
  29. self.assertEqual(get_date_resolution("2004W536"), DateResolution.Weekday)
  30. def test_get_date_resolution_year_ordinal(self):
  31. self.assertEqual(get_date_resolution("1981-095"), DateResolution.Ordinal)
  32. self.assertEqual(get_date_resolution("1981095"), DateResolution.Ordinal)
  33. def test_get_date_resolution_badtype(self):
  34. testtuples = (None, 1, False, 1.234)
  35. for testtuple in testtuples:
  36. with self.assertRaises(ValueError):
  37. get_date_resolution(testtuple)
  38. def test_get_date_resolution_extended_year(self):
  39. testtuples = ("+2000", "+30000")
  40. for testtuple in testtuples:
  41. with self.assertRaises(NotImplementedError):
  42. get_date_resolution(testtuple)
  43. def test_get_date_resolution_badweek(self):
  44. testtuples = ("2004-W1", "2004W1")
  45. for testtuple in testtuples:
  46. with self.assertRaises(ISOFormatError):
  47. get_date_resolution(testtuple)
  48. def test_get_date_resolution_badweekday(self):
  49. testtuples = ("2004-W53-67", "2004W5367")
  50. for testtuple in testtuples:
  51. with self.assertRaises(ISOFormatError):
  52. get_date_resolution(testtuple)
  53. def test_get_date_resolution_badstr(self):
  54. testtuples = (
  55. "W53",
  56. "2004-W",
  57. "2014-01-230",
  58. "2014-012-23",
  59. "201-01-23",
  60. "201401230",
  61. "201401",
  62. "",
  63. )
  64. for testtuple in testtuples:
  65. with self.assertRaises(ISOFormatError):
  66. get_date_resolution(testtuple)
  67. class TestDateParserFunctions(unittest.TestCase):
  68. def test_parse_date(self):
  69. testtuples = (
  70. (
  71. "2013",
  72. {
  73. "YYYY": "2013",
  74. "MM": None,
  75. "DD": None,
  76. "Www": None,
  77. "D": None,
  78. "DDD": None,
  79. },
  80. ),
  81. (
  82. "0001",
  83. {
  84. "YYYY": "0001",
  85. "MM": None,
  86. "DD": None,
  87. "Www": None,
  88. "D": None,
  89. "DDD": None,
  90. },
  91. ),
  92. (
  93. "19",
  94. {
  95. "YYYY": "19",
  96. "MM": None,
  97. "DD": None,
  98. "Www": None,
  99. "D": None,
  100. "DDD": None,
  101. },
  102. ),
  103. (
  104. "1981-04-05",
  105. {
  106. "YYYY": "1981",
  107. "MM": "04",
  108. "DD": "05",
  109. "Www": None,
  110. "D": None,
  111. "DDD": None,
  112. },
  113. ),
  114. (
  115. "19810405",
  116. {
  117. "YYYY": "1981",
  118. "MM": "04",
  119. "DD": "05",
  120. "Www": None,
  121. "D": None,
  122. "DDD": None,
  123. },
  124. ),
  125. (
  126. "1981-04",
  127. {
  128. "YYYY": "1981",
  129. "MM": "04",
  130. "DD": None,
  131. "Www": None,
  132. "D": None,
  133. "DDD": None,
  134. },
  135. ),
  136. (
  137. "2004-W53",
  138. {
  139. "YYYY": "2004",
  140. "MM": None,
  141. "DD": None,
  142. "Www": "53",
  143. "D": None,
  144. "DDD": None,
  145. },
  146. ),
  147. (
  148. "2009-W01",
  149. {
  150. "YYYY": "2009",
  151. "MM": None,
  152. "DD": None,
  153. "Www": "01",
  154. "D": None,
  155. "DDD": None,
  156. },
  157. ),
  158. (
  159. "2004-W53-6",
  160. {
  161. "YYYY": "2004",
  162. "MM": None,
  163. "DD": None,
  164. "Www": "53",
  165. "D": "6",
  166. "DDD": None,
  167. },
  168. ),
  169. (
  170. "2004W53",
  171. {
  172. "YYYY": "2004",
  173. "MM": None,
  174. "DD": None,
  175. "Www": "53",
  176. "D": None,
  177. "DDD": None,
  178. },
  179. ),
  180. (
  181. "2004W536",
  182. {
  183. "YYYY": "2004",
  184. "MM": None,
  185. "DD": None,
  186. "Www": "53",
  187. "D": "6",
  188. "DDD": None,
  189. },
  190. ),
  191. (
  192. "1981-095",
  193. {
  194. "YYYY": "1981",
  195. "MM": None,
  196. "DD": None,
  197. "Www": None,
  198. "D": None,
  199. "DDD": "095",
  200. },
  201. ),
  202. (
  203. "1981095",
  204. {
  205. "YYYY": "1981",
  206. "MM": None,
  207. "DD": None,
  208. "Www": None,
  209. "D": None,
  210. "DDD": "095",
  211. },
  212. ),
  213. (
  214. "1980366",
  215. {
  216. "YYYY": "1980",
  217. "MM": None,
  218. "DD": None,
  219. "Www": None,
  220. "D": None,
  221. "DDD": "366",
  222. },
  223. ),
  224. )
  225. for testtuple in testtuples:
  226. with mock.patch.object(
  227. aniso8601.date.PythonTimeBuilder, "build_date"
  228. ) as mockBuildDate:
  229. mockBuildDate.return_value = testtuple[1]
  230. result = parse_date(testtuple[0])
  231. self.assertEqual(result, testtuple[1])
  232. mockBuildDate.assert_called_once_with(**testtuple[1])
  233. def test_parse_date_badtype(self):
  234. testtuples = (None, 1, False, 1.234)
  235. for testtuple in testtuples:
  236. with self.assertRaises(ValueError):
  237. parse_date(testtuple, builder=None)
  238. def test_parse_date_badstr(self):
  239. testtuples = (
  240. "W53",
  241. "2004-W",
  242. "2014-01-230",
  243. "2014-012-23",
  244. "201-01-23",
  245. "201401230",
  246. "201401",
  247. "9999 W53",
  248. "20.50230",
  249. "198104",
  250. "bad",
  251. "",
  252. )
  253. for testtuple in testtuples:
  254. with self.assertRaises(ISOFormatError):
  255. parse_date(testtuple, builder=None)
  256. def test_parse_date_mockbuilder(self):
  257. mockBuilder = mock.Mock()
  258. expectedargs = {
  259. "YYYY": "1981",
  260. "MM": "04",
  261. "DD": "05",
  262. "Www": None,
  263. "D": None,
  264. "DDD": None,
  265. }
  266. mockBuilder.build_date.return_value = expectedargs
  267. result = parse_date("1981-04-05", builder=mockBuilder)
  268. self.assertEqual(result, expectedargs)
  269. mockBuilder.build_date.assert_called_once_with(**expectedargs)