|
9 | 9 | from starlette import status |
10 | 10 |
|
11 | 11 | from tests.misc.utils import fake |
12 | | -from tests.models import User |
13 | | -from tests.schemas import UserAttributesBaseSchema |
| 12 | +from tests.models import User, UserBio |
| 13 | +from tests.schemas import UserAttributesBaseSchema, UserBioBaseSchema |
14 | 14 |
|
15 | 15 | pytestmark = mark.asyncio |
16 | 16 |
|
@@ -198,3 +198,55 @@ async def test_atomic_rollback_on_create_error( |
198 | 198 | ) |
199 | 199 | result: Result = await async_session.execute(stmt) |
200 | 200 | assert result.scalar_one() == 0 |
| 201 | + |
| 202 | + async def test_create_bio_with_relationship_to_user_to_one( |
| 203 | + self, |
| 204 | + client: AsyncClient, |
| 205 | + async_session: AsyncSession, |
| 206 | + user_1: User, |
| 207 | + ): |
| 208 | + user_bio = UserBioBaseSchema( |
| 209 | + birth_city=fake.city(), |
| 210 | + favourite_movies=fake.sentence(), |
| 211 | + ) |
| 212 | + stmt_bio = select(UserBio).where(UserBio.user_id == user_1.id) |
| 213 | + res: Result = await async_session.execute(stmt_bio) |
| 214 | + assert res.scalar_one_or_none() is None, "user has to be w/o bio" |
| 215 | + |
| 216 | + data_atomic_request = { |
| 217 | + "atomic:operations": [ |
| 218 | + { |
| 219 | + "op": "add", |
| 220 | + "data": { |
| 221 | + "type": "user_bio", |
| 222 | + "attributes": user_bio.dict(), |
| 223 | + "relationships": { |
| 224 | + "user": { |
| 225 | + "data": { |
| 226 | + "id": user_1.id, |
| 227 | + "type": "user", |
| 228 | + }, |
| 229 | + }, |
| 230 | + }, |
| 231 | + }, |
| 232 | + }, |
| 233 | + ], |
| 234 | + } |
| 235 | + response = await client.post("/operations", json=data_atomic_request) |
| 236 | + assert response.status_code == status.HTTP_200_OK, response.text |
| 237 | + resp_data = response.json() |
| 238 | + assert resp_data |
| 239 | + results = resp_data["atomic:results"] |
| 240 | + assert len(results) == 1, results |
| 241 | + result_bio_data = results[0] |
| 242 | + res: Result = await async_session.execute(stmt_bio) |
| 243 | + user_bio_created: UserBio = res.scalar_one() |
| 244 | + assert user_bio == UserBioBaseSchema.from_orm(user_bio_created) |
| 245 | + assert result_bio_data == { |
| 246 | + "data": { |
| 247 | + "attributes": user_bio.dict(), |
| 248 | + "type": "user_bio", |
| 249 | + "id": str(user_bio_created.id), |
| 250 | + }, |
| 251 | + "meta": None, |
| 252 | + } |
0 commit comments