def test_remove_groups(self): # Test the Officer method for removing the user from groups. Note that # unlike test_add_groups, this method saves the Officer objets, as the # _remove_user_from_officer_groups method depends on database entries # to work properly. Thus, this method relies on post-save functions for # adding groups for a user. (Post-saves are also tested separately.) self.assertFalse(self.user.groups.exists()) # No groups yet # Add a regular officer position (for which the post-save should add # groups): officer_reg = Officer(user=self.user, position=self.position_regular, term=self.term) officer_reg.save() groups = list(self.user.groups.all()) self.assertTrue(len(groups) > 0) # Add the groups for an exec officer position manually so that the # Officer object is not in the database (and does not need to be # deleted here before we can test the removal function), and the user's # group count should increase: officer_exec = Officer(user=self.user, position=self.position_exec, term=self.term) officer_exec._add_user_to_officer_groups() self.assertTrue(len(groups) < self.user.groups.count()) # Now remove groups from the exec position, and the user's groups # should return to the same positions as from before the exec position # added any: officer_exec._remove_user_from_officer_groups() self.assertItemsEqual(groups, list(self.user.groups.all()))
def test_remove_groups(self): # Test the Officer method for removing the user from groups. Note that # unlike test_add_groups, this method saves the Officer objets, as the # _remove_user_from_officer_groups method depends on database entries # to work properly. Thus, this method relies on post-save functions for # adding groups for a user. (Post-saves are also tested separately.) self.assertFalse(self.user.groups.exists()) # No groups yet # Add a regular officer position (for which the post-save should add # groups): officer_reg = Officer(user=self.user, position=self.position_regular, term=self.term) officer_reg.save() groups = list(self.user.groups.all()) self.assertTrue(len(groups) > 0) # Add the groups for an exec officer position manually so that the # Officer object is not in the database (and does not need to be # deleted here before we can test the removal function), and the user's # group count should increase: officer_exec = Officer(user=self.user, position=self.position_exec, term=self.term) officer_exec._add_user_to_officer_groups() self.assertTrue(len(groups) < self.user.groups.count()) # Now remove groups from the exec position, and the user's groups # should return to the same positions as from before the exec position # added any: officer_exec._remove_user_from_officer_groups() self.assertItemsEqual(groups, list(self.user.groups.all()))
def test_add_groups(self): # Test the Officer method for adding the user to groups. Note that no # officer objects are saved, as that would activate post-saves, which # are tested seprately. officer = Officer(user=self.user, position=self.position_regular, term=self.term) expected_groups = self.position_regular.get_corresponding_groups(term=self.term) self.assertFalse(self.user.groups.exists()) officer._add_user_to_officer_groups() # Check that all of the expected groups were added for this user: self.assertTrue(self.user.groups.exists()) for group in expected_groups: self.assertTrue(self.user.groups.filter(pk=group.pk).exists())
def test_add_groups(self): # Test the Officer method for adding the user to groups. Note that no # officer objects are saved, as that would activate post-saves, which # are tested seprately. officer = Officer(user=self.user, position=self.position_regular, term=self.term) expected_groups = self.position_regular.get_corresponding_groups( term=self.term) self.assertFalse(self.user.groups.exists()) officer._add_user_to_officer_groups() # Check that all of the expected groups were added for this user: self.assertTrue(self.user.groups.exists()) for group in expected_groups: self.assertTrue(self.user.groups.filter(pk=group.pk).exists())