test data_model's Connection class

This commit is contained in:
Sina Atalay 2023-10-07 20:51:33 +02:00
parent f6ba41da4a
commit bb07677312
3 changed files with 45 additions and 7 deletions

View File

@ -833,21 +833,33 @@ class Connection(BaseModel):
]
value: str
@field_validator("value")
@classmethod
def check_type_of_value(
cls, value: str
) -> str:
if not re.search(r"[^\d\-+]", str(value)):
# If there is nothing other than digits, hyphens, and plus signs, then it is
# a phone number
value = "tel:" + value
return value
@computed_field
@cached_property
def url(self) -> HttpUrl:
if self.name == "LinkedIn":
url = f"https://www.linkedin.com/in/{self.value}"
elif self.name == "GitHub":
url = f"https:www.github.com/{self.value}"
url = f"https://www.github.com/{self.value}"
elif self.name == "Instagram":
url = f"https://www.instagram.com/{self.value}"
elif self.name == "email":
url = f"mailto:{self.value}"
elif self.name == "website":
url = f"{self.value}"
url = self.value
elif self.name == "phone":
url = f"{self.value}"
url = self.value
elif self.name == "location":
url = None
else:

View File

@ -8,11 +8,11 @@
((*- endmacro *))
((* macro Instagram(username, url) -*))
\mbox{\href{}{{\small\faInstagram}\hspace{0.13cm}<<username>>}}
\mbox{\href{<<url>>}{{\small\faInstagram}\hspace{0.13cm}<<username>>}}
((*- endmacro *))
((* macro phone(number, url) -*))
\mbox{{\footnotesize\faPhone*}\hspace{0.13cm}<<number|replace("tel:", "")|replace("-"," ")>>}
\mbox{\href{<<url|replace("-","")>>}{{\footnotesize\faPhone*}\hspace{0.13cm}<<number|replace("tel:", "")|replace("-"," ")>>}}
((*- endmacro *))
((* macro email(email, url) -*))

View File

@ -482,6 +482,32 @@ class TestRendercv(unittest.TestCase):
result = publication.doi_url
self.assertEqual(result, expected)
def test_data_connection_url(self):
# Github link:
inputs = [
{"name": "LinkedIn", "value": "username"},
{"name": "GitHub", "value": "sinaatalay"},
{"name": "Instagram", "value": "username"},
{"name": "phone", "value": "+909999999999"},
{"name": "email", "value": "example@example.com"},
{"name": "website", "value": "https://www.example.com/"},
{"name": "location", "value": "My Location"},
]
expected_results = [
"https://www.linkedin.com/in/username",
"https://www.github.com/sinaatalay",
"https://www.instagram.com/username",
"tel:+909999999999",
"mailto:example@example.com",
"https://www.example.com/",
None,
]
for input, expected in zip(inputs, expected_results):
with self.subTest(type=input["name"]):
connection = data_model.Connection(**input)
result = connection.url
self.assertEqual(result, expected)
if __name__ == "__main__":
unittest.main()