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_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_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_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_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
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_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)