def test_approval_updates_status(self): changeset = ChangesetFactory(status=Changeset.STATUS_NEW) user = give_approve_permission(self.user) changeset.approve(user) changeset = refresh_model(changeset) self.assertEqual(Changeset.STATUS_NEW, changeset.status) user2 = give_approve_permission(UserFactory()) changeset.approve(user2) changeset = refresh_model(changeset) self.assertEqual(Changeset.STATUS_APPROVED, changeset.status)
def test_model_queue_method(self): changeset = ChangesetFactory(status=Changeset.STATUS_APPROVED) with patch('changesets.models.execute_changeset') as mock_task_method: changeset.queue() mock_task_method.delay.assert_called_with(changeset.pk) changeset = refresh_model(changeset) self.assertEqual(Changeset.STATUS_QUEUED, changeset.status)
def test_model_queue_bad_status(self): changeset = ChangesetFactory(status=Changeset.STATUS_NEW) with patch('changesets.models.execute_changeset') as mock_task_method: with self.assertRaises(NotAnAllowedStatus): changeset.queue() mock_task_method.delay.assert_not_called() changeset = refresh_model(changeset) self.assertEqual(Changeset.STATUS_NEW, changeset.status)
def test_create_changeset(self): # We can create a changeset mock_request = MagicMock(user=UserFactory()) form = ChangesetForm(data=self.data, request=mock_request) self.assertTrue(form.is_valid(), msg=str(form.errors)) changeset = form.save() changeset = refresh_model(changeset) self.assertEqual(Changeset.CHANGE_CENTER, changeset.change) self.assertEqual(Changeset.SELECT_CENTERS, changeset.how_to_select) self.assertIn(self.center1, changeset.selected_centers.all()) self.assertIn(self.center2, changeset.selected_centers.all())
def test_exception_fails_execute(self): changeset = ChangesetFactory( status=Changeset.STATUS_APPROVED, change=Changeset.CHANGE_CENTER, how_to_select=Changeset.SELECT_UPLOADED_NIDS, ) with patch.object(Changeset, 'get_registrations_to_change') as get_reg: get_reg.side_effect = TypeError changeset.execute() changeset = refresh_model(changeset) self.assertEqual(Changeset.STATUS_FAILED, changeset.status)
def test_too_many_changes(self, get_records, open): cit1 = CitizenFactory() cit2 = CitizenFactory() citizen = CitizenFactory() data = model_to_dict(citizen) new_name = 'Jim Bob' data['first_name'] = new_name get_records.return_value = [model_to_dict(cit1), model_to_dict(cit2), data] # Import raises an exception: with self.assertRaises(TooManyChanges): import_citizen_dump(input_filename=None, max_change_percent=10) # Citizen is unchanged: new_citizen = refresh_model(citizen) self.assertEqual(citizen.first_name, new_citizen.first_name)
def test_too_many_changes(self, get_records, open): cit1 = CitizenFactory() cit2 = CitizenFactory() citizen = CitizenFactory() data = model_to_dict(citizen) new_name = 'Jim Bob' data['first_name'] = new_name get_records.return_value = [ model_to_dict(cit1), model_to_dict(cit2), data ] # Import raises an exception: with self.assertRaises(TooManyChanges): import_citizen_dump(input_filename=None, max_change_percent=10) # Citizen is unchanged: new_citizen = refresh_model(citizen) self.assertEqual(citizen.first_name, new_citizen.first_name)
def give_edit_permission(user): """Give user edit permission, and return updated object""" perm = get_permission_object_by_name(EDIT_CHANGESET_PERMISSION) user.user_permissions.add(perm) return refresh_model(user)
def give_queue_permission(user): """Give user approve permission, and return updated object""" perm = get_permission_object_by_name(QUEUE_CHANGESET_PERMISSION) user.user_permissions.add(perm) return refresh_model(user)
def give_approve_permission(user): """Give user approve permission, and return updated object""" user.user_permissions.add( get_permission_object_by_name(APPROVE_CHANGESET_PERMISSION)) return refresh_model(user)
def doit(self): # Execute the changeset and check out the results self.changeset.full_clean() citizens = self.changeset.get_citizens_to_change() self.assertIn(self.reg1.citizen, citizens) self.assertIn(self.reg2.citizen, citizens) self.assertNotIn(self.reg3.citizen, citizens) # citizen4 might or might not be in the 'get_citizens_to_change' result depending # on the test self.changeset.execute() self.assertEqual(self.to_center, refresh_model(self.reg1).registration_center) # There should also be an archived registration that is unchanged archives = Registration.objects.archived() self.assertEqual(1, archives.filter(citizen=self.reg1.citizen, registration_center=self.from_center_1).count()) self.assertEqual(self.to_center, refresh_model(self.reg2).registration_center) # There should also be an archived registration that is unchanged self.assertEqual(1, archives.filter(citizen=self.reg2.citizen, registration_center=self.from_center_2).count()) # reg 3 not moved, was at center 3 self.assertEqual(self.center_3, refresh_model(self.reg3).registration_center) # No archived center self.assertFalse(archives.filter(citizen=self.reg3.citizen).exists()) # reg 4 not moved, but was already at to_center self.assertEqual(self.to_center, refresh_model(self.reg4).registration_center) # No archived center self.assertFalse(archives.filter(citizen=self.reg4.citizen).exists()) changeset = refresh_model(self.changeset) if self.changeset.how_to_select == Changeset.SELECT_UPLOADED_NIDS: # Ugh - this was the only test where we could "add" citizen4 self.assertEqual(Changeset.STATUS_PARTIALLY_SUCCESSFUL, changeset.status) else: self.assertEqual(Changeset.STATUS_SUCCESSFUL, changeset.status) changes = changeset.change_records self.assertEqual(1, changes.filter(citizen=self.reg1.citizen, changed=True).count()) self.assertEqual(1, changes.filter(citizen=self.reg2.citizen, changed=True).count()) # self.reg3 shouldn't be mentioned, they weren't in the set to be changed self.assertFalse(changes.filter(citizen=self.reg3.citizen).exists()) # self.reg4 might or might not have a change record, but if it does, changed # should be False self.assertFalse(changes.filter(citizen=self.reg4.citizen, changed=True).exists()) # the citizen from self.reg2 moves themselves to another center, so they # should not be rolled back self.reg2.registration_center = self.center_3 self.reg2.save() # Do a rollback rollback = ChangesetFactory( status=Changeset.STATUS_APPROVED, change=Changeset.CHANGE_ROLLBACK, how_to_select=Changeset.SELECT_OTHER_CHANGESET, other_changeset=changeset, ) rollback.full_clean() citizens = list(rollback.get_citizens_to_change()) self.assertIn(self.reg1.citizen, citizens) self.assertIn(self.reg2.citizen, citizens) # In the list, but we won't change them self.assertNotIn(self.reg3.citizen, citizens) self.assertNotIn(self.reg4.citizen, citizens) # was not changed rollback.execute() rollback = refresh_model(rollback) self.assertEqual(Changeset.STATUS_PARTIALLY_SUCCESSFUL, rollback.status) changeset = refresh_model(changeset) self.assertEqual(Changeset.STATUS_ROLLED_BACK, changeset.status) self.assertEqual(rollback, changeset.rollback_changeset) self.assertEqual(self.from_center_1, refresh_model(self.reg1).registration_center) # reg1 was changed then changed back, so there should be two archived versions self.assertEqual(2, archives.filter(citizen=self.reg1.citizen).count()) self.assertEqual(1, archives.filter(citizen=self.reg1.citizen, registration_center=self.from_center_1).count()) self.assertEqual(1, archives.filter(citizen=self.reg1.citizen, registration_center=self.to_center).count()) self.assertEqual(self.center_3, refresh_model(self.reg2).registration_center) self.assertEqual(1, rollback.change_records.filter(citizen=self.reg1.citizen, changed=True) .count()) self.assertEqual(1, rollback.change_records.filter(citizen=self.reg2.citizen, changed=False) .count())
def doit(self): self.changeset.full_clean() citizens = list(self.changeset.get_citizens_to_change()) self.assertIn(self.citizen1, citizens) self.assertIn(self.citizen2, citizens) self.assertIn(self.citizen3, citizens) # In the list, but will not be changed self.assertNotIn(self.citizen4, citizens) self.changeset.execute() self.citizen1 = refresh_model(self.citizen1) self.citizen2 = refresh_model(self.citizen2) self.citizen3 = refresh_model(self.citizen3) self.citizen4 = refresh_model(self.citizen4) self.assertFalse(self.citizen1.blocked) self.assertFalse(self.citizen2.blocked) self.assertFalse(self.citizen3.blocked) self.assertTrue(self.citizen4.blocked) changes = self.changeset.change_records.all() self.assertTrue(changes.filter(citizen=self.citizen1, changed=True).exists()) self.assertTrue(changes.filter(citizen=self.citizen2, changed=True).exists()) self.assertTrue(changes.filter(citizen=self.citizen3, changed=False).exists()) self.assertFalse(changes.filter(citizen=self.citizen4).exists()) self.assertEqual(Changeset.STATUS_PARTIALLY_SUCCESSFUL, self.changeset.status) # Now block citizen 2 self.citizen2.block() rollback = ChangesetFactory( status=Changeset.STATUS_APPROVED, change=Changeset.CHANGE_ROLLBACK, how_to_select=Changeset.SELECT_OTHER_CHANGESET, other_changeset=self.changeset, ) citizens = list(rollback.get_citizens_to_change()) self.assertIn(self.citizen1, citizens) self.assertIn(self.citizen2, citizens) self.assertNotIn(self.citizen3, citizens) # not changed before, so not in list this time self.assertNotIn(self.citizen4, citizens) rollback.execute() self.changeset = refresh_model(self.changeset) self.assertEqual(Changeset.STATUS_ROLLED_BACK, self.changeset.status) self.citizen1 = refresh_model(self.citizen1) self.citizen2 = refresh_model(self.citizen2) self.citizen3 = refresh_model(self.citizen3) self.citizen4 = refresh_model(self.citizen4) self.assertTrue(self.citizen1.blocked) self.assertTrue(self.citizen2.blocked) self.assertFalse(self.citizen3.blocked) self.assertTrue(self.citizen4.blocked) changes = rollback.change_records.all() self.assertTrue(changes.filter(citizen=self.citizen1, changed=True).exists()) self.assertTrue(changes.filter(citizen=self.citizen2, changed=False).exists()) # reg 3 not in the list because they were changed previously self.assertFalse(changes.filter(citizen=self.citizen3).exists()) self.assertFalse(changes.filter(citizen=self.citizen4).exists()) self.assertEqual(Changeset.STATUS_PARTIALLY_SUCCESSFUL, rollback.status)
def give_approve_permission(user): """Give user approve permission, and return updated object""" user.user_permissions.add(get_permission_object_by_name(APPROVE_CHANGESET_PERMISSION)) return refresh_model(user)