commit 20df61969317fc83ba4ebcc35930a1a0a39acfcd Author: sesufv Date: Sat Apr 4 00:29:56 2026 +0200 hotovson diff --git a/__pycache__/tasks.cpython-313.pyc b/__pycache__/tasks.cpython-313.pyc new file mode 100644 index 0000000..2ad882a Binary files /dev/null and b/__pycache__/tasks.cpython-313.pyc differ diff --git a/__pycache__/tests.cpython-313-pytest-9.0.2.pyc b/__pycache__/tests.cpython-313-pytest-9.0.2.pyc new file mode 100644 index 0000000..f5198ca Binary files /dev/null and b/__pycache__/tests.cpython-313-pytest-9.0.2.pyc differ diff --git a/skj_class.xml b/skj_class.xml new file mode 100644 index 0000000..7f1340b --- /dev/null +++ b/skj_class.xml @@ -0,0 +1 @@ +444045504444321112323444412505542140252202231415 \ No newline at end of file diff --git a/tasks.py b/tasks.py new file mode 100644 index 0000000..f5126a5 --- /dev/null +++ b/tasks.py @@ -0,0 +1,67 @@ +import xml.etree.ElementTree as ET + +def create_student(xml_root, student_id): + ''' + Vytvořte studenta dle loginu. + Ujistěte se, že student neexistuje, jinak: raise Exception('student already exists') + ''' + for student in xml_root.findall('student'): + if student.get('student_id')==student_id: + raise Exception('student already exists') + new_student = ET.SubElement(xml_root, "student") + new_student.set("student_id", student_id) + return new_student + +def remove_student(xml_root, student_id): + ''' + Odstraňte studenta dle loginu + ''' + for student in xml_root.findall('student'): + if student.get('student_id')==student_id: + xml_root.remove(student) + return + raise Exception('student does not exist') + + +def set_task_points(xml_root, student_id, task_id, points): + ''' + Přepište body danému studentovi u jednoho tasku + ''' + + for student in xml_root.findall('student'): + if student.get('student_id')==student_id: + + for task in student.findall('task'): + if task.get('task_id')==task_id: + task.text=str(points) + return + + + return + raise Exception('student does not exist') + + +def create_task(xml_root, student_id, task_id, points): + ''' + Pro daného studenta vytvořte task s body. + Ujistěte se, že task (s task_id) u studenta neexistuje, jinak: raise Exception('task already exists') + ''' + for student in xml_root.findall('student'): + if student.get('student_id')==student_id: + for task in student.findall('task'): + if task.get('task_id')==task_id: + raise Exception('task already exists') + new_task = ET.SubElement(student, 'task') + new_task.set('task_id', task_id) + new_task.text = str(points) + return + + +def remove_task(xml_root, task_id): + ''' + Napříč všemi studenty smažte task s daným task_id + ''' + for student in xml_root.findall('student'): + for task in student.findall('task'): + if task.get('task_id')==task_id: + student.remove(task) \ No newline at end of file diff --git a/tests.py b/tests.py new file mode 100644 index 0000000..2184664 --- /dev/null +++ b/tests.py @@ -0,0 +1,151 @@ +import xml.etree.ElementTree as ET +import copy + +import pytest + +from tasks import create_student, remove_student, set_task_points, create_task, remove_task + +INPUT_XML = '444045504444321112323444412505542140252202231415' + + + +def test_create_student(): + root = ET.fromstring(INPUT_XML) + original_length = len([s.attrib['student_id'] for s in root]) + + create_student(root, "TST0000") + assert len([s.attrib['student_id'] for s in root]) == original_length+1 + assert "TST0000" in [s.attrib['student_id'] for s in root] + + with pytest.raises(Exception, match="^student already exists$"): + create_student(root, "ABC0123") + + + +def test_remove_student(): + root = ET.fromstring(INPUT_XML) + original_length = len([s.attrib['student_id'] for s in root]) + + remove_student(root, "ABC0123") + assert len([s.attrib['student_id'] for s in root]) == original_length-1 + assert "ABC0123" not in [s.attrib['student_id'] for s in root] + + + +def test_set_task_points(): + root = ET.fromstring(INPUT_XML) + assert int(root[0][0].text) == 4 + assert int(root[1][1].text) == 4 + assert int(root[2][2].text) == 3 + assert int(root[3][3].text) == 5 + assert int(root[4][4].text) == 2 + assert int(root[5][5].text) == 4 + assert int(root[0][6].text) == 5 + assert int(root[1][7].text) == 1 + set_task_points(root, 'ABC0123', '1', '5') + assert int(root[0][0].text) == 5 + assert int(root[1][1].text) == 4 + assert int(root[2][2].text) == 3 + assert int(root[3][3].text) == 5 + assert int(root[4][4].text) == 2 + assert int(root[5][5].text) == 4 + assert int(root[0][6].text) == 5 + assert int(root[1][7].text) == 1 + set_task_points(root, 'DEF4567', '2', '5') + assert int(root[0][0].text) == 5 + assert int(root[1][1].text) == 5 + assert int(root[2][2].text) == 3 + assert int(root[3][3].text) == 5 + assert int(root[4][4].text) == 2 + assert int(root[5][5].text) == 4 + assert int(root[0][6].text) == 5 + assert int(root[1][7].text) == 1 + set_task_points(root, 'GHI8901', '3', '5') + assert int(root[0][0].text) == 5 + assert int(root[1][1].text) == 5 + assert int(root[2][2].text) == 5 + assert int(root[3][3].text) == 5 + assert int(root[4][4].text) == 2 + assert int(root[5][5].text) == 4 + assert int(root[0][6].text) == 5 + assert int(root[1][7].text) == 1 + set_task_points(root, 'JKL2345', '4', '0') + assert int(root[0][0].text) == 5 + assert int(root[1][1].text) == 5 + assert int(root[2][2].text) == 5 + assert int(root[3][3].text) == 0 + assert int(root[4][4].text) == 2 + assert int(root[5][5].text) == 4 + assert int(root[0][6].text) == 5 + assert int(root[1][7].text) == 1 + set_task_points(root, 'MNO6789', '5', '5') + assert int(root[0][0].text) == 5 + assert int(root[1][1].text) == 5 + assert int(root[2][2].text) == 5 + assert int(root[3][3].text) == 0 + assert int(root[4][4].text) == 5 + assert int(root[5][5].text) == 4 + assert int(root[0][6].text) == 5 + assert int(root[1][7].text) == 1 + set_task_points(root, 'PQR0123', '6', '5') + assert int(root[0][0].text) == 5 + assert int(root[1][1].text) == 5 + assert int(root[2][2].text) == 5 + assert int(root[3][3].text) == 0 + assert int(root[4][4].text) == 5 + assert int(root[5][5].text) == 5 + assert int(root[0][6].text) == 5 + assert int(root[1][7].text) == 1 + set_task_points(root, 'ABC0123', '7', '0') + assert int(root[0][0].text) == 5 + assert int(root[1][1].text) == 5 + assert int(root[2][2].text) == 5 + assert int(root[3][3].text) == 0 + assert int(root[4][4].text) == 5 + assert int(root[5][5].text) == 5 + assert int(root[0][6].text) == 0 + assert int(root[1][7].text) == 1 + set_task_points(root, 'DEF4567', '8', '5') + assert int(root[0][0].text) == 5 + assert int(root[1][1].text) == 5 + assert int(root[2][2].text) == 5 + assert int(root[3][3].text) == 0 + assert int(root[4][4].text) == 5 + assert int(root[5][5].text) == 5 + assert int(root[0][6].text) == 0 + assert int(root[1][7].text) == 5 + + + +def test_create_task(): + root = ET.fromstring(INPUT_XML) + + create_task(root, 'PQR0123', '9', '5') + assert len(root[5]) == 9 + assert root[5][-1].text == '5' + assert root[5][-1].attrib['task_id'] == '9' + + create_task(root, 'PQR0123', '10', '1') + assert len(root[5]) == 10 + assert root[5][-1].text == '1' + assert root[5][-1].attrib['task_id'] == '10' + + with pytest.raises(Exception, match="^task already exists$"): + create_task(root, 'PQR0123', '1', '50') + + + +def test_remove_task(): + root = ET.fromstring(INPUT_XML) + + assert int(root[0][2].attrib['task_id']) == 3 + remove_task(root, '3') + assert int(root[0][2].attrib['task_id']) == 4 + assert int(root[0][3].attrib['task_id']) == 5 + remove_task(root, '5') + assert int(root[0][3].attrib['task_id']) == 6 + assert int(root[0][0].attrib['task_id']) == 1 + remove_task(root, '1') + assert int(root[0][0].attrib['task_id']) == 2 + remove_task(root, '2') + assert int(root[0][0].attrib['task_id']) == 4 \ No newline at end of file