From 30f241b4be26e072af62b3e51648d87d5f78313e Mon Sep 17 00:00:00 2001 From: dominik003 Date: Tue, 17 Oct 2023 18:07:06 +0200 Subject: [PATCH] test: Add tests for renaming tool models --- .../toolmodels/test_toolmodel_routes.py | 115 ++++++++++++++++++ 1 file changed, 115 insertions(+) create mode 100644 backend/tests/projects/toolmodels/test_toolmodel_routes.py diff --git a/backend/tests/projects/toolmodels/test_toolmodel_routes.py b/backend/tests/projects/toolmodels/test_toolmodel_routes.py new file mode 100644 index 0000000000..0767fb8fe2 --- /dev/null +++ b/backend/tests/projects/toolmodels/test_toolmodel_routes.py @@ -0,0 +1,115 @@ +# SPDX-FileCopyrightText: Copyright DB Netz AG and the capella-collab-manager contributors +# SPDX-License-Identifier: Apache-2.0 + + +from unittest import mock + +import pytest +from fastapi import testclient +from sqlalchemy import orm + +from capellacollab.__main__ import app +from capellacollab.projects import injectables as projects_injectables +from capellacollab.projects.toolmodels import ( + injectables as toolmodels_injectables, +) +from capellacollab.users import crud as users_crud +from capellacollab.users import models as users_models + + +@pytest.fixture(name="mock_tool") +def fixture_mock_tool(): + mock_tool = mock.Mock(name="tool") + mock_tool.name = "test" + + return mock_tool + + +@pytest.fixture(name="override_dependency") +def fixture_override_dependency(mock_tool): + mock_project = mock.Mock(name="DatabaseProject") + + mock_model = mock.Mock(name="DatabaseModel") + mock_model.slug = "any-slug" + mock_model.tool = mock_tool + + app.dependency_overrides[ + projects_injectables.get_existing_project + ] = lambda: mock_project + app.dependency_overrides[ + toolmodels_injectables.get_existing_capella_model + ] = lambda: mock_model + + yield + + del app.dependency_overrides[projects_injectables.get_existing_project] + del app.dependency_overrides[ + toolmodels_injectables.get_existing_capella_model + ] + + +@pytest.mark.usefixtures("override_dependency") +def test_rename_toolmodel_successful( + mock_tool, + capella_model, + client: testclient.TestClient, + executor_name: str, + db: orm.Session, +): + users_crud.create_user(db, executor_name, users_models.Role.ADMIN) + + with mock.patch( + "capellacollab.projects.toolmodels.crud.get_model_by_slugs", + autospec=True, + ) as mock_get_model_by_slugs, mock.patch( + "capellacollab.projects.toolmodels.crud.update_model", autospec=True + ) as mock_update_model, mock.patch( + "capellacollab.projects.toolmodels.routes.get_version_by_id_or_raise", + autospec=True, + ) as mock_get_version_by_id_or_raise, mock.patch( + "capellacollab.projects.toolmodels.routes.get_nature_by_id_or_raise", + autospec=True, + ) as mock_get_nature_by_id_or_raise: + mock_get_model_by_slugs.return_value = None + mock_update_model.return_value = capella_model + + mock_version = mock.Mock(name="versions") + mock_version.tool = mock_tool + mock_get_version_by_id_or_raise.return_value = mock_version + + mock_nature = mock.Mock(name="types") + mock_nature.tool = mock_tool + mock_get_nature_by_id_or_raise.return_value = mock_nature + + response = client.patch( + "/api/v1/projects/any/models/any", + json={"name": "new-name", "version_id": -1, "nature_id": -1}, + ) + + assert response.status_code == 200 + mock_get_model_by_slugs.assert_called_once() + mock_update_model.assert_called_once() + + +@pytest.mark.usefixtures("override_dependency") +def test_rename_toolmodel_where_name_already_exists( + client: testclient.TestClient, + executor_name: str, + db: orm.Session, +): + users_crud.create_user(db, executor_name, users_models.Role.ADMIN) + + with mock.patch( + "capellacollab.projects.toolmodels.crud.get_model_by_slugs", + autospec=True, + ) as mock_get_model_by_slugs: + mock_get_model_by_slugs.return_value = "anything" + + response = client.patch( + "/api/v1/projects/any/models/any", + json={"name": "new-name", "version_id": -1, "nature_id": -1}, + ) + + assert response.status_code == 409 + assert "A model with a similar name already exists" in response.text + mock_get_model_by_slugs.assert_called_once()