Refactored api_tests in charts. Split filter test into several unit tests cases. Added table id based on database id (#11249)

This commit is contained in:
Kasia Kucharczyk 2020-10-14 21:41:37 +02:00 committed by GitHub
parent 820fa473a7
commit 9caf875263
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 54 additions and 15 deletions

View File

@ -27,6 +27,7 @@ import pytest
from sqlalchemy import and_
from sqlalchemy.sql import func
from superset.connectors.sqla.models import SqlaTable
from superset.utils.core import get_example_database
from tests.fixtures.unicode_dashboard import load_unicode_dashboard_with_slice
from tests.test_app import app
@ -620,16 +621,48 @@ class TestChartApi(SupersetTestCase, ApiOwnersTestCaseMixin):
data = json.loads(rv.data.decode("utf-8"))
self.assertEqual(data["count"], 5)
@pytest.fixture()
def load_charts(self):
with app.app_context():
admin = self.get_user("admin")
energy_table = (
db.session.query(SqlaTable)
.filter_by(table_name="energy_usage")
.one_or_none()
)
energy_table_id = 1
if energy_table:
energy_table_id = energy_table.id
chart1 = self.insert_chart(
"foo_a", [admin.id], energy_table_id, description="ZY_bar"
)
chart2 = self.insert_chart(
"zy_foo", [admin.id], energy_table_id, description="desc1"
)
chart3 = self.insert_chart(
"foo_b", [admin.id], energy_table_id, description="desc1zy_"
)
chart4 = self.insert_chart(
"foo_c", [admin.id], energy_table_id, viz_type="viz_zy_"
)
chart5 = self.insert_chart(
"bar", [admin.id], energy_table_id, description="foo"
)
yield
# rollback changes
db.session.delete(chart1)
db.session.delete(chart2)
db.session.delete(chart3)
db.session.delete(chart4)
db.session.delete(chart5)
db.session.commit()
@pytest.mark.usefixtures("load_charts")
def test_get_charts_custom_filter(self):
"""
Chart API: Test get charts custom filter
"""
admin = self.get_user("admin")
chart1 = self.insert_chart("foo_a", [admin.id], 1, description="ZY_bar")
chart2 = self.insert_chart("zy_foo", [admin.id], 1, description="desc1")
chart3 = self.insert_chart("foo_b", [admin.id], 1, description="desc1zy_")
chart4 = self.insert_chart("foo_c", [admin.id], 1, viz_type="viz_zy_")
chart5 = self.insert_chart("bar", [admin.id], 1, description="foo")
arguments = {
"filters": [{"col": "slice_name", "opr": "chart_all_text", "value": "zy_"}],
@ -658,6 +691,8 @@ class TestChartApi(SupersetTestCase, ApiOwnersTestCaseMixin):
self.assertEqual(item["slice_name"], expected_response[index]["slice_name"])
self.assertEqual(item["viz_type"], expected_response[index]["viz_type"])
@pytest.mark.usefixtures("load_charts")
def test_admin_gets_filtered_energy_slices(self):
# test filtering on datasource_name
arguments = {
"filters": [
@ -666,13 +701,25 @@ class TestChartApi(SupersetTestCase, ApiOwnersTestCaseMixin):
"keys": ["none"],
"columns": ["slice_name"],
}
self.login(username="admin")
uri = f"api/v1/chart/?q={prison.dumps(arguments)}"
rv = self.get_assert_metric(uri, "get_list")
self.assertEqual(rv.status_code, 200)
data = json.loads(rv.data.decode("utf-8"))
self.assertEqual(data["count"], 8)
self.logout()
@pytest.mark.usefixtures("load_charts")
def test_user_gets_none_filtered_energy_slices(self):
# test filtering on datasource_name
arguments = {
"filters": [
{"col": "slice_name", "opr": "chart_all_text", "value": "energy",}
],
"keys": ["none"],
"columns": ["slice_name"],
}
self.login(username="gamma")
uri = f"api/v1/chart/?q={prison.dumps(arguments)}"
rv = self.get_assert_metric(uri, "get_list")
@ -680,14 +727,6 @@ class TestChartApi(SupersetTestCase, ApiOwnersTestCaseMixin):
data = json.loads(rv.data.decode("utf-8"))
self.assertEqual(data["count"], 0)
# rollback changes
db.session.delete(chart1)
db.session.delete(chart2)
db.session.delete(chart3)
db.session.delete(chart4)
db.session.delete(chart5)
db.session.commit()
@pytest.mark.usefixtures("create_charts")
def test_get_charts_favorite_filter(self):
"""