diff --git a/tests/conftest.py b/tests/conftest.py index c01a1e8..65d4f5d 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -1,4 +1,5 @@ import pathlib +from typing import Type import jinja2 import pytest @@ -53,6 +54,57 @@ def text_entry() -> str: return "My Text Entry" +@pytest.fixture +def invalid_entries() -> dict[ + Type[dm.EducationEntry] + | Type[dm.ExperienceEntry] + | Type[dm.PublicationEntry] + | Type[dm.OneLineEntry] + | Type[dm.NormalEntry], + list[dict[str, str]], +]: + invalid_entries = dict() + invalid_entries[dm.EducationEntry] = [ + { + "institution": "Boğaziçi University", + "area": "Mechanical Engineering", + "degree": "BS", + "date": "2028-12-08", + }, + { + "area": "Mechanical Engineering", + }, + ] + invalid_entries[dm.ExperienceEntry] = [ + { + "company": "CERN", + }, + { + "position": "Researcher", + }, + ] + invalid_entries[dm.PublicationEntry] = [ + { + "doi": "10.1109/TASC.2023.3340648", + }, + { + "authors": ["John Doe", "Jane Doe"], + }, + ] + invalid_entries[dm.OneLineEntry] = [ + { + "name": "My One Line Entry", + }, + ] + invalid_entries[dm.NormalEntry] = [ + { + "name": "My Entry", + }, + ] + + return invalid_entries + + @pytest.fixture def rendercv_data_model( education_entry, diff --git a/tests/test_user_communicator.py b/tests/test_user_communicator.py new file mode 100644 index 0000000..807d63f --- /dev/null +++ b/tests/test_user_communicator.py @@ -0,0 +1,68 @@ +import rendercv.user_communicator as uc + +import pydantic +import ruamel.yaml +import pytest + + +def test_welcome(): + uc.welcome() + + +def test_warning(): + uc.warning("This is a warning message.") + + +def test_error(): + uc.error("This is an error message.") + + +def test_information(): + uc.information("This is an information message.") + + +def test_get_error_message_and_location_and_value_from_a_custom_error(): + error_string = "('error message', 'location', 'value')" + result = uc.get_error_message_and_location_and_value_from_a_custom_error( + error_string + ) + assert result == ("error message", "location", "value") + + error_string = "error message" + result = uc.get_error_message_and_location_and_value_from_a_custom_error( + error_string + ) + assert result is None + + +def test_handle_validation_error(invalid_entries): + for entry_type, entries in invalid_entries.items(): + for entry in entries: + try: + entry_type(**entry) + except pydantic.ValidationError as e: + uc.handle_validation_error(e) + + +@pytest.mark.parametrize( + "exception", + [ruamel.yaml.YAMLError, RuntimeError], +) +def test_handle_exceptions(exception): + @uc.handle_exceptions + def function_that_raises_exception(): + raise exception("This is an exception!") + + function_that_raises_exception() + + +def test_live_progress_reporter_class(): + with uc.LiveProgressReporter(number_of_steps=3) as progress: + progress.start_a_step("Test step 1") + progress.finish_the_current_step() + + progress.start_a_step("Test step 2") + progress.finish_the_current_step() + + progress.start_a_step("Test step 3") + progress.finish_the_current_step()