Skip to content

Commit

Permalink
[API] Fix artifact storing without project in body (#1608)
Browse files Browse the repository at this point in the history
  • Loading branch information
Tankilevitch committed Jan 8, 2022
1 parent 3b5cfe4 commit 36e26e9
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 4 deletions.
9 changes: 9 additions & 0 deletions mlrun/api/crud/artifacts.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,15 @@ def store_artifact(
project: str = mlrun.mlconf.default_project,
):
project = project or mlrun.mlconf.default_project
# In case project is an empty string the setdefault won't catch it
if not data.setdefault("project", project):
data["project"] = project

if data["project"] != project:
raise mlrun.errors.MLRunInvalidArgumentError(
f"Artifact with conflicting project name - {data['project']} while request project : {project}."
f"key={key}, uid={uid}, data={data}"
)
mlrun.api.utils.singletons.db.get_db().store_artifact(
db_session, key, data, uid, iter, tag, project,
)
Expand Down
67 changes: 63 additions & 4 deletions tests/api/api/test_artifacts.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,68 @@
from fastapi.testclient import TestClient
from sqlalchemy.orm import Session

import mlrun.api.schemas

PROJECT = "prj"
KEY = "some-key"
UID = "some-uid"
TAG = "some-tag"
API_PROJECTS_PATH = "/api/projects"
API_ARTIFACT_PATH = "/api/artifact"
API_ARTIFACTS_PATH = "/api/artifacts"


def test_list_artifact_tags(db: Session, client: TestClient) -> None:
project = "p11"
resp = client.get(f"/api/projects/{project}/artifact-tags")
assert resp.status_code == HTTPStatus.OK.value, "status"
assert resp.json()["project"] == project, "project"
resp = client.get(f"{API_PROJECTS_PATH}/{PROJECT}/artifact-tags")
assert resp.status_code == HTTPStatus.OK, "status"
assert resp.json()["project"] == PROJECT, "project"


def _create_project(client: TestClient, project_name: str = PROJECT):
project = mlrun.api.schemas.Project(
metadata=mlrun.api.schemas.ProjectMetadata(name=project_name),
spec=mlrun.api.schemas.ProjectSpec(
description="banana", source="source", goals="some goals"
),
)
resp = client.post(API_PROJECTS_PATH, json=project.dict())
assert resp.status_code == HTTPStatus.CREATED
return resp


def test_store_artifact_with_empty_dict(db: Session, client: TestClient):
_create_project(client)

resp = client.post(
f"{API_ARTIFACT_PATH}/{PROJECT}/{UID}/{KEY}?tag={TAG}", data="{}"
)
assert resp.status_code == HTTPStatus.OK

resp = client.get(f"{API_PROJECTS_PATH}/{PROJECT}/artifact/{KEY}?tag={TAG}")
assert resp.status_code == HTTPStatus.OK


def test_delete_artifacts_after_storing_empty_dict(db: Session, client: TestClient):
_create_project(client)
empty_artifact = "{}"

resp = client.post(
f"{API_ARTIFACT_PATH}/{PROJECT}/{UID}/{KEY}?tag={TAG}", data=empty_artifact
)
assert resp.status_code == HTTPStatus.OK

resp = client.post(
f"{API_ARTIFACT_PATH}/{PROJECT}/{UID}2/{KEY}2?tag={TAG}", data=empty_artifact
)
assert resp.status_code == HTTPStatus.OK

project_artifacts_path = f"{API_ARTIFACTS_PATH}?project={PROJECT}"

resp = client.get(project_artifacts_path)
assert len(resp.json()["artifacts"]) == 2

resp = client.delete(project_artifacts_path)
assert resp.status_code == HTTPStatus.OK

resp = client.get(project_artifacts_path)
assert len(resp.json()["artifacts"]) == 0

0 comments on commit 36e26e9

Please sign in to comment.