def test_result_save_correct_data_error(self): data = {'error': 'Test'} self.DataBaseObjectsNumber() form = ResultForm(data=data) self.assertTrue(form.is_valid()) r = form.save() self.assertIsInstance(r, Result) self.assertEqual(r.error, 'Test') self.DataBaseObjectsId(r) self.assertEqual(r.value, None)
def test_result_save_correct_data(self): data = {"value": 200} self.assertEqual(len(Result.objects.all()), 0) form = ResultForm(data=data) self.assertTrue(form.is_valid()) r = form.save() self.assertIsInstance(r, Result) self.assertEqual(r.value, 200) self.assertEqual(r.id, 1) self.assertEqual(r.error, None)
def results_list(request): if request.method == "POST": form = ResultForm(data=request.POST) if form.is_valid(): if form.cleaned_data['error'] == '': form.cleaned_data['error'] = None form.save() messages.add_message(request, messages.SUCCESS, "Utworzono nowy Result!!") else: messages.add_message(request, messages.ERROR, form.errors['__all__']) form = ResultForm() results = Result.objects.all() return render(request=request, template_name="maths/results.html", context={ "results": results, "form": form })
def test_result_save_correct_data_error_value(self): data = {'value': 200, 'error': 'Test'} self.DataBaseObjectsNumber() form = ResultForm(data=data) self.assertFalse(form.is_valid())