Browse Source

Add and test cancel actions to crud views

pull/6/head
Marcio Mazza 10 years ago
parent
commit
02d88066a1
  1. 4
      sapl/crud.py
  2. 29
      sapl/layout.py
  3. 108
      sapl/test_crud.py

4
sapl/crud.py

@ -73,6 +73,7 @@ def build_crud(model, *layout):
form_valid_message = _('Registro criado com sucesso!') form_valid_message = _('Registro criado com sucesso!')
form_invalid_message = make_form_invalid_message( form_invalid_message = make_form_invalid_message(
_('O registro não foi criado.')) _('O registro não foi criado.'))
cancel_url = BaseMixin.list_url
def get_success_url(self): def get_success_url(self):
return self.detail_url return self.detail_url
@ -96,6 +97,9 @@ def build_crud(model, *layout):
def get_success_url(self): def get_success_url(self):
return self.detail_url return self.detail_url
def cancel_url(self):
return self.detail_url
class CrudDeleteView(BaseMixin, FormMessagesMixin, DeleteView): class CrudDeleteView(BaseMixin, FormMessagesMixin, DeleteView):
form_valid_message = _('Registro excluído com sucesso!') form_valid_message = _('Registro excluído com sucesso!')
form_invalid_message = make_form_invalid_message( form_invalid_message = make_form_invalid_message(

29
sapl/layout.py

@ -1,5 +1,5 @@
from crispy_forms_foundation.layout import ( 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 _ from django.utils.translation import ugettext as _
@ -29,22 +29,13 @@ def to_fieldsets(fields):
class SaplFormLayout(Layout): class SaplFormLayout(Layout):
def __init__(self, *fields): def __init__(self, *fields):
_fields = list(to_fieldsets(fields)) + buttons_crispies() buttons = Div(
HTML('<a href="{{ view.cancel_url }}"'
' class="button radius alert">%s</a>' % _('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) 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'
),
),
]

108
sapl/test_crud.py

@ -21,6 +21,13 @@ def assert_h1(res, title):
assert res.html.find('h1').text == 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): def assert_on_create_page(res):
assert_h1(res, 'Adicionar Comissão') assert_h1(res, 'Adicionar Comissão')
form = res.form form = res.form
@ -35,8 +42,9 @@ def assert_on_detail_page(res, stub_name):
assert 'Excluir Comissão' in res assert 'Excluir Comissão' in res
@pytest.mark.parametrize("make_invalid_submit", [True, False]) @pytest.mark.parametrize("cancel, make_invalid_submit", [
def test_flux_list_create_detail(app, 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 # to have a couple an option for tipo field
stub_tipo = mommy.make(TipoComissao) stub_tipo = mommy.make(TipoComissao)
@ -44,7 +52,8 @@ def test_flux_list_create_detail(app, make_invalid_submit):
res = app.get('/comissoes/') res = app.get('/comissoes/')
# on list page # on list page
assert_h1(res, 'Comissões') assert_on_list_page(res)
res = res.click('Adicionar Comissão') res = res.click('Adicionar Comissão')
previous_objects = set(Comissao.objects.all()) 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) assert_on_create_page(res)
# test bifurcation ! # test bifurcation !
if make_invalid_submit: if cancel:
# some fields are required => validation error res = res.click('Cancelar')
res = res.form.submit() # back to list page
'Formulário inválido. O registro não foi criado.' in res assert_on_list_page(res)
assert_on_create_page(res) # db has not changed
assert previous_objects == set(Comissao.objects.all()) 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 # on detail page
form = res.form assert_on_detail_page(res, stub_name)
stub_name = '### Nome Especial ###' assert 'Registro criado com sucesso!' in res
form['nome'] = stub_name [new_obj] = list(set(Comissao.objects.all()) - previous_objects)
form['sigla'] = 'SIGLA' assert new_obj.nome == stub_name
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
def get_detail_page(app): def get_detail_page(app):
@ -88,25 +106,35 @@ def get_detail_page(app):
return stub, res 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) stub, res = get_detail_page(app)
res = res.click('Editar Comissão') res = res.click('Editar Comissão')
# on update page # on update page
assert_h1(res, stub.nome) assert_h1(res, stub.nome)
form = res.form
new_name = '### New Name ###'
form['nome'] = new_name
res = form.submit()
# on redirect to detail page # test bifurcation !
assert res.url.endswith('/comissoes/%s' % stub.id) if cancel:
res = res.follow() 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 # back to detail page
assert 'Registro alterado com sucesso!' in res assert_on_detail_page(res, new_name)
assert_h1(res, new_name) assert 'Registro alterado com sucesso!' in res
assert Comissao.objects.get(pk=stub.pk).nome == new_name assert Comissao.objects.get(pk=stub.pk).nome == new_name
@pytest.mark.parametrize("cancel", [True, False]) @pytest.mark.parametrize("cancel", [True, False])
@ -123,7 +151,7 @@ def test_flux_detail_delete_list(app, cancel):
res = res.click('Cancelar') res = res.click('Cancelar')
# back to detail page # back to detail page
assert_h1(res, stub.nome) assert_on_detail_page(res, stub.nome)
assert Comissao.objects.filter(pk=stub.pk) assert Comissao.objects.filter(pk=stub.pk)
else: else:
res = res.form.submit() res = res.form.submit()

Loading…
Cancel
Save