quizcomp.testing.base

 1import os
 2import re
 3import typing
 4
 5import edq.testing.unittest
 6import edq.util.json
 7
 8import quizcomp.model.question
 9import quizcomp.model.quiz
10import quizcomp.parser.math
11
12THIS_DIR: str = os.path.abspath(os.path.dirname(os.path.realpath(__file__)))
13TESTDATA_DIR: str = os.path.join(THIS_DIR, '..', 'testdata')
14
15QUESTIONS_DIR = os.path.join(TESTDATA_DIR, "questions")
16GOOD_QUESTIONS_DIR = os.path.join(QUESTIONS_DIR, "good")
17BAD_QUESTIONS_DIR = os.path.join(QUESTIONS_DIR, "bad")
18
19QUIZZES_DIR = os.path.join(TESTDATA_DIR, "quizzes")
20GOOD_QUIZZES_DIR = os.path.join(QUIZZES_DIR, "good")
21BAD_QUIZZES_DIR = os.path.join(QUIZZES_DIR, "bad")
22
23class BaseTest(edq.testing.unittest.BaseTest):
24    """
25    Test parsing text.
26    Good and bad situations will be loaded from files into individual test methods.
27    """
28
29    _questions_cache: typing.Dict[str, quizcomp.model.question.Question] = {}
30    """ A cache for loaded question filess, keyed by absolute path. """
31
32    _quizzes_cache: typing.Dict[str, quizcomp.model.quiz.Quiz] = {}
33    """ A cache for loaded quiz filess, keyed by absolute path. """
34
35    @classmethod
36    def setUpClass(cls) -> None:
37        super().setUpClass()
38
39        # Disable KaTeX for testing.
40        quizcomp.parser.math._katex_available = False
41
42    @classmethod
43    def tearDownClass(cls) -> None:
44        super().tearDownClass()
45
46        quizcomp.parser.math._katex_available = None
47
48    def load_question(self, path: str) -> quizcomp.model.question.Question:
49        """ Load a question from either the cache or disk. """
50
51        path = os.path.abspath(path)
52
53        if (path in self._questions_cache):
54            return self._questions_cache[path]
55
56        question = quizcomp.model.question.Question.from_path(path)
57        self._questions_cache[path] = question
58
59        return question
60
61    def load_quiz(self, path: str) -> quizcomp.model.quiz.Quiz:
62        """ Load a quiz from either the cache or disk. """
63
64        path = os.path.abspath(path)
65
66        if (path in self._quizzes_cache):
67            return self._quizzes_cache[path]
68
69        quiz = quizcomp.model.quiz.Quiz.from_path(path)
70        self._quizzes_cache[path] = quiz
71
72        return quiz
73
74def clean_name_part(text: str) -> str:
75    """ Clean a test name component. """
76
77    clean_text = text.lower().strip().replace(' ', '_')
78    clean_text = re.sub(r'\W+', '', clean_text)
79    return clean_text
THIS_DIR: str = '/home/runner/work/quiz-composer/quiz-composer/quizcomp/testing'
TESTDATA_DIR: str = '/home/runner/work/quiz-composer/quiz-composer/quizcomp/testing/../testdata'
QUESTIONS_DIR = '/home/runner/work/quiz-composer/quiz-composer/quizcomp/testing/../testdata/questions'
GOOD_QUESTIONS_DIR = '/home/runner/work/quiz-composer/quiz-composer/quizcomp/testing/../testdata/questions/good'
BAD_QUESTIONS_DIR = '/home/runner/work/quiz-composer/quiz-composer/quizcomp/testing/../testdata/questions/bad'
QUIZZES_DIR = '/home/runner/work/quiz-composer/quiz-composer/quizcomp/testing/../testdata/quizzes'
GOOD_QUIZZES_DIR = '/home/runner/work/quiz-composer/quiz-composer/quizcomp/testing/../testdata/quizzes/good'
BAD_QUIZZES_DIR = '/home/runner/work/quiz-composer/quiz-composer/quizcomp/testing/../testdata/quizzes/bad'
class BaseTest(edq.testing.unittest.BaseTest):
24class BaseTest(edq.testing.unittest.BaseTest):
25    """
26    Test parsing text.
27    Good and bad situations will be loaded from files into individual test methods.
28    """
29
30    _questions_cache: typing.Dict[str, quizcomp.model.question.Question] = {}
31    """ A cache for loaded question filess, keyed by absolute path. """
32
33    _quizzes_cache: typing.Dict[str, quizcomp.model.quiz.Quiz] = {}
34    """ A cache for loaded quiz filess, keyed by absolute path. """
35
36    @classmethod
37    def setUpClass(cls) -> None:
38        super().setUpClass()
39
40        # Disable KaTeX for testing.
41        quizcomp.parser.math._katex_available = False
42
43    @classmethod
44    def tearDownClass(cls) -> None:
45        super().tearDownClass()
46
47        quizcomp.parser.math._katex_available = None
48
49    def load_question(self, path: str) -> quizcomp.model.question.Question:
50        """ Load a question from either the cache or disk. """
51
52        path = os.path.abspath(path)
53
54        if (path in self._questions_cache):
55            return self._questions_cache[path]
56
57        question = quizcomp.model.question.Question.from_path(path)
58        self._questions_cache[path] = question
59
60        return question
61
62    def load_quiz(self, path: str) -> quizcomp.model.quiz.Quiz:
63        """ Load a quiz from either the cache or disk. """
64
65        path = os.path.abspath(path)
66
67        if (path in self._quizzes_cache):
68            return self._quizzes_cache[path]
69
70        quiz = quizcomp.model.quiz.Quiz.from_path(path)
71        self._quizzes_cache[path] = quiz
72
73        return quiz

Test parsing text. Good and bad situations will be loaded from files into individual test methods.

@classmethod
def setUpClass(cls) -> None:
36    @classmethod
37    def setUpClass(cls) -> None:
38        super().setUpClass()
39
40        # Disable KaTeX for testing.
41        quizcomp.parser.math._katex_available = False

Hook method for setting up class fixture before running tests in the class.

@classmethod
def tearDownClass(cls) -> None:
43    @classmethod
44    def tearDownClass(cls) -> None:
45        super().tearDownClass()
46
47        quizcomp.parser.math._katex_available = None

Hook method for deconstructing the class fixture after running all tests in the class.

def load_question(self, path: str) -> quizcomp.model.question.Question:
49    def load_question(self, path: str) -> quizcomp.model.question.Question:
50        """ Load a question from either the cache or disk. """
51
52        path = os.path.abspath(path)
53
54        if (path in self._questions_cache):
55            return self._questions_cache[path]
56
57        question = quizcomp.model.question.Question.from_path(path)
58        self._questions_cache[path] = question
59
60        return question

Load a question from either the cache or disk.

def load_quiz(self, path: str) -> quizcomp.model.quiz.Quiz:
62    def load_quiz(self, path: str) -> quizcomp.model.quiz.Quiz:
63        """ Load a quiz from either the cache or disk. """
64
65        path = os.path.abspath(path)
66
67        if (path in self._quizzes_cache):
68            return self._quizzes_cache[path]
69
70        quiz = quizcomp.model.quiz.Quiz.from_path(path)
71        self._quizzes_cache[path] = quiz
72
73        return quiz

Load a quiz from either the cache or disk.

def clean_name_part(text: str) -> str:
75def clean_name_part(text: str) -> str:
76    """ Clean a test name component. """
77
78    clean_text = text.lower().strip().replace(' ', '_')
79    clean_text = re.sub(r'\W+', '', clean_text)
80    return clean_text

Clean a test name component.