Skip to content

Commit 3fdaf94

Browse files
committed
create tests for atomic
1 parent d907c52 commit 3fdaf94

File tree

2 files changed

+99
-2
lines changed

2 files changed

+99
-2
lines changed

pyproject.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,6 @@ black = "^25.1.0"
100100
mypy = "^1.14.1"
101101
pre-commit = "^4.1.0"
102102
ruff = "^0.9.4"
103-
sqlalchemy-stubs = "^0.4"
104103

105104
[tool.poetry.group.docs.dependencies]
106105
sphinx = "^7.0.1"

tests/test_atomic/test_create_objects.py

Lines changed: 99 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,12 @@
1010
from sqlalchemy.orm import joinedload
1111
from sqlalchemy.sql.functions import count
1212

13-
from examples.api_for_sqlalchemy.models import Child, Parent, ParentToChildAssociation, User, UserBio
13+
from examples.api_for_sqlalchemy.models import AgeRating, Child, Movie, Parent, ParentToChildAssociation, User, UserBio
1414
from examples.api_for_sqlalchemy.schemas import (
15+
AgeRatingAttributesSchema,
1516
ChildAttributesSchema,
1617
ComputerAttributesBaseSchema,
18+
MovieAttributesSchema,
1719
ParentAttributesSchema,
1820
ParentToChildAssociationAttributesSchema,
1921
UserAttributesBaseSchema,
@@ -254,6 +256,7 @@ async def test_create_user_and_user_bio_with_local_id(
254256
255257
:param client:
256258
:param async_session:
259+
:param user_attributes:
257260
:return:
258261
"""
259262
user_data = user_attributes
@@ -930,3 +933,98 @@ async def test_update_to_many_relationship_with_local_id(
930933
:param async_session:
931934
:return:
932935
"""
936+
937+
938+
class TestAtomicCreateObjectsMayBeWoId:
939+
940+
async def test_create_user_and_user_bio_with_local_id(
941+
self,
942+
client: AsyncClient,
943+
async_session: AsyncSession,
944+
):
945+
"""
946+
Prepare test data:
947+
948+
- create age rating
949+
- create movie with age rating
950+
951+
:param client:
952+
:param async_session:
953+
:return:
954+
"""
955+
age_rating_data = AgeRatingAttributesSchema(
956+
name=fake.word(),
957+
description=fake.sentence(),
958+
)
959+
movie_data = MovieAttributesSchema(
960+
title=fake.name(),
961+
description=fake.sentence(),
962+
)
963+
964+
age_rating_lid = fake.word()
965+
data_atomic_request = {
966+
# define operations
967+
"atomic:operations": [
968+
# first operation:
969+
# create a new age rating entity
970+
{
971+
"op": "add",
972+
"data": {
973+
"type": "age-rating",
974+
"lid": age_rating_lid,
975+
"attributes": age_rating_data.model_dump(),
976+
},
977+
},
978+
# second operation:
979+
# create a new movie,
980+
# this movie has relation
981+
# to the age rating with id=lid
982+
{
983+
"op": "add",
984+
"data": {
985+
"type": "movie",
986+
"attributes": movie_data.model_dump(exclude_unset=True),
987+
"relationships": {
988+
"age_rating_obj": {
989+
"data": {
990+
"lid": age_rating_lid,
991+
"type": "age-rating",
992+
},
993+
},
994+
},
995+
},
996+
},
997+
],
998+
}
999+
response = await client.post("/operations", json=data_atomic_request)
1000+
assert response.status_code == status.HTTP_200_OK, response.text
1001+
response_data = response.json()
1002+
movie = await async_session.scalar(
1003+
select(Movie).options(
1004+
joinedload(Movie.age_rating_obj),
1005+
),
1006+
)
1007+
assert isinstance(movie, Movie)
1008+
assert isinstance(movie.age_rating_obj, AgeRating)
1009+
1010+
movie_data.age_rating = movie.age_rating
1011+
assert response_data == {
1012+
"atomic:results": [
1013+
{
1014+
"data": {
1015+
"id": movie.age_rating,
1016+
"type": "age-rating",
1017+
"attributes": age_rating_data.model_dump(),
1018+
},
1019+
"meta": None,
1020+
},
1021+
{
1022+
"data": {
1023+
"id": f"{movie.id}",
1024+
"type": "movie",
1025+
"attributes": movie_data.model_dump(),
1026+
},
1027+
"meta": None,
1028+
},
1029+
],
1030+
}

0 commit comments

Comments
 (0)