From 2e947de2767ef19b73f3c88563df414a8500cab7 Mon Sep 17 00:00:00 2001 From: faucomte97 Date: Wed, 18 Sep 2024 13:00:19 +0100 Subject: [PATCH] Feedback --- codeforlife/tests/model_serializer.py | 13 +++++++++---- codeforlife/tests/model_view_set.py | 15 ++++++++------- 2 files changed, 17 insertions(+), 11 deletions(-) diff --git a/codeforlife/tests/model_serializer.py b/codeforlife/tests/model_serializer.py index 74e8b007..cf87536e 100644 --- a/codeforlife/tests/model_serializer.py +++ b/codeforlife/tests/model_serializer.py @@ -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, @@ -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) diff --git a/codeforlife/tests/model_view_set.py b/codeforlife/tests/model_view_set.py index e4f8e718..fe59186a 100644 --- a/codeforlife/tests/model_view_set.py +++ b/codeforlife/tests/model_view_set.py @@ -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]: @@ -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( @@ -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, )