Skip to content

Commit

Permalink
[BUGFIX] Create project with adding _user_id for NotFoundException
Browse files Browse the repository at this point in the history
GitOrigin-RevId: 5fb82814ac62c90c9a9389faa7fc938e1e4ca487
  • Loading branch information
anastasia-nesterenko committed Aug 9, 2024
1 parent 786ae78 commit 4d3d914
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 3 deletions.
9 changes: 6 additions & 3 deletions src/gretel_client/gretel/interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
from gretel_client.projects import get_project, Project
from gretel_client.projects.jobs import Status
from gretel_client.projects.models import Model
from gretel_client.rest.exceptions import ApiException
from gretel_client.rest.exceptions import ApiException, NotFoundException
from gretel_client.users.users import get_me

try:
Expand Down Expand Up @@ -170,8 +170,11 @@ def set_project(
create=True,
session=self._session,
)
except ApiException as exception:
if "Project name not available" not in exception.body:
except (ApiException, NotFoundException) as exception:
if (
"Project name not available" not in exception.body
and "not found" not in exception.body
):
raise exception
logger.warning(
f"Project name `{name}` is not unique -> "
Expand Down
56 changes: 56 additions & 0 deletions tests/gretel_client/test_gretel_interface.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
from unittest.mock import call, patch

from gretel_client.gretel.interface import Gretel
from gretel_client.projects.projects import Project
from gretel_client.rest.exceptions import NotFoundException
from python.tests.gretel_client.test_errors import MockResponse


@patch(
"gretel_client.gretel.interface.get_me", return_value={"_id": "01234567_user_id"}
)
@patch("gretel_client.gretel.interface.add_session_context", return_value=None)
@patch("gretel_client.gretel.interface.configure_session", return_value=None)
@patch("gretel_client.gretel.interface.get_project")
def test_project_creation(
mock_get_project, mock_configure_session, mock_add_session_context, mock_user_id
):
"""
Test Gretel with existing project creates project with user_id suffix.
"""
unique_project_name = "project-name-user_id"
mock_get_project.side_effect = [
NotFoundException(
http_resp=MockResponse(
reason="NotFound", status=404, resp={"message": "not found"}
)
),
Project(
name=unique_project_name,
project_id="project_id",
project_guid="project_guid",
session=mock_configure_session,
),
]
gretel = Gretel(
project_name="project-name",
api_key="grtu...",
endpoint="https://api-dev.gretel.cloud",
validate=True,
)
assert mock_get_project.call_count == 2
assert mock_get_project.call_args_list[0] == call(
name="project-name",
display_name="project-name",
desc=None,
create=True,
session=None,
)
assert mock_get_project.call_args_list[1] == call(
name=unique_project_name,
display_name="project-name",
desc=None,
create=True,
session=None,
)
assert gretel._project.name == unique_project_name

0 comments on commit 4d3d914

Please sign in to comment.