From d7b0f4e09ee5e17d01cdd434cada705dc213821c Mon Sep 17 00:00:00 2001 From: Benjamin Dauvergne Date: Sat, 6 Jul 2019 14:18:52 +0200 Subject: [PATCH 7/7] tests: wip add tests with bijoe --- tests/bijoe_settings.py | 1 + tests/conftest.py | 28 ++++++++++++++++ tests/test_bijoe.py | 8 +++++ tests/utils.py | 71 +++++++++++++++++++++++++++++++++++++++++ tox.ini | 7 ++++ 5 files changed, 115 insertions(+) create mode 100644 tests/bijoe_settings.py create mode 100644 tests/test_bijoe.py diff --git a/tests/bijoe_settings.py b/tests/bijoe_settings.py new file mode 100644 index 0000000..5e7ac02 --- /dev/null +++ b/tests/bijoe_settings.py @@ -0,0 +1 @@ +BIJOE_CACHE = False diff --git a/tests/conftest.py b/tests/conftest.py index 1b8fd70..389603a 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -11,6 +11,7 @@ from collections import namedtuple import psycopg2 import pytest +import django_webtest import utils @@ -250,3 +251,30 @@ schema = olap sys.argv = old_argv f.model_dir = model_dir return f + + +@pytest.fixture +def admin(db): + from django.contrib.auth.models import User + + u = User(username='admin', first_name='A', last_name='Dmin', + email='super.user@example.net') + u.set_password('admin') + u.is_superuser = True + u.is_staff = True + u.save() + return u + + +@pytest.fixture +def bijoe(db, settings, olap_cmd): + olap_cmd() + settings.BIJOE_SCHEMAS = [str(olap_cmd.model_dir) + '/*.model'] + settings.TEMPLATE_DEBUG = True + settings.DEBUG = True + wtm = django_webtest.WebTestMixin() + try: + wtm._patch_settings() + yield django_webtest.DjangoTestApp() + finally: + wtm._unpatch_settings diff --git a/tests/test_bijoe.py b/tests/test_bijoe.py new file mode 100644 index 0000000..539dd51 --- /dev/null +++ b/tests/test_bijoe.py @@ -0,0 +1,8 @@ + +from utils import login + + +def test_bijoe(bijoe, admin): + response = login(bijoe, admin, '/') + import pdb + pdb.set_trace() diff --git a/tests/utils.py b/tests/utils.py index 373eda5..0a58391 100644 --- a/tests/utils.py +++ b/tests/utils.py @@ -15,3 +15,74 @@ def run_wcs_script(wcs_dir, script, script_name): subprocess.check_call( [WCSCTL, 'runscript', '--app-dir', str(wcs_dir), '--vhost', HOSTNAME, str(script_path)]) + +import io +import zipfile +import xml.etree.ElementTree as ET + +from django.conf import settings + + +def login(app, user, path=None, password=None): + if path: + login_page = app.get(path) + else: + login_page = app.get(settings.LOGIN_URL) + login_page = login_page.maybe_follow() + form = login_page.form + form.set('username', user.username if hasattr(user, 'username') else user) + # password is supposed to be the same as username + form.set('password', password or user.username) + response = form.submit(name='login-password-submit').follow(expect_errors=True) + if path: + assert response.request.path == path + assert '_auth_user_id' in app.session + assert str(app.session['_auth_user_id']) == str(user.id) + return response + + +def get_table(response): + table = [] + + for tr in response.pyquery('table tr'): + row = [] + table.append(row) + for td in tr.findall('td'): + row.append((td.text or '').strip()) + return table + + +def xml_node_text_content(node): + '''Extract text content from node and all its children. Equivalent to + xmlNodeGetContent from libxml.''' + + if node is None: + return '' + + def helper(node): + s = [] + if node.text: + s.append(node.text) + for child in node: + s.extend(helper(child)) + if child.tail: + s.append(child.tail) + return s + return u''.join(helper(node)) + + +def get_ods_document(response): + return ET.fromstring(zipfile.ZipFile(io.BytesIO(response.content)).read('content.xml')) + + +def get_ods_table(response): + from bijoe.visualization.ods import TABLE_NS + + root = get_ods_document(response) + table = [] + for row_node in root.findall('.//{%s}table-row' % TABLE_NS): + row = [] + table.append(row) + for cell_node in row_node.findall('.//{%s}table-cell' % TABLE_NS): + row.append(xml_node_text_content(cell_node)) + return table diff --git a/tox.ini b/tox.ini index f035b6f..7128052 100644 --- a/tox.ini +++ b/tox.ini @@ -16,11 +16,17 @@ setenv = PGHOST={env:PGHOST:} PGUSER={env:PGUSER:} PGPASSWORD={env:PGPASSWORD:} + DJANGO_SETTINGS_MODULE=bijoe.settings + BIJOE_SETTINGS_FILE=tests/bijoe_settings.py deps = coverage pytest pytest-cov pytest-random + pytest-django + django-webtest + WebTest + pyquery quixote<3.0 psycopg2-binary vobject @@ -28,6 +34,7 @@ deps = django-ratelimit<3 gadjo mock + git+https://git.entrouvert.org/bijoe.git@wip/29914-Pouvoir-declarer-des-jointures-n django>=1.11,<1.12 commands = ./get_wcs.sh -- 2.23.0