add tests for PublicationEntry class

This commit is contained in:
Sina Atalay 2023-10-07 20:15:24 +02:00
parent 726e03fa05
commit ae2a21aa1a
2 changed files with 40 additions and 6 deletions

View File

@ -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

View File

@ -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__":