Browse Source

Use admin user and auto follow as defaults for WebTest app fixture

and refactor test accordingly
producao
Marcio Mazza 10 years ago
parent
commit
5f43a92e85
  1. 26
      conftest.py
  2. 14
      sigi/apps/parlamentares/test_parlamentares.py

26
conftest.py

@ -3,8 +3,30 @@ import pytest
from django_webtest import DjangoTestApp, WebTestMixin from django_webtest import DjangoTestApp, WebTestMixin
DEFAULT_MARK = object()
class SigiTestApp(DjangoTestApp):
def __init__(self, extra_environ=None, relative_to=None, default_user=None):
super(SigiTestApp, 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(SigiTestApp, self).get(url, params, headers, extra_environ,
status, expect_errors, user, auto_follow,
content_type, **kwargs)
@pytest.fixture(scope='function') @pytest.fixture(scope='function')
def app(request): def app(request, admin_user):
"""WebTest's TestApp. """WebTest's TestApp.
Patch and unpatch settings before and after each test. Patch and unpatch settings before and after each test.
@ -17,4 +39,4 @@ def app(request):
wtm = WebTestMixin() wtm = WebTestMixin()
wtm._patch_settings() wtm._patch_settings()
request.addfinalizer(wtm._unpatch_settings) request.addfinalizer(wtm._unpatch_settings)
return DjangoTestApp() return SigiTestApp(default_user=admin_user.username)

14
sigi/apps/parlamentares/test_parlamentares.py

@ -17,16 +17,16 @@ def some_parlamentarians():
return a, b, c return a, b, c
def test_list_all(some_parlamentarians, admin_user, app): def test_list_all(some_parlamentarians, app):
response = app.get('/parlamentares/parlamentar/', auto_follow=True, user=admin_user.username) response = app.get('/parlamentares/parlamentar/')
assert response.status_code == 200 assert response.status_code == 200
for x in some_parlamentarians: for x in some_parlamentarians:
assert x.nome_completo in response.content assert x.nome_completo in response.content
def test_list_filtered_by_capital_letter(some_parlamentarians, admin_client): def test_list_filtered_by_capital_letter(some_parlamentarians, app):
response = admin_client.get('/parlamentares/parlamentar/?nome_completo=B', follow=True) response = app.get('/parlamentares/parlamentar/?nome_completo=B')
assert response.status_code == 200 assert response.status_code == 200
decoded_content = response.content.decode('utf-8') decoded_content = response.content.decode('utf-8')
@ -36,8 +36,8 @@ def test_list_filtered_by_capital_letter(some_parlamentarians, admin_client):
assert c.nome_completo not in decoded_content assert c.nome_completo not in decoded_content
def test_add_to_cart(some_parlamentarians, admin_user, app): def test_add_to_cart(some_parlamentarians, app):
res = app.get('/parlamentares/parlamentar/', auto_follow=True, user=admin_user.username) res = app.get('/parlamentares/parlamentar/')
assert res.status_code == 200 assert res.status_code == 200
a, b, c = some_parlamentarians a, b, c = some_parlamentarians
@ -47,7 +47,7 @@ def test_add_to_cart(some_parlamentarians, admin_user, app):
form['action'] = 'adiciona_parlamentar' form['action'] = 'adiciona_parlamentar'
res = form.submit() res = form.submit()
"2 Parlamentares adicionados" in res.content "2 Parlamentares adicionados" in res.content
res = app.get('/parlamentares/parlamentar/carrinho/', auto_follow=True, user=admin_user.username) res = app.get('/parlamentares/parlamentar/carrinho/')
def right_people_present(content): def right_people_present(content):
assert a.nome_completo in content assert a.nome_completo in content

Loading…
Cancel
Save