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. 25
      sapl/layout.py
  3. 42
      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_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(

25
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()
super(SaplFormLayout, self).__init__(*_fields)
def buttons_crispies():
return [
Row(
Column(
Div(
buttons = Div(
HTML('<a href="{{ view.cancel_url }}"'
' class="button radius alert">%s</a>' % _('Cancelar')),
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'
),
),
]
)
_fields = list(to_fieldsets(fields)) + \
[Row(Column(buttons, css_class='clearfix'))]
super(SaplFormLayout, self).__init__(*_fields)

42
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,11 +61,20 @@ def test_flux_list_create_detail(app, make_invalid_submit):
assert_on_create_page(res)
# test bifurcation !
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
@ -74,8 +92,8 @@ def test_flux_list_create_detail(app, make_invalid_submit):
res = res.follow()
# on detail page
assert 'Registro criado com sucesso!' in res
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
@ -88,12 +106,22 @@ 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)
# 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
@ -104,8 +132,8 @@ def test_flux_detail_update_detail(app):
res = res.follow()
# back to detail page
assert_on_detail_page(res, new_name)
assert 'Registro alterado com sucesso!' in res
assert_h1(res, new_name)
assert Comissao.objects.get(pk=stub.pk).nome == new_name
@ -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()

Loading…
Cancel
Save