Skip to content

Commit

Permalink
updated test
Browse files Browse the repository at this point in the history
  • Loading branch information
hughhhh committed Jul 26, 2023
1 parent e1aa033 commit 903cdaa
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 3 deletions.
1 change: 0 additions & 1 deletion tests/integration_tests/tags/api_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -424,7 +424,6 @@ def test_add_delete_favorite_tag(self):
@pytest.mark.usefixtures("create_tags")
def test_add_tag_not_found(self):
self.login(username="admin")
user_id = self.get_user(username="admin").get_id()
uri = f"api/v1/tag/123/favorites/"
rv = self.client.post(uri, follow_redirects=True)

Expand Down
38 changes: 36 additions & 2 deletions tests/unit_tests/dao/tag_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,6 @@ def test_user_favorite_tag(mocker):
# Check that users_favorited was updated correctly
assert mock_TagDAO.find_by_id().users_favorited == [mock_g.user]

# Check that the tag was added to the session and the session was committed
mock_session.add.assert_called_once_with(mock_TagDAO.find_by_id())
mock_session.commit.assert_called_once()


Expand Down Expand Up @@ -74,3 +72,39 @@ def test_remove_user_favorite_tag(mocker):

# Check that the session was committed
mock_session.commit.assert_called_once()


def test_remove_user_favorite_tag_no_user(mocker):
from superset.daos.tag import TagDAO
from superset.exceptions import UserNotFound

# Mock the behavior of TagDAO and g
mock_session = mocker.patch("superset.daos.tag.db.session")
mock_TagDAO = mocker.patch("superset.daos.tag.TagDAO")
mock_tag = mocker.MagicMock(users_favorited=[])
mock_TagDAO.find_by_id.return_value = mock_tag

mock_g = mocker.patch("superset.daos.tag.g") # Replace with the actual path to g

# Test with no user
mock_g.user = None
with pytest.raises(UserNotFound):
TagDAO.remove_user_favorite_tag(1)


def test_remove_user_favorite_tag_exc_raise(mocker):
from superset.daos.tag import TagDAO
from superset.exceptions import UserNotFound

# Mock the behavior of TagDAO and g
mock_session = mocker.patch("superset.daos.tag.db.session")
mock_TagDAO = mocker.patch("superset.daos.tag.TagDAO")
mock_tag = mocker.MagicMock(users_favorited=[])
mock_TagDAO.find_by_id.return_value = mock_tag

mock_g = mocker.patch("superset.daos.tag.g") # Replace with the actual path to g

# Test that exception is raised when commit fails
mock_session.commit.side_effect = Exception("DB Error")
with pytest.raises(Exception):
TagDAO.remove_user_favorite_tag(1)

0 comments on commit 903cdaa

Please sign in to comment.