def visualizar(request, id_vistoria): id = id_vistoria obj = get_object_or_404(VistoriaModel, pk=id) form = VistoriaForm(instance=obj) form.read_only() context = {'form': form, 'vistoria': obj} return render(request, "vistorias_visualizar.html", context)
class Test_VistoriaForm_Test(TestCase): def setUp(self): self.empresa = EmpresaModel( ref_cad="12.5.12.01.001", name="INDUSTRIA STARK LTDA", atividade="ATIVIDADE MILITAR", endereco="RUA SHIELD, 199", quadra="10", lote="2", email="*****@*****.**", phone="055-19-3541-0000", responsavel="Antony Stark", observacao="", ) self.empresa.save() self.mocked_img = SimpleUploadedFile('tiny.gif', TINY_GIF) self.vistoria = { 'observacao':"aaa", 'empresa':self.empresa.id } self.imagem = { 'imagem':self.mocked_img } self.form = VistoriaForm(self.vistoria, self.imagem) self.form.is_valid() self.form.save() def test_valid_form(self): self.assertEqual(True, self.form.is_valid()) def test_create(self): self.assertTrue(EmpresaModel.objects.exists()) self.assertTrue(VistoriaModel.objects.exists()) def test_saved_image(self): self.assertEqual(self.mocked_img, self.form.cleaned_data['imagem']) def test_observacao_upper(self): self.assertEqual('AAA', self.form.cleaned_data['observacao']) def test_method_read_only_true(self): self.form.read_only() fields = list(self.form.fields) for field in fields: with self.subTest(): self.assertEqual(self.form.fields[field].widget.attrs['readonly'], True) def test_method_read_only_false(self): self.form.read_only(False) fields = list(self.form.fields) for field in fields: with self.subTest(): self.assertEqual(self.form.fields[field].widget.attrs['readonly'], False)