Files
rendercv/tests/schema/test_pydantic_error_handling.py
Sina Atalay 5cc5fbdf9e Massive Refactor: Architecture Redesign and Technical Debt Cleanup (#528)
* Rename `data` folder with schema

* Start refactoring data models

* Work on entry models

* Keep working on entries

* Keep working on data models

* Push old data files

* Keep working on data models

* First draft of schema.cv

* Keep working on schema

* Keep working on schema

* Improve schema.models

* Keep working on rendercv.schema

* Work on schema.design

* Keep working on rendercv.schema

* Complete variant_class_generator

* Keep working rendercv.schema

* Keep working on rendercv.schema

* Final touches to rendercv.schema

* Improve json schema descriptions in rendercv.schema

* Start working on rendercv.schema tests

* Keep implementing rendercv.schema tests

* Add more tests for rendercv.schema

* Improve rendercv.schema

* Improve docstrings and comments in rendercv.schema

* Implement better pydantic error handling in `rendercv.schema`

* Improve variant class system

* Fix rendercv.schema tests

* Start working on rendercv.templater

* Update template names

* Switching to new rendercv typst template soon

* Work on new templater

* Rename renderer with renderer_old

* Don't use utils in rendercv.schema

* Complete connections

* Update renderer folder structure

* Work on new renderer

* Work on new renderer

* Date processing on new renderer

* Improve date processing, support multiple emails, phones, and websites

* Improve markdown to Typst

* Complete entry template processing

* Time span computation in new renderer

* Better entry templates

* Setup new templates

* Improve rendercv.schema

* Start adding tests for rendercv.renderer

* New markdown parser!

* Improve markdown to typst conversion

* Finalize markdown parser

* Add new test files for rendercv.renderer

* Fix cv and connections

* Add connections test

* Improve connection tests

* Improve entry templates

* Add model processor tests

* Improve templater

* Rename old folders

* Improve schema

* Add file generation logic to renderer

* Fix naming issues

* Fix schema tests

* Add path type tests

* Add font family and typst dimension type tests

* Rename old tests

* Fix design tests

* Start integration testing of renderer

* Improve entry tempates

* Handle nested highlights properly

* Finalize Typst preamble template

* Start working on new CLI

* Remove old test files

* Implement override dictionary in new schema

* Start working on new CLI

* Better prints on render command

* New structure

* New render printer

* Add all the commands to new CLI

* Work on new command in new cli

* Improve new command

* Add error handler to new cli

* Work on create theme command

* Complete create theme command

* Remove old source files

* Improve exceptions

* Create new docs

* Add writing tests guide

* Fix cli printer and write tests

* Test copy templates

* Add app tests

* Bring back accidentally removed files

* Imporve cli and tests

* Fix path issues

* Improve

* Improve

* Add reference file comparison tests

* Fix path resolver

* Start working on test_pdf_png

* Implement comparison of multiple files (png)

* Start testing typst

* Fix templating issues

* Fix header and entry templates issues

* Implement short second rows

* Fix date issues

* Fix nested bullets and add summary

* Update testdata

* Implement footer

* Update testdata

* Reimagined design and locale schema, first iteration

* Reimagined design and locale second iteration

* Update design and locale schemas

* Adapt templater to the new design and locale

* Fix tests

* Update lib.typ and testdata for the new locale and design

* Implement proper tests with all combinations of entries

* Remove some docstrings

* fix connections logic

* Improve

* Start working on examples

* Update testdata

* Fix long second row issue

* fix templating issues

* Fix lib.typ issues

* Update testdata

* Fix clean_trailing_parts

* Update test cv

* update test cv

* Update theme defaults

* update schema and fix moderncv

* Fix moderncv issues

* Update testdata

* Update testdata and examples

* Fix issues about photo

* Fix typst photo path issues

* improve entry templates from yaml

* add new locale

* Rename writing tests doc

* Update writing tests

* Improve tests

* Add more cli tests

* Increase test coverage

* Rename variant pydantic model generator

* Improve tests

* Update testdata and improve tests

* Format, fix pre-commit errors

* Fix scripts and update entry figures

* Improve tests

* Write docstrings of schema

* Write schema docstrings

* Setup api reference

* Start working on new docs

* Work on docs

* Improve progress panel of render command

* Finalize new docs index

* Complete CLI docs

* Work on YAML input structure page

* Finalize user guide

* Start working on developer guide

* Improve api reference

* Improve developer guide

* Improve developer guide

* Improve developer gide

* Improve developer guide

* Improve developer guide

* Update developer guide

* Improve developer guide

* Improve developer guide

* Improve developer guide

* Developer guide first draft

* update developer guide

* Update examples

* Update testdata

* Handle wrong installation (rendercv instead of rendercv[full])

* Remove unnecessary files

* Write set up vs code page

* Update README.md

* Change docs description

* Compress design options gif

* minor updates

* Polish all the json schema descriptions

* Update testdata and examples

* Remove some emdashed from docs

* Add whatsapp support

* Add TestEscapeTypstCharacters to tests

* Implement custom connections

* Add page break before sections feature

* Revert page break before sections feature

* Rebase to main

* Fix social network tests, update schema
2025-12-09 17:03:56 +03:00

124 lines
4.5 KiB
Python

from dataclasses import asdict
import pydantic
import pytest
from rendercv.exception import RenderCVInternalError
from rendercv.schema.models.rendercv_model import RenderCVModel
from rendercv.schema.models.validation_context import ValidationContext
from rendercv.schema.pydantic_error_handling import (
get_inner_yaml_object_from_its_key,
parse_validation_errors,
)
from rendercv.schema.yaml_reader import read_yaml
class TestParseValidationErrors:
def test_parses_all_validation_errors_from_wrong_input(self, testdata_dir):
wrong_input_file_path = testdata_dir / "wrong_input.yaml"
wrong_input_commented_map = read_yaml(wrong_input_file_path)
expected_errors_file_path = testdata_dir / "expected_errors.yaml"
expected_errors = read_yaml(expected_errors_file_path)["expected_errors"]
try:
RenderCVModel.model_validate(
wrong_input_commented_map,
context={
"context": ValidationContext(
input_file_path=wrong_input_file_path,
current_date=wrong_input_commented_map.get("settings", {}).get(
"current_date", None
),
)
},
)
except pydantic.ValidationError as e:
validation_errors = [
asdict(error)
for error in parse_validation_errors(e, wrong_input_commented_map)
]
for validation_error, expected_error in zip(
validation_errors, expected_errors, strict=True
):
expected_error["yaml_location"] = tuple(
tuple(part) for part in expected_error["yaml_location"]
)
expected_error["location"] = tuple(expected_error["location"])
assert validation_error == expected_error, (
f"expected {expected_error} but got {validation_error}"
)
def test_provides_helpful_message_for_invalid_date_format(self, tmp_path):
yaml_content = """
cv:
name: John Doe
sections:
Experience:
- name: Job
start_date: 2020-01-01
end_date: invalid_date
"""
yaml_file = tmp_path / "test.yaml"
yaml_file.write_text(yaml_content, encoding="utf-8")
yaml_object = read_yaml(yaml_file)
try:
RenderCVModel.model_validate(
yaml_object,
context={
"context": ValidationContext(
input_file_path=yaml_file,
)
},
)
except pydantic.ValidationError as e:
errors = parse_validation_errors(e, yaml_object)
end_date_error = next(
(err for err in errors if "end_date" in err.location), None
)
assert end_date_error is not None
assert "YYYY-MM-DD, YYYY-MM" in end_date_error.message
assert 'or "present"' in end_date_error.message
class TestGetInnerYamlObjectFromItsKey:
def test_returns_object_and_coordinates_for_valid_key(self):
yaml_content = "name: John\nage: 30"
yaml_object = read_yaml(yaml_content)
inner_object, coordinates = get_inner_yaml_object_from_its_key(
yaml_object, "name"
)
assert inner_object == "John"
assert len(coordinates) == 2
assert len(coordinates[0]) == 2
assert len(coordinates[1]) == 2
def test_returns_object_and_coordinates_for_valid_list_index(self):
yaml_content = "items:\n - first\n - second"
yaml_object = read_yaml(yaml_content)
items_list = yaml_object["items"]
inner_object, coordinates = get_inner_yaml_object_from_its_key(items_list, "0")
assert inner_object == "first"
assert len(coordinates) == 2
def test_raises_error_for_out_of_range_index(self):
yaml_content = "items:\n - first\n - second"
yaml_object = read_yaml(yaml_content)
items_list = yaml_object["items"]
with pytest.raises(RenderCVInternalError, match="Index 10 is out of range"):
get_inner_yaml_object_from_its_key(items_list, "10")
def test_raises_error_for_missing_key(self):
yaml_content = "name: John"
yaml_object = read_yaml(yaml_content)
with pytest.raises(RenderCVInternalError, match="Key 'nonexistent' not found"):
get_inner_yaml_object_from_its_key(yaml_object, "nonexistent")