def test_list_queries(self): datasource = DataSource(name='datasource') self.session.add(datasource) dash = add_dashboard(self.session, 'Dash', 'Dashboard') dash2 = add_dashboard(self.session, 'Dash2', 'Dashboard') self.session.add( Query(name='Query', query='query string', dashboard_id=dash.id, data_source_id=datasource.id)) self.session.add( Query(name='Query2', query='query string', dashboard_id=dash.id, data_source_id=datasource.id)) self.session.add( Query(name='Query3', query='query', dashboard_id=dash2.id, data_source_id=datasource.id)) req = dummy_request(self.session) req.params = {} resp = list_queries_view(req) self.assertEqual(len(resp), 3) req.matchdict = {'dashboard_id': dash.id} resp = list_queries_view(req) self.assertEqual(len(resp), 2)
def test_get_chart(self): req = dummy_request(self.session) dashboard = Dashboard(title='Dashboard Title') self.session.add(dashboard) data_source = DataSource(name='Datasource') self.session.add(data_source) self.session.flush() with self.assertRaises(exc.HTTPBadRequest): get_chart_view(req) with self.assertRaises(exc.HTTPNotFound): req.matchdict = {'id': 100} get_chart_view(req) req.json_body = { 'title': 'Title', 'dashboard_id': dashboard.id, 'data_source_id': data_source.id, 'unit': 'U' } chart = create_chart_view(req) chart_id = chart['id'] req.matchdict = {'id': chart_id} chart = get_chart_view(req) self.assertEqual(chart_id, chart['id'])
def test_get_query(self): datasource = DataSource(name='datasource') self.session.add(datasource) dash = add_dashboard(self.session, 'Dash', 'Dashboard') query = Query(name='Query', query='query string', dashboard_id=dash.id, data_source_id=datasource.id) self.session.add(query) self.session.flush() req = dummy_request(self.session) req.matchdict = {} with self.assertRaises(exc.HTTPBadRequest): get_query_view(req) with self.assertRaises(exc.HTTPNotFound): req.matchdict['id'] = 100 get_query_view(req) req.matchdict['id'] = query.id resp = get_query_view(req) self.assertEqual(resp['id'], query.id) self.assertEqual(resp['name'], 'Query')
def test_delete_chart(self): req = dummy_request(self.session) dashboard = Dashboard(title='Dashboard Title') self.session.add(dashboard) data_source = DataSource(name='Datasource') self.session.add(data_source) self.session.flush() req.json_body = { 'title': 'Title', 'dashboard_id': dashboard.id, 'data_source_id': data_source.id, 'unit': 'U' } chart = create_chart_view(req) with self.assertRaises(exc.HTTPNotFound): req.matchdict = {'id': 0} delete_chart(req) req.matchdict = {"id": chart['id']} delete_chart(req) with self.assertRaises(exc.HTTPNotFound): get_chart_view(req)
def test_list_charts(self): req = dummy_request(self.session) dashboard = Dashboard(title='Dashboard Title') self.session.add(dashboard) dashboard2 = Dashboard(title='Dashboard Title 2') self.session.add(dashboard2) data_source = DataSource(name='Datasource') self.session.add(data_source) self.session.flush() req.json_body = { 'title': 'Title', 'dashboard_id': dashboard.id, 'data_source_id': data_source.id, 'unit': 'U' } chart = create_chart_view(req) req.json_body = { 'title': 'Title 2', 'dashboard_id': dashboard2.id, 'data_source_id': data_source.id, 'unit': 'U' } create_chart_view(req) req = dummy_request(self.session) req.params = {} charts = list_charts_view(req) self.assertEqual(len(charts), 2) req.params = {'dashboard_id': dashboard.id} charts = list_charts_view(req) self.assertEqual(len(charts), 1) self.assertEqual(charts[0]['id'], chart['id'])
def test_get_single_stat(self): req = dummy_request(self.session) dashboard = Dashboard(title='Dashboard Title') self.session.add(dashboard) data_source = DataSource(name='Data source') self.session.add(data_source) self.session.flush() with self.assertRaises(exc.HTTPBadRequest): get_single_stat_view(req) with self.assertRaises(exc.HTTPNotFound): req.matchdict = {'id': 100} get_single_stat_view(req) req.json_body = { 'title': 'Title', 'dashboard_id': dashboard.id, 'data_source_id': data_source.id, 'query': 'query', 'colors': '#eee,#000', 'thresholds': '1' } single_stat = create_single_stat_view(req) single_stat_id = single_stat['id'] req.matchdict = {'id': single_stat_id} single_stat = get_single_stat_view(req) self.assertEqual(single_stat_id, single_stat['id'])
def test_paste_dashboard_view(self): collection = Collection(title='Collection 1') self.session.add(collection) self.session.flush() collection_id = collection.id collection2 = Collection(title='Collection 2') self.session.add(collection2) self.session.flush() collection2_id = collection2.id data_source = DataSource(name='Data source') self.session.add(data_source) self.session.flush() user = add_user(self.session) dashboard = add_dashboard(self.session, title='Test title', collection_id=collection.id, users=[user]) dashboard_id = dashboard.id self.session.add( Variable(name='Variable', value='V', dashboard_id=dashboard.id)) self.session.add( Chart(title='Chart', data_source_id=data_source.id, dashboard_id=dashboard.id)) self.session.add( Chart(title='Chart', data_source_id=data_source.id, dashboard_id=dashboard.id)) self.session.add( SingleStat(title='Single stat', data_source_id=data_source.id, dashboard_id=dashboard_id)) self.session.add( Query(name="Query", query="query", dashboard_id=dashboard.id, data_source_id=data_source.id)) self.session.flush() req = dummy_request(self.session) req.json_body = {} with self.assertRaises(exc.HTTPBadRequest): paste_dashboard_view(req) req.matchdict = {'id': dashboard_id} req.json_body = {'collection_id': collection_id} dashboard = paste_dashboard_view(req) self.assertEqual(dashboard['title'], 'Copy of Test title') self.assertEqual(len(dashboard['variables']), 1) req.matchdict = {'id': dashboard_id} req.json_body = {'collection_id': collection2_id} dashboard = paste_dashboard_view(req) self.assertEqual(dashboard['title'], 'Test title') dashboard = self.session.query(Dashboard).get(dashboard['id']) self.assertEqual(dashboard.queries[0].name, "Query")
def test_list_single_stats(self): req = dummy_request(self.session) dashboard = Dashboard(title='Dashboard Title') self.session.add(dashboard) dashboard2 = Dashboard(title='Dashboard Title 2') self.session.add(dashboard2) data_source = DataSource(name='Data source') self.session.add(data_source) self.session.flush() req.json_body = { 'title': 'Title 1', 'dashboard_id': dashboard.id, 'data_source_id': data_source.id, 'query': 'query', 'colors': '#eee,#000', 'thresholds': '1' } single_stat = create_single_stat_view(req) req.json_body = { 'title': 'Title 2', 'dashboard_id': dashboard.id, 'data_source_id': data_source.id, 'query': 'query', 'colors': '#eee,#000', 'thresholds': '1' } create_single_stat_view(req) req.json_body = { 'title': 'Title 3', 'dashboard_id': dashboard2.id, 'data_source_id': data_source.id, 'query': 'query', 'colors': '#eee,#000', 'thresholds': '1' } create_single_stat_view(req) req = dummy_request(self.session) req.params = {} single_stats = list_single_stats_view(req) self.assertEqual(len(single_stats), 3) req.params = {'dashboard_id': dashboard.id} single_stats = list_single_stats_view(req) self.assertEqual(len(single_stats), 2) self.assertEqual(single_stats[0]['id'], single_stat['id'])
def test_edit_query(self): datasource = DataSource(name='datasource') self.session.add(datasource) dash = add_dashboard(self.session, 'Dash', 'Dashboard') query = Query(name='Query', query='query string', dashboard_id=dash.id, data_source_id=datasource.id) self.session.add( Query(name='Query2', query='query string 2', dashboard_id=dash.id, data_source_id=datasource.id)) self.session.add(query) self.session.flush() req = dummy_request(self.session) req.matchdict = {} req.json_body = {} with self.assertRaises(exc.HTTPBadRequest): edit_query_view(req) req.matchdict['id'] = 100 req.json_body = {'name': 'Edited name'} with self.assertRaises(exc.HTTPNotFound): edit_query_view(req) req.matchdict['id'] = query.id with self.assertRaises(exc.HTTPBadRequest): edit_query_view(req) req.json_body = {'query': 'query string'} with self.assertRaises(exc.HTTPBadRequest): edit_query_view(req) req.json_body = { 'name': 'Query2', 'query': 'query string', 'dashboard_id': dash.id } with self.assertRaises(exc.HTTPBadRequest): edit_query_view(req) req.json_body = { 'name': 'Edited name', 'query': 'query string', 'dashboard_id': dash.id, 'data_source_id': datasource.id } resp = edit_query_view(req) self.assertEqual(resp['name'], 'Edited name') self.assertEqual(resp['dashboard_id'], dash.id)
def test_copy_chart(self): req = dummy_request(self.session) dashboard_view = DashboardView(name='Dashboard View', icon='icon') self.session.add(dashboard_view) dashboard1 = Dashboard(title='Dashboard Title 1') self.session.add(dashboard1) dashboard2 = Dashboard(title='Dashboard Title 2') self.session.add(dashboard2) data_source = DataSource(name='Datasource') self.session.add(data_source) self.session.flush() req.json_body = { 'title': 'Title', 'dashboard_id': dashboard1.id, 'data_source_id': data_source.id, 'unit': 'U', 'group_by': [{ 'dashboard_view_id': dashboard_view.id, 'value': 1 }] } chart = create_chart_view(req) chart_id = chart['id'] req.json_body = {} with self.assertRaises(exc.HTTPBadRequest): paste_chart_view(req) req.matchdict = {'id': chart_id} req.json_body = {'dashboard_id': dashboard1.id} chart = paste_chart_view(req) req.matchdict = {'id': chart['id']} chart = get_chart_view(req) self.assertEqual(chart['title'], 'Copy of Title') req.matchdict = {'id': chart_id} req.json_body = {'dashboard_id': dashboard2.id} chart = paste_chart_view(req) req.matchdict = {'id': chart['id']} chart = get_chart_view(req) self.assertEqual(chart['title'], 'Title')
def test_edit_chart_with_group_by(self): req = dummy_request(self.session) dashboard = Dashboard(title='Dashboard Title') dashboard_view = DashboardView(name='Name', icon='') self.session.add(dashboard) self.session.add(dashboard_view) data_source = DataSource(name='Datasource') self.session.add(data_source) self.session.flush() req.json_body = { 'title': 'Title', 'dashboard_id': dashboard.id, 'data_source_id': data_source.id, 'unit': 'U', 'group_by': [{ 'value': '10', 'dashboard_view_id': dashboard_view.id }] } chart = create_chart_view(req) self.assertEqual(chart['group_by'][0]['value'], '10') chart_id = chart['id'] req.json_body = {} req.matchdict = {'id': chart_id} req.json_body = { 'title': 'Title Edit', 'dashboard_id': dashboard.id, 'data_source_id': data_source.id, 'unit': 'R', 'group_by': [{ 'value': '100', 'dashboard_view_id': dashboard_view.id }] } edit_chart(req) chart = get_chart_view(req) self.assertEqual(chart['title'], 'Title Edit') self.assertEqual(chart['group_by'][0]['value'], '100')
def test_delete_query(self): datasource = DataSource(name='datasource') self.session.add(datasource) dash = add_dashboard(self.session, 'Dash', 'Dashboard') query = Query(name='Query', query='query', dashboard_id=dash.id, data_source_id=datasource.id) self.session.add(query) req = dummy_request(self.session) with self.assertRaises(exc.HTTPNotFound): req.matchdict = {'id': 100} delete_query_view(req) req.matchdict['id'] = query.id delete_query_view(req) with self.assertRaises(exc.HTTPNotFound): delete_query_view(req)
def test_create_query(self): datasource = DataSource(name='datasource') self.session.add(datasource) dash = add_dashboard(self.session, 'Dash', 'Dashboard') req = dummy_request(self.session) req.json_body = {} with self.assertRaises(exc.HTTPBadRequest): create_query_view(req) req.json_body = { 'name': 'Query', 'query': 'query string', 'data_source_id': datasource.id, 'dashboard_id': dash.id } query = create_query_view(req) self.assertIsNotNone(query['id']) self.assertEqual(query['name'], 'Query') self.assertEqual(query['dashboard_id'], dash.id)
def test_reorder_charts(self): req = dummy_request(self.session) dashboard = Dashboard(title='Dashboard Title') self.session.add(dashboard) data_source = DataSource(name='Datasource') self.session.add(data_source) self.session.flush() req.json_body = { 'title': 'Title', 'dashboard_id': dashboard.id, 'data_source_id': data_source.id, 'unit': 'U' } chart = create_chart_view(req) req.json_body = { 'title': 'Title 2', 'dashboard_id': dashboard.id, 'data_source_id': data_source.id, 'unit': 'U' } create_chart_view(req) with self.assertRaises(exc.HTTPNotFound): req.matchdict = {'id': 100} req.json_body = {'dashboard_id': dashboard.id, 'index': 1} reorder_chart_view(req) req = dummy_request(self.session) req.params = {} charts = list_charts_view(req) self.assertEqual(charts[0]['id'], chart['id']) req.matchdict = {'id': chart['id']} req.json_body = {'dashboard_id': dashboard.id, 'index': 1} reorder_chart_view(req) charts = list_charts_view(req) self.assertEqual(charts[1]['id'], chart['id']) self.assertEqual(charts[1]['index'], 1) self.assertEqual(charts[0]['index'], 0)
def test_create_chart(self): req = dummy_request(self.session) dashboard = Dashboard(title='Dashboard Title') self.session.add(dashboard) data_source = DataSource(name='Datasource') self.session.add(data_source) self.session.flush() # Test error validation with self.assertRaises(exc.HTTPBadRequest): req.json_body = { 'data_source_id': data_source.id, 'dashboard_id': dashboard.id } create_chart_view(req) with self.assertRaises(exc.HTTPBadRequest): req.json_body = {'title': 'Title', 'dashboard_id': dashboard.id} create_chart_view(req) req.json_body = { 'title': 'Title', 'dashboard_id': dashboard.id, 'data_source_id': data_source.id, 'unit': 'U' } chart = create_chart_view(req) self.assertIsNotNone(chart.get('id')) self.assertEqual(chart['unit'], 'U') req.json_body = { 'title': 'Title 2', 'dashboard_id': dashboard.id, 'data_source_id': data_source.id, 'unit': 'U' } chart2 = create_chart_view(req) self.assertEqual(chart2['index'], 1)
def test_run_http_query(self, mock_http_connection): datasource = DataSource(name='datasource', url='http://ipaddress:port', username='******', password='******') self.session.add(datasource) dash = add_dashboard(self.session, 'Dash', 'Dashboard') self.session.add( Variable(name='variable', value='5', dashboard_id=dash.id)) query_string = "select * from test where column = ${params} and column2 = ${variable};" query = Query(name='Query', query=query_string, dashboard_id=dash.id, data_source_id=datasource.id) self.session.add(query) req = dummy_request(self.session) req.matchdict['dashboard_id'] = dash.id req.matchdict['name'] = 'Query' req.json_body = {'params': '10'} req.accept_encoding = 'encoding' results = [{'id': 1, 'name': 'name'}, {'id': 2, 'name': 'test'}] # mock connection mock_response = Mock() mock_response.headers = { 'Content-Encoding': 'encoding', 'Content-Type': 'type' } mock_response.read.return_value = results mock_response.status = 200 mock_http_connection.return_value.getresponse.return_value = mock_response # Test that the method returns the expected ID. resp = run_query_view(req) self.assertIsNotNone(resp) resp = resp.body self.assertEquals(resp[0]['id'], 1)
def test_create_single_stat(self): req = dummy_request(self.session) dashboard = Dashboard(title='Dashboard Title') self.session.add(dashboard) data_source = DataSource(name='Datasource') self.session.add(data_source) self.session.flush() # Test error validation with self.assertRaises(exc.HTTPBadRequest): req.json_body = {'title': 'Title'} create_single_stat_view(req) with self.assertRaises(exc.HTTPBadRequest): req.json_body = {'query': 'query'} create_single_stat_view(req) with self.assertRaises(exc.HTTPBadRequest): req.json_body = {'title': 'Title', 'query': 'query'} create_single_stat_view(req) with self.assertRaises(exc.HTTPBadRequest): req.json_body = {'title': 'Title', 'query': 'query', 'colors': '#eee,#000'} create_single_stat_view(req) with self.assertRaises(exc.HTTPBadRequest): req.json_body = {'title': 'Title', 'query': 'query', 'colors': '#eee,#000', 'thresholds': '1,2'} create_single_stat_view(req) req.json_body = { 'title': 'Title', 'dashboard_id': dashboard.id, 'data_source_id': data_source.id, 'query': 'query', 'colors': '#eee,#000', 'thresholds': '1' } single_stat = create_single_stat_view(req) self.assertIsNotNone(single_stat.get('id')) self.assertEqual(single_stat['title'], 'Title')
def test_run_database_query(self, mock_engine): datasource = DataSource(name='datasource', url='sqlite:///:memory:') self.session.add(datasource) dash = add_dashboard(self.session, 'Dash', 'Dashboard') self.session.add( Variable(name='variable', value='5', dashboard_id=dash.id)) dash2 = add_dashboard(self.session, 'Dash2', 'Dashboard2') query_string = "select * from test where column = ${params} and column2 = ${variable};" query = Query(name='Query', query=query_string, dashboard_id=dash.id, data_source_id=datasource.id) self.session.add(query) self.session.add( Query(name='Query', query='query2', dashboard_id=dash2.id, data_source_id=datasource.id)) req = dummy_request(self.session) with self.assertRaises(exc.HTTPNotFound): req.matchdict = {'dashboard_id': dash.id, 'name': 'notfound'} run_query_view(req) req.matchdict['dashboard_id'] = dash.id req.matchdict['name'] = 'Query' req.json_body = {'params': '10'} results = [{'id': 1, 'name': 'name'}, {'id': 2, 'name': 'test'}] # mock query response mock_engine.return_value.connect.return_value.__enter__.return_value.execute.return_value = results # Test that the method returns the expected ID. resp = run_query_view(req) self.assertIsNotNone(resp) resp = json.loads(resp.json_body) self.assertEquals(resp[0]['id'], 1)