def test_post_valid_form(self, mock_get_object, mock_object, mock_form, mock_view): """ BaseUpdateView: def post(self, request, *args, **kwargs): self.object = self.get_object() return super().post(request, *args, **kwargs) ProcessFormView: def post(self, request, *args, **kwargs): "Handle POST requests: instantiate a form instance with the passed POST variables and then check if it's valid." form = self.get_form() if form.is_valid(): return self.form_valid(form) else: return self.form_invalid(form) """ mock_get_object.return_value = mock_object mock_form.is_valid.return_value = True mock_view.get_form.return_value = mock_form mock_view.form_valid.return_value = True request = RequestFactory().get('/fake-path') view = setup_viewTest(self.view, request) response = view.post(request) self.assertTrue(response)
def test_post_valid_form(self, mock_legal_form, mock_view): """ BaseCreateView: def post(self, request, *args, **kwargs): self.object = None return super().post(request, *args, **kwargs) ProcessFormView: def post(self, request, *args, **kwargs): form = self.get_form() if form.is_valid(): return self.form_valid(form) else: return self.form_invalid(form) """ mock_legal_form.is_valid.return_value = True mock_view.get_form.return_value = mock_legal_form mock_view.form_valid.return_value = True request = RequestFactory().get('/fake-path') view = setup_viewTest(self.view, request) response = view.post(request) self.assertTrue(response)
def test_get(self, mock_legal_form, mock_get_form): mock_get_form.return_value = mock_legal_form request = RequestFactory().get('/fake-path') view = setup_viewTest(self.view, request) response = view.get(request) self.assertDictContainsSubset({'form': mock_legal_form}, response.context_data)
def test_save_client_form(self): """ Test that client passed to form kwargs. """ request = RequestFactory().get('/fake-path') view = setup_viewTest(views.client_create, request) self.assertIsInstance(request.context['form'], NoteForm) import pdb pdb.set_trace()
def test_get_multi_forms(self, mock_forms, mock_get_form): mock_get_form.return_value = mock_forms view = ClientReferralCreateView() request = RequestFactory().get('/fake-path') view = setup_viewTest(view, request) response = view.get(request) self.assertDictContainsSubset({'form': mock_forms}, response.context_data)
def test_get_forms(self, mock_client_form, mock_referral_form, mock_note_form, mock_decision_form): """ Tests that all forms are added to context dictionary when get_context_data called after view receives GET request. """ request = RequestFactory().get('/fake-path') view = setup_viewTest(self.view, request) response = view.get(request) mock_client_form.assert_called_once() mock_referral_form.assert_called_once() mock_note_form.assert_called_once() mock_decision_form.assert_called_once()
def test_get(self, mock_render): """ Test that GET request creates and renders ReferralFilter Object in context. """ # referral_filter = ReferralFilter( # request.GET) # return self.render_to_response({'filter': referral_filter}) request = RequestFactory().get('/fake-path') view = setup_viewTest(self.view, request) response = view.get(request) # mock_filter.assert_called_once() mock_render.assert_called_once()
def test_post_invalid_form_not_saved(self, mock_form, mock_response, mock_message): """ Test whether form instance is saved depending on form validation. _get_form is used after POST request received. Bound Form """ mock_form.return_value.is_bound = False mock_form.return_value.is_valid.return_value = False mock_client = MagicMock() mock_client.client_id = 11111 mock_form.return_value.save.return_value = mock_client request = RequestFactory().post('/fake-path') view = setup_viewTest(self.view, request) view.post(request) mock_form.return_value.save.assert_not_called()
def test_get(self, mock_client, mock_get_object): """ BasUpdateView: def get(self, request, *args, **kwargs): self.object = self.get_object() return super().get(request, *args, **kwargs) ProcessFormView: def get(self, request, *args, **kwargs): "Handle GET requests: instantiate a blank version of the form." return self.render_to_response(self.get_context_data()) """ mock_get_object.return_value = mock_client request = RequestFactory().get('/fake-path') view = setup_viewTest(self.view, request) response = view.get(request) self.assertDictContainsSubset({'object': mock_client}, response.context_data)
def test_post_valid_form_saved(self, mock_form, mock_response, mock_message): """ Tests that valid bound forms are saved """ mock_form.return_value.is_bound = True mock_form.return_value.is_valid.return_value = True mock_client = MagicMock() mock_client.client_id = 11111 mock_form.return_value.save.return_value = mock_client request = RequestFactory().post('/fake-path') view = setup_viewTest(self.view, request) view.post(request) self.assertEqual(mock_form.call_count, 4) mock_form.return_value.is_valid.assert_called_once() mock_form.return_value.save.assert_called_once() mock_response.assert_called_once()
def test_get_form_kwargs(self, mock_get_object, mock_model): """ Test that instances are in context """ mock_get_object.return_value = mock_model mock_model.decisions.return_value = [ 'decision_0', 'decision_1', 'decision_2' ] request = RequestFactory().get('/fake-path') view = setup_viewTest(self.view, request) response = view.get(request) objects = response.context_data['form'].instances mock_objects = { 'referral': mock_model, 'pre_decision': mock_model.decisions[0], 'da_decision': mock_model.decisions[1], 'dc_decision': mock_model.decisions[2], } self.assertEqual(mock_objects, objects)