tests: wip add tests with bijoe

This commit is contained in:
Benjamin Dauvergne 2019-07-06 14:18:52 +02:00
parent 8f82ed5a18
commit d7b0f4e09e
5 changed files with 115 additions and 0 deletions

1
tests/bijoe_settings.py Normal file
View File

@ -0,0 +1 @@
BIJOE_CACHE = False

View File

@ -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

8
tests/test_bijoe.py Normal file
View File

@ -0,0 +1,8 @@
from utils import login
def test_bijoe(bijoe, admin):
response = login(bijoe, admin, '/')
import pdb
pdb.set_trace()

View File

@ -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

View File

@ -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