def test_formset_can_validate(self): "Just a spike test to learn about formset validation" post_data, file_data = self.create_formset_data() formset = DocumentFormSet(post_data, file_data) self.assertTrue(formset.is_valid())
def test_formset_save_return_new_documents_pks(self): user = UserFactory() post_data, file_data = self.create_formset_data() formset = DocumentFormSet(post_data, file_data) return_value = formset.save(user) expected_return_value = [i for i in range(1, len(file_data)+1)] self.assertEqual(return_value, expected_return_value)
def test_formset_save_instantiate_new_objects(self): user = UserFactory() # assert no objs exist self.assertEqual(Document.objects.count(), 0) post_data, file_data = self.create_formset_data() formset = DocumentFormSet(post_data, file_data) formset.save(user) # assert expected objs now exist expected_num_documents = len(file_data) self.assertEqual(Document.objects.count(), expected_num_documents)
def audit_page(request, audit_pk): audit = Audit.objects.get(id=audit_pk) if request.method == "POST": formset = DocumentFormSet(request.POST, request.FILES) if formset.is_valid(): new_docs_pks = formset.save(request.user) job = Job.objects.create(audit=audit, user=request.user) job.documents.add(*new_docs_pks) return redirect(job) if request.method == "GET": formset = DocumentFormSet(audit_pk=audit_pk) return render(request, "audit.html", {"audit": audit, "formset": formset})
def test_formset_save_wont_create_new_objects_if_form_is_invalid( self, mock_form_method ): mock_form_method.return_value = False user = UserFactory() # assert no objs exist self.assertEqual(Document.objects.count(), 0) post_data, file_data = self.create_formset_data() formset = DocumentFormSet(post_data, file_data) with self.assertRaises(RuntimeError): formset.save(user.pk) self.assertEqual(Document.objects.count(), 0)