Skip to content

Commit b169491

Browse files
committed
update and refactor generic user api test
1 parent a28046f commit b169491

File tree

1 file changed

+97
-38
lines changed

1 file changed

+97
-38
lines changed

tests/common_user_api_test.py

Lines changed: 97 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,39 @@ class BaseGenericUserCreateUpdateWithBodyDependency:
3737
validator_create = ValidateCustomNameEqualsBase(None)
3838
validator_update = ValidateCustomNameEqualsBase(None)
3939

40-
def validate_field_not_passed_response(self, response):
41-
assert response.status_code == status.HTTP_422_UNPROCESSABLE_ENTITY, response.text
40+
def prepare_user_create_data(
41+
self,
42+
user_attributes: UserAttributesBaseSchema,
43+
resource_type: str,
44+
):
45+
data_user_attributes = user_attributes.dict()
46+
data_user_attributes[self.FIELD_CUSTOM_NAME] = self.validator_create.expected_value
47+
data_user_create = {
48+
"type": resource_type,
49+
"attributes": data_user_attributes,
50+
}
51+
return data_user_create
52+
53+
def prepare_user_update_data(
54+
self,
55+
user: User,
56+
user_attributes: UserAttributesBaseSchema,
57+
resource_type: str,
58+
):
59+
for field_name, value in user_attributes:
60+
assert getattr(user, field_name) != value
61+
62+
data_user_attributes = user_attributes.dict()
63+
data_user_attributes[self.FIELD_CUSTOM_NAME] = self.validator_update.expected_value
64+
data_user_update = {
65+
"id": user.id,
66+
"type": resource_type,
67+
"attributes": data_user_attributes,
68+
}
69+
return data_user_update
70+
71+
def validate_field_not_passed_response(self, response, expected_status=status.HTTP_422_UNPROCESSABLE_ENTITY):
72+
assert response.status_code == expected_status, response.text
4273
response_data = response.json()
4374
assert response_data == {
4475
"detail": [
@@ -68,6 +99,7 @@ async def validate_user_creation_on_error_key_not_passed(
6899
user_attributes: UserAttributesBaseSchema,
69100
):
70101
attributes_data = user_attributes.dict()
102+
assert self.FIELD_CUSTOM_NAME not in attributes_data
71103
data_user_create = {
72104
"data": {
73105
"type": resource_type,
@@ -87,6 +119,7 @@ async def validate_user_creation_test_error_value_passed_but_invalid(
87119
):
88120
attributes_data = user_attributes.dict()
89121
attributes_data[self.FIELD_CUSTOM_NAME] = fake.word()
122+
assert attributes_data[self.FIELD_CUSTOM_NAME] != self.validator_create.expected_value
90123
data_user_create = {
91124
"data": {
92125
"type": resource_type,
@@ -106,6 +139,7 @@ async def validate_user_update_error_key_not_passed(
106139
user_attributes: UserAttributesBaseSchema,
107140
):
108141
attributes_data = user_attributes.dict()
142+
assert self.FIELD_CUSTOM_NAME not in attributes_data
109143
data_user_update = {
110144
"data": {
111145
"id": user.id,
@@ -127,35 +161,25 @@ async def validate_user_update_error_value_passed_but_invalid(
127161
):
128162
attributes_data = user_attributes.dict()
129163
attributes_data[self.FIELD_CUSTOM_NAME] = fake.word()
130-
data_user_create = {
164+
assert attributes_data[self.FIELD_CUSTOM_NAME] != self.validator_update.expected_value
165+
data_user_update = {
131166
"data": {
167+
"id": user.id,
132168
"type": resource_type,
133169
"attributes": attributes_data,
134170
},
135171
}
136172
url = app.url_path_for(f"update_{resource_type}_detail", obj_id=user.id)
137-
response = await client.patch(url, json=data_user_create)
173+
response = await client.patch(url, json=data_user_update)
138174
self.validate_field_value_invalid_response(response, self.validator_update)
139175

140-
async def validate_generic_user_create_works(
176+
async def validate_created_user(
141177
self,
142-
app: FastAPI,
143-
client: AsyncClient,
144178
async_session: AsyncSession,
145-
resource_type: str,
179+
user_created_data: dict,
146180
user_attributes: UserAttributesBaseSchema,
181+
resource_type: str,
147182
):
148-
data_user_attributes = user_attributes.dict()
149-
data_user_attributes[self.FIELD_CUSTOM_NAME] = self.validator_create.expected_value
150-
data_user_create = {
151-
"data": {
152-
"type": resource_type,
153-
"attributes": data_user_attributes,
154-
},
155-
}
156-
url = app.url_path_for(f"create_{resource_type}_list")
157-
response = await client.post(url, json=data_user_create)
158-
assert response.status_code == status.HTTP_201_CREATED, response.text
159183
user = await async_session.scalar(
160184
select(User).where(
161185
*(
@@ -167,37 +191,72 @@ async def validate_generic_user_create_works(
167191
),
168192
)
169193
assert isinstance(user, User)
170-
response_data = response.json()
171-
user_created_data = response_data["data"]
172194
assert user_created_data["id"] == str(user.id)
173195
assert user_created_data["attributes"] == user_attributes.dict()
196+
assert user_created_data["type"] == resource_type
197+
assert user_attributes == UserAttributesBaseSchema.from_orm(user)
174198

175-
async def validate_generic_user_update_works(
199+
async def validate_generic_user_create_works(
176200
self,
177201
app: FastAPI,
178202
client: AsyncClient,
179203
async_session: AsyncSession,
180204
resource_type: str,
181205
user_attributes: UserAttributesBaseSchema,
206+
):
207+
data_user_create = self.prepare_user_create_data(
208+
user_attributes=user_attributes,
209+
resource_type=resource_type,
210+
)
211+
url = app.url_path_for(f"create_{resource_type}_list")
212+
response = await client.post(url, json={"data": data_user_create})
213+
assert response.status_code == status.HTTP_201_CREATED, response.text
214+
response_data = response.json()
215+
user_created_data = response_data["data"]
216+
await self.validate_created_user(
217+
async_session=async_session,
218+
user_created_data=user_created_data,
219+
user_attributes=user_attributes,
220+
resource_type=resource_type,
221+
)
222+
223+
async def validate_updated_user(
224+
self,
182225
user: User,
226+
async_session: AsyncSession,
227+
user_updated_data: dict,
228+
user_attributes: UserAttributesBaseSchema,
229+
resource_type: str,
183230
):
184-
for field_name, value in user_attributes:
185-
assert getattr(user, field_name) != value
231+
await async_session.refresh(user)
232+
assert user_updated_data["id"] == str(user.id)
233+
assert user_updated_data["attributes"] == user_attributes.dict()
234+
assert user_updated_data["type"] == resource_type
235+
assert user_attributes == UserAttributesBaseSchema.from_orm(user)
186236

187-
data_user_attributes = user_attributes.dict()
188-
data_user_attributes[self.FIELD_CUSTOM_NAME] = self.validator_update.expected_value
189-
data_user_update = {
190-
"data": {
191-
"id": user.id,
192-
"type": resource_type,
193-
"attributes": data_user_attributes,
194-
},
195-
}
237+
async def validate_generic_user_update_works(
238+
self,
239+
app: FastAPI,
240+
client: AsyncClient,
241+
async_session: AsyncSession,
242+
resource_type: str,
243+
user_attributes: UserAttributesBaseSchema,
244+
user: User,
245+
):
246+
data_user_update = self.prepare_user_update_data(
247+
user=user,
248+
user_attributes=user_attributes,
249+
resource_type=resource_type,
250+
)
196251
url = app.url_path_for(f"update_{resource_type}_detail", obj_id=user.id)
197-
response = await client.patch(url, json=data_user_update)
252+
response = await client.patch(url, json={"data": data_user_update})
198253
assert response.status_code == status.HTTP_200_OK, response.text
199-
await async_session.refresh(user)
200254
response_data = response.json()
201-
user_created_data = response_data["data"]
202-
assert user_created_data["id"] == str(user.id)
203-
assert user_created_data["attributes"] == user_attributes.dict()
255+
user_updated_data = response_data["data"]
256+
await self.validate_updated_user(
257+
user=user,
258+
async_session=async_session,
259+
user_updated_data=user_updated_data,
260+
user_attributes=user_attributes,
261+
resource_type=resource_type,
262+
)

0 commit comments

Comments
 (0)