Skip to content

Commit 1bc1406

Browse files
authored
Make register auto tests support multi exercises with same name (#352)
1 parent 9499e89 commit 1bc1406

File tree

2 files changed

+33
-17
lines changed

2 files changed

+33
-17
lines changed

lms/lmstests/public/unittests/import_tests.py

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -13,28 +13,31 @@
1313

1414
def register_test_class(file_path: str, test_class: typing.ClassVar):
1515
subject = test_class.__doc__
16-
exercise = models.Exercise.get_or_none(models.Exercise.subject == subject)
17-
if not exercise:
18-
log.info(f'Failed to find exercise subject {subject}')
16+
exercises = tuple(
17+
models.Exercise.filter(models.Exercise.subject == subject),
18+
)
19+
if not exercises:
20+
log.info(f'Failed to find exercises for subject {subject}')
1921
raise SystemError
2022

2123
with open(file_path, 'r') as file_reader:
2224
code = file_reader.read()
2325

24-
exercise_test = models.ExerciseTest.get_or_create_exercise_test(
25-
exercise=exercise,
26-
code=code,
27-
)
26+
for exercise in exercises:
27+
exercise_test = models.ExerciseTest.get_or_create_exercise_test(
28+
exercise=exercise,
29+
code=code,
30+
)
2831

29-
for test_func_name in inspect.getmembers(test_class):
30-
test_func_name = test_func_name[0]
31-
if test_func_name.startswith('test_'):
32-
test_func = getattr(test_class, test_func_name)
33-
models.ExerciseTestName.create_exercise_test_name(
34-
exercise_test=exercise_test,
35-
test_name=test_func_name,
36-
pretty_test_name=test_func.__doc__,
37-
)
32+
for test_func_name in inspect.getmembers(test_class):
33+
test_func_name = test_func_name[0]
34+
if test_func_name.startswith('test_'):
35+
test_func = getattr(test_class, test_func_name)
36+
models.ExerciseTestName.create_exercise_test_name(
37+
exercise_test=exercise_test,
38+
test_name=test_func_name,
39+
pretty_test_name=test_func.__doc__,
40+
)
3841

3942

4043
def load_tests_from_path(file_path: str):

tests/test_exercise_unit_tests.py

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
from lms.models import notifications
88
from tests import conftest
99

10-
1110
STUDENT_CODE = """
1211
def foo(bar=None):
1312
return 'bar' if bar == 'bar' else 'foo'
@@ -70,6 +69,20 @@ def _verify_comments():
7069
assert expected == first.user_message
7170
assert "foo('bar') == 'barbaron'" in first.staff_message
7271

72+
def test_register_two_exercises_with_same_name(
73+
self,
74+
course: models.Course,
75+
):
76+
ex1 = conftest.create_exercise(course, 0)
77+
ex2 = conftest.create_exercise(course, 0)
78+
import_tests.load_test_from_module(EXERCISE_TESTS)
79+
assert models.ExerciseTest.select().filter(
80+
models.ExerciseTest.exercise == ex1,
81+
).get()
82+
assert models.ExerciseTest.select().filter(
83+
models.ExerciseTest.exercise == ex2,
84+
).get()
85+
7386
@staticmethod
7487
def _verify_notifications(solution):
7588
all_notifications = notifications.get(user=solution.solver)

0 commit comments

Comments
 (0)