428 lines
15 KiB
Python
428 lines
15 KiB
Python
# -*- coding: utf-8 -*-
|
|
|
|
import json
|
|
|
|
from utils import login, get_table, get_ods_table, get_ods_document
|
|
|
|
from bijoe.visualization.ods import OFFICE_NS, TABLE_NS
|
|
from bijoe.visualization.models import Visualization as VisualizationModel
|
|
from bijoe.visualization.utils import Visualization
|
|
|
|
|
|
def test_simple(schema1, app, admin):
|
|
login(app, admin)
|
|
response = app.get('/')
|
|
response = response.click('schema1')
|
|
response = response.click('Facts 1')
|
|
assert 'big-msg-info' in response
|
|
assert u'le champ « pouët »' in response
|
|
assert 'warning2' in response
|
|
form = response.form
|
|
form.set('representation', 'table')
|
|
form.set('measure', 'simple_count')
|
|
form.set('drilldown_x', 'innersubcategory')
|
|
response = form.submit('visualize')
|
|
assert 'big-msg-info' not in response
|
|
assert get_table(response) == [
|
|
['Inner SubCategory', u'sub\xe94', u'sub\xe95', u'sub\xe96', u'sub\xe98',
|
|
u'sub\xe99', u'sub\xe97', u'sub\xe92', u'sub\xe93', u'sub\xe91'],
|
|
['number of rows', '0', '0', '0', '0', '0', '0', '0', '1', '15']
|
|
]
|
|
form = response.form
|
|
form.set('representation', 'table')
|
|
form.set('measure', 'simple_count')
|
|
form.set('drilldown_x', 'date__month')
|
|
response = form.submit('visualize')
|
|
assert 'big-msg-info' not in response
|
|
assert get_table(response) == [
|
|
['mois (Date)', 'janvier', u'f\xe9vrier', 'mars', 'avril', 'mai', 'juin', 'juillet', u'ao\xfbt'],
|
|
['number of rows', '10', '1', '1', '1', '1', '1', '1', '1'],
|
|
]
|
|
|
|
|
|
def test_truncated_previous_year_range(schema1, app, admin, freezer):
|
|
login(app, admin)
|
|
response = app.get('/')
|
|
response = response.click('schema1')
|
|
response = response.click('Facts 1')
|
|
form = response.form
|
|
form.set('representation', 'table')
|
|
form.set('measure', 'simple_count')
|
|
form.set('drilldown_x', 'date__month')
|
|
form.set('drilldown_y', 'date__year')
|
|
form.set('filter__date_2', 'since_1jan_last_year')
|
|
freezer.move_to('2019-01-01 01:00:00')
|
|
response = form.submit('visualize')
|
|
assert get_table(response) == [
|
|
['', 'janvier', u'f\xe9vrier', 'mars', 'avril', 'mai', 'juin', 'juillet', u'ao\xfbt', 'Total'],
|
|
['2017', '0', '0', '0', '0', '0', '0', '0', '0', '0'],
|
|
]
|
|
|
|
freezer.move_to('2018-01-01 01:00:00')
|
|
response = form.submit('visualize')
|
|
assert get_table(response) == [
|
|
['', 'janvier', u'f\xe9vrier', 'mars', 'avril', 'mai', 'juin', 'juillet', u'ao\xfbt', 'Total'],
|
|
['2017', '10', '1', '1', '1', '1', '1', '1', '1', '17'],
|
|
]
|
|
|
|
|
|
def test_boolean_dimension(schema1, app, admin):
|
|
login(app, admin)
|
|
response = app.get('/')
|
|
response = response.click('schema1')
|
|
response = response.click('Facts 1')
|
|
form = response.form
|
|
form.set('representation', 'table')
|
|
form.set('measure', 'simple_count')
|
|
form.set('drilldown_x', 'boolean')
|
|
response = form.submit('visualize')
|
|
assert get_table(response) == [['Boolean', 'Oui', 'Non'], ['number of rows', '8', '9']]
|
|
form.set('filter__boolean', [o[0] for o in form.fields['filter__boolean'][0].options if o[2] == 'Oui'][0])
|
|
response = form.submit('visualize')
|
|
assert get_table(response) == [['Boolean', 'Oui', 'Non'], ['number of rows', '8', '0']]
|
|
|
|
|
|
def test_string_dimension(schema1, app, admin):
|
|
login(app, admin)
|
|
response = app.get('/')
|
|
response = response.click('schema1')
|
|
response = response.click('Facts 1')
|
|
form = response.form
|
|
form.set('representation', 'table')
|
|
form.set('measure', 'simple_count')
|
|
form.set('drilldown_x', 'string')
|
|
response = form.submit('visualize')
|
|
assert get_table(response) == [['String', 'a', 'b', 'c', 'Aucun(e)'], ['number of rows', '11', '2', '3', '1']]
|
|
form.set('filter__string', ['a', 'b', '__none__'])
|
|
response = form.submit('visualize')
|
|
assert get_table(response) == [['String', 'a', 'b', 'Aucun(e)'], ['number of rows', '11', '2', '1']]
|
|
|
|
|
|
def test_string_dimension_json_data(schema1, app, admin):
|
|
# test conversion to Javascript declaration
|
|
visu = Visualization.from_json({
|
|
'warehouse': 'schema1',
|
|
'cube': 'facts1',
|
|
'representation': 'table',
|
|
'measure': 'simple_count',
|
|
'drilldown_x': 'string'
|
|
})
|
|
assert json.loads(json.dumps(visu.json_data())) == [
|
|
{u'coords': [{u'value': u'a'}], u'measures': [{u'value': 11}]},
|
|
{u'coords': [{u'value': u'b'}], u'measures': [{u'value': 2}]},
|
|
{u'coords': [{u'value': u'c'}], u'measures': [{u'value': 3}]},
|
|
{u'coords': [{u'value': u'Aucun(e)'}], u'measures': [{u'value': 1}]}
|
|
]
|
|
|
|
|
|
def test_item_dimension(schema1, app, admin):
|
|
login(app, admin)
|
|
response = app.get('/')
|
|
response = response.click('schema1')
|
|
response = response.click('Facts 1')
|
|
form = response.form
|
|
form.set('representation', 'table')
|
|
form.set('measure', 'simple_count')
|
|
form.set('drilldown_x', 'outersubcategory')
|
|
response = form.submit('visualize')
|
|
assert get_table(response) == [
|
|
['Outer SubCategory', u'sub\xe94', u'sub\xe95', u'sub\xe96', u'sub\xe98',
|
|
u'sub\xe99', u'sub\xe97', u'sub\xe92', u'sub\xe93', u'sub\xe91', 'Aucun(e)'],
|
|
['number of rows', '0', '0', '0', '0', '0', '0', '0', '1', '15', '1']
|
|
]
|
|
form.set('filter__outersubcategory', ['__none__'])
|
|
response = form.submit('visualize')
|
|
assert get_table(response) == [
|
|
['Outer SubCategory', 'Aucun(e)'],
|
|
['number of rows', '1']
|
|
]
|
|
|
|
|
|
def test_yearmonth_drilldown(schema1, app, admin):
|
|
login(app, admin)
|
|
response = app.get('/')
|
|
response = response.click('schema1')
|
|
response = response.click('Facts 1')
|
|
form = response.form
|
|
form.set('representation', 'table')
|
|
form.set('measure', 'simple_count')
|
|
form.set('drilldown_x', 'date__yearmonth')
|
|
response = form.submit('visualize')
|
|
assert get_table(response) == [
|
|
[u'ann\xe9e et mois (Date)', '01/2017', '02/2017', '03/2017',
|
|
'04/2017', '05/2017', '06/2017', '07/2017', '08/2017'],
|
|
['number of rows', '10', '1', '1', '1', '1', '1', '1', '1']
|
|
]
|
|
|
|
|
|
def test_ods(schema1, app, admin):
|
|
login(app, admin)
|
|
response = app.get('/')
|
|
response = response.click('schema1')
|
|
response = response.click('Facts 1')
|
|
form = response.form
|
|
form.set('representation', 'table')
|
|
form.set('measure', 'simple_count')
|
|
form.set('drilldown_x', 'innersubcategory')
|
|
form.set('drilldown_y', 'hour')
|
|
response = form.submit('visualize')
|
|
assert 'big-msg-info' not in response
|
|
response = response.click(href='save')
|
|
response.form['name'] = 'test'
|
|
response = response.form.submit().follow()
|
|
ods_response = response.click(href='ods')
|
|
ods_url = ods_response.request.url
|
|
# skip first line of ODS table as it's a header not present in the HTML display
|
|
assert get_table(response) == get_ods_table(ods_response)[1:]
|
|
root = get_ods_document(ods_response)
|
|
nodes = root.findall('.//{%s}table-cell' % TABLE_NS)
|
|
assert len([node for node in nodes if node.attrib['{%s}value-type' % OFFICE_NS] == 'float']) == 11
|
|
|
|
app.reset() # logout
|
|
assert 'login' in app.get(ods_response.request.url, status=302).location
|
|
|
|
|
|
def test_truncated_previous_year_range_on_datetime(schema1, app, admin, freezer):
|
|
login(app, admin)
|
|
response = app.get('/')
|
|
response = response.click('schema1')
|
|
response = response.click('Facts 1')
|
|
form = response.form
|
|
form.set('representation', 'table')
|
|
form.set('measure', 'simple_count')
|
|
form.set('drilldown_x', 'date__month')
|
|
form.set('drilldown_y', 'date__year')
|
|
form.set('filter__datetime_2', 'since_1jan_last_year')
|
|
freezer.move_to('2019-01-01 01:00:00')
|
|
response = form.submit('visualize')
|
|
assert get_table(response) == [
|
|
['', 'janvier', u'f\xe9vrier', 'mars', 'avril', 'mai', 'juin', 'juillet', u'ao\xfbt', 'Total'],
|
|
['2017', '0', '0', '0', '0', '0', '0', '0', '0', '0'],
|
|
]
|
|
freezer.move_to('2018-01-01 01:00:00')
|
|
response = form.submit('visualize')
|
|
assert get_table(response) == [
|
|
['', 'janvier', u'f\xe9vrier', 'mars', 'avril', 'mai', 'juin', 'juillet', u'ao\xfbt', 'Total'],
|
|
['2017', '10', '1', '1', '1', '1', '1', '1', '1', '17'],
|
|
]
|
|
|
|
|
|
def test_none_percent_json_data_0d(schema1, app, admin):
|
|
# test conversion to Javascript declaration
|
|
visu = Visualization.from_json({
|
|
'warehouse': 'schema1',
|
|
'cube': 'facts1',
|
|
'representation': 'graphical',
|
|
'measure': 'percent',
|
|
})
|
|
assert visu.json_data() == [{u'coords': [], u'measures': [{u'value': 100.0}]}]
|
|
|
|
|
|
def test_none_percent_json_data_2d(schema1, app, admin):
|
|
# test conversion to Javascript declaration
|
|
visu = Visualization.from_json({
|
|
'warehouse': 'schema1',
|
|
'cube': 'facts1',
|
|
'representation': 'graphical',
|
|
'measure': 'percent',
|
|
'drilldown_y': 'leftcategory',
|
|
'drilldown_x': 'date__year',
|
|
})
|
|
assert visu.json_data() == [
|
|
{
|
|
'coords': [{'value': u'2017'}, {'value': u'cat\xe92'}],
|
|
'measures': [{'value': 0}]
|
|
},
|
|
{
|
|
'coords': [{'value': u'2017'}, {'value': u'cat\xe93'}],
|
|
'measures': [{'value': 0}]},
|
|
{
|
|
'coords': [{'value': u'2017'}, {'value': u'cat\xe91'}],
|
|
'measures': [{'value': 94.11764705882354}]},
|
|
{
|
|
'coords': [{'value': u'2017'}, {'value': u'Aucun(e)'}],
|
|
'measures': [{'value': 5.882352941176471}]
|
|
}
|
|
]
|
|
|
|
|
|
def test_geoloc(schema1, app, admin):
|
|
# test conversion to Javascript declaration
|
|
visu = VisualizationModel.objects.create(
|
|
slug='visu',
|
|
name='Visu',
|
|
parameters={
|
|
'warehouse': 'schema1',
|
|
'cube': 'facts1',
|
|
'representation': 'graphical',
|
|
'measure': 'percent',
|
|
'drilldown_y': 'outercategory',
|
|
'drilldown_x': 'date__year',
|
|
})
|
|
response = app.get('/visualization/%d/geojson/' % visu.pk)
|
|
assert response.json == {
|
|
'type': 'FeatureCollection',
|
|
'features': [{
|
|
u'geometry': {
|
|
u'coordinates': [
|
|
[1.0, 1.0], [1.0, 1.0], [1.0, 1.0], [1.0, 1.0], [1.0, 1.0], [1.0, 1.0],
|
|
[1.0, 1.0], [1.0, 1.0], [1.0, 1.0], [1.0, 1.0], [1.0, 1.0], [1.0, 1.0],
|
|
[1.0, 1.0], [1.0, 1.0], [1.0, 1.0], [1.0, 1.0]
|
|
],
|
|
u'type': u'MultiPoint'
|
|
},
|
|
u'properties': {
|
|
u'Outer Category': u'cat\xe91',
|
|
u'ann\xe9e (Date)': u'2017'
|
|
},
|
|
u'type': u'Feature'
|
|
},
|
|
{
|
|
u'geometry': {
|
|
u'coordinates': [[1.0, 1.0]],
|
|
u'type': u'MultiPoint'
|
|
},
|
|
u'properties': {
|
|
u'Outer Category': u'Aucun(e)',
|
|
u'ann\xe9e (Date)': u'2017'
|
|
},
|
|
u'type': u'Feature'
|
|
},
|
|
{
|
|
u'geometry': {
|
|
u'coordinates': [],
|
|
u'type': u'MultiPoint'
|
|
},
|
|
u'properties': {
|
|
u'Outer Category': u'cat\xe92',
|
|
u'ann\xe9e (Date)': u'Aucun(e)'
|
|
},
|
|
u'type': u'Feature'
|
|
},
|
|
{
|
|
u'geometry': {
|
|
u'coordinates': [],
|
|
u'type': u'MultiPoint'
|
|
},
|
|
u'properties': {
|
|
u'Outer Category': u'cat\xe93',
|
|
u'ann\xe9e (Date)': u'Aucun(e)'
|
|
},
|
|
u'type': u'Feature'
|
|
},
|
|
{
|
|
u'geometry': {
|
|
u'coordinates': [],
|
|
u'type': u'MultiPoint'
|
|
},
|
|
u'properties': {
|
|
u'Outer Category': u'cat\xe91',
|
|
u'ann\xe9e (Date)': u'Aucun(e)'
|
|
},
|
|
u'type': u'Feature'
|
|
}
|
|
]}
|
|
|
|
|
|
def test_filter_type_mismatch(schema1, app, admin):
|
|
# test conversion to Javascript declaration
|
|
visu = Visualization.from_json({
|
|
'warehouse': 'schema1',
|
|
'cube': 'facts1',
|
|
'representation': 'graphical',
|
|
'measure': 'simple_count',
|
|
'filters': {
|
|
'string': [1],
|
|
}
|
|
})
|
|
assert visu.json_data() == [{'coords': [], 'measures': [{'value': 0}]}]
|
|
|
|
|
|
def test_empty_filter(schema1, app, admin):
|
|
visu = Visualization.from_json({
|
|
'warehouse': 'schema1',
|
|
'cube': 'facts1',
|
|
'representation': 'graphical',
|
|
'measure': 'simple_count',
|
|
'filters': {
|
|
'innercategory': [],
|
|
}
|
|
})
|
|
assert visu.json_data() == [{'coords': [], 'measures': [{'value': 17}]}]
|
|
|
|
|
|
def test_json_dimensions(schema1, app, admin):
|
|
login(app, admin)
|
|
response = app.get('/')
|
|
response = response.click('schema1')
|
|
response = response.click('Facts 1')
|
|
form = response.form
|
|
|
|
form.set('representation', 'table')
|
|
form.set('measure', 'simple_count')
|
|
form.set('drilldown_x', 'a')
|
|
response = form.submit('visualize')
|
|
assert get_table(response) == [
|
|
['A', 'x', 'y', 'z'],
|
|
['number of rows', '7', '9', '1']
|
|
]
|
|
|
|
assert 'filter__a' in form.fields
|
|
assert set([o[0] for o in form['filter__a'].options]) == {'x', 'y', 'z', '__none__'}
|
|
|
|
form.set('filter__a', ['x', 'y'])
|
|
response = form.submit('visualize')
|
|
assert get_table(response) == [
|
|
['A', 'x', 'y', 'z'],
|
|
['number of rows', '7', '9', '0']
|
|
]
|
|
|
|
|
|
def test_json_dimensions_having_percent(schema1, app, admin):
|
|
login(app, admin)
|
|
response = app.get('/')
|
|
response = response.click('schema1')
|
|
response = response.click('Facts 1')
|
|
form = response.form
|
|
|
|
form.set('representation', 'table')
|
|
form.set('measure', 'percent')
|
|
form.set('drilldown_x', 'a')
|
|
response = form.submit('visualize')
|
|
assert get_table(response) == [
|
|
['A', 'x', 'y', 'z'],
|
|
['pourcentage des demandes', '41,18 %', '52,94 %', '5,88 %']
|
|
]
|
|
|
|
assert 'filter__a' in form.fields
|
|
assert set([o[0] for o in form['filter__a'].options]) == {'x', 'y', 'z', '__none__'}
|
|
|
|
form.set('filter__a', ['x', 'y'])
|
|
response = form.submit('visualize')
|
|
assert get_table(response) == [
|
|
['A', 'x', 'y', 'z'],
|
|
['pourcentage des demandes', '43,75 %', '56,25 %', '0,00 %']
|
|
]
|
|
|
|
|
|
def test_sum_integer_measure(schema1, app, admin):
|
|
login(app, admin)
|
|
response = app.get('/')
|
|
response = response.click('schema1')
|
|
response = response.click('Facts 1')
|
|
form = response.form
|
|
form.set('representation', 'table')
|
|
form.set('measure', 'sum_integer')
|
|
form.set('drilldown_x', 'string')
|
|
response = form.submit('visualize')
|
|
assert get_table(response) == [
|
|
['String', 'a', 'b', 'c', 'Aucun(e)'],
|
|
['sum of integer column', '11', '2', '3', '1'],
|
|
]
|
|
form.set('filter__string', ['a', 'b', '__none__'])
|
|
response = form.submit('visualize')
|
|
assert get_table(response) == [
|
|
['String', 'a', 'b', 'Aucun(e)'],
|
|
['sum of integer column', '11', '2', '1'],
|
|
]
|