-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: enhance session management and JSON serialization
- Introduced `global_session` for session management in `SessionManager`. - Updated `PydanticJSONMixin` to better handle JSONB columns conversion. - Expanded test suite with new test cases for session and serialization. - Added computed properties to models, supporting validation and serialization. - Ensured session state is correctly managed, especially in test scenarios. Generated-by: aiautocommit
- Loading branch information
1 parent
085c6a9
commit c74804b
Showing
5 changed files
with
146 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,75 @@ | ||
from fastapi import FastAPI | ||
from typing import Annotated | ||
|
||
from test.models import ExampleWithId | ||
import pytest | ||
import sqlalchemy | ||
from fastapi import Depends, FastAPI, Path, Request | ||
from fastapi.testclient import TestClient | ||
from starlette.testclient import TestClient | ||
|
||
from activemodel.session_manager import global_session | ||
from activemodel.types.typeid import TypeIDType | ||
from test.models import AnotherExample, ExampleWithComputedProperty, ExampleWithId | ||
|
||
|
||
def fake_app(): | ||
api_app = FastAPI() # type: ignore | ||
api_app = FastAPI(dependencies=[Depends(global_session)]) | ||
|
||
@api_app.get("/typeid") | ||
async def index() -> ExampleWithId: | ||
return "hi" | ||
return ExampleWithId().save() | ||
|
||
@api_app.get("/computed") | ||
async def computed(): | ||
another_example = AnotherExample(note="hello").save() | ||
return ExampleWithComputedProperty(another_example_id=another_example.id).save() | ||
|
||
@api_app.post("/example/{example_id}") | ||
async def get_record( | ||
request: Request, | ||
example_id: Annotated[TypeIDType, Path()], | ||
) -> ExampleWithId: | ||
example = ExampleWithId.get(id=example_id) | ||
assert example | ||
return example | ||
|
||
return api_app | ||
|
||
|
||
def test_openapi(): | ||
def fake_client(): | ||
app = fake_app() | ||
return app, TestClient(app) | ||
|
||
|
||
def test_openapi_generation(): | ||
openapi = fake_app().openapi() | ||
breakpoint() | ||
|
||
|
||
def test_typeid_input_parsing(create_and_wipe_database): | ||
example = ExampleWithId().save() | ||
example_id = example.id | ||
|
||
app, client = fake_client() | ||
|
||
response = client.post(f"/example/{example_id}") | ||
|
||
assert response.status_code == 200 | ||
|
||
|
||
def test_typeid_invalid_prefix_match(create_and_wipe_database): | ||
app, client = fake_client() | ||
|
||
# TODO we should really be able to assert against this: | ||
# with pytest.raises(TypeIDValidationError): | ||
# we'll need to | ||
|
||
with pytest.raises(sqlalchemy.exc.StatementError): | ||
response = client.post("/example/user_01h45ytscbebyvny4gc8cr8ma2") | ||
|
||
|
||
def test_computed_property(create_and_wipe_database): | ||
app, client = fake_client() | ||
|
||
response = client.get("/computed") | ||
|
||
assert response.status_code == 200 | ||
assert response.json()["special_note"] == "SPECIAL: hello" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,29 @@ | ||
from pydantic import computed_field | ||
from sqlmodel import Field, Relationship | ||
|
||
from activemodel import BaseModel | ||
from activemodel.mixins import TypeIDMixin | ||
from activemodel.types.typeid import TypeIDType | ||
from sqlmodel import Relationship | ||
|
||
TYPEID_PREFIX = "myid" | ||
|
||
|
||
class AnotherExample(BaseModel, TypeIDMixin("myotherid"), table=True): | ||
pass | ||
note: str | None = Field(nullable=True) | ||
|
||
|
||
class ExampleWithId(BaseModel, TypeIDMixin(TYPEID_PREFIX), table=True): | ||
another_example_id: TypeIDType = AnotherExample.foreign_key(nullable=True) | ||
another_example: AnotherExample = Relationship() | ||
|
||
|
||
class ExampleWithComputedProperty( | ||
BaseModel, TypeIDMixin("example_computed"), table=True | ||
): | ||
another_example_id: TypeIDType = AnotherExample.foreign_key() | ||
another_example: AnotherExample = Relationship() | ||
|
||
@computed_field | ||
@property | ||
def special_note(self) -> str: | ||
return f"SPECIAL: {self.another_example.note}" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters