Skip to content

Commit

Permalink
Simplified endpoints by default
Browse files Browse the repository at this point in the history
  • Loading branch information
igorbenav committed Sep 15, 2024
1 parent 6819c5d commit ae87ec2
Show file tree
Hide file tree
Showing 10 changed files with 191 additions and 18 deletions.
21 changes: 6 additions & 15 deletions fastcrud/endpoint/endpoint_creator.py
Original file line number Diff line number Diff line change
Expand Up @@ -273,24 +273,15 @@ def __init__(
self.deleted_at_column = deleted_at_column
self.updated_at_column = updated_at_column
self.default_endpoint_names = {
"create": "create",
"read": "get",
"update": "update",
"delete": "delete",
"db_delete": "db_delete",
"read_multi": "get_multi",
"create": "",
"read": "",
"update": "",
"delete": "",
"db_delete": "",
"read_multi": "",
"read_paginated": "get_paginated",
}
self.endpoint_names = {**self.default_endpoint_names, **(endpoint_names or {})}
if self.endpoint_names == self.default_endpoint_names:
warnings.warn(
"Old default_endpoint_names are getting deprecated. "
"Default values are going to be replaced by empty strings, "
"resulting in plain endpoint names. "
"For details see:"
" https://github.com/igorbenav/fastcrud/issues/67",
DeprecationWarning,
)
if filter_config:
if isinstance(filter_config, dict):
filter_config = FilterConfig(**filter_config)
Expand Down
8 changes: 5 additions & 3 deletions fastcrud/endpoint/helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,11 +132,13 @@ def _inject_dependencies(
"""Wraps a list of functions in FastAPI's Depends."""
if funcs is None:
return None

for func in funcs:
if not callable(func):
raise TypeError(f"All dependencies must be callable. Got {type(func)} instead.")

raise TypeError(
f"All dependencies must be callable. Got {type(func)} instead."
)

return [Depends(func) for func in funcs]


Expand Down
63 changes: 63 additions & 0 deletions tests/sqlalchemy/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -481,6 +481,15 @@ def client(
delete_schema=delete_schema,
path="/test",
tags=["test"],
endpoint_names={
"create": "create",
"read": "get",
"update": "update",
"delete": "delete",
"db_delete": "db_delete",
"read_multi": "get_multi",
"read_paginated": "get_paginated",
},
)
)

Expand All @@ -494,6 +503,15 @@ def client(
delete_schema=tier_delete_schema,
path="/tier",
tags=["tier"],
endpoint_names={
"create": "create",
"read": "get",
"update": "update",
"delete": "delete",
"db_delete": "db_delete",
"read_multi": "get_multi",
"read_paginated": "get_paginated",
},
)
)

Expand All @@ -508,6 +526,15 @@ def client(
read_deps=[test_read_dep],
path="/multi_pk",
tags=["multi_pk"],
endpoint_names={
"create": "create",
"read": "get",
"update": "update",
"delete": "delete",
"db_delete": "db_delete",
"read_multi": "get_multi",
"read_paginated": "get_paginated",
},
)
)

Expand All @@ -531,6 +558,15 @@ def filtered_client(
filter_config=FilterConfig(tier_id=None, name=None),
path="/test",
tags=["test"],
endpoint_names={
"create": "create",
"read": "get",
"update": "update",
"delete": "delete",
"db_delete": "db_delete",
"read_multi": "get_multi",
"read_paginated": "get_paginated",
},
)
)

Expand All @@ -554,6 +590,15 @@ def dict_filtered_client(
filter_config={"tier_id": None, "name": None},
path="/test",
tags=["test"],
endpoint_names={
"create": "create",
"read": "get",
"update": "update",
"delete": "delete",
"db_delete": "db_delete",
"read_multi": "get_multi",
"read_paginated": "get_paginated",
},
)
)

Expand All @@ -578,6 +623,15 @@ def invalid_filtered_client(
filter_config=filter_config,
path="/test",
tags=["test"],
endpoint_names={
"create": "create",
"read": "get",
"update": "update",
"delete": "delete",
"db_delete": "db_delete",
"read_multi": "get_multi",
"read_paginated": "get_paginated",
},
)


Expand All @@ -593,4 +647,13 @@ def endpoint_creator(test_model, async_session) -> EndpointCreator:
delete_schema=DeleteSchemaTest,
path="/custom_test",
tags=["custom_test"],
endpoint_names={
"create": "create",
"read": "get",
"update": "update",
"delete": "delete",
"db_delete": "db_delete",
"read_multi": "get_multi",
"read_paginated": "get_paginated",
},
)
9 changes: 9 additions & 0 deletions tests/sqlalchemy/endpoint/test_custom_endpoint_creator.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,15 @@ async def test_custom_endpoint_creator(
endpoint_creator=CustomEndpointCreator,
path="/custom",
tags=["Test"],
endpoint_names={
"create": "create",
"read": "get",
"update": "update",
"delete": "delete",
"db_delete": "db_delete",
"read_multi": "get_multi",
"read_paginated": "get_paginated",
},
)

client.app.include_router(custom_router)
Expand Down
9 changes: 9 additions & 0 deletions tests/sqlalchemy/endpoint/test_deleted_methods.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,15 @@ async def test_deleted_methods(
deleted_methods=["delete"],
path="/test_custom",
tags=["Test"],
endpoint_names={
"create": "create",
"read": "get",
"update": "update",
"delete": "delete",
"db_delete": "db_delete",
"read_multi": "get_multi",
"read_paginated": "get_paginated",
},
)

client.app.include_router(custom_router)
Expand Down
9 changes: 9 additions & 0 deletions tests/sqlalchemy/endpoint/test_included_methods.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,15 @@ async def test_included_methods(
included_methods=["create", "read"],
path="/test_custom",
tags=["Test"],
endpoint_names={
"create": "create",
"read": "get",
"update": "update",
"delete": "delete",
"db_delete": "db_delete",
"read_multi": "get_multi",
"read_paginated": "get_paginated",
},
)

client.app.include_router(custom_router)
Expand Down
63 changes: 63 additions & 0 deletions tests/sqlmodel/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -469,6 +469,15 @@ def client(
delete_schema=delete_schema,
path="/test",
tags=["test"],
endpoint_names={
"create": "create",
"read": "get",
"update": "update",
"delete": "delete",
"db_delete": "db_delete",
"read_multi": "get_multi",
"read_paginated": "get_paginated",
},
)
)

Expand All @@ -482,6 +491,15 @@ def client(
delete_schema=tier_delete_schema,
path="/tier",
tags=["tier"],
endpoint_names={
"create": "create",
"read": "get",
"update": "update",
"delete": "delete",
"db_delete": "db_delete",
"read_multi": "get_multi",
"read_paginated": "get_paginated",
},
)
)

Expand All @@ -496,6 +514,15 @@ def client(
delete_schema=multi_pk_test_schema,
path="/multi_pk",
tags=["multi_pk"],
endpoint_names={
"create": "create",
"read": "get",
"update": "update",
"delete": "delete",
"db_delete": "db_delete",
"read_multi": "get_multi",
"read_paginated": "get_paginated",
},
)
)

Expand Down Expand Up @@ -523,6 +550,15 @@ def filtered_client(
filter_config=FilterConfig(tier_id=None, name=None),
path="/test",
tags=["test"],
endpoint_names={
"create": "create",
"read": "get",
"update": "update",
"delete": "delete",
"db_delete": "db_delete",
"read_multi": "get_multi",
"read_paginated": "get_paginated",
},
)
)

Expand Down Expand Up @@ -550,6 +586,15 @@ def dict_filtered_client(
filter_config={"tier_id": None, "name": None},
path="/test",
tags=["test"],
endpoint_names={
"create": "create",
"read": "get",
"update": "update",
"delete": "delete",
"db_delete": "db_delete",
"read_multi": "get_multi",
"read_paginated": "get_paginated",
},
)
)

Expand Down Expand Up @@ -578,6 +623,15 @@ def invalid_filtered_client(
filter_config=filter_config,
path="/test",
tags=["test"],
endpoint_names={
"create": "create",
"read": "get",
"update": "update",
"delete": "delete",
"db_delete": "db_delete",
"read_multi": "get_multi",
"read_paginated": "get_paginated",
},
)


Expand All @@ -593,4 +647,13 @@ def endpoint_creator(test_model, async_session) -> EndpointCreator:
delete_schema=DeleteSchemaTest,
path="/custom_test",
tags=["custom_test"],
endpoint_names={
"create": "create",
"read": "get",
"update": "update",
"delete": "delete",
"db_delete": "db_delete",
"read_multi": "get_multi",
"read_paginated": "get_paginated",
},
)
9 changes: 9 additions & 0 deletions tests/sqlmodel/endpoint/test_custom_endpoint_creator.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,15 @@ async def test_custom_endpoint_creator(
endpoint_creator=CustomEndpointCreator,
path="/custom",
tags=["Test"],
endpoint_names={
"create": "create",
"read": "get",
"update": "update",
"delete": "delete",
"db_delete": "db_delete",
"read_multi": "get_multi",
"read_paginated": "get_paginated",
},
)

client.app.include_router(custom_router)
Expand Down
9 changes: 9 additions & 0 deletions tests/sqlmodel/endpoint/test_deleted_methods.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,15 @@ async def test_deleted_methods(
deleted_methods=["delete"],
path="/test_custom",
tags=["Test"],
endpoint_names={
"create": "create",
"read": "get",
"update": "update",
"delete": "delete",
"db_delete": "db_delete",
"read_multi": "get_multi",
"read_paginated": "get_paginated",
},
)

client.app.include_router(custom_router)
Expand Down
9 changes: 9 additions & 0 deletions tests/sqlmodel/endpoint/test_included_methods.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,15 @@ async def test_included_methods(
included_methods=["create", "read"],
path="/test_custom",
tags=["Test"],
endpoint_names={
"create": "create",
"read": "get",
"update": "update",
"delete": "delete",
"db_delete": "db_delete",
"read_multi": "get_multi",
"read_paginated": "get_paginated",
},
)

client.app.include_router(custom_router)
Expand Down

0 comments on commit ae87ec2

Please sign in to comment.