Skip to content

Commit

Permalink
♻️ Fix unit tests after master merge (ITISFoundation#6727)
Browse files Browse the repository at this point in the history
  • Loading branch information
giancarloromeo authored Nov 15, 2024
1 parent 44bc9f7 commit a2d3742
Show file tree
Hide file tree
Showing 9 changed files with 97 additions and 83 deletions.
23 changes: 14 additions & 9 deletions packages/models-library/src/models_library/folders.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,14 @@
from enum import auto
from typing import TypeAlias

from pydantic import BaseModel, ConfigDict, Field, PositiveInt, field_validator
from pydantic import (
BaseModel,
ConfigDict,
Field,
PositiveInt,
ValidationInfo,
field_validator,
)

from .access_rights import AccessRights
from .users import GroupID, UserID
Expand All @@ -24,16 +31,14 @@ class FolderQuery(BaseModel):

@field_validator("folder_id", mode="before")
@classmethod
def validate_folder_id(cls, value, values):
scope = values.get("folder_scope")
def validate_folder_id(cls, value, info: ValidationInfo):
scope = info.data.get("folder_scope")
if scope == FolderScope.SPECIFIC and value is None:
raise ValueError(
"folder_id must be provided when folder_scope is SPECIFIC."
)
msg = "folder_id must be provided when folder_scope is SPECIFIC."
raise ValueError(msg)
if scope != FolderScope.SPECIFIC and value is not None:
raise ValueError(
"folder_id should be None when folder_scope is not SPECIFIC."
)
msg = "folder_id should be None when folder_scope is not SPECIFIC."
raise ValueError(msg)
return value


Expand Down
23 changes: 14 additions & 9 deletions packages/models-library/src/models_library/workspaces.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,14 @@
from enum import auto
from typing import TypeAlias

from pydantic import BaseModel, ConfigDict, Field, PositiveInt, field_validator
from pydantic import (
BaseModel,
ConfigDict,
Field,
PositiveInt,
ValidationInfo,
field_validator,
)

from .access_rights import AccessRights
from .users import GroupID
Expand All @@ -23,16 +30,14 @@ class WorkspaceQuery(BaseModel):

@field_validator("workspace_id", mode="before")
@classmethod
def validate_workspace_id(cls, value, values):
scope = values.get("workspace_scope")
def validate_workspace_id(cls, value, info: ValidationInfo):
scope = info.data.get("workspace_scope")
if scope == WorkspaceScope.SHARED and value is None:
raise ValueError(
"workspace_id must be provided when workspace_scope is SHARED."
)
msg = "workspace_id must be provided when workspace_scope is SHARED."
raise ValueError(msg)
if scope != WorkspaceScope.SHARED and value is not None:
raise ValueError(
"workspace_id should be None when workspace_scope is not SHARED."
)
msg = "workspace_id should be None when workspace_scope is not SHARED."
raise ValueError(msg)
return value


Expand Down
2 changes: 1 addition & 1 deletion packages/models-library/tests/test_projects_state.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ def test_project_locked_with_missing_owner_raises():


def test_project_locked_with_missing_owner_ok_during_maintaining():
ProjectLocked.parse_obj({"value": True, "status": ProjectStatus.MAINTAINING})
ProjectLocked.model_validate({"value": True, "status": ProjectStatus.MAINTAINING})


@pytest.mark.parametrize(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,15 @@
from models_library.projects_nodes_io import NodeID
from models_library.projects_pipeline import ComputationTask
from models_library.projects_state import RunningState
from pydantic import AnyHttpUrl, AnyUrl, BaseModel, Field, PositiveInt, TypeAdapter
from pydantic import (
AnyHttpUrl,
AnyUrl,
BaseModel,
ConfigDict,
Field,
PositiveInt,
TypeAdapter,
)
from settings_library.tracing import TracingSettings
from starlette import status

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from pydantic import AliasChoices, Field, TypeAdapter, field_validator
from pydantic.networks import AnyUrl
from servicelib.logging_utils_filtering import LoggerName, MessageSubstring
from settings_library.application import BaseApplicationSettings
from settings_library.base import BaseCustomSettings
from settings_library.tracing import TracingSettings
from settings_library.utils_logging import MixinLoggingSettings
Expand All @@ -19,10 +20,7 @@ class PennsieveSettings(BaseCustomSettings):
PENNSIEVE_HEALTCHCHECK_TIMEOUT: float = 1.0


class ApplicationSettings(BaseCustomSettings, MixinLoggingSettings):
# DOCKER
SC_BOOT_MODE: BootModeEnum | None

class ApplicationSettings(BaseApplicationSettings, MixinLoggingSettings):
LOG_LEVEL: LogLevel = Field(
default=LogLevel.INFO.value,
validation_alias=AliasChoices(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,10 +82,10 @@ async def list_folders(request: web.Request):
trashed=query_params.filters.trashed,
offset=query_params.offset,
limit=query_params.limit,
order_by=parse_obj_as(OrderBy, query_params.order_by),
order_by=OrderBy.model_validate(query_params.order_by),
)

page = Page[FolderGet].parse_obj(
page = Page[FolderGet].model_validate(
paginate_data(
chunk=folders.items,
request_url=request.url,
Expand All @@ -95,7 +95,7 @@ async def list_folders(request: web.Request):
)
)
return web.Response(
text=page.json(**RESPONSE_MODEL_POLICY),
text=page.model_dump_json(**RESPONSE_MODEL_POLICY),
content_type=MIMETYPE_APPLICATION_JSON,
)

Expand All @@ -105,7 +105,7 @@ async def list_folders(request: web.Request):
@permission_required("folder.read")
@handle_plugin_requests_exceptions
async def list_folders_full_search(request: web.Request):
req_ctx = FoldersRequestContext.parse_obj(request)
req_ctx = FoldersRequestContext.model_validate(request)
query_params: FolderListFullSearchWithJsonStrQueryParams = (
parse_request_query_parameters_as(
FolderListFullSearchWithJsonStrQueryParams, request
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import logging
from typing import Annotated

from models_library.basic_types import IDStr
from models_library.folders import FolderID
Expand All @@ -11,7 +12,14 @@
null_or_none_str_to_none_validator,
)
from models_library.workspaces import WorkspaceID
from pydantic import BaseModel, Extra, Field, Json, validator
from pydantic import (
BaseModel,
BeforeValidator,
ConfigDict,
Field,
Json,
field_validator,
)
from servicelib.aiohttp.requests_validation import RequestParams, StrictRequestParams
from servicelib.request_keys import RQT_USERID_KEY

Expand Down Expand Up @@ -41,11 +49,11 @@ class FolderListSortParams(BaseModel):
order_by: Json[OrderBy] = Field(
default=OrderBy(field=IDStr("modified"), direction=OrderDirection.DESC),
description="Order by field (modified_at|name|description) and direction (asc|desc). The default sorting order is ascending.",
example='{"field": "name", "direction": "desc"}',
examples=['{"field": "name", "direction": "desc"}'],
alias="order_by",
)

@validator("order_by", check_fields=False)
@field_validator("order_by", check_fields=False)
@classmethod
def _validate_order_by_field(cls, v):
if v.field not in {
Expand All @@ -59,51 +67,41 @@ def _validate_order_by_field(cls, v):
v.field = "modified"
return v

class Config:
extra = Extra.forbid
model_config = ConfigDict(extra="forbid")


class FolderListWithJsonStrQueryParams(
PageQueryParameters, FolderListSortParams, FiltersQueryParameters[FolderFilters]
):
folder_id: FolderID | None = Field(
folder_id: Annotated[
FolderID | None, BeforeValidator(null_or_none_str_to_none_validator)
] = Field(
default=None,
description="List the subfolders of this folder. By default, list the subfolders of the root directory (Folder ID is None).",
)
workspace_id: WorkspaceID | None = Field(
workspace_id: Annotated[
WorkspaceID | None, BeforeValidator(null_or_none_str_to_none_validator)
] = Field(
default=None,
description="List folders in specific workspace. By default, list in the user private workspace",
)

class Config:
extra = Extra.forbid

# validators
_null_or_none_str_to_none_validator = validator(
"folder_id", allow_reuse=True, pre=True
)(null_or_none_str_to_none_validator)

_null_or_none_str_to_none_validator2 = validator(
"workspace_id", allow_reuse=True, pre=True
)(null_or_none_str_to_none_validator)
model_config = ConfigDict(extra="forbid")


class FolderListFullSearchWithJsonStrQueryParams(
PageQueryParameters, FolderListSortParams, FiltersQueryParameters[FolderFilters]
):
text: str | None = Field(
text: Annotated[
str | None, BeforeValidator(empty_str_to_none_pre_validator)
] = Field(
default=None,
description="Multi column full text search, across all folders and workspaces",
max_length=100,
example="My Project",
)

_empty_is_none = validator("text", allow_reuse=True, pre=True)(
empty_str_to_none_pre_validator
examples=["My Project"],
)

class Config:
extra = Extra.forbid
model_config = ConfigDict(extra="forbid")


class RemoveQueryParams(BaseModel):
Expand Down
Loading

0 comments on commit a2d3742

Please sign in to comment.