improve tests

This commit is contained in:
Sina Atalay 2023-10-13 22:32:23 +02:00
parent cd029ed88e
commit 18901990de
2 changed files with 91 additions and 34 deletions

View File

@ -46,6 +46,25 @@ class TestDataModel(unittest.TestCase):
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)
# invalid inputs:
start_date = None
end_date = Date(year=2023, month=3, day=2)
with self.subTest(msg="start_date is None"):
with self.assertRaises(TypeError):
data_model.compute_time_span_string(start_date, end_date) # type: ignore
start_date = Date(year=2020, month=1, day=1)
end_date = None
with self.subTest(msg="end_date is None"):
with self.assertRaises(TypeError):
data_model.compute_time_span_string(start_date, end_date) # type: ignore
start_date = 324
end_date = "test"
with self.subTest(msg="start_date and end_date are not dates"):
with self.assertRaises(TypeError):
data_model.compute_time_span_string(start_date, end_date) # type: ignore
def test_format_date(self): def test_format_date(self):
date = Date(year=2020, month=1, day=1) date = Date(year=2020, month=1, day=1)
expected = "Jan. 2020" expected = "Jan. 2020"
@ -71,7 +90,7 @@ class TestDataModel(unittest.TestCase):
"font": "SourceSans3", "font": "SourceSans3",
} }
with self.subTest(msg="valid font"): with self.subTest(msg="valid font"):
design = data_model.Design(**input) design = data_model.Design(**input) # type: ignore
self.assertEqual(design.font, input["font"]) self.assertEqual(design.font, input["font"])
# Invalid font: # Invalid font:
@ -80,7 +99,7 @@ class TestDataModel(unittest.TestCase):
} }
with self.subTest(msg="invalid font"): with self.subTest(msg="invalid font"):
with self.assertRaises(ValidationError): with self.assertRaises(ValidationError):
data_model.Design(**input) data_model.Design(**input) # type: ignore
def test_data_event_check_dates(self): def test_data_event_check_dates(self):
# Inputs with valid dates: # Inputs with valid dates:
@ -156,7 +175,7 @@ class TestDataModel(unittest.TestCase):
"end_date": "present", "end_date": "present",
"date": "My Birthday", "date": "My Birthday",
} }
event = data_model.Event(**input) event = data_model.Event(**input) # type: ignore
with self.subTest(msg="start_date, end_date, and date are all provided"): with self.subTest(msg="start_date, end_date, and date are all provided"):
self.assertEqual(event.date, None, msg="Date is not correct.") self.assertEqual(event.date, None, msg="Date is not correct.")
self.assertEqual( self.assertEqual(
@ -219,7 +238,7 @@ class TestDataModel(unittest.TestCase):
} }
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) # type: ignore
input = { input = {
"start_date": "2020-01-01", "start_date": "2020-01-01",
@ -227,7 +246,7 @@ class TestDataModel(unittest.TestCase):
} }
with self.subTest(msg="end_date > present"): with self.subTest(msg="end_date > present"):
with self.assertRaises(ValidationError): with self.assertRaises(ValidationError):
data_model.Event(**input) data_model.Event(**input) # type: ignore
def test_data_event_date_and_location_strings_with_timespan(self): def test_data_event_date_and_location_strings_with_timespan(self):
input = { input = {
@ -240,7 +259,7 @@ class TestDataModel(unittest.TestCase):
"Jan. 2020 to Jan. 2021", "Jan. 2020 to Jan. 2021",
"1 year 1 month", "1 year 1 month",
] ]
event = data_model.Event(**input) event = data_model.Event(**input) # type: ignore
result = event.date_and_location_strings_with_timespan result = event.date_and_location_strings_with_timespan
with self.subTest(msg="start_date, end_date, and location are provided"): with self.subTest(msg="start_date, end_date, and location are provided"):
self.assertEqual(result, expected) self.assertEqual(result, expected)
@ -253,7 +272,7 @@ class TestDataModel(unittest.TestCase):
"My Location", "My Location",
"My Birthday", "My Birthday",
] ]
event = data_model.Event(**input) event = data_model.Event(**input) # type: ignore
result = event.date_and_location_strings_with_timespan result = event.date_and_location_strings_with_timespan
with self.subTest(msg="date and location are provided"): with self.subTest(msg="date and location are provided"):
self.assertEqual(result, expected) self.assertEqual(result, expected)
@ -264,7 +283,7 @@ class TestDataModel(unittest.TestCase):
expected = [ expected = [
"Jan. 2020", "Jan. 2020",
] ]
event = data_model.Event(**input) event = data_model.Event(**input) # type: ignore
result = event.date_and_location_strings_with_timespan result = event.date_and_location_strings_with_timespan
with self.subTest(msg="date is provided"): with self.subTest(msg="date is provided"):
self.assertEqual(result, expected) self.assertEqual(result, expected)
@ -277,7 +296,7 @@ class TestDataModel(unittest.TestCase):
"Jan. 2020 to Jan. 2021", "Jan. 2020 to Jan. 2021",
"1 year 1 month", "1 year 1 month",
] ]
event = data_model.Event(**input) event = data_model.Event(**input) # type: ignore
result = event.date_and_location_strings_with_timespan result = event.date_and_location_strings_with_timespan
with self.subTest(msg="start_date and end_date are provided"): with self.subTest(msg="start_date and end_date are provided"):
self.assertEqual(result, expected) self.assertEqual(result, expected)
@ -288,7 +307,7 @@ class TestDataModel(unittest.TestCase):
expected = [ expected = [
"My Location", "My Location",
] ]
event = data_model.Event(**input) event = data_model.Event(**input) # type: ignore
result = event.date_and_location_strings_with_timespan result = event.date_and_location_strings_with_timespan
with self.subTest(msg="location is provided"): with self.subTest(msg="location is provided"):
self.assertEqual(result, expected) self.assertEqual(result, expected)
@ -303,7 +322,7 @@ class TestDataModel(unittest.TestCase):
"My Location", "My Location",
"Jan. 2020 to Jan. 2021", "Jan. 2020 to Jan. 2021",
] ]
event = data_model.Event(**input) event = data_model.Event(**input) # type: ignore
result = event.date_and_location_strings_without_timespan result = event.date_and_location_strings_without_timespan
with self.subTest(expected=expected): with self.subTest(expected=expected):
self.assertEqual(result, expected) self.assertEqual(result, expected)
@ -316,7 +335,7 @@ class TestDataModel(unittest.TestCase):
"My Location", "My Location",
"My Birthday", "My Birthday",
] ]
event = data_model.Event(**input) event = data_model.Event(**input) # type: ignore
result = event.date_and_location_strings_without_timespan result = event.date_and_location_strings_without_timespan
with self.subTest(expected=expected): with self.subTest(expected=expected):
self.assertEqual(result, expected) self.assertEqual(result, expected)
@ -332,7 +351,7 @@ class TestDataModel(unittest.TestCase):
"My Highlight 1", "My Highlight 1",
"My Highlight 2", "My Highlight 2",
] ]
event = data_model.Event(**input) event = data_model.Event(**input) # type: ignore
result = event.highlight_strings result = event.highlight_strings
with self.subTest(msg="highlights are provided"): with self.subTest(msg="highlights are provided"):
self.assertEqual(result, expected) self.assertEqual(result, expected)
@ -348,7 +367,7 @@ class TestDataModel(unittest.TestCase):
# Github link: # Github link:
input = {"url": "https://github.com/sinaatalay"} input = {"url": "https://github.com/sinaatalay"}
expected = "[view on GitHub](https://github.com/sinaatalay)" expected = "[view on GitHub](https://github.com/sinaatalay)"
event = data_model.Event(**input) event = data_model.Event(**input) # type: ignore
result = event.markdown_url result = event.markdown_url
with self.subTest(msg="Github link"): with self.subTest(msg="Github link"):
self.assertEqual(result, expected) self.assertEqual(result, expected)
@ -356,7 +375,7 @@ class TestDataModel(unittest.TestCase):
# LinkedIn link: # LinkedIn link:
input = {"url": "https://www.linkedin.com/"} input = {"url": "https://www.linkedin.com/"}
expected = "[view on LinkedIn](https://www.linkedin.com/)" expected = "[view on LinkedIn](https://www.linkedin.com/)"
event = data_model.Event(**input) event = data_model.Event(**input) # type: ignore
result = event.markdown_url result = event.markdown_url
with self.subTest(msg="LinkedIn link"): with self.subTest(msg="LinkedIn link"):
self.assertEqual(result, expected) self.assertEqual(result, expected)
@ -364,7 +383,7 @@ class TestDataModel(unittest.TestCase):
# Instagram link: # Instagram link:
input = {"url": "https://www.instagram.com/"} input = {"url": "https://www.instagram.com/"}
expected = "[view on Instagram](https://www.instagram.com/)" expected = "[view on Instagram](https://www.instagram.com/)"
event = data_model.Event(**input) event = data_model.Event(**input) # type: ignore
result = event.markdown_url result = event.markdown_url
with self.subTest(msg="Instagram link"): with self.subTest(msg="Instagram link"):
self.assertEqual(result, expected) self.assertEqual(result, expected)
@ -372,7 +391,7 @@ class TestDataModel(unittest.TestCase):
# Youtube link: # Youtube link:
input = {"url": "https://www.youtube.com/"} input = {"url": "https://www.youtube.com/"}
expected = "[view on YouTube](https://www.youtube.com/)" expected = "[view on YouTube](https://www.youtube.com/)"
event = data_model.Event(**input) event = data_model.Event(**input) # type: ignore
result = event.markdown_url result = event.markdown_url
with self.subTest(msg="Youtube link"): with self.subTest(msg="Youtube link"):
self.assertEqual(result, expected) self.assertEqual(result, expected)
@ -380,7 +399,7 @@ class TestDataModel(unittest.TestCase):
# Other links: # Other links:
input = {"url": "https://www.google.com/"} input = {"url": "https://www.google.com/"}
expected = "[view on my website](https://www.google.com/)" expected = "[view on my website](https://www.google.com/)"
event = data_model.Event(**input) event = data_model.Event(**input) # type: ignore
result = event.markdown_url result = event.markdown_url
with self.subTest(msg="Other links"): with self.subTest(msg="Other links"):
self.assertEqual(result, expected) self.assertEqual(result, expected)
@ -391,7 +410,7 @@ class TestDataModel(unittest.TestCase):
"end_date": "2021-01-16", "end_date": "2021-01-16",
} }
expected = None expected = None
event = data_model.Event(**input) event = data_model.Event(**input) # type: ignore
result = event.month_and_year result = event.month_and_year
with self.subTest(msg="start_date and end_date are provided"): with self.subTest(msg="start_date and end_date are provided"):
self.assertEqual(result, expected) self.assertEqual(result, expected)
@ -400,7 +419,7 @@ class TestDataModel(unittest.TestCase):
"date": "My Birthday", "date": "My Birthday",
} }
expected = "My Birthday" expected = "My Birthday"
event = data_model.Event(**input) event = data_model.Event(**input) # type: ignore
result = event.month_and_year result = event.month_and_year
with self.subTest(msg="custom date is provided"): with self.subTest(msg="custom date is provided"):
self.assertEqual(result, expected) self.assertEqual(result, expected)
@ -409,7 +428,7 @@ class TestDataModel(unittest.TestCase):
"date": "2020-01-01", "date": "2020-01-01",
} }
expected = "Jan. 2020" expected = "Jan. 2020"
event = data_model.Event(**input) event = data_model.Event(**input) # type: ignore
result = event.month_and_year result = event.month_and_year
with self.subTest(msg="date is provided"): with self.subTest(msg="date is provided"):
self.assertEqual(result, expected) self.assertEqual(result, expected)
@ -581,7 +600,7 @@ class TestDataModel(unittest.TestCase):
] ]
for input, expected in zip(inputs, expected_results): for input, expected in zip(inputs, expected_results):
with self.subTest(type=input["name"]): with self.subTest(type=input["name"]):
connection = data_model.Connection(**input) connection = data_model.Connection(**input) # type: ignore
result = connection.url result = connection.url
self.assertEqual(result, expected) self.assertEqual(result, expected)
@ -594,7 +613,7 @@ class TestDataModel(unittest.TestCase):
"website": "https://www.example.com/", "website": "https://www.example.com/",
} }
exptected_length = 4 exptected_length = 4
cv = data_model.CurriculumVitae(**input) cv = data_model.CurriculumVitae(**input) # type: ignore
result = len(cv.connections) result = len(cv.connections)
with self.subTest(msg="without social networks"): with self.subTest(msg="without social networks"):
self.assertEqual(result, exptected_length) self.assertEqual(result, exptected_length)
@ -703,12 +722,16 @@ class TestDataModel(unittest.TestCase):
cv = data_model.CurriculumVitae(**input) cv = data_model.CurriculumVitae(**input)
self.assertEqual(len(cv.sections), 5) self.assertEqual(len(cv.sections), 5)
# Invalid section_order:
input["section_order"] = ["invalid section"]
with self.subTest(msg="invalid section_order"):
data = data_model.CurriculumVitae(**input)
with self.assertRaises(ValueError):
data.sections
del input["section_order"]
# Custom sections with duplicate titles: # Custom sections with duplicate titles:
input["custom_sections"][1]["title"] = "My Custom Section 1" input["custom_sections"][1]["title"] = "My Custom Section 1"
with self.subTest(msg="custom sections with duplicate titles"): with self.subTest(msg="custom sections with duplicate titles"):
with self.assertRaises(ValidationError): with self.assertRaises(ValidationError):
data_model.CurriculumVitae(**input) data_model.CurriculumVitae(**input)
if __name__ == "__main__":
unittest.main()

View File

@ -1,5 +1,6 @@
import unittest import unittest
import os import os
import json
from datetime import date from datetime import date
import shutil import shutil
@ -57,7 +58,7 @@ class TestDataModel(unittest.TestCase):
input = 20 input = 20
with self.subTest(msg="float input"): with self.subTest(msg="float input"):
with self.assertRaises(ValueError): with self.assertRaises(ValueError):
rendering.markdown_to_latex(input) rendering.markdown_to_latex(input) # type: ignore
def test_markdown_link_to_url(self): def test_markdown_link_to_url(self):
input = "[link](www.example.com)" input = "[link](www.example.com)"
@ -82,7 +83,7 @@ class TestDataModel(unittest.TestCase):
input = 20 input = 20
with self.subTest(msg="float input"): with self.subTest(msg="float input"):
with self.assertRaises(ValueError): with self.assertRaises(ValueError):
rendering.markdown_link_to_url(input) rendering.markdown_link_to_url(input) # type: ignore
input = "not a markdown link" input = "not a markdown link"
with self.subTest(msg="invalid input"): with self.subTest(msg="invalid input"):
@ -101,17 +102,21 @@ class TestDataModel(unittest.TestCase):
with self.subTest(msg="without match_str input"): with self.subTest(msg="without match_str input"):
self.assertEqual(output, expected) self.assertEqual(output, expected)
input = "some text"
match_str = "text" match_str = "text"
expected = r"some \textbf{text}" expected = r"some \textbf{text}"
output = rendering.make_it_bold(input, match_str) output = rendering.make_it_bold(input, match_str)
with self.subTest(msg="with match_str input"): with self.subTest(msg="with match_str input"):
self.assertEqual(output, expected) self.assertEqual(output, expected)
match_str = 2423
with self.subTest(msg="invalid match_str input"):
with self.assertRaises(ValueError):
rendering.make_it_bold(input, match_str) # type: ignore
input = 20 input = 20
with self.subTest(msg="float input"): with self.subTest(msg="float input"):
with self.assertRaises(ValueError): with self.assertRaises(ValueError):
rendering.make_it_bold(input) rendering.make_it_bold(input) # type: ignore
def test_make_it_underlined(self): def test_make_it_underlined(self):
input = "some text" input = "some text"
@ -130,7 +135,7 @@ class TestDataModel(unittest.TestCase):
input = 20 input = 20
with self.subTest(msg="float input"): with self.subTest(msg="float input"):
with self.assertRaises(ValueError): with self.assertRaises(ValueError):
rendering.make_it_underlined(input) rendering.make_it_underlined(input) # type: ignore
def test_make_it_italic(self): def test_make_it_italic(self):
input = "some text" input = "some text"
@ -149,7 +154,7 @@ class TestDataModel(unittest.TestCase):
input = 20 input = 20
with self.subTest(msg="float input"): with self.subTest(msg="float input"):
with self.assertRaises(ValueError): with self.assertRaises(ValueError):
rendering.make_it_italic(input) rendering.make_it_italic(input) # type: ignore
def test_divide_length_by(self): def test_divide_length_by(self):
lengths = [ lengths = [
@ -190,6 +195,35 @@ class TestDataModel(unittest.TestCase):
result = rendering.get_path_to_font_directory(font_name) result = rendering.get_path_to_font_directory(font_name)
self.assertEqual(expected, result, msg="Font directory path is not correct.") self.assertEqual(expected, result, msg="Font directory path is not correct.")
def test_read_input_file(self):
test_input = {
"cv": {
"name": "John Doe",
}
}
# write dictionary to a file as json:
input_file_path = os.path.join(os.path.dirname(__file__), "test_input.json")
json_string = json.dumps(test_input)
with open(input_file_path, "w") as file:
file.write(json_string)
# read the file:
result = rendering.read_input_file(input_file_path)
# remove the file:
os.remove(input_file_path)
with self.subTest(msg="read input file"):
self.assertEqual(
result.cv.name,
test_input["cv"]["name"],
)
with self.subTest(msg="nonexistent file"):
with self.assertRaises(FileNotFoundError):
rendering.read_input_file("nonexistent.json")
def test_render_template(self): def test_render_template(self):
test_input = { test_input = {
"cv": { "cv": {
@ -360,7 +394,7 @@ class TestDataModel(unittest.TestCase):
"theme": "classic", "theme": "classic",
}, },
} }
data = data_model.RenderCVDataModel(**test_input) data = data_model.RenderCVDataModel(**test_input) # type: ignore
rendering.render_template(data=data, output_path=os.path.dirname(__file__)) rendering.render_template(data=data, output_path=os.path.dirname(__file__))
# Check if the output file exists: # Check if the output file exists: