From b4b461b698ca01ba98c08bed0d5be4ee9d69601b Mon Sep 17 00:00:00 2001 From: cdeery Date: Thu, 5 Aug 2021 13:40:03 -0400 Subject: [PATCH] fix: collapse unit tests by using ddt --- .../courseware/tests/test_masquerade.py | 53 +++++++++---------- 1 file changed, 25 insertions(+), 28 deletions(-) diff --git a/lms/djangoapps/courseware/tests/test_masquerade.py b/lms/djangoapps/courseware/tests/test_masquerade.py index 42568e6af9..a625e6e173 100644 --- a/lms/djangoapps/courseware/tests/test_masquerade.py +++ b/lms/djangoapps/courseware/tests/test_masquerade.py @@ -2,7 +2,6 @@ Unit tests for masquerade. """ - import json import pickle from datetime import datetime @@ -45,6 +44,7 @@ class MasqueradeTestCase(SharedModuleStoreTestCase, LoginEnrollmentTestCase, Mas """ Base class for masquerade tests that sets up a test course and enrolls a user in the course. """ + @classmethod def setUpClass(cls): super().setUpClass() @@ -172,17 +172,6 @@ class MasqueradeTestCase(SharedModuleStoreTestCase, LoginEnrollmentTestCase, Mas assert self.problem_display_name in problem_html assert show_answer_expected == ('Show answer' in problem_html) - def verify_learner_masquerade_available(self, learner_option_expected): - """ - Verifies if learner masquerade option is available - Args: - learner_option_expected: True to test if Learner is in options, False to test it is NOT - """ - response = self.get_available_masquerade_identities() - items = response.json()['available'] - is_it_there = 'Learner' in map(itemgetter('name'), items) - assert learner_option_expected == is_it_there - def ensure_masquerade_as_group_member(self, partition_id, group_id): """ Installs a masquerade for the test_user and test course, to enable the @@ -201,6 +190,7 @@ class NormalStudentVisibilityTest(MasqueradeTestCase): """ Verify the course displays as expected for a "normal" student (to ensure test setup is correct). """ + def create_user(self): """ Creates a normal student user. @@ -226,6 +216,7 @@ class StaffMasqueradeTestCase(MasqueradeTestCase): """ Base class for tests of the masquerade behavior for a staff member. """ + def create_user(self): """ Creates a staff user. @@ -233,30 +224,30 @@ class StaffMasqueradeTestCase(MasqueradeTestCase): return StaffFactory(course_key=self.course.id) -class TestMasqueradeOptionsForNoPartitions(StaffMasqueradeTestCase): +@ddt.ddt +class TestMasqueradeLearnerOptions(StaffMasqueradeTestCase): """ - Check that 'Learner' option is available if there are no groups or partitions + Check that 'View as Learner' option is available only if there are NO groups or partitions """ + + @ddt.data(True, False) @patch.dict('django.conf.settings.FEATURES', {'ENABLE_MASQUERADE': True}) - @patch.dict('django.conf.settings.FEATURES', {'ENABLE_ENROLLMENT_TRACK_USER_PARTITION': False}) - def test_masquerade_options_enrollment_track(self): - self.verify_learner_masquerade_available(True) - - -class TestMasqueradeOptionsForUserPartition(StaffMasqueradeTestCase): - """ - Check that 'Learner' option is NOT available if there are no groups or partitions - """ - @ patch.dict('django.conf.settings.FEATURES', {'ENABLE_MASQUERADE': True}) - @ patch.dict('django.conf.settings.FEATURES', {'ENABLE_ENROLLMENT_TRACK_USER_PARTITION': True}) - def test_masquerade_options_no_cohort(self): - self.verify_learner_masquerade_available(False) + def test_masquerade_options_for_learner(self, partitions_enabled): + """ + If there are partitions, then the View as Learner should NOT be available + """ + with patch.dict('django.conf.settings.FEATURES', + {'ENABLE_ENROLLMENT_TRACK_USER_PARTITION': partitions_enabled}): + response = self.get_available_masquerade_identities() + is_learner_available = 'Learner' in map(itemgetter('name'), response.json()['available']) + assert partitions_enabled != is_learner_available class TestStaffMasqueradeAsStudent(StaffMasqueradeTestCase): """ Check for staff being able to masquerade as student. """ + @patch.dict('django.conf.settings.FEATURES', {'DISABLE_START_DATES': False}) def test_staff_debug_with_masquerade(self): """ @@ -295,6 +286,7 @@ class TestStaffMasqueradeAsSpecificStudent(StaffMasqueradeTestCase, ProblemSubmi """ Check for staff being able to masquerade as a specific student. """ + def setUp(self): super().setUp() self.student_user = self.create_user() @@ -481,6 +473,7 @@ class TestGetMasqueradingGroupId(StaffMasqueradeTestCase): """ Check for staff being able to masquerade as belonging to a group. """ + def setUp(self): super().setUp() self.user_partition = UserPartition( @@ -514,6 +507,7 @@ class ReadOnlyKeyValueStore(DictKeyValueStore): Used to make sure MasqueradingKeyValueStore does not try to modify the underlying KeyValueStore. """ + def set(self, key, value): assert False, "ReadOnlyKeyValueStore may not be modified." @@ -533,6 +527,7 @@ class MasqueradingKeyValueStoreTest(TestCase): """ Unit tests for the MasqueradingKeyValueStore class. """ + def setUp(self): super().setUp() self.ro_kvs = ReadOnlyKeyValueStore({'a': 42, 'b': None, 'c': 'OpenCraft'}) @@ -572,6 +567,7 @@ class CourseMasqueradeTest(TestCase): """ Unit tests for the CourseMasquerade class. """ + def test_unpickling_sets_all_attributes(self): """ Make sure that old CourseMasquerade objects receive missing attributes when unpickled from @@ -584,10 +580,11 @@ class CourseMasqueradeTest(TestCase): assert unpickled_cmasq.user_name is None -class SetupMasqueradeTests(SharedModuleStoreTestCase,): +class SetupMasqueradeTests(SharedModuleStoreTestCase, ): """ Tests for the setup_masquerade function. """ + def setUp(self): super().setUp() self.course = CourseFactory.create(number='setup-masquerade-test', metadata={'start': datetime.now(UTC)})