|
| 1 | +#!/usr/bin/env python3 |
| 2 | + |
| 3 | +# Contest Management System - http://cms-dev.github.io/ |
| 4 | +# Copyright © 2026 Luca Versari <veluca93@gmail.com> |
| 5 | +# |
| 6 | +# This program is free software: you can redistribute it and/or modify |
| 7 | +# it under the terms of the GNU Affero General Public License as |
| 8 | +# published by the Free Software Foundation, either version 3 of the |
| 9 | +# License, or (at your option) any later version. |
| 10 | +# |
| 11 | +# This program is distributed in the hope that it will be useful, |
| 12 | +# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | +# GNU Affero General Public License for more details. |
| 15 | +# |
| 16 | +# You should have received a copy of the GNU Affero General Public License |
| 17 | +# along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 18 | + |
| 19 | +"""Tests for the Interactive task type.""" |
| 20 | + |
| 21 | +import unittest |
| 22 | +from unittest.mock import MagicMock |
| 23 | + |
| 24 | +from cms.db import File, Manager |
| 25 | +from cms.grading.Job import CompilationJob |
| 26 | +from cms.grading.tasktypes.Interactive import Interactive |
| 27 | +from cmstestsuite.unit_tests.grading.tasktypes.tasktypetestutils import ( |
| 28 | + COMPILATION_COMMAND_1, |
| 29 | + COMPILATION_COMMAND_2, |
| 30 | + LANG_1, |
| 31 | + LANG_2, |
| 32 | + STATS_OK, |
| 33 | + TEXT, |
| 34 | + TaskTypeTestMixin, |
| 35 | + fake_compilation_commands, |
| 36 | +) |
| 37 | + |
| 38 | + |
| 39 | +FILE_FOO_L1 = File(digest="digest of foo.l1", filename="foo.%l") |
| 40 | +GRADER_L1 = Manager(digest="digest of grader.l1", filename="grader.l1") |
| 41 | + |
| 42 | + |
| 43 | +class TestInteractiveTaskType(TaskTypeTestMixin, unittest.TestCase): |
| 44 | + |
| 45 | + def setUp(self): |
| 46 | + super().setUp() |
| 47 | + self.setUpMocks("Interactive") |
| 48 | + self.languages.update({LANG_1, LANG_2}) |
| 49 | + self.file_cacher = MagicMock() |
| 50 | + |
| 51 | + def test_get_compilation_commands_with_grader(self): |
| 52 | + tt = Interactive([200, "grader", True, 128.0, 1.0, 5.0]) |
| 53 | + cc = tt.get_compilation_commands(["foo.%l"]) |
| 54 | + self.assertEqual( |
| 55 | + cc, |
| 56 | + { |
| 57 | + "L1": fake_compilation_commands( |
| 58 | + COMPILATION_COMMAND_1, ["grader.l1", "foo.l1"], "foo" |
| 59 | + ), |
| 60 | + "L2": fake_compilation_commands( |
| 61 | + COMPILATION_COMMAND_2, ["grader.l2", "foo.l2"], "foo.ext" |
| 62 | + ), |
| 63 | + }, |
| 64 | + ) |
| 65 | + |
| 66 | + def test_get_compilation_commands_alone(self): |
| 67 | + tt = Interactive([200, "alone", True, 128.0, 1.0, 5.0]) |
| 68 | + cc = tt.get_compilation_commands(["foo.%l"]) |
| 69 | + self.assertEqual( |
| 70 | + cc, |
| 71 | + { |
| 72 | + "L1": fake_compilation_commands( |
| 73 | + COMPILATION_COMMAND_1, ["foo.l1"], "foo" |
| 74 | + ), |
| 75 | + "L2": fake_compilation_commands( |
| 76 | + COMPILATION_COMMAND_2, ["foo.l2"], "foo.ext" |
| 77 | + ), |
| 78 | + }, |
| 79 | + ) |
| 80 | + |
| 81 | + def test_get_user_managers_with_grader(self): |
| 82 | + tt = Interactive([200, "grader", True, 128.0, 1.0, 5.0]) |
| 83 | + self.assertEqual(tt.get_user_managers(), ["grader.%l"]) |
| 84 | + |
| 85 | + def test_get_user_managers_alone(self): |
| 86 | + tt = Interactive([200, "alone", True, 128.0, 1.0, 5.0]) |
| 87 | + self.assertEqual(tt.get_user_managers(), []) |
| 88 | + |
| 89 | + def test_get_auto_managers(self): |
| 90 | + tt = Interactive([200, "grader", True, 128.0, 1.0, 5.0]) |
| 91 | + self.assertEqual(tt.get_auto_managers(), []) |
| 92 | + |
| 93 | + def test_compile_with_grader(self): |
| 94 | + tt = Interactive([200, "grader", True, 128.0, 1.0, 5.0]) |
| 95 | + job = CompilationJob( |
| 96 | + language="L1", |
| 97 | + files={"foo.%l": FILE_FOO_L1}, |
| 98 | + managers={"grader.l1": GRADER_L1}, |
| 99 | + ) |
| 100 | + sandbox = self.expect_sandbox() |
| 101 | + sandbox.get_file_to_storage.return_value = "exe_digest" |
| 102 | + self.compilation_step.return_value = (True, True, TEXT, STATS_OK) |
| 103 | + |
| 104 | + tt.compile(job, self.file_cacher) |
| 105 | + |
| 106 | + self.assertTrue(job.success) |
| 107 | + self.assertTrue(job.compilation_success) |
| 108 | + sandbox.create_file_from_storage.assert_any_call( |
| 109 | + "grader.l1", "digest of grader.l1", self.file_cacher |
| 110 | + ) |
| 111 | + sandbox.create_file_from_storage.assert_any_call( |
| 112 | + "foo.l1", "digest of foo.l1", self.file_cacher |
| 113 | + ) |
| 114 | + |
| 115 | + def test_compile_missing_grader(self): |
| 116 | + tt = Interactive([200, "grader", True, 128.0, 1.0, 5.0]) |
| 117 | + job = CompilationJob( |
| 118 | + language="L1", |
| 119 | + files={"foo.%l": FILE_FOO_L1}, |
| 120 | + managers={}, |
| 121 | + ) |
| 122 | + tt.compile(job, self.file_cacher) |
| 123 | + self.assertFalse(job.success) |
| 124 | + |
| 125 | + |
| 126 | +if __name__ == "__main__": |
| 127 | + unittest.main() |
0 commit comments