Projet

Général

Profil

0007-tests-wip-add-tests-with-bijoe.patch

Benjamin Dauvergne, 24 septembre 2019 15:28

Télécharger (4,99 ko)

Voir les différences:

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
tests/bijoe_settings.py
1
BIJOE_CACHE = False
tests/conftest.py
11 11

  
12 12
import psycopg2
13 13
import pytest
14
import django_webtest
14 15

  
15 16
import utils
16 17

  
......
250 251
            sys.argv = old_argv
251 252
    f.model_dir = model_dir
252 253
    return f
254

  
255

  
256
@pytest.fixture
257
def admin(db):
258
    from django.contrib.auth.models import User
259

  
260
    u = User(username='admin', first_name='A', last_name='Dmin',
261
             email='super.user@example.net')
262
    u.set_password('admin')
263
    u.is_superuser = True
264
    u.is_staff = True
265
    u.save()
266
    return u
267

  
268

  
269
@pytest.fixture
270
def bijoe(db, settings, olap_cmd):
271
    olap_cmd()
272
    settings.BIJOE_SCHEMAS = [str(olap_cmd.model_dir) + '/*.model']
273
    settings.TEMPLATE_DEBUG = True
274
    settings.DEBUG = True
275
    wtm = django_webtest.WebTestMixin()
276
    try:
277
        wtm._patch_settings()
278
        yield django_webtest.DjangoTestApp()
279
    finally:
280
        wtm._unpatch_settings
tests/test_bijoe.py
1

  
2
from utils import login
3

  
4

  
5
def test_bijoe(bijoe, admin):
6
    response = login(bijoe, admin, '/')
7
    import pdb
8
    pdb.set_trace()
tests/utils.py
15 15
    subprocess.check_call(
16 16
        [WCSCTL, 'runscript', '--app-dir', str(wcs_dir), '--vhost', HOSTNAME,
17 17
         str(script_path)])
18

  
19
import io
20
import zipfile
21
import xml.etree.ElementTree as ET
22

  
23
from django.conf import settings
24

  
25

  
26
def login(app, user, path=None, password=None):
27
    if path:
28
        login_page = app.get(path)
29
    else:
30
        login_page = app.get(settings.LOGIN_URL)
31
    login_page = login_page.maybe_follow()
32
    form = login_page.form
33
    form.set('username', user.username if hasattr(user, 'username') else user)
34
    # password is supposed to be the same as username
35
    form.set('password', password or user.username)
36
    response = form.submit(name='login-password-submit').follow(expect_errors=True)
37
    if path:
38
        assert response.request.path == path
39
    assert '_auth_user_id' in app.session
40
    assert str(app.session['_auth_user_id']) == str(user.id)
41
    return response
42

  
43

  
44
def get_table(response):
45
    table = []
46

  
47
    for tr in response.pyquery('table tr'):
48
        row = []
49
        table.append(row)
50
        for td in tr.findall('td'):
51
            row.append((td.text or '').strip())
52
    return table
53

  
54

  
55
def xml_node_text_content(node):
56
    '''Extract text content from node and all its children. Equivalent to
57
       xmlNodeGetContent from libxml.'''
58

  
59
    if node is None:
60
        return ''
61

  
62
    def helper(node):
63
        s = []
64
        if node.text:
65
            s.append(node.text)
66
        for child in node:
67
            s.extend(helper(child))
68
            if child.tail:
69
                s.append(child.tail)
70
        return s
71
    return u''.join(helper(node))
72

  
73

  
74
def get_ods_document(response):
75
    return ET.fromstring(zipfile.ZipFile(io.BytesIO(response.content)).read('content.xml'))
76

  
77

  
78
def get_ods_table(response):
79
    from bijoe.visualization.ods import TABLE_NS
80

  
81
    root = get_ods_document(response)
82
    table = []
83
    for row_node in root.findall('.//{%s}table-row' % TABLE_NS):
84
        row = []
85
        table.append(row)
86
        for cell_node in row_node.findall('.//{%s}table-cell' % TABLE_NS):
87
            row.append(xml_node_text_content(cell_node))
88
    return table
tox.ini
16 16
	PGHOST={env:PGHOST:}
17 17
	PGUSER={env:PGUSER:}
18 18
	PGPASSWORD={env:PGPASSWORD:}
19
	DJANGO_SETTINGS_MODULE=bijoe.settings
20
	BIJOE_SETTINGS_FILE=tests/bijoe_settings.py
19 21
deps =
20 22
	coverage
21 23
	pytest
22 24
	pytest-cov
23 25
	pytest-random
26
	pytest-django
27
	django-webtest
28
	WebTest
29
	pyquery
24 30
	quixote<3.0
25 31
	psycopg2-binary
26 32
	vobject
......
28 34
	django-ratelimit<3
29 35
	gadjo
30 36
	mock
37
	git+https://git.entrouvert.org/bijoe.git@wip/29914-Pouvoir-declarer-des-jointures-n
31 38
	django>=1.11,<1.12
32 39
commands =
33 40
	./get_wcs.sh
34
-