From f2a8e152c6f2cbcdcf0345117f34ace3dae2b2f1 Mon Sep 17 00:00:00 2001 From: Sina Atalay Date: Mon, 2 Oct 2023 19:02:28 +0200 Subject: [PATCH] add tests --- rendercv/data_model.py | 5 ++++ tests/__init__.py | 0 tests/test_rendercv.py | 60 ++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 65 insertions(+) create mode 100644 tests/__init__.py create mode 100644 tests/test_rendercv.py diff --git a/rendercv/data_model.py b/rendercv/data_model.py index 941c88b..7a31c24 100644 --- a/rendercv/data_model.py +++ b/rendercv/data_model.py @@ -123,6 +123,11 @@ def compute_time_span_string(start_date: Date, end_date: Date) -> str: # calculate the number of days between start_date and end_date: timespan_in_days = (end_date - start_date).days + if timespan_in_days < 0: + raise ValueError( + "The start date is after the end date. Please check the dates!" + ) + # calculate the number of years between start_date and end_date: how_many_years = timespan_in_days // 365 if how_many_years == 0: diff --git a/tests/__init__.py b/tests/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/tests/test_rendercv.py b/tests/test_rendercv.py new file mode 100644 index 0000000..2d11c8f --- /dev/null +++ b/tests/test_rendercv.py @@ -0,0 +1,60 @@ +import unittest +from rendercv import data_model, rendering + +from datetime import date as Date + + +class TestRendercv(unittest.TestCase): + def test_check_spelling(self): + sentences = [ + "This is a sentence.", + "This is a sentance with special characters &@#&^@*#&)((!@#_)()).", + "12312309 Thisdf sdfsd is a sentence *safds\{\}[[[]]]", + ] + + for sentence in sentences: + data_model.check_spelling(sentence) + + def test_compute_time_span_string(self): + start_date = Date(year=2020, month=1, day=1) + end_date = Date(year=2021, month=1, day=1) + expected = "1 year 1 month" + result = data_model.compute_time_span_string(start_date, end_date) + self.assertEqual(result, expected) + + start_date = Date(year=2020, month=1, day=1) + end_date = Date(year=2020, month=2, day=1) + expected = "1 month" + result = data_model.compute_time_span_string(start_date, end_date) + self.assertEqual(result, expected) + + start_date = Date(year=2020, month=1, day=1) + end_date = Date(year=2023, month=3, day=2) + expected = "3 years 2 months" + result = data_model.compute_time_span_string(start_date, end_date) + self.assertEqual(result, expected) + + start_date = Date(year=2020, month=1, day=1) + end_date = Date(year=1982, month=1, day=1) + with self.assertRaises(ValueError): + data_model.compute_time_span_string(start_date, end_date) + + def test_format_date(self): + date = Date(year=2020, month=1, day=1) + expected = "Jan. 2020" + result = data_model.format_date(date) + self.assertEqual(result, expected) + + date = Date(year=1983, month=12, day=1) + expected = "Dec. 1983" + result = data_model.format_date(date) + self.assertEqual(result, expected) + + date = Date(year=2045, month=6, day=1) + expected = "June 2045" + result = data_model.format_date(date) + self.assertEqual(result, expected) + + +if __name__ == "__main__": + unittest.main()