# -*- 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('/').follow() response = response.click('Facts 1') assert 'big-msg-info' 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('/').follow() 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('/').follow() 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('/').follow() 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', 'c', 'Aucun(e)'], ['number of rows', '11', '2', '0', '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('/').follow() 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('/').follow() 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('/').follow() 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('/').follow() 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(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': 'leftcategory', 'drilldown_x': 'date__year', }) response = app.get('/visualization/%d/geojson/' % visu.pk) assert response.json == [ { 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'Left 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'Left Category': u'Aucun(e)', u'ann\xe9e (Date)': u'2017' }, u'type': u'Feature' } ]