Skip to content

Commit

Permalink
feat: Verify compatibility of TeamForCapella model links at all times
Browse files Browse the repository at this point in the history
  • Loading branch information
MoritzWeber0 committed Sep 11, 2024
1 parent bc0ea05 commit c75a139
Show file tree
Hide file tree
Showing 15 changed files with 302 additions and 56 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import sqlalchemy as sa
from sqlalchemy import orm

from capellacollab.core import database
from capellacollab.projects.toolmodels import models as toolmodels_models
from capellacollab.projects.toolmodels.modelsources.t4c import models
from capellacollab.settings.modelsources.t4c.repositories import (
Expand Down Expand Up @@ -59,10 +58,13 @@ def create_t4c_model(
def patch_t4c_model(
db: orm.Session,
t4c_model: models.DatabaseT4CModel,
patch_model: models.SubmitT4CModel,
name: str | None = None,
repository: repositories_models.DatabaseT4CRepository | None = None,
) -> models.DatabaseT4CModel:
database.patch_database_with_pydantic_object(t4c_model, patch_model)

if name is not None:
t4c_model.name = name
if repository is not None:
t4c_model.repository = repository
db.commit()
return t4c_model

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,12 @@ class SubmitT4CModel(core_pydantic.BaseModel):
t4c_repository_id: int


class PatchT4CModel(core_pydantic.BaseModel):
name: str | None = None
t4c_instance_id: int | None = None
t4c_repository_id: int | None = None


class SimpleT4CModel(core_pydantic.BaseModel):
project_name: str
repository_name: str
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
from capellacollab.projects.toolmodels.backups import crud as backups_crud
from capellacollab.projects.users import models as projects_users_models
from capellacollab.settings.modelsources.t4c import (
injectables as settings_t4c_injecatbles,
injectables as settings_t4c_injectables,
)
from capellacollab.settings.modelsources.t4c.repositories import (
injectables as settings_t4c_repositories_injectables,
Expand Down Expand Up @@ -87,7 +87,7 @@ def create_t4c_model(
),
db: orm.Session = fastapi.Depends(database.get_db),
):
instance = settings_t4c_injecatbles.get_existing_unarchived_instance(
instance = settings_t4c_injectables.get_existing_unarchived_instance(
body.t4c_instance_id, db
)
repository = (
Expand Down Expand Up @@ -118,14 +118,26 @@ def create_t4c_model(
],
),
)
def edit_t4c_model(
body: models.SubmitT4CModel,
def update_t4c_model(
body: models.PatchT4CModel,
t4c_model: models.DatabaseT4CModel = fastapi.Depends(
injectables.get_existing_t4c_model
),
db: orm.Session = fastapi.Depends(database.get_db),
):
return crud.patch_t4c_model(db, t4c_model, body)
if body.t4c_instance_id is not None:
instance = settings_t4c_injectables.get_existing_unarchived_instance(
body.t4c_instance_id, db
)
else:
instance = t4c_model.repository.instance

repository = (
settings_t4c_repositories_injectables.get_existing_t4c_repository(
body.t4c_repository_id or t4c_model.repository.id, db, instance
)
)
return crud.patch_t4c_model(db, t4c_model, body.name, repository)


@router.delete(
Expand Down
1 change: 0 additions & 1 deletion backend/capellacollab/settings/modelsources/t4c/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,6 @@ class PatchT4CInstance(core_pydantic.BaseModel):
username: str | None = None
password: str | None = None
protocol: Protocol | None = None
version_id: int | None = None
is_archived: bool | None = None

_validate_rest_api_url = pydantic.field_validator("rest_api")(
Expand Down
33 changes: 0 additions & 33 deletions backend/tests/projects/toolmodels/fixtures.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,6 @@
import capellacollab.projects.models as projects_models
import capellacollab.projects.toolmodels.crud as toolmodels_crud
import capellacollab.projects.toolmodels.models as toolmodels_models
import capellacollab.projects.toolmodels.modelsources.git.crud as project_git_crud
import capellacollab.projects.toolmodels.modelsources.git.models as project_git_models
import capellacollab.projects.toolmodels.modelsources.t4c.crud as models_t4c_crud
import capellacollab.projects.toolmodels.modelsources.t4c.models as models_t4c_models
import capellacollab.settings.modelsources.t4c.repositories.models as settings_t4c_repositories_models
import capellacollab.tools.models as tools_models


Expand Down Expand Up @@ -41,7 +36,6 @@ def fixture_jupyter_model(
name="Jupyter test",
description="",
tool_id=jupyter_tool.id,
configuration={"workspace": str(uuid.uuid4())},
)
return toolmodels_crud.create_model(
db,
Expand All @@ -50,30 +44,3 @@ def fixture_jupyter_model(
tool=jupyter_tool,
configuration={"workspace": "test"},
)


@pytest.fixture(name="git_model")
def fixture_git_model(
db: orm.Session, capella_model: toolmodels_models.DatabaseToolModel
) -> project_git_models.DatabaseGitModel:
git_model = project_git_models.PostGitModel(
path="https://example.com/test/project",
entrypoint="test/test.aird",
revision="main",
username="user",
password="password",
)
return project_git_crud.add_git_model_to_capellamodel(
db, capella_model, git_model
)


@pytest.fixture(name="t4c_model")
def fixture_t4c_model(
db: orm.Session,
capella_model: toolmodels_models.DatabaseToolModel,
t4c_repository: settings_t4c_repositories_models.DatabaseT4CRepository,
) -> models_t4c_models.DatabaseT4CModel:
return models_t4c_crud.create_t4c_model(
db, capella_model, t4c_repository, "default"
)
39 changes: 39 additions & 0 deletions backend/tests/projects/toolmodels/modelsources/fixtures.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# SPDX-FileCopyrightText: Copyright DB InfraGO AG and contributors
# SPDX-License-Identifier: Apache-2.0

import pytest
from sqlalchemy import orm

import capellacollab.projects.toolmodels.models as toolmodels_models
import capellacollab.projects.toolmodels.modelsources.git.crud as project_git_crud
import capellacollab.projects.toolmodels.modelsources.git.models as project_git_models
import capellacollab.projects.toolmodels.modelsources.t4c.crud as models_t4c_crud
import capellacollab.projects.toolmodels.modelsources.t4c.models as models_t4c_models
import capellacollab.settings.modelsources.t4c.repositories.models as settings_t4c_repositories_models


@pytest.fixture(name="t4c_model")
def fixture_t4c_model(
db: orm.Session,
capella_model: toolmodels_models.DatabaseToolModel,
t4c_repository: settings_t4c_repositories_models.DatabaseT4CRepository,
) -> models_t4c_models.DatabaseT4CModel:
return models_t4c_crud.create_t4c_model(
db, capella_model, t4c_repository, "default"
)


@pytest.fixture(name="git_model")
def fixture_git_model(
db: orm.Session, capella_model: toolmodels_models.DatabaseToolModel
) -> project_git_models.DatabaseGitModel:
git_model = project_git_models.PostGitModel(
path="https://example.com/test/project",
entrypoint="test/test.aird",
revision="main",
username="user",
password="password",
)
return project_git_crud.add_git_model_to_capellamodel(
db, capella_model, git_model
)
186 changes: 186 additions & 0 deletions backend/tests/projects/toolmodels/modelsources/test_t4c_routes.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,186 @@
# SPDX-FileCopyrightText: Copyright DB InfraGO AG and contributors
# SPDX-License-Identifier: Apache-2.0


import pytest
from fastapi import testclient
from sqlalchemy import orm

import capellacollab.projects.models as projects_models
import capellacollab.settings.modelsources.t4c.repositories.crud as t4c_repositories_crud
import capellacollab.settings.modelsources.t4c.repositories.models as t4c_repositories_models
from capellacollab.projects.toolmodels import models as toolmodels_models
from capellacollab.projects.toolmodels.modelsources.t4c import crud as t4c_crud
from capellacollab.projects.toolmodels.modelsources.t4c import (
models as t4c_models,
)
from capellacollab.settings.modelsources.t4c import crud as t4c_servers_crud
from capellacollab.settings.modelsources.t4c import (
models as t4c_servers_models,
)
from capellacollab.settings.modelsources.t4c.repositories import (
models as t4c_repositories_models,
)
from capellacollab.tools import models as tools_models


@pytest.mark.usefixtures("project_manager")
def test_list_t4c_models(
client: testclient.TestClient,
project: projects_models.DatabaseProject,
capella_model: toolmodels_models.DatabaseToolModel,
t4c_model: t4c_models.DatabaseT4CModel,
):
response = client.get(
f"/api/v1/projects/{project.slug}/models/{capella_model.slug}/modelsources/t4c"
)

assert response.status_code == 200
assert len(response.json()) == 1
assert response.json()[0]["name"] == t4c_model.name


@pytest.mark.usefixtures("project_manager")
def test_get_t4c_model(
client: testclient.TestClient,
project: projects_models.DatabaseProject,
capella_model: toolmodels_models.DatabaseToolModel,
t4c_model: t4c_models.DatabaseT4CModel,
):
response = client.get(
f"/api/v1/projects/{project.slug}/models/{capella_model.slug}/modelsources/t4c/{t4c_model.id}"
)

assert response.status_code == 200
assert response.json()["name"] == t4c_model.name
assert (
response.json()["repository"]["instance"]["id"]
== t4c_model.repository.instance.id
)


@pytest.mark.usefixtures("admin")
def test_create_t4c_model(
client: testclient.TestClient,
project: projects_models.DatabaseProject,
t4c_repository: t4c_repositories_models.DatabaseT4CRepository,
capella_model: toolmodels_models.DatabaseToolModel,
):
response = client.post(
f"/api/v1/projects/{project.slug}/models/{capella_model.slug}/modelsources/t4c",
json={
"name": "project",
"t4c_instance_id": t4c_repository.instance.id,
"t4c_repository_id": t4c_repository.id,
},
)

assert response.status_code == 200
assert response.json()["name"] == "project"
assert response.json()["repository"]["id"] == t4c_repository.id
assert (
response.json()["repository"]["instance"]["id"]
== t4c_repository.instance.id
)


@pytest.mark.usefixtures("admin", "t4c_model")
def test_create_t4c_model_twice_fails(
client: testclient.TestClient,
project: projects_models.DatabaseProject,
t4c_repository: t4c_repositories_models.DatabaseT4CRepository,
capella_model: toolmodels_models.DatabaseToolModel,
):
response = client.post(
f"/api/v1/projects/{project.slug}/models/{capella_model.slug}/modelsources/t4c",
json={
"name": "default",
"t4c_instance_id": t4c_repository.instance.id,
"t4c_repository_id": t4c_repository.id,
},
)

assert response.status_code == 409
assert (
response.json()["detail"]["err_code"]
== "T4C_INTEGRATION_ALREADY_EXISTS"
)


@pytest.mark.usefixtures("admin")
def test_change_server_of_t4c_model(
db: orm.Session,
client: testclient.TestClient,
project: projects_models.DatabaseProject,
capella_model: toolmodels_models.DatabaseToolModel,
t4c_model: t4c_models.DatabaseT4CModel,
tool_version: tools_models.DatabaseVersion,
):
server = t4c_servers_models.DatabaseT4CInstance(
name="test server 2",
license="lic",
host="localhost",
usage_api="http://localhost:8086",
rest_api="http://localhost:8080/api/v1.0",
username="user",
password="pass",
protocol=t4c_servers_models.Protocol.tcp,
version=tool_version,
)
db_server = t4c_servers_crud.create_t4c_instance(db, server)

second_t4c_repository = t4c_repositories_crud.create_t4c_repository(
db=db, repo_name="test2", instance=db_server
)

response = client.patch(
f"/api/v1/projects/{project.slug}/models/{capella_model.slug}/modelsources/t4c/{t4c_model.id}",
json={
"t4c_instance_id": db_server.id,
"t4c_repository_id": second_t4c_repository.id,
},
)

assert response.status_code == 200
assert response.json()["name"] == t4c_model.name
assert response.json()["repository"]["id"] == second_t4c_repository.id
assert (
response.json()["repository"]["instance"]["id"]
== second_t4c_repository.instance.id
)


@pytest.mark.usefixtures("admin")
def test_patch_name_of_t4c_model(
client: testclient.TestClient,
project: projects_models.DatabaseProject,
capella_model: toolmodels_models.DatabaseToolModel,
t4c_model: t4c_models.DatabaseT4CModel,
):
response = client.patch(
f"/api/v1/projects/{project.slug}/models/{capella_model.slug}/modelsources/t4c/{t4c_model.id}",
json={"name": "new_default"},
)

assert response.status_code == 200
assert response.json()["name"] == "new_default"
assert (
response.json()["repository"]["instance"]["id"]
== t4c_model.repository.instance.id
)


@pytest.mark.usefixtures("admin")
def test_unlink_t4c_model(
db: orm.Session,
client: testclient.TestClient,
project: projects_models.DatabaseProject,
capella_model: toolmodels_models.DatabaseToolModel,
t4c_model: t4c_models.DatabaseT4CModel,
):
response = client.delete(
f"/api/v1/projects/{project.slug}/models/{capella_model.slug}/modelsources/t4c/{t4c_model.id}",
)

assert response.status_code == 204
assert t4c_crud.get_t4c_model_by_id(db, t4c_model.id) is None
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
-->

<h2 class="mb-2 text-xl font-medium">Git repositories</h2>
<div class="flex flex-wrap gap-2">
<div class="flex flex-wrap gap-5">
<a [routerLink]="['..', 'git-model', 'create']">
<div matRipple class="mat-card-overview new collab-card">
<div class="content">
Expand Down Expand Up @@ -58,7 +58,7 @@ <h2 class="mb-2 text-xl font-medium">Git repositories</h2>
userService.user?.role === "administrator"
) {
<h2 class="mb-2 mt-5 text-xl font-medium">TeamForCapella repositories</h2>
<div class="flex flex-wrap gap-2">
<div class="flex flex-wrap gap-5">
<a [routerLink]="['..', 't4c-model', 'create-existing']">
<div matRipple class="mat-card-overview new collab-card">
<div class="content">
Expand Down
Loading

0 comments on commit c75a139

Please sign in to comment.