From 02d88066a1a4910e409293aaff17a004d24bcb2d Mon Sep 17 00:00:00 2001 From: Marcio Mazza Date: Wed, 22 Jul 2015 20:42:56 -0300 Subject: [PATCH] Add and test cancel actions to crud views --- sapl/crud.py | 4 ++ sapl/layout.py | 29 +++++-------- sapl/test_crud.py | 108 +++++++++++++++++++++++++++++----------------- 3 files changed, 82 insertions(+), 59 deletions(-) diff --git a/sapl/crud.py b/sapl/crud.py index 1c41dd597..0a155e945 100644 --- a/sapl/crud.py +++ b/sapl/crud.py @@ -73,6 +73,7 @@ def build_crud(model, *layout): form_valid_message = _('Registro criado com sucesso!') form_invalid_message = make_form_invalid_message( _('O registro não foi criado.')) + cancel_url = BaseMixin.list_url def get_success_url(self): return self.detail_url @@ -96,6 +97,9 @@ def build_crud(model, *layout): def get_success_url(self): return self.detail_url + def cancel_url(self): + return self.detail_url + class CrudDeleteView(BaseMixin, FormMessagesMixin, DeleteView): form_valid_message = _('Registro excluído com sucesso!') form_invalid_message = make_form_invalid_message( diff --git a/sapl/layout.py b/sapl/layout.py index deb3842b7..78b2c4afa 100644 --- a/sapl/layout.py +++ b/sapl/layout.py @@ -1,5 +1,5 @@ from crispy_forms_foundation.layout import ( - Div, Layout, Fieldset, Row, Column, Button, Submit) + Div, Layout, Fieldset, Row, Column, Button, Submit, HTML) from django.utils.translation import ugettext as _ @@ -29,22 +29,13 @@ def to_fieldsets(fields): class SaplFormLayout(Layout): def __init__(self, *fields): - _fields = list(to_fieldsets(fields)) + buttons_crispies() + buttons = Div( + HTML('%s' % _('Cancelar')), + Submit('submit', _('Enviar'), + css_class='button radius success right'), + css_class='radius clearfix' + ) + _fields = list(to_fieldsets(fields)) + \ + [Row(Column(buttons, css_class='clearfix'))] super(SaplFormLayout, self).__init__(*_fields) - - -def buttons_crispies(): - return [ - Row( - Column( - Div( - Submit('submit', _('Enviar'), - css_class='button radius success right'), - Button('cancel', _('Cancelar'), - css_class='button radius alert left'), - css_class='radius clearfix' - ), - css_class='clearfix' - ), - ), - ] diff --git a/sapl/test_crud.py b/sapl/test_crud.py index 57a48e18e..17e1d4d8c 100644 --- a/sapl/test_crud.py +++ b/sapl/test_crud.py @@ -21,6 +21,13 @@ def assert_h1(res, title): assert res.html.find('h1').text == title +def assert_on_list_page(res): + assert_h1(res, 'Comissões') + assert 'Adicionar Comissão' in res + assert res.html.find('table') + # XXX ... characterize better + + def assert_on_create_page(res): assert_h1(res, 'Adicionar Comissão') form = res.form @@ -35,8 +42,9 @@ def assert_on_detail_page(res, stub_name): assert 'Excluir Comissão' in res -@pytest.mark.parametrize("make_invalid_submit", [True, False]) -def test_flux_list_create_detail(app, make_invalid_submit): +@pytest.mark.parametrize("cancel, make_invalid_submit", [ + (a, b) for a in (True, False) for b in (True, False)]) +def test_flux_list_create_detail(app, cancel, make_invalid_submit): # to have a couple an option for tipo field stub_tipo = mommy.make(TipoComissao) @@ -44,7 +52,8 @@ def test_flux_list_create_detail(app, make_invalid_submit): res = app.get('/comissoes/') # on list page - assert_h1(res, 'Comissões') + assert_on_list_page(res) + res = res.click('Adicionar Comissão') previous_objects = set(Comissao.objects.all()) @@ -52,32 +61,41 @@ def test_flux_list_create_detail(app, make_invalid_submit): assert_on_create_page(res) # test bifurcation ! - if make_invalid_submit: - # some fields are required => validation error - res = res.form.submit() - 'Formulário inválido. O registro não foi criado.' in res - assert_on_create_page(res) + if cancel: + res = res.click('Cancelar') + # back to list page + assert_on_list_page(res) + # db has not changed assert previous_objects == set(Comissao.objects.all()) + else: + # and a test detour ! + if make_invalid_submit: + # some fields are required => validation error + res = res.form.submit() + 'Formulário inválido. O registro não foi criado.' in res + assert_on_create_page(res) + # db has not changed + assert previous_objects == set(Comissao.objects.all()) + + # now fill out some fields + form = res.form + stub_name = '### Nome Especial ###' + form['nome'] = stub_name + form['sigla'] = 'SIGLA' + form['tipo'] = stub_tipo.id + form['data_criacao'] = '1/1/2001' + res = form.submit() + + # on redirect to detail page + created = Comissao.objects.get(nome=stub_name) + assert res.url.endswith('/comissoes/%s' % created.id) + res = res.follow() - # now fill out some fields - form = res.form - stub_name = '### Nome Especial ###' - form['nome'] = stub_name - form['sigla'] = 'SIGLA' - form['tipo'] = stub_tipo.id - form['data_criacao'] = '1/1/2001' - res = form.submit() - - # on redirect to detail page - created = Comissao.objects.get(nome=stub_name) - assert res.url.endswith('/comissoes/%s' % created.id) - res = res.follow() - - # on detail page - assert 'Registro criado com sucesso!' in res - assert_on_detail_page(res, stub_name) - [new_obj] = list(set(Comissao.objects.all()) - previous_objects) - assert new_obj.nome == stub_name + # on detail page + assert_on_detail_page(res, stub_name) + assert 'Registro criado com sucesso!' in res + [new_obj] = list(set(Comissao.objects.all()) - previous_objects) + assert new_obj.nome == stub_name def get_detail_page(app): @@ -88,25 +106,35 @@ def get_detail_page(app): return stub, res -def test_flux_detail_update_detail(app): +@pytest.mark.parametrize("cancel", [True, False]) +def test_flux_detail_update_detail(app, cancel): stub, res = get_detail_page(app) res = res.click('Editar Comissão') # on update page assert_h1(res, stub.nome) - form = res.form - new_name = '### New Name ###' - form['nome'] = new_name - res = form.submit() - # on redirect to detail page - assert res.url.endswith('/comissoes/%s' % stub.id) - res = res.follow() + # test bifurcation ! + if cancel: + res = res.click('Cancelar') + + # back to detail page + assert_on_detail_page(res, stub.nome) + assert Comissao.objects.get(pk=stub.pk).nome == stub.nome + else: + form = res.form + new_name = '### New Name ###' + form['nome'] = new_name + res = form.submit() + + # on redirect to detail page + assert res.url.endswith('/comissoes/%s' % stub.id) + res = res.follow() - # back to detail page - assert 'Registro alterado com sucesso!' in res - assert_h1(res, new_name) - assert Comissao.objects.get(pk=stub.pk).nome == new_name + # back to detail page + assert_on_detail_page(res, new_name) + assert 'Registro alterado com sucesso!' in res + assert Comissao.objects.get(pk=stub.pk).nome == new_name @pytest.mark.parametrize("cancel", [True, False]) @@ -123,7 +151,7 @@ def test_flux_detail_delete_list(app, cancel): res = res.click('Cancelar') # back to detail page - assert_h1(res, stub.nome) + assert_on_detail_page(res, stub.nome) assert Comissao.objects.filter(pk=stub.pk) else: res = res.form.submit()