diff --git a/eox_nelp/stats/tests/tests_metrics.py b/eox_nelp/stats/tests/tests_metrics.py index 6e6779a0..0ad65a9f 100644 --- a/eox_nelp/stats/tests/tests_metrics.py +++ b/eox_nelp/stats/tests/tests_metrics.py @@ -15,6 +15,7 @@ from opaque_keys.edx.keys import CourseKey from eox_nelp.edxapp_wrapper.branding import get_visible_courses +from eox_nelp.edxapp_wrapper.modulestore import modulestore from eox_nelp.edxapp_wrapper.site_configuration import configuration_helpers from eox_nelp.edxapp_wrapper.student import CourseAccessRole, CourseEnrollment from eox_nelp.stats.metrics import ( @@ -179,12 +180,9 @@ def setUp(self): # pylint: disable=invalid-name """ Set base variables and objects across metrics test cases. """ - self.patch = patch("eox_nelp.stats.metrics.modulestore") self.course_key = CourseKey.from_string("course-v1:test+Cx105+2022_T4") - self.modulestore = self.patch.start() - # Prepare verticals - verticals = generate_list_mock_data([ + self.verticals = generate_list_mock_data([ { "children": [ { @@ -227,16 +225,16 @@ def setUp(self): # pylint: disable=invalid-name ]) # Prepare sequentials sequential = Mock() - sequential.get_children.return_value = verticals - sequentials = [ + sequential.get_children.return_value = self.verticals + self.sequentials = [ sequential, sequential, sequential, ] # Prepare chapters chapter = Mock() - chapter.get_children.return_value = sequentials - chapters = [ + chapter.get_children.return_value = self.sequentials + self.chapters = [ chapter, chapter, chapter, @@ -245,31 +243,33 @@ def setUp(self): # pylint: disable=invalid-name # Prepare course course = Mock() course.display_name = "testing" - course.get_children.return_value = chapters + course.get_children.return_value = self.chapters # Set course - self.modulestore.return_value.get_course.return_value = course + modulestore.return_value.get_course.return_value = course + self.expected_returned_enrollments = 5874 + self.expected_returned_roles = 5 # this block set the CourseEnrollment mock and its returned values. filter_result = CourseEnrollment.objects.filter.return_value values_result = filter_result.values.return_value distinct_result = values_result.distinct.return_value - distinct_result.count.return_value = 5874 + distinct_result.count.return_value = self.expected_returned_enrollments # this block set the CourseAccessRole mock and its returned values. filter_result = CourseAccessRole.objects.filter.return_value values_result = filter_result.values.return_value distinct_result = values_result.distinct.return_value - distinct_result.count.return_value = 5 + distinct_result.count.return_value = self.expected_returned_roles def tearDown(self): """Clean cache and restarts mocks""" # This line just verifies that de get_course modulestore method cwas called with the right parameter - self.modulestore.return_value.get_course.assert_called_once_with(self.course_key) + modulestore.return_value.get_course.assert_called_once_with(self.course_key) CourseAccessRole.reset_mock() CourseEnrollment.reset_mock() + modulestore.reset_mock() cache.clear() - self.patch.stop() def test_get_right_id(self): """Based on the initial conditions, this check that the course metrics has the expected id. @@ -287,7 +287,7 @@ def test_get_right_name(self): Expected behavior: - 'name' value is the expected """ - self.modulestore.return_value.get_course.return_value.display_name = "Amazing course" + modulestore.return_value.get_course.return_value.display_name = "Amazing course" course = get_course_metrics(self.course_key) @@ -301,7 +301,7 @@ def test_get_right_learners_metric(self): """ course = get_course_metrics(self.course_key) - self.assertEqual(5874, course["learners"]) + self.assertEqual(self.expected_returned_enrollments, course["learners"]) def test_get_right_instructors_metric(self): """Based on the initial conditions, this check that the course metrics has the expected instructors value. @@ -311,7 +311,7 @@ def test_get_right_instructors_metric(self): """ course = get_course_metrics(self.course_key) - self.assertEqual(5, course["instructors"]) + self.assertEqual(self.expected_returned_roles, course["instructors"]) def test_get_right_sections_metric(self): """Based on the initial conditions, this check that the course metrics has the expected sections value. @@ -321,7 +321,7 @@ def test_get_right_sections_metric(self): """ course = get_course_metrics(self.course_key) - self.assertEqual(4, course["sections"]) + self.assertEqual(len(self.chapters), course["sections"]) def test_get_right_sub_sections_metric(self): """Based on the initial conditions, this check that the course metrics has the expected sub_sections value. @@ -331,7 +331,7 @@ def test_get_right_sub_sections_metric(self): """ course = get_course_metrics(self.course_key) - self.assertEqual(12, course["sub_sections"]) + self.assertEqual(len(self.chapters) * len(self.sequentials), course["sub_sections"]) def test_get_right_units_metric(self): """Based on the initial conditions, this check that the course metrics has the expected units value. @@ -341,7 +341,7 @@ def test_get_right_units_metric(self): """ course = get_course_metrics(self.course_key) - self.assertEqual(36, course["units"]) + self.assertEqual(len(self.chapters) * len(self.sequentials) * len(self.verticals), course["units"]) def test_set_empty_allowed_components(self): """Based on the initial conditions, this check that the course metrics has the expected components value. @@ -363,9 +363,16 @@ def test_set_allowed_components(self): - 'problem' value is the expected - 'video' value is the expected """ + expected_components = {} + + for vertical in self.verticals: + for child in vertical.children: + expected_components[child.block_type] = expected_components.get(child.block_type, 0) + 1 + + expected_components = { + k: (v * len(self.chapters) * len(self.sequentials)) for k, v in expected_components.items() + } + course = get_course_metrics(self.course_key) - components = course["components"] - self.assertEqual(48, components["html"]) - self.assertEqual(36, components["problem"]) - self.assertEqual(24, components["video"]) + self.assertEqual(expected_components, course["components"]) diff --git a/eox_nelp/tests/utils.py b/eox_nelp/tests/utils.py index ee8091d4..da7b8410 100644 --- a/eox_nelp/tests/utils.py +++ b/eox_nelp/tests/utils.py @@ -20,6 +20,20 @@ def generate_list_mock_data(data): "due": "due_date", "location": "location" }, + { + "due" : "due_date", + "components": [ + { + "block_type": "problem", + }, + { + "block_type": "video", + }, + { + "block_type": "html", + }, + ] + }, ] Every dictionary should be direct key values.No way if there is nested dict the model