def test_add_to_principle_investigator_role_existing_different_user( self, remove_local_role_mock, add_local_role_mock, role_mock): ''' If the current user in the PI role is the same as the existing one then nothing should happen. ''' settings.PRINCIPLE_INVESTIGATOR_ROLE = 'Principle_Investigator' user1 = self.test_user user2 = User.objects.create_user('test2', '*****@*****.**', 'password2') a_role_mock = MagicMock(name='a_role') a_role_mock.get_local_users.return_value = [user2] role_mock.objects.get.return_value = a_role_mock test_application = EthicsApplication(title='test', principle_investigator=user1) test_application._add_to_principle_investigator_role() a_role_mock.get_local_users.assert_called_once_with(test_application) remove_local_role_mock.assert_called_once_with(test_application, user2, a_role_mock) add_local_role_mock.assert_called_once_with(test_application, user1, a_role_mock)
def test_get_active_applications_valid_user_with_applications(self): ''' If the user is valid and has applications this function should return a list of these applications objects ''' test_application_2 = EthicsApplication(title='test application', principle_investigator=self.test_user) test_application_2.save() self.assertEqual([self.ethics_application, test_application_2], EthicsApplication.objects.get_active_applications(self.test_user))
def test_with_applications_no_filter(self): ''' If the user is valid and has applications this function should return a list of these applications objects ''' test_application_2 = EthicsApplication(title='test application', principle_investigator=self.test_user) test_application_2.save() with patch('ethicsapplication.models.EthicsApplicationManager._filter_applications_on_state') as filter_mock: self.assertEqual([self.ethics_application, test_application_2], EthicsApplication.objects.get_applications_for_principle_investigator(self.test_user)) self.assertEqual(filter_mock.call_count, 0) #no filter was supplied so no filtering should be done
def test_get_current_state(self): ''' This function should return the name of the state that this application is currently in which is achieved by calling the get_state function from the workflows utils package ''' with patch('ethicsapplication.models.get_state') as get_state_mock: test_state_name = 'test_state' get_state_mock.return_value = test_state_name test_application = EthicsApplication() self.assertEqual(test_application.get_current_state(), test_state_name) get_state_mock.assert_called_once_with(test_application)
def test_add_to_workflow_setting_valid(self, set_workflow_mock): ''' If this is a new EthicsApplication object then it should be added to the workflow defined in the settings.APPLICATION_WORKFLOW setting. In this case the setting is present and the workflow has been configured in the db, so no exception should be run and a call should be made to the set_workflow function. ''' approval_workflow = Workflow.objects.get(name='Ethics_Application_Approval') settings.APPLICATION_WORKFLOW = 'Ethics_Application_Approval' test_application = EthicsApplication(title='test', principle_investigator=self.test_user) test_application.save() set_workflow_mock.assert_called_once_with(test_application, approval_workflow)
def test_invalid_EthicsApplication_creation_no_pi(self): ''' If you don't supply a principle investigator then you will get an exception ''' ethicsApplication = EthicsApplication(title='test application') self.assertRaises(IntegrityError, ethicsApplication.save)
def test_invalid_EthicsApplication_creation_no_title(self): ''' If you don't supply a title then you will get an exception ''' a_user = self.test_user ethicsApplication = EthicsApplication(principle_investigator=a_user) self.assertRaises(IntegrityError, ethicsApplication.save)
def test_application_created_signal_on_new(self): ''' When a new application is created the application_created signal should be dispatched, and this should send the application in a field called application ''' with mock_signal_receiver( application_created) as application_created_receiver: a_user = self.test_user my_application = EthicsApplication(title='test', principle_investigator=a_user) my_application.save() application_created_receiver.assert_called_once_with( application=my_application, signal=application_created, sender=my_application) #the signal should not be sent again on an edit: my_application.title = 'changed title' my_application.save() self.assertEqual( application_created_receiver.call_count, 1) # should only be one as we have not called it again
def test_add_to_workflow_setting_valid(self, set_workflow_mock): ''' If this is a new EthicsApplication object then it should be added to the workflow defined in the settings.APPLICATION_WORKFLOW setting. In this case the setting is present and the workflow has been configured in the db, so no exception should be run and a call should be made to the set_workflow function. ''' approval_workflow = Workflow.objects.get( name='Ethics_Application_Approval') settings.APPLICATION_WORKFLOW = 'Ethics_Application_Approval' test_application = EthicsApplication( title='test', principle_investigator=self.test_user) test_application.save() set_workflow_mock.assert_called_once_with(test_application, approval_workflow)
def test_valid_EthicsApplication_creation(self): ''' You should be able to create a valid EthicsApplication with: 1. Title (less than 255 characters) 2. Principle Investigator 3. No application The active flag should default to True ''' a_user = self.test_user ethicsApplication = EthicsApplication(title='test application', principle_investigator=a_user) ethicsApplication.save() self.assertTrue(ethicsApplication.title == 'test application') self.assertTrue(ethicsApplication.principle_investigator == a_user) self.assertTrue(ethicsApplication.application_form == None)
def test_with_applications_no_filter(self): ''' If the user is valid and has applications this function should return a list of these applications objects ''' test_application_2 = EthicsApplication( title='test application', principle_investigator=self.test_user) test_application_2.save() with patch( 'ethicsapplication.models.EthicsApplicationManager._filter_applications_on_state' ) as filter_mock: self.assertEqual([self.ethics_application, test_application_2], EthicsApplication.objects. get_applications_for_principle_investigator( self.test_user)) self.assertEqual( filter_mock.call_count, 0) #no filter was supplied so no filtering should be done
def test_add_to_workflow_setting_absent(self): ''' If this is a new EthicsApplication object then it should be added to the workflow defined in the settings.APPLICATION_WORKFLOW setting. In this case the setting is not present so an ImproperlyConfigured exception is raised ''' settings.APPLICATION_WORKFLOW = None test_application = EthicsApplication( title='test', principle_investigator=self.test_user) self.assertRaises(ImproperlyConfigured, test_application._add_to_workflow)
def test_add_to_principle_investigator_role_no_existing_user(self, remove_local_role_mock, add_local_role_mock, role_mock): ''' If the current user in the PI role is the same as the existing one then nothing should happen. ''' settings.PRINCIPLE_INVESTIGATOR_ROLE = 'Principle_Investigator' a_role_mock = MagicMock(name='a_role') a_role_mock.get_local_users.return_value = [] role_mock.objects.get.return_value = a_role_mock user1 = self.test_user test_application = EthicsApplication(title = 'test', principle_investigator=user1) test_application._add_to_principle_investigator_role() a_role_mock.get_local_users.assert_called_once_with(test_application) self.assertEqual(0, remove_local_role_mock.call_count) self.assertEqual([call(test_application, user1, a_role_mock)], add_local_role_mock.mock_calls)
def test_update_principle_investigator(self, function_mock): ''' When the save method is called, if the principle investigator property has been changed then _add_to_principle_investigator_role function should be called to update the membership of this group ''' test_application = EthicsApplication( title='test', principle_investigator=self.test_user) test_application.save() self.assertEqual(function_mock.call_count, 1) test_application.principle_investigator = User.objects.create_user( 'test2', '*****@*****.**', 'password2') test_application.save() self.assertEqual(function_mock.call_count, 2) test_application.active = False test_application.save() self.assertEqual(function_mock.call_count, 2)
def test_add_to_principle_investigator_role_existing_different_user(self, remove_local_role_mock, add_local_role_mock, role_mock): ''' If the current user in the PI role is the same as the existing one then nothing should happen. ''' settings.PRINCIPLE_INVESTIGATOR_ROLE = 'Principle_Investigator' user1 = self.test_user user2 = User.objects.create_user('test2', '*****@*****.**', 'password2') a_role_mock = MagicMock(name='a_role') a_role_mock.get_local_users.return_value = [user2] role_mock.objects.get.return_value = a_role_mock test_application = EthicsApplication(title = 'test', principle_investigator=user1) test_application._add_to_principle_investigator_role() a_role_mock.get_local_users.assert_called_once_with(test_application) remove_local_role_mock.assert_called_once_with(test_application, user2, a_role_mock) add_local_role_mock.assert_called_once_with(test_application, user1, a_role_mock)
def test_add_to_principle_investigator_role_existing_same_user( self, remove_local_role_mock, add_local_role_mock, role_mock): ''' If the current user in the PI role is the same as the existing one then nothing should happen. ''' settings.PRINCIPLE_INVESTIGATOR_ROLE = 'Principle_Investigator' user1 = self.test_user a_role_mock = MagicMock(name='a_role') a_role_mock.get_local_users.return_value = [user1] role_mock.objects.get.return_value = a_role_mock test_application = EthicsApplication(title='test', principle_investigator=user1) test_application._add_to_principle_investigator_role() a_role_mock.get_local_users.assert_called_once_with(test_application) self.assertEqual(remove_local_role_mock.call_count, 0) self.assertEqual(add_local_role_mock.call_count, 0)
def test_update_principle_investigator(self, function_mock): ''' When the save method is called, if the principle investigator property has been changed then _add_to_principle_investigator_role function should be called to update the membership of this group ''' test_application = EthicsApplication(title='test', principle_investigator=self.test_user) test_application.save() self.assertEqual(function_mock.call_count , 1) test_application.principle_investigator = User.objects.create_user('test2', '*****@*****.**', 'password2') test_application.save() self.assertEqual(function_mock.call_count , 2) test_application.active = False test_application.save() self.assertEqual(function_mock.call_count , 2)
def test_workflow_add_on_new_ethicsapplications(self, patched_function): ''' If this is a new EthicsApplication object then it should be added to the workflow using the _add_to_workflow function, if it is an existing entity then the _add_to_wokrflow function should not be called ''' #create a new ethicsapplication object and save it a_user = self.test_user my_Application = EthicsApplication(title='test', principle_investigator=a_user) my_Application.save() #assert that it was called once patched_function.assert_called_once_with() #make a change and save, the patch should not be called again! my_Application.title = 'changed' my_Application.save() patched_function.assert_called_once_with()
def test_application_created_signal_on_new(self): ''' When a new application is created the application_created signal should be dispatched, and this should send the application in a field called application ''' with mock_signal_receiver(application_created) as application_created_receiver: a_user = self.test_user my_application = EthicsApplication(title='test', principle_investigator=a_user) my_application.save() application_created_receiver.assert_called_once_with(application=my_application, signal=application_created, sender=my_application) #the signal should not be sent again on an edit: my_application.title = 'changed title' my_application.save() self.assertEqual(application_created_receiver.call_count, 1)# should only be one as we have not called it again