From ae2a21aa1af868d987688830809529c8ecafb639 Mon Sep 17 00:00:00 2001 From: Sina Atalay Date: Sat, 7 Oct 2023 20:15:24 +0200 Subject: [PATCH] add tests for PublicationEntry class --- rendercv/data_model.py | 10 ++++++---- tests/test_rendercv.py | 36 ++++++++++++++++++++++++++++++++++-- 2 files changed, 40 insertions(+), 6 deletions(-) diff --git a/rendercv/data_model.py b/rendercv/data_model.py index 426cab7..b18f3c3 100644 --- a/rendercv/data_model.py +++ b/rendercv/data_model.py @@ -775,6 +775,7 @@ class PublicationEntry(Event): examples=[10, 100], ) journal: Optional[str] = Field( + default=None, title="Journal", description="The journal or the conference name.", examples=[ @@ -787,11 +788,12 @@ class PublicationEntry(Event): @classmethod def check_doi(cls, doi: str) -> str: doi_url = f"https://doi.org/{doi}" - - html = urllib.request.urlopen(doi_url).read().decode("utf-8") - if "DOI NOT FOUND" in html: + + try: + urllib.request.urlopen(doi_url) + except: raise ValueError(f"{doi} cannot be found in the DOI System.") - + return doi @computed_field diff --git a/tests/test_rendercv.py b/tests/test_rendercv.py index 13c37ba..a09b19c 100644 --- a/tests/test_rendercv.py +++ b/tests/test_rendercv.py @@ -338,7 +338,7 @@ class TestRendercv(unittest.TestCase): with self.subTest(msg="date is provided"): self.assertEqual(result, expected) - def test_data_education_highlight_strings(self): + def test_data_education_entry_highlight_strings(self): input = { "institution": "My Institution", "area": "My Area", @@ -439,7 +439,8 @@ class TestRendercv(unittest.TestCase): with self.subTest(msg="gpa, transcript_url, and highlights are provided"): self.assertEqual(result, expected) - def test_data_publication_netry_doi_url(self): + def test_data_publication_entry_check_doi(self): + # Invalid DOI: input = { "title": "My Publication", "authors": [ @@ -449,6 +450,37 @@ class TestRendercv(unittest.TestCase): "doi": "invalidDoi", "date": "2020-01-01", } + with self.subTest(msg="invalid doi"): + with self.assertRaises(ValidationError): + data_model.PublicationEntry(**input) + + # Valid DOI: + input = { + "title": "My Publication", + "authors": [ + "Author 1", + "Author 2", + ], + "doi": "10.1103/PhysRevB.76.054309", + "date": "2007-08-01", + } + with self.subTest(msg="valid doi"): + data_model.PublicationEntry(**input) + + def test_data_publication_entry_doi_url(self): + input = { + "title": "My Publication", + "authors": [ + "Author 1", + "Author 2", + ], + "doi": "10.1103/PhysRevB.76.054309", + "date": "2007-08-01", + } + expected = "https://doi.org/10.1103/PhysRevB.76.054309" + publication = data_model.PublicationEntry(**input) + result = publication.doi_url + self.assertEqual(result, expected) if __name__ == "__main__":