From 6a5cebadc5f69214df7c3252b592ef36fa34c8f5 Mon Sep 17 00:00:00 2001 From: Muhammad Anas Date: Thu, 15 Feb 2024 09:51:10 +0000 Subject: [PATCH] feat: added integration tests in CI workflow --- .github/workflows/ci.yml | 12 ++++++++- edx_sga/sga.py | 4 +-- edx_sga/tests/integration_tests.py | 41 +++++++++++++++--------------- run_devstack_integration_tests.sh | 8 ++++-- 4 files changed, 39 insertions(+), 26 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index fbf27992..cd75e27b 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -28,6 +28,16 @@ jobs: with: python-version: ${{ matrix.python-version }} + - name: Run Integration Tests + run: | + cd .. + git clone https://github.com/edx/devstack.git + cd devstack + sed -i 's/:cached//g' ./docker-compose-host.yml + make dev.clone.https + DEVSTACK_WORKSPACE=$PWD/.. docker-compose -f docker-compose.yml -f docker-compose-host.yml run -v $PWD/../edx-sga:/edx-sga lms /edx-sga/run_devstack_integration_tests.sh + + - name: tox install run: pip install tox @@ -35,7 +45,7 @@ jobs: env: TOXENV: ${{ matrix.toxenv }} run: tox - + - name: Upload coverage to CodeCov if: matrix.python-version == '3.8' && matrix.toxenv == 'py38-django32' uses: codecov/codecov-action@v3 diff --git a/edx_sga/sga.py b/edx_sga/sga.py index aecbbe0a..636200a9 100644 --- a/edx_sga/sga.py +++ b/edx_sga/sga.py @@ -656,7 +656,7 @@ def get_student_item_dict(self, student_id=None): """ if student_id is None and (user_service := self.runtime.service(self, 'user')): student_id = user_service.get_current_user().opt_attrs.get(ATTR_KEY_ANONYMOUS_USER_ID) - + assert student_id != ("MOCK", "Forgot to call 'personalize' in test.") return { "student_id": student_id, @@ -762,7 +762,7 @@ def student_state(self): if score: score = score.get("points_earned") graded = {"score": score, "comment": force_str(self.comment)} - + else: uploaded = None diff --git a/edx_sga/tests/integration_tests.py b/edx_sga/tests/integration_tests.py index c85311e2..6d2f7af9 100644 --- a/edx_sga/tests/integration_tests.py +++ b/edx_sga/tests/integration_tests.py @@ -7,7 +7,6 @@ import json import os import shutil -import functools import tempfile from unittest import mock @@ -32,16 +31,13 @@ from submissions.models import StudentItem from xblock.field_data import DictFieldData from xblock.fields import ScopeIds -from xblock.runtime import DictKeyValueStore, KvsFieldData +from xblock.runtime import DictKeyValueStore, KvsFieldData, Mixologist from xblock.test.tools import TestRuntime from xmodule.modulestore.django import modulestore -from xmodule.modulestore.tests.django_utils import ModuleStoreTestCase, mixed_store_config, StoreConstructors +from xmodule.modulestore.tests.django_utils import ModuleStoreTestCase from xmodule.modulestore.tests.factories import CourseFactory, BlockFactory from xmodule.modulestore.xml_exporter import export_course_to_xml from xmodule.modulestore.xml_importer import import_course_from_xml -from xmodule.modulestore.inheritance import InheritanceMixin -from openedx.core.lib import tempdir -from xblock.runtime import Mixologist from edx_sga.constants import ShowAnswer from edx_sga.sga import StaffGradedAssignmentXBlock @@ -166,7 +162,7 @@ def make_student(self, block, name, make_state=True, **state): module.save() anonymous_id = anonymous_id_for_user(user, self.course_id) - + item = StudentItem( student_id=anonymous_id, course_id=self.course_id, @@ -175,14 +171,14 @@ def make_student(self, block, name, make_state=True, **state): ) item.save() - if answer: + if answer: student_id = block.get_student_item_dict(anonymous_id) submission = submissions_api.create_submission(student_id, answer) if score is not None: submissions_api.set_score( submission["uuid"], score, block.max_score() ) - pass + else: submission = None @@ -204,7 +200,7 @@ def personalize(self, block, module, item, submission): state = json.loads(student_module.state) for key, value in state.items(): setattr(block, key, value) - + self.runtime.deprecated_anonymous_student_id = item.student_id def test_ctor(self): @@ -254,7 +250,6 @@ def test_student_view(self, fragment, render_template): self.assertEqual(student_state["upload_allowed"], True) self.assertEqual(student_state["max_score"], 100) self.assertEqual(student_state["graded"], None) - # pylint: disable=no-member fragment.add_css.assert_called_once_with( DummyResource("static/css/edx_sga.css") ) @@ -327,7 +322,6 @@ def test_student_view_with_score(self, fragment, render_template): self.assertEqual(student_state["upload_allowed"], False) self.assertEqual(student_state["max_score"], 100) self.assertEqual(student_state["graded"], {"comment": "", "score": 10}) - # pylint: disable=no-member fragment.add_css.assert_called_once_with( DummyResource("static/css/edx_sga.css") ) @@ -566,7 +560,8 @@ def test_staff_download(self): user = student["module"].student student_id = anonymous_id_for_user(user,self.course_id) - with mock.patch.object(StaffGradedAssignmentXBlock.get_student_item_dict,"__defaults__",(student_id,)), self.dummy_upload(filename, text) as (upload, __): + with mock.patch.object(StaffGradedAssignmentXBlock.get_student_item_dict,"__defaults__",(student_id,)),\ + self.dummy_upload(filename, text) as (upload, __): block.upload_assignment(mock.Mock(params={"assignment": upload})) students.append( ( @@ -627,8 +622,9 @@ def test_staff_download_unicode_filename(self): self.personalize(block, **student) user = student["module"].student student_id = anonymous_id_for_user(user,self.course_id) - - with mock.patch.object(StaffGradedAssignmentXBlock.get_student_item_dict,"__defaults__",(student_id,)), self.dummy_upload("файл.txt") as (upload, expected): + + with mock.patch.object(StaffGradedAssignmentXBlock.get_student_item_dict,"__defaults__",(student_id,)),\ + self.dummy_upload("файл.txt") as (upload, expected): block.upload_assignment(mock.Mock(params={"assignment": upload})) response = block.staff_download( mock.Mock(params={"student_id": student["item"].student_id}) @@ -656,8 +652,9 @@ def test_staff_download_filename_with_spaces(self): self.personalize(block, **student) user = student["module"].student student_id = anonymous_id_for_user(user,self.course_id) - - with mock.patch.object(StaffGradedAssignmentXBlock.get_student_item_dict,"__defaults__",(student_id,)), self.dummy_upload(file_name) as (upload, expected): + + with mock.patch.object(StaffGradedAssignmentXBlock.get_student_item_dict,"__defaults__",(student_id,)),\ + self.dummy_upload(file_name) as (upload, expected): block.upload_assignment(mock.Mock(params={"assignment": upload})) response = block.staff_download( mock.Mock(params={"student_id": student["item"].student_id}) @@ -678,8 +675,9 @@ def test_file_download_comma_in_name(self, file_name): self.personalize(block, **student) user = student["module"].student student_id = anonymous_id_for_user(user,self.course_id) - - with mock.patch.object(StaffGradedAssignmentXBlock.get_student_item_dict,"__defaults__",(student_id,)), self.dummy_upload(file_name) as (upload, expected): + + with mock.patch.object(StaffGradedAssignmentXBlock.get_student_item_dict,"__defaults__",(student_id,)),\ + self.dummy_upload(file_name) as (upload, expected): block.upload_assignment(mock.Mock(params={"assignment": upload})) response = block.staff_download( mock.Mock(params={"student_id": student["item"].student_id}) @@ -695,7 +693,8 @@ def test_get_staff_grading_data_not_staff(self): test staff grading data for non staff members. """ block = self.make_one() - with mock.patch("edx_sga.sga.StaffGradedAssignmentXBlock.is_course_staff", return_value=False), self.assertRaises(PermissionDenied): + with mock.patch("edx_sga.sga.StaffGradedAssignmentXBlock.is_course_staff", return_value=False),\ + self.assertRaises(PermissionDenied): block.get_staff_grading_data(None) def test_get_staff_grading_data(self): @@ -934,7 +933,7 @@ def test_has_attempted(self): @data(True, False) def test_runtime_user_is_staff(self, is_staff): - + staff = StaffFactory.create(course_key=self.course.id) render.prepare_runtime_for_user( diff --git a/run_devstack_integration_tests.sh b/run_devstack_integration_tests.sh index 9b85c10e..a36471b0 100755 --- a/run_devstack_integration_tests.sh +++ b/run_devstack_integration_tests.sh @@ -16,8 +16,12 @@ pip install -e . pip freeze # adjust test files for integration tests -cp /edx/app/edxapp/edx-platform/setup.cfg . -rm ./pytest.ini +# cp /edx/app/edxapp/edx-platform/setup.cfg ./edx_sga +# rm ./pytest.ini mkdir test_root # for edx +ls +pwd +# head -10 setup.cfg +python -c "import sys; print('\n'.join(x for x in sys.path if x))" pytest ./edx_sga/tests/integration_tests.py