Skip to content

Commit

Permalink
update test
Browse files Browse the repository at this point in the history
  • Loading branch information
hughhhh committed Sep 18, 2023
1 parent 26746d0 commit b30eaa6
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 9 deletions.
3 changes: 0 additions & 3 deletions tests/unit_tests/dao/tag_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,3 @@ def test_create_tag_relationship(mocker):
# Verify that the correct number of TaggedObjects are added to the session
assert mock_session.add_all.call_count == 1
assert len(mock_session.add_all.call_args[0][0]) == len(objects_to_tag)

# Verify that commit is called
mock_session.commit.assert_called_once()
19 changes: 17 additions & 2 deletions tests/unit_tests/tags/commands/create_test.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import pytest
from pytest_mock import MockFixture
from sqlalchemy.orm.session import Session

from superset.utils.core import DatasourceType
Expand Down Expand Up @@ -47,7 +48,7 @@ def session_with_data(session: Session):
yield session


def test_create_command_success(session_with_data: Session):
def test_create_command_success(session_with_data: Session, mocker: MockFixture):
from superset.connectors.sqla.models import SqlaTable
from superset.daos.tag import TagDAO
from superset.models.dashboard import Dashboard
Expand All @@ -61,6 +62,12 @@ def test_create_command_success(session_with_data: Session):
chart = session_with_data.query(Slice).first()
dashboard = session_with_data.query(Dashboard).first()

mocker.patch(
"superset.security.SupersetSecurityManager.is_admin", return_value=True
)
mocker.patch("superset.daos.chart.ChartDAO.find_by_id", return_value=chart)
mocker.patch("superset.daos.query.SavedQueryDAO.find_by_id", return_value=query)

objects_to_tag = [
(ObjectTypes.query, query.id),
(ObjectTypes.chart, chart.id),
Expand All @@ -84,7 +91,9 @@ def test_create_command_success(session_with_data: Session):
)


def test_create_command_failed_validate(session_with_data: Session):
def test_create_command_failed_validate(
session_with_data: Session, mocker: MockFixture
):
from superset.connectors.sqla.models import SqlaTable
from superset.daos.tag import TagDAO
from superset.models.dashboard import Dashboard
Expand All @@ -98,6 +107,12 @@ def test_create_command_failed_validate(session_with_data: Session):
chart = session_with_data.query(Slice).first()
dashboard = session_with_data.query(Dashboard).first()

mocker.patch(
"superset.security.SupersetSecurityManager.is_admin", return_value=True
)
mocker.patch("superset.daos.chart.ChartDAO.find_by_id", return_value=query)
mocker.patch("superset.daos.query.SavedQueryDAO.find_by_id", return_value=chart)

objects_to_tag = [
(ObjectTypes.query, query.id),
(ObjectTypes.chart, chart.id),
Expand Down
35 changes: 31 additions & 4 deletions tests/unit_tests/tags/commands/update_test.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import pytest
from pytest_mock import MockFixture
from sqlalchemy.orm.session import Session

from superset.utils.core import DatasourceType
Expand Down Expand Up @@ -56,13 +57,19 @@ def session_with_data(session: Session):
yield session


def test_update_command_success(session_with_data: Session):
def test_update_command_success(session_with_data: Session, mocker: MockFixture):
from superset.daos.tag import TagDAO
from superset.models.dashboard import Dashboard
from superset.tags.commands.update import UpdateTagCommand
from superset.tags.models import ObjectTypes, TaggedObject

dashboard = session_with_data.query(Dashboard).first()
mocker.patch(
"superset.security.SupersetSecurityManager.is_admin", return_value=True
)
mocker.patch(
"superset.daos.dashboard.DashboardDAO.find_by_id", return_value=dashboard
)

objects_to_tag = [
(ObjectTypes.dashboard, dashboard.id),
Expand All @@ -84,7 +91,9 @@ def test_update_command_success(session_with_data: Session):
assert len(session_with_data.query(TaggedObject).all()) == len(objects_to_tag)


def test_update_command_success_duplicates(session_with_data: Session):
def test_update_command_success_duplicates(
session_with_data: Session, mocker: MockFixture
):
from superset.daos.tag import TagDAO
from superset.models.dashboard import Dashboard
from superset.models.slice import Slice
Expand All @@ -95,6 +104,14 @@ def test_update_command_success_duplicates(session_with_data: Session):
dashboard = session_with_data.query(Dashboard).first()
chart = session_with_data.query(Slice).first()

mocker.patch(
"superset.security.SupersetSecurityManager.is_admin", return_value=True
)
mocker.patch("superset.daos.chart.ChartDAO.find_by_id", return_value=chart)
mocker.patch(
"superset.daos.dashboard.DashboardDAO.find_by_id", return_value=dashboard
)

objects_to_tag = [
(ObjectTypes.dashboard, dashboard.id),
]
Expand Down Expand Up @@ -124,21 +141,31 @@ def test_update_command_success_duplicates(session_with_data: Session):
assert changed_model.objects[0].object_id == chart.id


def test_update_command_failed_validation(session_with_data: Session):
def test_update_command_failed_validation(
session_with_data: Session, mocker: MockFixture
):
from superset.daos.tag import TagDAO
from superset.models.dashboard import Dashboard
from superset.models.slice import Slice
from superset.tags.commands.create import CreateCustomTagWithRelationshipsCommand
from superset.tags.commands.exceptions import TagInvalidError
from superset.tags.commands.update import UpdateTagCommand
from superset.tags.models import ObjectTypes, TaggedObject
from superset.tags.models import ObjectTypes

dashboard = session_with_data.query(Dashboard).first()
chart = session_with_data.query(Slice).first()
objects_to_tag = [
(ObjectTypes.chart, chart.id),
]

mocker.patch(
"superset.security.SupersetSecurityManager.is_admin", return_value=True
)
mocker.patch("superset.daos.chart.ChartDAO.find_by_id", return_value=chart)
mocker.patch(
"superset.daos.dashboard.DashboardDAO.find_by_id", return_value=dashboard
)

CreateCustomTagWithRelationshipsCommand(
data={"name": "test_tag", "objects_to_tag": objects_to_tag}
).run()
Expand Down

0 comments on commit b30eaa6

Please sign in to comment.