Esempio n. 1
0
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)
Esempio n. 2
0
def create(request):
    form = VistoriaForm(request.POST, request.FILES)
    if not form.is_valid():
        return render(request, 'vistorias_cadastro.html', {'form': form})
    else:
        VistoriaModel.objects.create(**form.cleaned_data)
        return HttpResponseRedirect(
            reverse('vistorias:vistorias_listar_todas'))