wcs/tests/backoffice_pages/test_statistics.py

180 lines
6.4 KiB
Python

import os
import re
import pytest
from wcs.formdef import FormDef
from wcs.qommon.http_request import HTTPRequest
from ..utilities import clean_temporary_pub, create_temporary_pub, get_app, login
from .test_all import create_environment, create_superuser, create_user
@pytest.fixture
def pub(request, emails):
pub = create_temporary_pub(sql_mode=True)
req = HTTPRequest(None, {'SCRIPT_NAME': '/', 'SERVER_NAME': 'example.net'})
pub.set_app_dir(req)
pub.cfg['identification'] = {'methods': ['password']}
pub.cfg['language'] = {'language': 'en'}
pub.write_cfg()
with open(os.path.join(pub.app_dir, 'site-options.cfg'), 'w') as fd:
fd.write(
'''
[api-secrets]
coucou = 1234
'''
)
return pub
def teardown_module(module):
clean_temporary_pub()
def test_backoffice_statistics_with_no_formdefs(pub):
create_user(pub)
create_environment(pub)
FormDef.wipe()
from wcs.sql import drop_global_views, get_connection_and_cursor
conn, cur = get_connection_and_cursor()
drop_global_views(conn, cur)
conn.commit()
cur.close()
app = login(get_app(pub))
resp = app.get('/backoffice/management/statistics')
assert 'This site is currently empty.' in resp
def test_backoffice_statistics_status_filter(pub):
create_superuser(pub)
create_environment(pub)
app = login(get_app(pub))
resp = app.get('/backoffice/management/form-title/')
resp = resp.click('Statistics')
assert 'filter' not in resp.forms['listing-settings'].fields # status is not displayed by default
assert '<h2>Filters</h2>' not in resp.text
# add 'status' as a filter
resp.forms['listing-settings']['filter-status'].checked = True
resp = resp.forms['listing-settings'].submit()
assert 'filter' in resp.forms['listing-settings'].fields
assert '<h2>Filters</h2>' not in resp.text
assert resp.forms['listing-settings']['filter'].value == 'all'
resp.forms['listing-settings']['filter'].value = 'pending'
resp = resp.forms['listing-settings'].submit()
assert 'Total number of records: 17' in resp.text
assert '<h2>Filters</h2>' in resp.text
assert 'Status: Open' in resp.text
resp.forms['listing-settings']['filter'].value = 'waiting'
resp = resp.forms['listing-settings'].submit()
assert 'Total number of records: 17' in resp.text
assert '<h2>Filters</h2>' in resp.text
assert 'Status: Waiting for an action' in resp.text
resp.forms['listing-settings']['filter'].value = 'done'
resp = resp.forms['listing-settings'].submit()
assert 'Total number of records: 33' in resp.text
assert '<h2>Filters</h2>' in resp.text
assert 'Status: Done' in resp.text
resp.forms['listing-settings']['filter'].value = 'rejected'
resp = resp.forms['listing-settings'].submit()
assert 'Total number of records: 0' in resp.text
assert '<h2>Filters</h2>' in resp.text
assert 'Status: Rejected' in resp.text
resp.forms['listing-settings']['filter'].value = 'all'
resp = resp.forms['listing-settings'].submit()
assert 'Total number of records: 50' in resp.text
def test_backoffice_statistics_status_select(pub):
create_superuser(pub)
create_environment(pub)
app = login(get_app(pub))
resp = app.get('/backoffice/management/form-title/')
resp = resp.click('Statistics')
assert 'filter-2-value' not in resp.form.fields
resp.forms['listing-settings']['filter-2'].checked = True
resp = resp.forms['listing-settings'].submit()
resp.forms['listing-settings']['filter-2-value'].value = 'bar'
resp = resp.forms['listing-settings'].submit()
assert 'Total number of records: 13' in resp.text
resp.forms['listing-settings']['filter-2-value'].value = 'baz'
resp = resp.forms['listing-settings'].submit()
assert 'Total number of records: 24' in resp.text
resp.forms['listing-settings']['filter-2-value'].value = 'foo'
resp = resp.forms['listing-settings'].submit()
assert 'Total number of records: 13' in resp.text
assert '<h2>Filters</h2>' in resp.text
assert '2nd field: foo' in resp.text
# check it's also possible to get back to the complete list
resp.forms['listing-settings']['filter-2-value'].value = ''
resp = resp.forms['listing-settings'].submit()
assert 'Total number of records: 50' in resp.text
# check it also works with item fields with a data source
resp = app.get('/backoffice/management/form-title/')
resp = resp.click('Statistics')
resp.forms['listing-settings']['filter-3'].checked = True
resp = resp.forms['listing-settings'].submit()
resp.forms['listing-settings']['filter-3-value'].value = 'A'
resp = resp.forms['listing-settings'].submit()
assert 'Total number of records: 13' in resp.text
assert '<h2>Filters</h2>' in resp.text
assert '3rd field: aa' in resp.text
# set field to be displayed by default in filters
formdef = FormDef.get_by_urlname('form-title')
formdef.fields[1].in_filters = True
formdef.store()
resp = app.get('/backoffice/management/form-title/')
resp = resp.click('Statistics')
assert 'filter-2-value' in resp.form.fields
def test_global_statistics(pub):
create_superuser(pub)
create_environment(pub)
app = login(get_app(pub))
resp = app.get('/backoffice/management/forms')
resp = resp.click('Global statistics')
assert 'Total count: 70' in resp.text
resp.forms[0]['start'] = '2014-01-01'
resp.forms[0]['end'] = '2014-12-31'
resp = resp.forms[0].submit()
assert 'Total count: 20' in resp.text
def test_backoffice_statistics(pub):
create_superuser(pub)
create_environment(pub)
app = login(get_app(pub))
resp = app.get('/backoffice/management/form-title/')
resp = resp.click('Statistics')
assert 'Total number of records: 50' in resp.text
assert 'New: 17' in resp.text
assert 'Finished: 33' in resp.text
assert re.findall('foo.*26.*bar.*26.*bar.*48', resp.text) # percentages
assert 'Resolution time' in resp.text
assert 'To Status &quot;New&quot;' in resp.text
assert 'To Status &quot;Finished&quot;' in resp.text
assert '<h2>Filters</h2>' not in resp.text
resp.forms['listing-settings']['filter-end-value'] = '2013-01-01'
resp = resp.forms['listing-settings'].submit()
assert 'Total number of records: 0' in resp.text
assert '<h2>Filters</h2>' in resp.text
assert 'End: 2013-01-01' in resp.text