Browse Source

Test crud flux from list to create to detail

pull/6/head
Marcio Mazza 10 years ago
parent
commit
cc47c68240
  1. 43
      conftest.py
  2. 6
      sapl/crud.py
  3. 52
      sapl/test_crud.py
  4. 6
      templates/comissoes/comissao_list.html

43
conftest.py

@ -0,0 +1,43 @@
import pytest
from django_webtest import DjangoTestApp, WebTestMixin
DEFAULT_MARK = object()
class SaplTestApp(DjangoTestApp):
def __init__(self, extra_environ=None, relative_to=None,
default_user=None):
super(SaplTestApp, self).__init__(extra_environ, relative_to)
self.default_user = default_user
def get(self, url, params=None, headers=None, extra_environ=None,
status=None, expect_errors=False, user=DEFAULT_MARK,
auto_follow=True, content_type=None, **kwargs):
# note we altered the default values for user and auto_follow
if user is DEFAULT_MARK: # a trick to allow explicit user=None
user = self.default_user
return super(SaplTestApp, self).get(
url, params, headers, extra_environ, status, expect_errors, user,
auto_follow, content_type, **kwargs)
@pytest.fixture(scope='function')
def app(request, admin_user):
"""WebTest's TestApp.
Patch and unpatch settings before and after each test.
WebTestMixin, when used in a unittest.TestCase, automatically calls
_patch_settings() and _unpatchsettings.
source: https://gist.github.com/magopian/6673250
"""
wtm = WebTestMixin()
wtm._patch_settings()
request.addfinalizer(wtm._unpatch_settings)
# XXX change this admin user to "saploper"
return SaplTestApp(default_user=admin_user.username)

6
sapl/crud.py

@ -33,6 +33,9 @@ class Crud(object):
create_url = reverse_lazy(in_namespace('create')) create_url = reverse_lazy(in_namespace('create'))
help_url = '/comissoes/ajuda' # FIXME help_url = '/comissoes/ajuda' # FIXME
create_title = _('Adicionar %(model_name)s') % {
'model_name': self.model._meta.verbose_name}
def get_url_for_this_object(self, url_name): def get_url_for_this_object(self, url_name):
return reverse(in_namespace(url_name), args=(self.object.id,)) return reverse(in_namespace(url_name), args=(self.object.id,))
@ -55,8 +58,7 @@ class Crud(object):
class CrudCreateView(BaseMixin, FormMessagesMixin, CreateView): class CrudCreateView(BaseMixin, FormMessagesMixin, CreateView):
model = self.model model = self.model
form_class = model_form form_class = model_form
title = _('Adicionar %(model_name)s') % { title = BaseMixin.create_title
'model_name': self.model._meta.verbose_name}
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.'))

52
sapl/test_crud.py

@ -1,4 +1,9 @@
from django.core.urlresolvers import reverse from django.core.urlresolvers import reverse
from model_mommy import mommy
from comissoes.models import Comissao, TipoComissao
# XXX These tests are based on comissoes app
# but could be done with a stub one
def test_reverse(): def test_reverse():
@ -7,3 +12,50 @@ def test_reverse():
assert '/comissoes/2' == reverse('comissao:detail', args=(2,)) assert '/comissoes/2' == reverse('comissao:detail', args=(2,))
assert '/comissoes/2/edit' == reverse('comissao:update', args=(2,)) assert '/comissoes/2/edit' == reverse('comissao:update', args=(2,))
assert '/comissoes/2/delete' == reverse('comissao:delete', args=(2,)) assert '/comissoes/2/delete' == reverse('comissao:delete', args=(2,))
def assert_h1(res, title):
assert res.html.find('h1').text == title
def test_flux_list_create_detail(app):
# to have a couple an option for tipo field
stub_tipo = mommy.make(TipoComissao)
res = app.get('/comissoes/')
print(res.url)
# on list page
assert_h1(res, 'Comissões')
res = res.click('Adicionar Comissão')
print(res.url)
# on create page
assert_h1(res, 'Adicionar Comissão')
form = res.form
assert not any(
form[k].value for k in form.fields if k != 'csrfmiddlewaretoken')
# some fields are required => validation error
res = res.form.submit()
print(res.url)
'Formulário inválido. O registro não foi criado.' in res
# 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_h1(res, stub_name)

6
templates/comissoes/comissao_list.html

@ -2,7 +2,11 @@
{% load i18n %} {% load i18n %}
{% block base_content %} {% block base_content %}
<a href="/admin/comissoes/comissao/add/">Incluir Comissão</a>
{# FIXME is this the best markup to use? #}
<dl class="sub-nav">
<dd><a href="{{ view.create_url }}">{{ view.create_title }}</a></dd>
</dl>
<table class="table table-hover"> <table class="table table-hover">
<thead> <thead>

Loading…
Cancel
Save