Skip to content

Commit

Permalink
Feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
faucomte97 committed Sep 18, 2024
1 parent 4975dcb commit 2e947de
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 11 deletions.
13 changes: 9 additions & 4 deletions codeforlife/tests/model_serializer.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,12 @@ def _assert_data_is_subset_of_model(self, data: DataDict, model):
elif isinstance(value, Model):
data[field] = getattr(value, "id")

self.assertEqual(model_to_dict(model), {**model_to_dict(model), **data})
model_dict = {
field: value
for field, value in model.__dict__.items()
if not field.startswith("_")
}
self.assertDictEqual(model_dict | data, model_dict)

def _assert_many(
self,
Expand All @@ -146,9 +151,9 @@ def _assert_many(
assert len(new_data) == len(validated_data)

kwargs.pop("many", None) # many must be True
serializer: ModelListSerializer[RequestUser, AnyModel] = (
self._init_model_serializer(*args, **kwargs, many=True)
)
serializer: ModelListSerializer[
RequestUser, AnyModel
] = self._init_model_serializer(*args, **kwargs, many=True)

models = get_models(serializer, deepcopy(validated_data))
assert len(models) == len(validated_data)
Expand Down
15 changes: 8 additions & 7 deletions codeforlife/tests/model_view_set.py
Original file line number Diff line number Diff line change
Expand Up @@ -614,9 +614,9 @@ class ModelViewSetTestCase(
basename: str
model_view_set_class: t.Type[ModelViewSet[RequestUser, AnyModel]]
client: ModelViewSetClient[RequestUser, AnyModel]
client_class: t.Type[ModelViewSetClient[RequestUser, AnyModel]] = (
ModelViewSetClient
)
client_class: t.Type[
ModelViewSetClient[RequestUser, AnyModel]
] = ModelViewSetClient

@classmethod
def get_model_class(cls) -> t.Type[AnyModel]:
Expand Down Expand Up @@ -743,8 +743,9 @@ def datetime_values_to_representation(data: DataDict):

# Assert the JSON model provided in the response is an exact match or
# subset of the serialized model.
(self.assertEqual if contains_subset else self.assertDictEqual)(
json_model, serialized_model
self.assertDictEqual(
serialized_model | json_model if contains_subset else json_model,
serialized_model,
)

def assert_get_serializer_class(
Expand Down Expand Up @@ -818,7 +819,7 @@ def assert_get_serializer_context(
*args, **kwargs, action=action
)
actual_serializer_context = model_view_set.get_serializer_context()
self.assertEqual(
actual_serializer_context,
self.assertDictEqual(
actual_serializer_context | serializer_context,
actual_serializer_context,
)

0 comments on commit 2e947de

Please sign in to comment.