fix: collapse unit tests by using ddt

This commit is contained in:
cdeery
2021-08-05 13:40:03 -04:00
parent eafea504ac
commit b4b461b698

View File

@@ -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)})