combo/tests/test_dataviz.py

466 lines
17 KiB
Python

import json
import mock
import pytest
from httmock import HTTMock
from django.contrib.auth.models import User, Group
from django.test import override_settings
from combo.data.models import Page
from combo.apps.dataviz.models import Gauge, ChartNgCell, UnsupportedDataSet
from .test_public import login, normal_user
pytestmark = pytest.mark.django_db
@pytest.fixture
def cell():
page = Page(title='One', slug='index')
page.save()
cell = Gauge(page=page, order=0, placeholder='content')
cell.data_source = '[test_url]/XXX'
cell.save()
return cell
def test_jsonp_gauge(app, cell):
with override_settings(TEMPLATE_VARS={'test_url': 'http://www.example.net'}):
resp = app.get('/')
assert 'data-gauge-count-jsonp-url="http://www.example.net/XXX"' in resp.text
def test_json_gauge(app, cell):
cell.jsonp_data_source = False
cell.save()
with override_settings(TEMPLATE_VARS={'test_url': 'http://www.example.net'}):
with mock.patch('combo.apps.dataviz.views.requests.get') as requests_get:
requests_get.return_value = mock.Mock(content='xxx', status_code=200)
resp = app.get('/')
assert 'data-gauge-count-url="/ajax/gauge-count/%s/"' % cell.id in resp.text
resp = app.get('/ajax/gauge-count/%s/' % cell.id)
assert resp.text == 'xxx'
assert requests_get.call_args[0][0] == 'http://www.example.net/XXX'
VISUALIZATION_JSON = [
{
'data-url': 'https://bijoe.example.com/visualization/1/json/',
'path': 'https://bijoe.example.com/visualization/1/iframe/?signature=123',
'name': 'example visualization (X)',
'slug': 'example',
},
{
'data-url': 'https://bijoe.example.com/visualization/2/json/',
'path': 'https://bijoe.example.com/visualization/2/iframe/?signature=123',
'name': 'second visualization (Y)',
'slug': 'second',
},
{
'data-url': 'https://bijoe.example.com/visualization/3/json/',
'path': 'https://bijoe.example.com/visualization/3/iframe/?signature=123',
'name': 'third visualization (X/Y)',
'slug': 'third',
},
{
'data-url': 'https://bijoe.example.com/visualization/4/json/',
'path': 'https://bijoe.example.com/visualization/4/iframe/?signature=123',
'name': 'fourth visualization (no axis)',
'slug': 'fourth',
},
{
'data-url': 'https://bijoe.example.com/visualization/5/json/',
'path': 'https://bijoe.example.com/visualization/5/iframe/?signature=123',
'name': 'fifth visualization (loop/X)',
'slug': 'fifth',
},
{
'data-url': 'https://bijoe.example.com/visualization/6/json/',
'path': 'https://bijoe.example.com/visualization/6/iframe/?signature=123',
'name': 'sixth visualization (loop/Y)',
'slug': 'sixth',
},
{
'data-url': 'https://bijoe.example.com/visualization/7/json/',
'path': 'https://bijoe.example.com/visualization/7/iframe/?signature=123',
'name': 'seventh visualization (loop/X/Y)',
'slug': 'seventh',
},
{
'data-url': 'https://bijoe.example.com/visualization/8/json/',
'path': 'https://bijoe.example.com/visualization/8/iframe/?signature=123',
'name': 'eighth visualization (duration)',
'slug': 'eighth',
},
{
'data-url': 'https://bijoe.example.com/visualization/9/json/',
'path': 'https://bijoe.example.com/visualization/9/iframe/?signature=123',
'name': 'nineth visualization (loop over varying dimensions)',
'slug': 'nineth',
},
{
'data-url': 'https://bijoe.example.com/visualization/10/json/',
'path': 'https://bijoe.example.com/visualization/10/iframe/?signature=123',
'name': 'tenth visualization (percents)',
'slug': 'tenth',
},
]
def bijoe_mock(url, request):
if url.path == '/visualization/json/':
return {'content': json.dumps(VISUALIZATION_JSON), 'request': request, 'status_code': 200}
if url.path == '/visualization/1/json/':
response = {
'format': '1',
'data': [222, 134, 53],
'axis': {
'x_labels': ['web', 'mail', 'email']
}
}
return {'content': json.dumps(response), 'request': request, 'status_code': 200}
if url.path == '/visualization/2/json/':
response = {
'format': '1',
'data': [222, 134, 53],
'axis': {
'y_labels': ['web', 'mail', 'email']
}
}
return {'content': json.dumps(response), 'request': request, 'status_code': 200}
if url.path == '/visualization/3/json/':
response = {
'format': '1',
'data': [
[222, 134, 53],
[122, 114, 33],
],
'axis': {
'x_labels': ['web', 'mail', 'email'],
'y_labels': ['foo', 'bar'],
}
}
return {'content': json.dumps(response), 'request': request, 'status_code': 200}
if url.path == '/visualization/4/json/':
response = {
'format': '1',
'data': 222,
'axis': {}
}
return {'content': json.dumps(response), 'request': request, 'status_code': 200}
if url.path == '/visualization/5/json/':
response = {
'format': '1',
'data': [
[222, 134, 53],
[122, 114, 33],
],
'axis': {
'x_labels': ['web', 'mail', 'email'],
'loop': ['foo', 'bar'],
}
}
return {'content': json.dumps(response), 'request': request, 'status_code': 200}
if url.path == '/visualization/6/json/':
response = {
'format': '1',
'data': [
[222, 134, 53],
[122, 114, 33],
],
'axis': {
'y_labels': ['web', 'mail', 'email'],
'loop': ['foo', 'bar'],
}
}
return {'content': json.dumps(response), 'request': request, 'status_code': 200}
if url.path == '/visualization/7/json/':
response = {
'format': '1',
'data': [
[[222, 134, 53], [122, 114, 33]],
[[222, 134, 53], [122, 114, 33]],
[[222, 134, 53], [122, 114, 33]],
[[222, 134, 53], [122, 114, 33]],
],
'axis': {
'x_labels': ['foo', 'bar'],
'y_labels': ['web', 'mail', 'email'],
'loop': ['a', 'b', 'c', 'd'],
}
}
return {'content': json.dumps(response), 'request': request, 'status_code': 200}
if url.path == '/visualization/8/json/':
response = {
'format': '1',
'data': [1000, 123000, 8600, 86400],
'axis': {
'y_labels': ['web', 'mail', 'email', 'fax']
},
'unit': 'seconds',
'measure': 'duration',
}
return {'content': json.dumps(response), 'request': request, 'status_code': 200}
if url.path == '/visualization/9/json/':
response = {
'format': '1',
'data': [
[1, 1, 1, 1],
[1],
[1, 1],
],
'axis': {
'y_labels': ['web', 'mail', 'email'],
'loop': ['a', 'b', 'c', 'd'],
}
}
return {'content': json.dumps(response), 'request': request, 'status_code': 200}
if url.path == '/visualization/10/json/':
response = {
'format': '1',
'data': [10, 20, 30, 40],
'axis': {
'y_labels': ['web', 'mail', 'email', 'fax']
},
'unit': None,
'measure': 'percent',
}
return {'content': json.dumps(response), 'request': request, 'status_code': 200}
def test_chartng_cell(app):
page = Page(title='One', slug='index')
page.save()
with override_settings(KNOWN_SERVICES={
'bijoe': {'plop': {'title': 'test', 'url': 'https://bijoe.example.com',
'secret': 'combo', 'orig': 'combo'}}}):
with HTTMock(bijoe_mock):
cell = ChartNgCell(page=page, order=1)
cell.data_reference = 'plop:example'
cell.save()
assert cell.cached_json == VISUALIZATION_JSON[0]
# bar
chart = cell.get_chart()
assert chart.__class__.__name__ == 'Bar'
assert chart.x_labels == ['web', 'mail', 'email']
assert chart.raw_series == [([222, 134, 53], {'title': ''})]
# horizontal bar
cell.chart_type = 'horizontal-bar'
chart = cell.get_chart()
assert chart.__class__.__name__ == 'HorizontalBar'
assert chart.x_labels == ['web', 'mail', 'email']
assert chart.raw_series == [([222, 134, 53], {'title': ''})]
# pie
cell.chart_type = 'pie'
chart = cell.get_chart()
assert chart.__class__.__name__ == 'Pie'
assert chart.x_labels == ['web', 'mail', 'email']
assert chart.raw_series == [
([222], {'title': u'web'}),
([134], {'title': u'mail'}),
([53], {'title': u'email'})
]
# data in Y
cell.chart_type = 'bar'
cell.data_reference = 'plop:second'
cell.save()
assert cell.cached_json == VISUALIZATION_JSON[1]
chart = cell.get_chart()
assert chart.x_labels == ['web', 'mail', 'email']
assert chart.raw_series == [([222, 134, 53], {'title': ''})]
# data in X/Y
cell.chart_type = 'bar'
cell.data_reference = 'plop:third'
cell.save()
assert cell.cached_json == VISUALIZATION_JSON[2]
chart = cell.get_chart()
assert chart.x_labels == ['web', 'mail', 'email']
assert chart.raw_series == [
([222, 134, 53], {'title': u'foo'}),
([122, 114, 33], {'title': u'bar'}),
]
# single data point
cell.chart_type = 'bar'
cell.data_reference = 'plop:fourth'
cell.save()
assert cell.cached_json == VISUALIZATION_JSON[3]
chart = cell.get_chart()
assert chart.x_labels == ['']
assert chart.raw_series == [([222], {'title': ''})]
# loop/X
cell.data_reference = 'plop:fifth'
cell.save()
chart = cell.get_chart()
assert chart.x_labels == ['web', 'mail', 'email']
assert chart.raw_series == [
([222, 134, 53], {'title': u'foo'}),
([122, 114, 33], {'title': u'bar'}),
]
# loop/Y
cell.data_reference = 'plop:sixth'
cell.save()
chart = cell.get_chart()
assert chart.x_labels == ['web', 'mail', 'email']
assert chart.raw_series == [
([222, 134, 53], {'title': u'foo'}),
([122, 114, 33], {'title': u'bar'}),
]
# loop/X/Y
cell.data_reference = 'plop:seventh'
cell.save()
with pytest.raises(UnsupportedDataSet):
chart = cell.get_chart()
# duration
cell.data_reference = 'plop:eighth'
cell.save()
chart = cell.get_chart()
# loop/X/Y
cell.data_reference = 'plop:nineth'
cell.save()
with pytest.raises(UnsupportedDataSet):
chart = cell.get_chart()
def test_chartng_cell_view(app, normal_user):
page = Page(title='One', slug='index')
page.save()
with override_settings(KNOWN_SERVICES={
'bijoe': {'plop': {'title': 'test', 'url': 'https://bijoe.example.com',
'secret': 'combo', 'orig': 'combo'}}}):
with HTTMock(bijoe_mock):
cell = ChartNgCell(page=page, order=1, placeholder='content')
cell.data_reference = 'plop:example'
cell.save()
resp = app.get('/api/dataviz/graph/1/') # get data in cache
resp = app.get('/')
assert 'min-height: 250px' in resp.text
assert '/api/dataviz/graph/1/' in resp.text
resp = app.get('/api/dataviz/graph/1/?width=400')
assert resp.content_type == 'image/svg+xml'
page.public = False
page.save()
resp = app.get('/api/dataviz/graph/1/?width=400', status=403)
page.public = True
page.save()
group = Group(name='plop')
group.save()
cell.public = False
cell.groups.set([group])
cell.save()
resp = app.get('/api/dataviz/graph/1/?width=400', status=403)
app = login(app, username='normal-user', password='normal-user')
resp = app.get('/api/dataviz/graph/1/?width=400', status=403)
normal_user.groups.set([group])
normal_user.save()
resp = app.get('/api/dataviz/graph/1/?width=400', status=200)
# table visualization
cell.chart_type = 'table'
cell.save()
resp = app.get('/')
assert '<td>222</td>' in resp.text
# unsupported dataset
cell.data_reference = 'plop:seventh'
cell.save()
resp = app.get('/api/dataviz/graph/1/') # get data in cache
resp = app.get('/')
assert 'Unsupported dataset' in resp.text
cell.chart_type = 'bar'
cell.save()
resp = app.get('/api/dataviz/graph/1/?width=400', status=200)
assert 'Unsupported dataset' in resp.text
# durations
cell.data_reference = 'plop:eighth'
cell.chart_type = 'table'
cell.save()
resp = app.get('/api/dataviz/graph/1/') # get data in cache
resp = app.get('/')
assert '<td>Less than an hour</td>' in resp.text
assert '<td>1 day and 10 hours</td>' in resp.text
assert '<td>2 hours</td>' in resp.text
assert '<td>1 day</td>' in resp.text
cell.chart_type = 'bar'
cell.save()
resp = app.get('/api/dataviz/graph/1/?width=400', status=200)
assert '>Less than an hour<' in resp.text
assert '>1 day and 10 hours<' in resp.text
assert '>2 hours<' in resp.text
assert '>1 day<' in resp.text
# percents
cell.data_reference = 'plop:tenth'
cell.chart_type = 'table'
cell.save()
resp = app.get('/api/dataviz/graph/1/') # get data in cache
resp = app.get('/')
assert '<td>10.0%</td>' in resp.text
cell.chart_type = 'bar'
cell.save()
resp = app.get('/api/dataviz/graph/1/?width=400', status=200)
assert '>10.0%<' in resp.text
# cell with missing cached_json (probably after import and missing
# bijoe visualisation)
cell.chart_type = 'table'
cell.save()
ChartNgCell.objects.filter(id=cell.id).update(cached_json={})
resp = app.get('/')
assert 'warningnotice' in resp.text
def test_chartng_cell_manager(app, admin_user):
page = Page(title='One', slug='index')
page.save()
app = login(app)
with override_settings(KNOWN_SERVICES={
'bijoe': {'plop': {'title': 'test', 'url': 'https://bijoe.example.com',
'secret': 'combo', 'orig': 'combo'}}}):
with HTTMock(bijoe_mock):
cell = ChartNgCell(page=page, order=1, placeholder='content')
cell.data_reference = 'plop:example'
cell.save()
resp = app.get('/manage/pages/%s/' % page.id)
assert resp.form['cdataviz_chartngcell-%s-data_reference' % cell.id].options == [
(u'plop:eighth', False, u'eighth visualization (duration)'),
(u'plop:example', True, u'example visualization (X)'),
(u'plop:fifth', False, u'fifth visualization (loop/X)'),
(u'plop:fourth', False, u'fourth visualization (no axis)'),
(u'plop:nineth', False, u'nineth visualization (loop over varying dimensions)'),
(u'plop:second', False, u'second visualization (Y)'),
(u'plop:seventh', False, u'seventh visualization (loop/X/Y)'),
(u'plop:sixth', False, u'sixth visualization (loop/Y)'),
(u'plop:tenth', False, u'tenth visualization (percents)'),
(u'plop:third', False, u'third visualization (X/Y)'),
]