Coverage for apis_core/utils/test_DateParser.py: 100%

14 statements  

« prev     ^ index     » next       coverage.py v7.6.8, created at 2024-12-20 09:24 +0000

1# SPDX-FileCopyrightText: 2023 Birger Schacht 

2# SPDX-License-Identifier: MIT 

3 

4from datetime import datetime 

5 

6from django.test import TestCase 

7 

8from .DateParser import parse_date 

9 

10help_text_default = "Dates are interpreted by defined rules. If this fails, an iso-date can be explicitly set with '<YYYY-MM-DD>'." 

11 

12 

13# helper function so we don't have to write as much 

14def fi(datestring): 

15 return datetime.fromisoformat(datestring) 

16 

17 

18# a dict of dates with the written date as key 

19# followed by a tuple of (single, start, end) 

20dates = { 

21 "vor dem 23.10.1449 <1449-10-23>": ( 

22 fi("1449-10-23"), 

23 None, 

24 None, 

25 ), 

26 "1459-12": ( 

27 fi("1459-12-16"), 

28 fi("1459-12-01"), 

29 fi("1459-12-31"), 

30 ), 

31 "1460-03-30": (fi("1460-03-30"), None, None), 

32 "": ( 

33 None, 

34 None, 

35 None, 

36 ), 

37} 

38 

39 

40class DateParserTest(TestCase): 

41 def test_dates(self): 

42 for datestring, (expsingle, expstart, expend) in dates.items(): 

43 single, start, end = parse_date(datestring) 

44 self.assertEqual(expsingle, single) 

45 self.assertEqual(expstart, start) 

46 self.assertEqual(expend, end)