Add tests for courses, change study guide description

pull/105/head
Stijn De Clercq 2022-02-11 22:26:23 +01:00
parent c6958d22f3
commit 9819e82638
3 changed files with 37 additions and 2 deletions

View File

@ -70,7 +70,7 @@ class SchoolSlash(commands.Cog):
@slash_command(name="fiche", description="Zoek de studiefiche voor een vak.")
async def _study_guide_slash(self, ctx: ApplicationContext,
vak: Option(str, description="Naam van het vak. Afkortingen werken ook, maar worden niet ge-autocompletet.",
vak: Option(str, description="Naam van het vak. Afkortingen werken ook, maar worden niet geautocompletet.",
required=True, autocomplete=course_autocomplete)):
# Find code corresponding to the search query
course = find_course_from_name(vak, courses)

View File

@ -3,6 +3,7 @@ from typing import Optional
import dacite
import json
from os import path
@dataclass
@ -16,7 +17,9 @@ class Course:
def load_courses() -> dict[str, Course]:
"""Create a list of all courses"""
with open("files/courses.json", "r") as file:
# Allows testing
filepath = path.join(path.dirname(__file__), "..", "files", "courses.json")
with open(filepath, "r") as file:
data = json.load(file)
courses = {}

View File

@ -0,0 +1,32 @@
import unittest
from data.courses import load_courses, find_course_from_name
class TestCourses(unittest.TestCase):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.courses = load_courses()
def test_find_course(self):
self.assertIsNone(find_course_from_name("garbage", self.courses))
self.assertIsNone(find_course_from_name("garbage"))
# Find by name
webdev = find_course_from_name("Webdevelopment", self.courses)
self.assertIsNotNone(webdev)
self.assertEqual(webdev.code, "C003779")
# Find by abbreviation
infosec = find_course_from_name("infosec", self.courses)
self.assertIsNotNone(infosec)
self.assertEqual(infosec.code, "E019400")
# Case sensitive
not_found = find_course_from_name("ad3", self.courses, case_insensitive=False)
self.assertIsNone(not_found)
# Find by alt name
pcs = find_course_from_name("parallel computer systems", self.courses)
self.assertIsNotNone(pcs)
self.assertEqual(pcs.code, "E034140")