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

18 statements  

« prev     ^ index     » next       coverage.py v7.6.4, created at 2024-11-22 07:51 +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 get_date_help_text_from_dates, 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, help_text) 

20dates = { 

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

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

23 None, 

24 None, 

25 "Date interpreted as 1449-10-23", 

26 ), 

27 "1459-12": ( 

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

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

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

31 "Date interpreted as 1459-12-1 until 1459-12-31", 

32 ), 

33 "1460-03-30": (fi("1460-03-30"), None, None, "Date interpreted as 1460-3-30"), 

34 "": ( 

35 None, 

36 None, 

37 None, 

38 "<b>Date could not be interpreted</b><br>" + help_text_default, 

39 ), 

40} 

41 

42 

43class DateParserTest(TestCase): 

44 def test_dates(self): 

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

46 single, start, end = parse_date(datestring) 

47 self.assertEqual(expsingle, single) 

48 self.assertEqual(expstart, start) 

49 self.assertEqual(expend, end) 

50 

51 def test_help_text(self): 

52 for datestring, (single, start, end, exp_help_text) in dates.items(): 

53 help_text = get_date_help_text_from_dates(single, start, end, datestring) 

54 self.assertEqual(exp_help_text, help_text)