Skip to content

Commit

Permalink
test: add tests for filters
Browse files Browse the repository at this point in the history
  • Loading branch information
Ian2012 committed Nov 7, 2023
1 parent f0b436d commit d8e84a0
Show file tree
Hide file tree
Showing 6 changed files with 164 additions and 3 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ quality: ## Run the quality checks
test: ## Run the tests
mkdir -p var
rm -rf .coverage
DJANGO_SETTINGS_MODULE=test_settings python -m coverage run --rcfile=.coveragerc -m pytest
DJANGO_SETTINGS_MODULE=feedback.settings.test python -m coverage run --rcfile=.coveragerc -m pytest

covreport: ## Show the coverage results
python -m coverage report -m --skip-covered
Expand Down
4 changes: 4 additions & 0 deletions feedback/extensions/filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,10 @@ def load_blocks(request, course):
)

blocks = []

if not feedback_blocks:
return []

students = get_user_enrollments(course_id).values_list(
"user_id", "user__username"
)
Expand Down
16 changes: 15 additions & 1 deletion feedback/settings/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,20 @@
For the full list of settings and their values, see
https://docs.djangoproject.com/en/2.22/ref/settings/
"""
from workbench.settings import *

from django.conf.global_settings import LOGGING

INSTALLED_APPS = [
"feedback",
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'feedback',
'workbench',
]

FEATURES = {
"ENABLE_FEEDBACK_INSTRUCTOR_VIEW": True,
}

SECRET_KEY = 'fake-key'
139 changes: 139 additions & 0 deletions feedbacktests/test_filters.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
"""
Test for the instructor dashboard filters.
"""

from unittest import TestCase
from unittest.mock import Mock, patch
from django.test.utils import override_settings


from feedback.extensions.filters import AddFeedbackTab, load_xblock_answers

class TestFilters(TestCase):
"""
Test suite for the FeedbackXBlock filters.
"""

def setUp(self) -> None:
"""
Set up the test suite.
"""
self.filter = AddFeedbackTab(filter_type=Mock(), running_pipeline=Mock())

@patch("feedback.extensions.filters.get_user_enrollments")
@patch("feedback.extensions.filters.get_block_by_usage_id")
@patch("feedback.extensions.filters.modulestore")
def test_run_filter_without_blocks(self, modulestore_mock, get_block_by_usage_id_mock, get_user_enrollments_mock):
"""
Check the filter is not executed when there are no LimeSurvey blocks in the course.
Expected result:
- The context is returned without modifications.
"""
modulestore_mock().get_items.return_value = []
context = {"course": Mock(id="test-course-id"), "sections": []}
template_name = "test-template-name"

self.filter.run_filter(context, template_name)

get_block_by_usage_id_mock.assert_not_called()
get_user_enrollments_mock.assert_not_called()


@patch("feedback.extensions.filters.get_lms_link_for_item")
@patch("feedback.extensions.filters.get_user_enrollments")
@patch("feedback.extensions.filters.get_block_by_usage_id")
@patch("feedback.extensions.filters.load_single_xblock")
@patch("feedback.extensions.filters.modulestore")
def test_run_filter(self, modulestore_mock, load_single_xblock_mock, get_block_by_usage_id_mock, get_user_enrollments_mock,
get_lms_link_for_item_mock):
"""
Check the filter is executed when there are LimeSurvey blocks in the course.
Expected result:
- The context is returned with the LimeSurvey blocks information.
"""
modulestore_mock().get_items.return_value = [Mock(location="test-location")]
context = {"course": Mock(id="test-course-id"), "sections": []}
template_name = "test-template-name"
get_user_enrollments_mock.value_list = [(1, "test-username")]
block_mock = Mock(
vote_aggregate=[],
)
block_mock.get_prompt.return_value = {"scale_text": ["test-scale-text"]}
get_block_by_usage_id_mock.return_value = block_mock, None
get_lms_link_for_item_mock.return_value = "test-url"
load_single_xblock_mock.return_value = Mock(
user_vote=1,
user_freeform="test-user-freeform",
)

result = self.filter.run_filter(context, template_name)

get_block_by_usage_id_mock.assert_called()
get_user_enrollments_mock.assert_called_once()
self.assertEqual(1, len(result.get("context", {})["sections"]))

@override_settings(
FEATURES={"ENABLE_FEEDBACK_INSTRUCTOR_VIEW":False}
)
def test_run_filter_disable(self):
context = {"course": Mock(id="test-course-id"), "sections": []}
template_name = "test-template-name"

new_context = self.filter.run_filter(context, template_name)["context"]

self.assertEqual(context, new_context)

@patch("feedback.extensions.filters.load_single_xblock")
def test_load_xblock_answers(self, load_single_xblock_mock):
request_mock = Mock()
students = [(1, "test-username")]
course_id = "test-course-id"
block_id = "test-block-id"
course = Mock()

single_block = Mock(
user_vote=0,
user_freeform="test-user-freeform",
)
single_block.get_prompt.return_value = {"scale_text": ["test-scale-text"]}

load_single_xblock_mock.return_value = single_block

answers = load_xblock_answers(request_mock, students, course_id, block_id, course)

self.assertEqual(
[
{
"username": "test-username",
"user_vote": "test-scale-text",
"user_freeform": "test-user-freeform",
}
],
answers,
)


@patch("feedback.extensions.filters.load_single_xblock")
def test_load_xblock_answers_skip_empty(self, load_single_xblock_mock):
request_mock = Mock()
students = [(1, "test-username")]
course_id = "test-course-id"
block_id = "test-block-id"
course = Mock()

single_block = Mock(
user_vote=-1,
user_freeform="",
)
single_block.get_prompt.return_value = {"scale_text": ["test-scale-text"]}

load_single_xblock_mock.return_value = single_block

answers = load_xblock_answers(request_mock, students, course_id, block_id, course)

self.assertEqual(
[],
answers,
)
4 changes: 4 additions & 0 deletions test_settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,7 @@
'django.contrib.contenttypes',
'workbench',
]

FEATURES = {
"ENABLE_FEEDBACK_INSTRUCTOR_VIEW": True,
}
2 changes: 1 addition & 1 deletion tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ usedevelop=True
passenv =
SELENIUM_BROWSER
setenv =
DJANGO_SETTINGS_MODULE = test_settings
DJANGO_SETTINGS_MODULE = feedback.settings.test

[testenv:quality]
allowlist_externals =
Expand Down

0 comments on commit d8e84a0

Please sign in to comment.