From 72048b6d3390e00d8b7b06dc171455897792ce5c Mon Sep 17 00:00:00 2001 From: Sina Atalay Date: Sat, 7 Oct 2023 19:14:56 +0200 Subject: [PATCH] create tests for data_model's EducationEntry class --- rendercv/data_model.py | 20 +++--- tests/test_rendercv.py | 146 ++++++++++++++++++++++++++++++++++++++++- 2 files changed, 151 insertions(+), 15 deletions(-) diff --git a/rendercv/data_model.py b/rendercv/data_model.py index 386c05c..9a7c2db 100644 --- a/rendercv/data_model.py +++ b/rendercv/data_model.py @@ -520,7 +520,7 @@ class Event(BaseModel): end_date = Date.today() else: end_date = model.end_date - + if model.start_date > end_date: raise ValueError( '"start_date" is after "end_date". Please check the dates!' @@ -537,17 +537,13 @@ class Event(BaseModel): date_and_location_strings.append(self.location) if self.date is not None: - # Then it means start_date and end_date are not provided. - try: - # If this runs, it means the date is an ISO format string, and it can be - # parsed - date = format_date(Date.fromisoformat(self.date)) - date_and_location_strings.append(date) - except: + if isinstance(self.date, str): date_and_location_strings.append(self.date) - else: - # Then it means start_date and end_date are provided. - + elif isinstance(self.date, Date): + date_and_location_strings.append(format_date(self.date)) + else: + raise RuntimeError("Date is neither a string nor a Date object!") + elif self.start_date is not None and self.end_date is not None: start_date = format_date(self.start_date) if self.end_date == "present": @@ -694,7 +690,7 @@ class EducationEntry(Event): description="The type of the degree.", examples=["BS", "BA", "PhD", "MS"], ) - gpa: Optional[str] = Field( + gpa: Optional[str | float] = Field( default=None, title="GPA", description="The GPA of the degree.", diff --git a/tests/test_rendercv.py b/tests/test_rendercv.py index 4dd901d..be9aef0 100644 --- a/tests/test_rendercv.py +++ b/tests/test_rendercv.py @@ -87,6 +87,10 @@ class TestRendercv(unittest.TestCase): with self.subTest(msg="valid dates"): data_model.Event(**input) + # Inputs without dates: + with self.subTest(msg="no dates"): + data_model.Event(**{}) + # Inputs with invalid dates: input = { "start_date": Date(year=2020, month=1, day=1), @@ -162,7 +166,7 @@ class TestRendercv(unittest.TestCase): ] event = data_model.Event(**input) result = event.date_and_location_strings_with_timespan - with self.subTest(expected=expected): + with self.subTest(msg="start_date, end_date, and location are provided"): self.assertEqual(result, expected) input = { @@ -175,7 +179,42 @@ class TestRendercv(unittest.TestCase): ] event = data_model.Event(**input) result = event.date_and_location_strings_with_timespan - with self.subTest(expected=expected): + with self.subTest(msg="date and location are provided"): + self.assertEqual(result, expected) + + input = { + "date": Date(year=2020, month=1, day=1), + } + expected = [ + "Jan. 2020", + ] + event = data_model.Event(**input) + result = event.date_and_location_strings_with_timespan + with self.subTest(msg="date is provided"): + self.assertEqual(result, expected) + + input = { + "start_date": Date(year=2020, month=1, day=1), + "end_date": Date(year=2021, month=1, day=16), + } + expected = [ + "Jan. 2020 to Jan. 2021", + "1 year 1 month", + ] + event = data_model.Event(**input) + result = event.date_and_location_strings_with_timespan + with self.subTest(msg="start_date and end_date are provided"): + self.assertEqual(result, expected) + + input = { + "location": "My Location", + } + expected = [ + "My Location", + ] + event = data_model.Event(**input) + result = event.date_and_location_strings_with_timespan + with self.subTest(msg="location is provided"): self.assertEqual(result, expected) def test_data_event_date_and_location_strings_without_timespan(self): @@ -219,7 +258,7 @@ class TestRendercv(unittest.TestCase): ] event = data_model.Event(**input) result = event.highlight_strings - with self.subTest(expected=expected): + with self.subTest(msg="highlights are provided"): self.assertEqual(result, expected) input = {} @@ -299,6 +338,107 @@ class TestRendercv(unittest.TestCase): with self.subTest(msg="date is provided"): self.assertEqual(result, expected) + def test_data_education_highlight_strings(self): + input = { + "institution": "My Institution", + "area": "My Area", + "gpa": 3.5, + "highlights": [ + "My Highlight 1", + "My Highlight 2", + ], + } + expected = [ + "GPA: 3.5", + "My Highlight 1", + "My Highlight 2", + ] + education = data_model.EducationEntry(**input) + result = education.highlight_strings + with self.subTest(msg="gpa and highlights are provided"): + self.assertEqual(result, expected) + + input = { + "institution": "My Institution", + "area": "My Area", + "gpa": None, + "highlights": [ + "My Highlight 1", + "My Highlight 2", + ], + } + expected = [ + "My Highlight 1", + "My Highlight 2", + ] + education = data_model.EducationEntry(**input) + result = education.highlight_strings + with self.subTest(msg="gpa is not provided, but highlights are"): + self.assertEqual(result, expected) + + input = { + "institution": "My Institution", + "area": "My Area", + "gpa": 3.5, + "highlights": [], + } + expected = [ + "GPA: 3.5", + ] + education = data_model.EducationEntry(**input) + result = education.highlight_strings + with self.subTest(msg="gpa is provided, but highlights are not"): + self.assertEqual(result, expected) + + input = { + "institution": "My Institution", + "area": "My Area", + "gpa": None, + "highlights": [], + } + expected = [] + education = data_model.EducationEntry(**input) + result = education.highlight_strings + with self.subTest(msg="neither gpa nor highlights are provided"): + self.assertEqual(result, expected) + + input = { + "institution": "My Institution", + "area": "My Area", + "gpa": 3.5, + "transcript_url": "https://www.example.com/", + "highlights": None, + } + expected = [ + "GPA: 3.5 ([Transcript](https://www.example.com/))", + ] + education = data_model.EducationEntry(**input) + result = education.highlight_strings + with self.subTest( + msg="gpa and transcript_url are provided, but highlights are not" + ): + self.assertEqual(result, expected) + + input = { + "institution": "My Institution", + "area": "My Area", + "gpa": "3.5", + "transcript_url": "https://www.example.com/", + "highlights": [ + "My Highlight 1", + "My Highlight 2", + ], + } + expected = [ + "GPA: 3.5 ([Transcript](https://www.example.com/))", + "My Highlight 1", + "My Highlight 2", + ] + education = data_model.EducationEntry(**input) + result = education.highlight_strings + with self.subTest(msg="gpa, transcript_url, and highlights are provided"): + self.assertEqual(result, expected) + if __name__ == "__main__": unittest.main()