add data_model's Event class tests

This commit is contained in:
Sina Atalay 2023-10-07 18:54:16 +02:00
parent 91ab4a3d0a
commit b1f2777b2b
2 changed files with 207 additions and 34 deletions

View File

@ -439,7 +439,7 @@ class Event(BaseModel):
), ),
examples=["2020-09-24", "present"], examples=["2020-09-24", "present"],
) )
date: Optional[str] = Field( date: Optional[str | PastDate] = Field(
default=None, default=None,
title="Date", title="Date",
description=( description=(
@ -450,7 +450,7 @@ class Event(BaseModel):
), ),
examples=["2020-09-24"], examples=["2020-09-24"],
) )
highlights: list[SpellCheckedString] = Field( highlights: Optional[list[SpellCheckedString]] = Field(
default=[], default=[],
title="Highlights", title="Highlights",
description=( description=(
@ -515,6 +515,17 @@ class Event(BaseModel):
) )
model.end_date = "present" model.end_date = "present"
if model.start_date is not None and model.end_date is not None:
if model.end_date == "present":
end_date = Date.today()
else:
end_date = model.end_date
if model.start_date > end_date:
raise ValueError(
'"start_date" is after "end_date". Please check the dates!'
)
return model return model
@computed_field @computed_field
@ -578,7 +589,7 @@ class Event(BaseModel):
@cached_property @cached_property
def highlight_strings(self) -> list[SpellCheckedString]: def highlight_strings(self) -> list[SpellCheckedString]:
highlight_strings = [] highlight_strings = []
if self.highlights is not None:
highlight_strings.extend(self.highlights) highlight_strings.extend(self.highlights)
return highlight_strings return highlight_strings
@ -614,7 +625,7 @@ class Event(BaseModel):
try: try:
# If this runs, it means the date is an ISO format string, and it can be # If this runs, it means the date is an ISO format string, and it can be
# parsed # parsed
month_and_year = format_date(Date.fromisoformat(self.date)) month_and_year = format_date(self.date)
except: except:
month_and_year = self.date month_and_year = self.date
else: else:
@ -709,6 +720,7 @@ class EducationEntry(Event):
gpaString += f" ([Transcript]({self.transcript_url}))" gpaString += f" ([Transcript]({self.transcript_url}))"
highlight_strings.append(gpaString) highlight_strings.append(gpaString)
if self.highlights is not None:
highlight_strings.extend(self.highlights) highlight_strings.extend(self.highlights)
return highlight_strings return highlight_strings
@ -784,7 +796,9 @@ class Connection(BaseModel):
the $\LaTeX$ templating easier. the $\LaTeX$ templating easier.
""" """
name: Literal["LinkedIn", "GitHub", "Instagram", "phone", "email", "website", "location"] name: Literal[
"LinkedIn", "GitHub", "Instagram", "phone", "email", "website", "location"
]
value: str value: str
@computed_field @computed_field

View File

@ -15,6 +15,7 @@ class TestRendercv(unittest.TestCase):
] ]
for sentence in sentences: for sentence in sentences:
with self.subTest(sentence=sentence):
data_model.check_spelling(sentence) data_model.check_spelling(sentence)
def test_compute_time_span_string(self): def test_compute_time_span_string(self):
@ -22,22 +23,26 @@ class TestRendercv(unittest.TestCase):
end_date = Date(year=2021, month=1, day=1) end_date = Date(year=2021, month=1, day=1)
expected = "1 year 1 month" expected = "1 year 1 month"
result = data_model.compute_time_span_string(start_date, end_date) result = data_model.compute_time_span_string(start_date, end_date)
with self.subTest(expected=expected):
self.assertEqual(result, expected) self.assertEqual(result, expected)
start_date = Date(year=2020, month=1, day=1) start_date = Date(year=2020, month=1, day=1)
end_date = Date(year=2020, month=2, day=1) end_date = Date(year=2020, month=2, day=1)
expected = "1 month" expected = "1 month"
result = data_model.compute_time_span_string(start_date, end_date) result = data_model.compute_time_span_string(start_date, end_date)
with self.subTest(expected=expected):
self.assertEqual(result, expected) self.assertEqual(result, expected)
start_date = Date(year=2020, month=1, day=1) start_date = Date(year=2020, month=1, day=1)
end_date = Date(year=2023, month=3, day=2) end_date = Date(year=2023, month=3, day=2)
expected = "3 years 2 months" expected = "3 years 2 months"
result = data_model.compute_time_span_string(start_date, end_date) result = data_model.compute_time_span_string(start_date, end_date)
with self.subTest(expected=expected):
self.assertEqual(result, expected) self.assertEqual(result, expected)
start_date = Date(year=2020, month=1, day=1) start_date = Date(year=2020, month=1, day=1)
end_date = Date(year=1982, month=1, day=1) end_date = Date(year=1982, month=1, day=1)
with self.subTest(msg="start_date > end_date"):
with self.assertRaises(ValueError): with self.assertRaises(ValueError):
data_model.compute_time_span_string(start_date, end_date) data_model.compute_time_span_string(start_date, end_date)
@ -45,20 +50,23 @@ class TestRendercv(unittest.TestCase):
date = Date(year=2020, month=1, day=1) date = Date(year=2020, month=1, day=1)
expected = "Jan. 2020" expected = "Jan. 2020"
result = data_model.format_date(date) result = data_model.format_date(date)
with self.subTest(expected=expected):
self.assertEqual(result, expected) self.assertEqual(result, expected)
date = Date(year=1983, month=12, day=1) date = Date(year=1983, month=12, day=1)
expected = "Dec. 1983" expected = "Dec. 1983"
result = data_model.format_date(date) result = data_model.format_date(date)
with self.subTest(expected=expected):
self.assertEqual(result, expected) self.assertEqual(result, expected)
date = Date(year=2045, month=6, day=1) date = Date(year=2045, month=6, day=1)
expected = "June 2045" expected = "June 2045"
result = data_model.format_date(date) result = data_model.format_date(date)
with self.subTest(expected=expected):
self.assertEqual(result, expected) self.assertEqual(result, expected)
def test_data_Event_check_dates(self): def test_data_event_check_dates(self):
# Inputs with correct dates: # Inputs with valid dates:
inputs = [ inputs = [
{ {
"start_date": Date(year=2020, month=1, day=1), "start_date": Date(year=2020, month=1, day=1),
@ -76,25 +84,26 @@ class TestRendercv(unittest.TestCase):
] ]
for input in inputs: for input in inputs:
with self.subTest(msg="start_date < end_date"): with self.subTest(msg="valid dates"):
data_model.Event(**input) data_model.Event(**input)
# Inputs with incorrect dates: # Inputs with invalid dates:
inputs = [ input = {
{
"start_date": Date(year=2020, month=1, day=1), "start_date": Date(year=2020, month=1, day=1),
"end_date": Date(year=2019, month=1, day=1), "end_date": Date(year=2019, month=1, day=1),
}, }
{
"start_date": Date(year=2020, month=1, day=1),
"end_date": Date(year=2400, month=1, day=1),
},
]
for input in inputs:
with self.subTest(msg="start_date > end_date"): with self.subTest(msg="start_date > end_date"):
with self.assertRaises(ValidationError): with self.assertRaises(ValidationError):
data_model.Event(**input) data_model.Event(**input)
input = {
"start_date": Date(year=2020, month=1, day=1),
"end_date": Date(year=2900, month=1, day=1),
}
with self.subTest(msg="end_date > present"):
with self.assertRaises(ValidationError):
data_model.Event(**input)
# Other inputs: # Other inputs:
input = { input = {
"start_date": Date(year=2020, month=1, day=1), "start_date": Date(year=2020, month=1, day=1),
@ -140,6 +149,156 @@ class TestRendercv(unittest.TestCase):
self.assertEqual(event.end_date, None) self.assertEqual(event.end_date, None)
self.assertEqual(event.date, input["date"]) self.assertEqual(event.date, input["date"])
def test_data_event_date_and_location_strings_with_timespan(self):
input = {
"start_date": Date(year=2020, month=1, day=1),
"end_date": Date(year=2021, month=1, day=16),
"location": "My Location",
}
expected = [
"My Location",
"Jan. 2020 to Jan. 2021",
"1 year 1 month",
]
event = data_model.Event(**input)
result = event.date_and_location_strings_with_timespan
with self.subTest(expected=expected):
self.assertEqual(result, expected)
input = {
"date": "My Birthday",
"location": "My Location",
}
expected = [
"My Location",
"My Birthday",
]
event = data_model.Event(**input)
result = event.date_and_location_strings_with_timespan
with self.subTest(expected=expected):
self.assertEqual(result, expected)
def test_data_event_date_and_location_strings_without_timespan(self):
input = {
"start_date": Date(year=2020, month=1, day=1),
"end_date": Date(year=2021, month=1, day=16),
"location": "My Location",
}
expected = [
"My Location",
"Jan. 2020 to Jan. 2021",
]
event = data_model.Event(**input)
result = event.date_and_location_strings_without_timespan
with self.subTest(expected=expected):
self.assertEqual(result, expected)
input = {
"date": "My Birthday",
"location": "My Location",
}
expected = [
"My Location",
"My Birthday",
]
event = data_model.Event(**input)
result = event.date_and_location_strings_without_timespan
with self.subTest(expected=expected):
self.assertEqual(result, expected)
def test_data_event_highlight_strings(self):
input = {
"highlights": [
"My Highlight 1",
"My Highlight 2",
],
}
expected = [
"My Highlight 1",
"My Highlight 2",
]
event = data_model.Event(**input)
result = event.highlight_strings
with self.subTest(expected=expected):
self.assertEqual(result, expected)
input = {}
expected = []
event = data_model.Event(**input)
result = event.highlight_strings
with self.subTest(msg="no highlights"):
self.assertEqual(result, expected)
def test_data_event_markdown_url(self):
# Github link:
input = {"url": "https://github.com/sinaatalay"}
expected = "[view on GitHub](https://github.com/sinaatalay)"
event = data_model.Event(**input)
result = event.markdown_url
with self.subTest(msg="Github link"):
self.assertEqual(result, expected)
# LinkedIn link:
input = {"url": "https://www.linkedin.com/"}
expected = "[view on LinkedIn](https://www.linkedin.com/)"
event = data_model.Event(**input)
result = event.markdown_url
with self.subTest(msg="LinkedIn link"):
self.assertEqual(result, expected)
# Instagram link:
input = {"url": "https://www.instagram.com/"}
expected = "[view on Instagram](https://www.instagram.com/)"
event = data_model.Event(**input)
result = event.markdown_url
with self.subTest(msg="Instagram link"):
self.assertEqual(result, expected)
# Youtube link:
input = {"url": "https://www.youtube.com/"}
expected = "[view on YouTube](https://www.youtube.com/)"
event = data_model.Event(**input)
result = event.markdown_url
with self.subTest(msg="Youtube link"):
self.assertEqual(result, expected)
# Other links:
input = {"url": "https://www.google.com/"}
expected = "[view on my website](https://www.google.com/)"
event = data_model.Event(**input)
result = event.markdown_url
with self.subTest(msg="Other links"):
self.assertEqual(result, expected)
def test_data_event_month_and_year(self):
input = {
"start_date": Date(year=2020, month=1, day=1),
"end_date": Date(year=2021, month=1, day=16),
}
expected = None
event = data_model.Event(**input)
result = event.month_and_year
with self.subTest(msg="start_date and end_date are provided"):
self.assertEqual(result, expected)
input = {
"date": "My Birthday",
}
expected = "My Birthday"
event = data_model.Event(**input)
result = event.month_and_year
with self.subTest(msg="custom date is provided"):
self.assertEqual(result, expected)
input = {
"date": Date(year=2020, month=1, day=1),
}
expected = "Jan. 2020"
event = data_model.Event(**input)
result = event.month_and_year
with self.subTest(msg="date is provided"):
self.assertEqual(result, expected)
if __name__ == "__main__": if __name__ == "__main__":
unittest.main() unittest.main()