From cfea7426a659922571303267b358d83868fae8f0 Mon Sep 17 00:00:00 2001 From: Julien Barreau Date: Fri, 26 Jan 2024 10:52:54 +0100 Subject: [PATCH 1/4] fix(enum): force enums to be strings --- .../datasource_django/utils/model_introspection.py | 10 +--------- .../tests/utils/test_django_model_introspection.py | 2 +- 2 files changed, 2 insertions(+), 10 deletions(-) diff --git a/src/datasource_django/forestadmin/datasource_django/utils/model_introspection.py b/src/datasource_django/forestadmin/datasource_django/utils/model_introspection.py index 97150f9c7..26dc1db1a 100644 --- a/src/datasource_django/forestadmin/datasource_django/utils/model_introspection.py +++ b/src/datasource_django/forestadmin/datasource_django/utils/model_introspection.py @@ -35,19 +35,11 @@ GeneratedField = None -def serialize_enum_value(value): - try: - json.dumps(value) - except TypeError: - value = str(value) - return value - - class FieldFactory: @staticmethod def _build_enum_values(field: Field) -> Optional[List[str]]: if field.choices: - return [serialize_enum_value(c[0]) for c in field.choices] # type: ignore + return [str(c[0]) for c in field.choices] # type: ignore return None @staticmethod diff --git a/src/datasource_django/tests/utils/test_django_model_introspection.py b/src/datasource_django/tests/utils/test_django_model_introspection.py index b0e7a416b..63df85872 100644 --- a/src/datasource_django/tests/utils/test_django_model_introspection.py +++ b/src/datasource_django/tests/utils/test_django_model_introspection.py @@ -50,7 +50,7 @@ def test_build_should_handle_enums(self): field = models.IntegerField(choices=choices) field_schema = FieldFactory.build(field) - self.assertEqual(field_schema["enum_values"], [1, 2, 3]) + self.assertEqual(field_schema["enum_values"], ["1", "2", "3"]) self.assertEqual(field_schema["column_type"], PrimitiveType.ENUM) def test_build_should_use_str_on_enum_values_when_value_is_not_json_serializable(self): From cb4e555c8e1099d4a30e2db9d15843fc312f29c3 Mon Sep 17 00:00:00 2001 From: Julien Barreau Date: Fri, 26 Jan 2024 12:21:48 +0100 Subject: [PATCH 2/4] chore: try to fix black would reformat --- .../tests/resources/collections/test_crud.py | 18 +++++++++++++++--- .../services/test_ip_whitelist_service.py | 5 ++++- 2 files changed, 19 insertions(+), 4 deletions(-) diff --git a/src/agent_toolkit/tests/resources/collections/test_crud.py b/src/agent_toolkit/tests/resources/collections/test_crud.py index cb82bfd59..017deb362 100644 --- a/src/agent_toolkit/tests/resources/collections/test_crud.py +++ b/src/agent_toolkit/tests/resources/collections/test_crud.py @@ -877,7 +877,11 @@ def test_edit_errors( self.permission_service.can.reset_mock() assert response.status == 500 response_content = json.loads(response.body) - assert response_content["errors"][0] == {"detail": "🌳🌳🌳", "name": "CollectionResourceException", "status": 500} + assert response_content["errors"][0] == { + "detail": "🌳🌳🌳", + "name": "CollectionResourceException", + "status": 500, + } # JsonApiException mocked_json_serializer_get.return_value.load = Mock(side_effect=JsonApiException) @@ -964,7 +968,11 @@ def test_delete_error(self): assert response.status == 500 response_content = json.loads(response.body) - assert response_content["errors"][0] == {"detail": "🌳🌳🌳", "name": "CollectionResourceException", "status": 500} + assert response_content["errors"][0] == { + "detail": "🌳🌳🌳", + "name": "CollectionResourceException", + "status": 500, + } @patch( "forestadmin.agent_toolkit.resources.collections.crud.ConditionTreeFactory.match_ids", @@ -1095,7 +1103,11 @@ def test_csv_errors(self): assert response.status == 500 response_content = json.loads(response.body) - assert response_content["errors"][0] == {"detail": "🌳🌳🌳cannot make csv", "name": "CsvException", "status": 500} + assert response_content["errors"][0] == { + "detail": "🌳🌳🌳cannot make csv", + "name": "CsvException", + "status": 500, + } def test_csv_should_not_apply_pagination(self): mock_orders = [{"id": 10, "cost": 200}, {"id": 11, "cost": 201}] diff --git a/src/agent_toolkit/tests/services/test_ip_whitelist_service.py b/src/agent_toolkit/tests/services/test_ip_whitelist_service.py index 3b1fd8d2a..c0eaab1ce 100644 --- a/src/agent_toolkit/tests/services/test_ip_whitelist_service.py +++ b/src/agent_toolkit/tests/services/test_ip_whitelist_service.py @@ -150,5 +150,8 @@ def test_call_to_retrieve_should_raise_if_forest_backend_not_available(self): side_effect=Exception("backend not available"), ): self.assertRaisesRegex( - ForestException, r"🌳🌳🌳backend not available", self.loop.run_until_complete, self.ip_whitelist.retrieve() + ForestException, + r"🌳🌳🌳backend not available", + self.loop.run_until_complete, + self.ip_whitelist.retrieve(), ) From 63c243e704f05a7072db6439382157bf7bcd3d4b Mon Sep 17 00:00:00 2001 From: Julien Barreau Date: Fri, 26 Jan 2024 14:04:56 +0100 Subject: [PATCH 3/4] chore: try to fix black would reformat --- .../tests/decorators/action/types/test_fields.py | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/datasource_toolkit/tests/decorators/action/types/test_fields.py b/src/datasource_toolkit/tests/decorators/action/types/test_fields.py index 77bb2537a..77be5c346 100644 --- a/src/datasource_toolkit/tests/decorators/action/types/test_fields.py +++ b/src/datasource_toolkit/tests/decorators/action/types/test_fields.py @@ -33,7 +33,12 @@ def test_field_factory_should_raise_if_unknown_type(self): value="1", default_value="10", ) - self.assertRaisesRegex(FieldFactoryException, r"🌳🌳🌳Unknown field type: 'bla'", FieldFactory.build, plain_field) + self.assertRaisesRegex( + FieldFactoryException, + r"🌳🌳🌳Unknown field type: 'bla'", + FieldFactory.build, + plain_field, + ) def test_field_factory_should_raise_if_bad_param(self): plain_field = PlainStringDynamicField( From a9d68b319e04e114d48ecb78ca01a3f7ce490587 Mon Sep 17 00:00:00 2001 From: Julien Barreau Date: Fri, 26 Jan 2024 14:09:40 +0100 Subject: [PATCH 4/4] chore: try to fix black would reformat --- src/agent_toolkit/tests/resources/actions/test_resources.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/agent_toolkit/tests/resources/actions/test_resources.py b/src/agent_toolkit/tests/resources/actions/test_resources.py index d19cf35b5..bc420cf42 100644 --- a/src/agent_toolkit/tests/resources/actions/test_resources.py +++ b/src/agent_toolkit/tests/resources/actions/test_resources.py @@ -203,7 +203,11 @@ def test_dispatch_should_return_error_if_no_action_name_specified(self): self.assertEqual(response.status, 500) self.assertEqual( content["errors"][0], - {"name": "RequestActionException", "detail": "🌳🌳🌳'action_name' is missing in the request", "status": 500}, + { + "name": "RequestActionException", + "detail": "🌳🌳🌳'action_name' is missing in the request", + "status": 500, + }, ) def test_should_return_the_return_value_of_hook_when_method_name_is_hook(self):