|
10 | 10 | from sqlalchemy.orm import joinedload |
11 | 11 | from sqlalchemy.sql.functions import count |
12 | 12 |
|
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 |
14 | 14 | from examples.api_for_sqlalchemy.schemas import ( |
| 15 | + AgeRatingAttributesSchema, |
15 | 16 | ChildAttributesSchema, |
16 | 17 | ComputerAttributesBaseSchema, |
| 18 | + MovieAttributesSchema, |
17 | 19 | ParentAttributesSchema, |
18 | 20 | ParentToChildAssociationAttributesSchema, |
19 | 21 | UserAttributesBaseSchema, |
@@ -254,6 +256,7 @@ async def test_create_user_and_user_bio_with_local_id( |
254 | 256 |
|
255 | 257 | :param client: |
256 | 258 | :param async_session: |
| 259 | + :param user_attributes: |
257 | 260 | :return: |
258 | 261 | """ |
259 | 262 | user_data = user_attributes |
@@ -930,3 +933,98 @@ async def test_update_to_many_relationship_with_local_id( |
930 | 933 | :param async_session: |
931 | 934 | :return: |
932 | 935 | """ |
| 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