Skip to content

Commit

Permalink
add more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
eschutho committed Jun 2, 2022
1 parent a07a6dc commit 917dcf4
Show file tree
Hide file tree
Showing 2 changed files with 267 additions and 38 deletions.
6 changes: 3 additions & 3 deletions superset/explore/form_data/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ def post(self) -> Response:
@safe
@event_logger.log_this_with_context(
action=lambda self, *args, **kwargs: f"{self.__class__.__name__}.put",
log_to_statsd=False,
log_to_statsd=True,
)
@requires_json
def put(self, key: str) -> Response:
Expand Down Expand Up @@ -198,7 +198,7 @@ def put(self, key: str) -> Response:
@safe
@event_logger.log_this_with_context(
action=lambda self, *args, **kwargs: f"{self.__class__.__name__}.get",
log_to_statsd=False,
log_to_statsd=True,
)
def get(self, key: str) -> Response:
"""Retrives a form_data.
Expand Down Expand Up @@ -249,7 +249,7 @@ def get(self, key: str) -> Response:
@safe
@event_logger.log_this_with_context(
action=lambda self, *args, **kwargs: f"{self.__class__.__name__}.delete",
log_to_statsd=False,
log_to_statsd=True,
)
def delete(self, key: str) -> Response:
"""Deletes a form_data.
Expand Down
299 changes: 264 additions & 35 deletions tests/integration_tests/explore/form_data/commands_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,21 @@
# specific language governing permissions and limitations
# under the License.

import json
from unittest.mock import patch

import pytest

from superset import app, db, security_manager
from superset import app, db, security, security_manager
from superset.commands.exceptions import DatasourceTypeInvalidError
from superset.connectors.sqla.models import SqlaTable
from superset.explore.form_data.commands.create import CreateFormDataCommand
from superset.explore.form_data.commands.delete import DeleteFormDataCommand
from superset.explore.form_data.commands.get import GetFormDataCommand
from superset.explore.form_data.commands.parameters import CommandParameters
from superset.explore.form_data.commands.update import UpdateFormDataCommand
from superset.models.slice import Slice
from superset.models.sql_lab import Query
from superset.utils.core import DatasourceType, get_example_default_schema
from superset.utils.database import get_example_database
from tests.integration_tests.base_tests import SupersetTestCase
Expand Down Expand Up @@ -73,6 +78,26 @@ def create_slice(self):
session.delete(slice)
session.commit()

@pytest.fixture()
def create_query(self):
with self.create_app().app_context():
session = db.session

query = Query(
sql="select 1 as foo;",
client_id="sldkfjlk",
database=get_example_database(),
)

session.add(query)
session.commit()

yield query

# rollback
session.delete(query)
session.commit()

@patch("superset.security.manager.g")
@pytest.mark.usefixtures("create_dataset", "create_slice")
def test_create_form_data_command(self, mock_g):
Expand All @@ -82,49 +107,253 @@ def test_create_form_data_command(self, mock_g):
db.session.query(SqlaTable).filter_by(table_name="dummy_sql_table").first()
)
slice = db.session.query(Slice).filter_by(slice_name="slice_name").first()

datasource = f"{dataset.id}__{DatasourceType.TABLE}"
args = CommandParameters(
actor=mock_g.user,
datasource_id=dataset.id,
datasource_type=DatasourceType.TABLE,
chart_id=slice.id,
tab_id=1,
form_data="",
form_data=json.dumps({"datasource": datasource}),
)
command = CreateFormDataCommand(args)

assert isinstance(command.run(), str)

@patch("superset.security.manager.g")
@pytest.mark.usefixtures("create_dataset", "create_slice", "create_query")
def test_create_form_data_command_invalid_type(self, mock_g):
mock_g.user = security_manager.find_user("admin")
app.config["EXPLORE_FORM_DATA_CACHE_CONFIG"] = {
"REFRESH_TIMEOUT_ON_RETRIEVAL": True
}

dataset = (
db.session.query(SqlaTable).filter_by(table_name="dummy_sql_table").first()
)
slice = db.session.query(Slice).filter_by(slice_name="slice_name").first()

datasource = f"{dataset.id}__{DatasourceType.TABLE}"
create_args = CommandParameters(
actor=mock_g.user,
datasource_id=dataset.id,
datasource_type="InvalidType",
chart_id=slice.id,
tab_id=1,
form_data=json.dumps({"datasource": datasource}),
)
with pytest.raises(DatasourceTypeInvalidError) as exc:
CreateFormDataCommand(create_args).run()

assert "Datasource type is invalid" in str(exc.value)

@patch("superset.security.manager.g")
@pytest.mark.usefixtures("create_dataset", "create_slice", "create_query")
def test_create_form_data_command_type_as_string(self, mock_g):
mock_g.user = security_manager.find_user("admin")
app.config["EXPLORE_FORM_DATA_CACHE_CONFIG"] = {
"REFRESH_TIMEOUT_ON_RETRIEVAL": True
}

dataset = (
db.session.query(SqlaTable).filter_by(table_name="dummy_sql_table").first()
)
slice = db.session.query(Slice).filter_by(slice_name="slice_name").first()

datasource = f"{dataset.id}__{DatasourceType.TABLE}"
create_args = CommandParameters(
actor=mock_g.user,
datasource_id=dataset.id,
datasource_type="table",
chart_id=slice.id,
tab_id=1,
form_data=json.dumps({"datasource": datasource}),
)
command = CreateFormDataCommand(create_args)

assert isinstance(command.run(), str)

@patch("superset.security.manager.g")
@pytest.mark.usefixtures("create_dataset", "create_slice")
def test_get_form_data_command(self, mock_g):
mock_g.user = security_manager.find_user("admin")
app.config["EXPLORE_FORM_DATA_CACHE_CONFIG"] = {
"REFRESH_TIMEOUT_ON_RETRIEVAL": True
}

dataset = (
db.session.query(SqlaTable).filter_by(table_name="dummy_sql_table").first()
)
slice = db.session.query(Slice).filter_by(slice_name="slice_name").first()

datasource = f"{dataset.id}__{DatasourceType.TABLE}"
create_args = CommandParameters(
actor=mock_g.user,
datasource_id=dataset.id,
datasource_type=DatasourceType.TABLE,
chart_id=slice.id,
tab_id=1,
form_data=json.dumps({"datasource": datasource}),
)
key = CreateFormDataCommand(create_args).run()

key_args = CommandParameters(actor=mock_g.user, key=key)
get_command = GetFormDataCommand(key_args)
cache_data = json.loads(get_command.run())

assert cache_data.get("datasource") == datasource

@patch("superset.security.manager.g")
@pytest.mark.usefixtures("create_dataset", "create_slice", "create_query")
def test_update_form_data_command(self, mock_g):
mock_g.user = security_manager.find_user("admin")
app.config["EXPLORE_FORM_DATA_CACHE_CONFIG"] = {
"REFRESH_TIMEOUT_ON_RETRIEVAL": True
}

dataset = (
db.session.query(SqlaTable).filter_by(table_name="dummy_sql_table").first()
)
slice = db.session.query(Slice).filter_by(slice_name="slice_name").first()

query = db.session.query(Query).filter_by(sql="select 1 as foo;").first()

datasource = f"{dataset.id}__{DatasourceType.TABLE}"
create_args = CommandParameters(
actor=mock_g.user,
datasource_id=dataset.id,
datasource_type=DatasourceType.TABLE,
chart_id=slice.id,
tab_id=1,
form_data=json.dumps({"datasource": datasource}),
)
key = CreateFormDataCommand(create_args).run()

query_datasource = f"{dataset.id}__{DatasourceType.TABLE}"
update_args = CommandParameters(
actor=mock_g.user,
datasource_id=query.id,
datasource_type=DatasourceType.QUERY,
chart_id=slice.id,
tab_id=1,
form_data=json.dumps({"datasource": query_datasource}),
key=key,
)

update_command = UpdateFormDataCommand(update_args)
new_key = update_command.run()

# it should return a key
assert isinstance(new_key, str)
# the updated key returned should be different from the old one
assert new_key != key

key_args = CommandParameters(actor=mock_g.user, key=key)
get_command = GetFormDataCommand(key_args)

cache_data = json.loads(get_command.run())

assert cache_data.get("datasource") == query_datasource

@patch("superset.security.manager.g")
@pytest.mark.usefixtures("create_dataset", "create_slice", "create_query")
def test_update_form_data_command_same_form_data(self, mock_g):
mock_g.user = security_manager.find_user("admin")
app.config["EXPLORE_FORM_DATA_CACHE_CONFIG"] = {
"REFRESH_TIMEOUT_ON_RETRIEVAL": True
}

dataset = (
db.session.query(SqlaTable).filter_by(table_name="dummy_sql_table").first()
)
slice = db.session.query(Slice).filter_by(slice_name="slice_name").first()

datasource = f"{dataset.id}__{DatasourceType.TABLE}"
create_args = CommandParameters(
actor=mock_g.user,
datasource_id=dataset.id,
datasource_type=DatasourceType.TABLE,
chart_id=slice.id,
tab_id=1,
form_data=json.dumps({"datasource": datasource}),
)
key = CreateFormDataCommand(create_args).run()

update_args = CommandParameters(
actor=mock_g.user,
datasource_id=dataset.id,
datasource_type=DatasourceType.TABLE,
chart_id=slice.id,
tab_id=1,
form_data=json.dumps({"datasource": datasource}),
key=key,
)

update_command = UpdateFormDataCommand(update_args)
new_key = update_command.run()

# it should return a key
assert isinstance(new_key, str)

# the updated key returned should be the same as the old one
assert new_key == key

key_args = CommandParameters(actor=mock_g.user, key=key)
get_command = GetFormDataCommand(key_args)

cache_data = json.loads(get_command.run())

assert cache_data.get("datasource") == datasource

@patch("superset.security.manager.g")
@pytest.mark.usefixtures("create_dataset", "create_slice", "create_query")
def test_delete_form_data_command(self, mock_g):
mock_g.user = security_manager.find_user("admin")
app.config["EXPLORE_FORM_DATA_CACHE_CONFIG"] = {
"REFRESH_TIMEOUT_ON_RETRIEVAL": True
}

dataset = (
db.session.query(SqlaTable).filter_by(table_name="dummy_sql_table").first()
)
slice = db.session.query(Slice).filter_by(slice_name="slice_name").first()

datasource = f"{dataset.id}__{DatasourceType.TABLE}"
create_args = CommandParameters(
actor=mock_g.user,
datasource_id=dataset.id,
datasource_type=DatasourceType.TABLE,
chart_id=slice.id,
tab_id=1,
form_data=json.dumps({"datasource": datasource}),
)
key = CreateFormDataCommand(create_args).run()

delete_args = CommandParameters(
actor=mock_g.user,
key=key,
)

delete_command = DeleteFormDataCommand(delete_args)
response = delete_command.run()

assert response == True

@patch("superset.security.manager.g")
@pytest.mark.usefixtures("create_dataset", "create_slice", "create_query")
def test_delete_form_data_command_key_expired(self, mock_g):
mock_g.user = security_manager.find_user("admin")
app.config["EXPLORE_FORM_DATA_CACHE_CONFIG"] = {
"REFRESH_TIMEOUT_ON_RETRIEVAL": True
}

delete_args = CommandParameters(
actor=mock_g.user,
key="some_expired_key",
)

delete_command = DeleteFormDataCommand(delete_args)
response = delete_command.run()

# TODO
# @patch("superset.security.manager.g")
# @pytest.mark.usefixtures("create_dataset", "create_slice")
# def test_update_form_data_command(self, mock_g):
# mock_g.user = security_manager.find_user("admin")

# dataset = (
# db.session.query(SqlaTable).filter_by(
# table_name="dummy_sql_table").first()
# )
# slice = db.session.query(Slice).filter_by(
# slice_name="slice_name").first()
# create_args = CommandParameters(
# actor=mock_g.user,
# datasource_id=dataset.id,
# datasource_type=DatasourceType.TABLE,
# chart_id=slice.id,
# tab_id=1,
# form_data="",
# )
# key = CreateFormDataCommand(create_args)
# update_args = CommandParameters(
# actor=mock_g.user,
# datasource_id=dataset.id,
# datasource_type=DatasourceType.TABLE,
# chart_id=slice.id,
# tab_id=1,
# form_data="",
# key=key
# )
# command = UpdateFormDataCommand(update_args)

# assert isinstance(command.run(), str)
assert response == False

0 comments on commit 917dcf4

Please sign in to comment.