Skip to content

Commit

Permalink
@sanderegg review: renamed all as metadata
Browse files Browse the repository at this point in the history
  • Loading branch information
pcrespov committed Jul 7, 2023
1 parent ce0c5a3 commit 3ae0229
Show file tree
Hide file tree
Showing 9 changed files with 43 additions and 43 deletions.
12 changes: 6 additions & 6 deletions api/specs/webserver/scripts/openapi_projects_metadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,12 @@
from _common import CURRENT_DIR, create_openapi_specs
from fastapi import Depends, FastAPI, status
from models_library.api_schemas_webserver.projects_metadata import (
ProjectCustomMetadataGet,
ProjectCustomMetadataUpdate,
ProjectMetadataGet,
ProjectMetadataUpdate,
)
from models_library.generics import Envelope
from simcore_service_webserver.projects._metadata_handlers import (
ProjectCustomMetadataGet,
ProjectMetadataGet,
ProjectPathParams,
)

Expand All @@ -36,7 +36,7 @@

@app.get(
"/projects/{project_id}/metadata",
response_model=Envelope[ProjectCustomMetadataGet],
response_model=Envelope[ProjectMetadataGet],
tags=TAGS,
operation_id="get_project_custom_metadata",
status_code=status.HTTP_200_OK,
Expand All @@ -47,13 +47,13 @@ async def get_project_custom_metadata(params: Annotated[ProjectPathParams, Depen

@app.patch(
"/projects/{project_id}/metadata",
response_model=Envelope[ProjectCustomMetadataGet],
response_model=Envelope[ProjectMetadataGet],
tags=TAGS,
operation_id="update_project_custom_metadata",
status_code=status.HTTP_200_OK,
)
async def update_project_custom_metadata(
params_: Annotated[ProjectPathParams, Depends()], body_: ProjectCustomMetadataUpdate
params_: Annotated[ProjectPathParams, Depends()], body_: ProjectMetadataUpdate
):
...

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@
MetadataDict: TypeAlias = dict[str, MetaValueType]


class ProjectCustomMetadataGet(OutputSchema):
class ProjectMetadataGet(OutputSchema):
project_uuid: ProjectID
metadata: MetadataDict = Field(
custom: MetadataDict = Field(
default_factory=dict, description="Custom key-value map"
)


class ProjectCustomMetadataUpdate(InputSchema):
metadata: MetadataDict
class ProjectMetadataUpdate(InputSchema):
custom: MetadataDict
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ def upgrade():
"projects_metadata",
sa.Column("project_uuid", sa.String(), nullable=False),
sa.Column(
"custom_metadata",
"custom",
postgresql.JSONB(astext_type=sa.Text()),
server_default=sa.text("'{}'::jsonb"),
nullable=False,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,11 @@
doc="The project unique identifier is also used to identify the associated job",
),
sa.Column(
"custom_metadata",
"custom",
JSONB,
nullable=False,
server_default=sa.text("'{}'::jsonb"),
doc="Unstructured free json for user to store metadata",
doc="Reserved for the user to store custom metadata",
),
# TIME STAMPS ----ß
column_created_datetime(timezone=True),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class DBProjectNotFoundError(Exception):

@dataclass(frozen=True, slots=True, kw_only=True)
class ProjectMetadata(FromRowMixin):
custom_metadata: dict[str, Any] | None
custom: dict[str, Any] | None
created: datetime.datetime | None
modified: datetime.datetime | None

Expand Down Expand Up @@ -76,7 +76,7 @@ async def upsert(
) -> ProjectMetadata:
data = {
"project_uuid": f"{project_uuid}",
"custom_metadata": custom_metadata,
"custom": custom_metadata,
}

try:
Expand Down
28 changes: 16 additions & 12 deletions packages/postgres-database/tests/test_utils_projects_metadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,24 +57,28 @@ async def test_projects_metadata_repository(
connection, project_uuid=faker.uuid4(), custom_metadata=user_metadata
)

pm = await ProjectMetadataRepo.get(connection, project_uuid=project["uuid"])
assert pm is not None
assert pm.custom_metadata is None
project_metadata = await ProjectMetadataRepo.get(
connection, project_uuid=project["uuid"]
)
assert project_metadata is not None
assert project_metadata.custom is None

got = await ProjectMetadataRepo.upsert(
connection, project_uuid=project["uuid"], custom_metadata=user_metadata
)
assert got.custom_metadata
assert user_metadata == got.custom_metadata
assert got.custom
assert user_metadata == got.custom

pm = await ProjectMetadataRepo.get(connection, project_uuid=project["uuid"])
assert pm is not None
assert pm == got
project_metadata = await ProjectMetadataRepo.get(
connection, project_uuid=project["uuid"]
)
assert project_metadata is not None
assert project_metadata == got

got2 = await ProjectMetadataRepo.upsert(
got_after_update = await ProjectMetadataRepo.upsert(
connection, project_uuid=project["uuid"], custom_metadata={}
)
assert got2.custom_metadata == {}
assert got_after_update.custom == {}
assert got.modified
assert got2.modified
assert got.modified < got2.modified
assert got_after_update.modified
assert got.modified < got_after_update.modified
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ async def get_project_metadata(engine: Engine, project_uuid: ProjectID) -> Metad
async with _acquire_and_handle(engine, project_uuid) as connection:
pm = await ProjectMetadataRepo.get(connection, project_uuid=project_uuid)
# NOTE: if no metadata in table, it returns None -- which converts here to --> {}
return parse_obj_as(MetadataDict, pm.custom_metadata or {})
return parse_obj_as(MetadataDict, pm.custom or {})


async def upsert_project_metadata(
Expand All @@ -47,4 +47,4 @@ async def upsert_project_metadata(
pm = await ProjectMetadataRepo.upsert(
connection, project_uuid=project_uuid, custom_metadata=custom_metadata
)
return parse_obj_as(MetadataDict, pm.custom_metadata)
return parse_obj_as(MetadataDict, pm.custom)
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@

from aiohttp import web
from models_library.api_schemas_webserver.projects_metadata import (
ProjectCustomMetadataGet,
ProjectCustomMetadataUpdate,
ProjectMetadataGet,
ProjectMetadataUpdate,
)
from servicelib.aiohttp.requests_validation import (
parse_request_body_as,
Expand Down Expand Up @@ -72,9 +72,7 @@ async def get_project_custom_metadata(request: web.Request) -> web.Response:
)

return envelope_json_response(
ProjectCustomMetadataGet(
project_uuid=path_params.project_id, metadata=custom_metadata
)
ProjectMetadataGet(project_uuid=path_params.project_id, custom=custom_metadata)
)


Expand All @@ -88,17 +86,15 @@ async def get_project_custom_metadata(request: web.Request) -> web.Response:
async def update_project_custom_metadata(request: web.Request) -> web.Response:
req_ctx = RequestContext.parse_obj(request)
path_params = parse_request_path_parameters_as(ProjectPathParams, request)
update = await parse_request_body_as(ProjectCustomMetadataUpdate, request)
update = await parse_request_body_as(ProjectMetadataUpdate, request)

custom_metadata = await _metadata_api.set_project_custom_metadata(
request.app,
user_id=req_ctx.user_id,
project_uuid=path_params.project_id,
value=update.metadata,
value=update.custom,
)

return envelope_json_response(
ProjectCustomMetadataGet(
project_uuid=path_params.project_id, metadata=custom_metadata
)
ProjectMetadataGet(project_uuid=path_params.project_id, custom=custom_metadata)
)
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@
from aiohttp.test_utils import TestClient
from faker import Faker
from models_library.api_schemas_webserver.projects_metadata import (
ProjectCustomMetadataGet,
ProjectCustomMetadataUpdate,
ProjectMetadataGet,
ProjectMetadataUpdate,
)
from pydantic import parse_obj_as
from pytest_simcore.helpers.utils_assert import assert_status
Expand Down Expand Up @@ -75,12 +75,12 @@ async def test_custom_metadata_handlers(
project_id=user_project["uuid"]
)
response = await client.patch(
f"{url}", json=ProjectCustomMetadataUpdate(metadata=custom_metadata).dict()
f"{url}", json=ProjectMetadataUpdate(custom=custom_metadata).dict()
)

data, _ = await assert_status(response, expected_cls=web.HTTPOk)

assert parse_obj_as(ProjectCustomMetadataGet, data).metadata == custom_metadata
assert parse_obj_as(ProjectMetadataGet, data).custom == custom_metadata

# delete project
url = client.app.router["delete_project"].url_for(project_id=user_project["uuid"])
Expand Down

0 comments on commit 3ae0229

Please sign in to comment.