From 666c240998714766ca8bef238b6e387eaed83b8a Mon Sep 17 00:00:00 2001 From: Gil Forcada Codinachs Date: Thu, 5 Oct 2023 14:44:07 +0200 Subject: [PATCH 1/3] fix(py3.12): deprecated methods I used the approach suggested on https://stackoverflow.com/questions/20050913 This makes plone.restapi tests pass on Python 3.12. --- src/plone/restapi/tests/test_batching.py | 8 +-- src/plone/restapi/tests/test_permissions.py | 10 ++- src/plone/restapi/tests/test_search.py | 14 ++-- .../restapi/tests/test_serializer_catalog.py | 72 +++++++++---------- src/plone/restapi/tests/test_services.py | 6 +- .../tests/test_services_querystring.py | 43 +++++------ 6 files changed, 67 insertions(+), 86 deletions(-) diff --git a/src/plone/restapi/tests/test_batching.py b/src/plone/restapi/tests/test_batching.py index 810846298c..3e809ebabc 100644 --- a/src/plone/restapi/tests/test_batching.py +++ b/src/plone/restapi/tests/test_batching.py @@ -455,7 +455,7 @@ def test_first_link_contained(self): self.request.form["b_size"] = 10 batch = HypermediaBatch(self.request, items) - self.assertDictContainsSubset({"first": "http://nohost?b_start=0"}, batch.links) + self.assertEqual(batch.links["first"], "http://nohost?b_start=0") def test_first_link_preserves_list_like_querystring_params(self): items = list(range(1, 26)) @@ -478,14 +478,14 @@ def test_last_link_contained(self): self.request.form["b_size"] = 10 batch = HypermediaBatch(self.request, items) - self.assertDictContainsSubset({"last": "http://nohost?b_start=20"}, batch.links) + self.assertEqual(batch.links["last"], "http://nohost?b_start=20") def test_next_link_contained_if_necessary(self): items = list(range(1, 26)) self.request.form["b_size"] = 10 batch = HypermediaBatch(self.request, items) - self.assertDictContainsSubset({"next": "http://nohost?b_start=10"}, batch.links) + self.assertEqual(batch.links["next"], "http://nohost?b_start=10") def test_next_link_omitted_on_last_page(self): items = list(range(1, 26)) @@ -503,7 +503,7 @@ def test_prev_link_contained_if_necessary(self): self.request.form["b_size"] = 10 self.request.form["b_start"] = 20 batch = HypermediaBatch(self.request, items) - self.assertDictContainsSubset({"prev": "http://nohost?b_start=10"}, batch.links) + self.assertEqual(batch.links["prev"], "http://nohost?b_start=10") def test_prev_link_omitted_on_first_page(self): items = list(range(1, 26)) diff --git a/src/plone/restapi/tests/test_permissions.py b/src/plone/restapi/tests/test_permissions.py index 917ab4a588..bcd5bfd8e7 100644 --- a/src/plone/restapi/tests/test_permissions.py +++ b/src/plone/restapi/tests/test_permissions.py @@ -55,10 +55,8 @@ def test_unauthorized_if_missing_permission(self): response = self.api_session.get(self.portal_url) self.assertEqual(response.status_code, 401) - self.assertDictContainsSubset( - { - "type": "Unauthorized", - "message": "Missing 'plone.restapi: Use REST API' permission", - }, - response.json(), + data = response.json() + self.assertEqual(data["type"], "Unauthorized") + self.assertEqual( + data["message"], "Missing 'plone.restapi: Use REST API' permission" ) diff --git a/src/plone/restapi/tests/test_search.py b/src/plone/restapi/tests/test_search.py index 2f66ecdba9..e4ddb4c38d 100644 --- a/src/plone/restapi/tests/test_search.py +++ b/src/plone/restapi/tests/test_search.py @@ -247,15 +247,11 @@ def test_partial_metadata_retrieval(self): } response = self.api_session.get("/@search", params=query) - self.assertDictContainsSubset( - { - "@id": self.portal_url + "/folder/doc", - "title": "Lorem Ipsum", - "portal_type": "DXTestDocument", - "review_state": "private", - }, - response.json()["items"][0], - ) + item = response.json()["items"][0] + self.assertEqual(item["@id"], self.portal_url + "/folder/doc") + self.assertEqual(item["title"], "Lorem Ipsum") + self.assertEqual(item["portal_type"], "DXTestDocument") + self.assertEqual(item["review_state"], "private") def test_full_metadata_retrieval(self): query = {"SearchableText": "lorem", "metadata_fields": "_all"} diff --git a/src/plone/restapi/tests/test_serializer_catalog.py b/src/plone/restapi/tests/test_serializer_catalog.py index 453680f02c..90abb5a0ae 100644 --- a/src/plone/restapi/tests/test_serializer_catalog.py +++ b/src/plone/restapi/tests/test_serializer_catalog.py @@ -55,8 +55,8 @@ def test_lazy_map_serialization(self): lazy_map = self.catalog() results = getMultiAdapter((lazy_map, self.request), ISerializeToJson)() - self.assertDictContainsSubset({"@id": "http://nohost"}, results) - self.assertDictContainsSubset({"items_total": 3}, results) + self.assertEqual(results["@id"], "http://nohost") + self.assertEqual(results["@items_total"], 3) self.assertEqual(3, len(results["items"])) @unittest.skipIf(HAS_PLONE_6, "... before it was not") @@ -65,8 +65,8 @@ def test_lazy_map_serialization_plone5(self): lazy_map = self.catalog() results = getMultiAdapter((lazy_map, self.request), ISerializeToJson)() - self.assertDictContainsSubset({"@id": "http://nohost"}, results) - self.assertDictContainsSubset({"items_total": 2}, results) + self.assertEqual(results["@id"], "http://nohost") + self.assertEqual(results["@items_total"], 2) self.assertEqual(2, len(results["items"])) def test_lazy_map_serialization_with_fullobjects(self): @@ -76,45 +76,43 @@ def test_lazy_map_serialization_with_fullobjects(self): fullobjects=True ) - self.assertDictContainsSubset({"@id": "http://nohost"}, results) - self.assertDictContainsSubset({"items_total": 1}, results) + self.assertEqual(results["@id"], "http://nohost") + self.assertEqual(results["@items_total"], 1) self.assertEqual(1, len(results["items"])) result_item = results["items"][0] - self.assertDictContainsSubset( - { - "@id": "http://nohost/plone/my-folder/my-document", - "@type": "Document", - "changeNote": "", - "contributors": [], - "creators": ["test_user_1_"], + expected = { + "@id": "http://nohost/plone/my-folder/my-document", + "@type": "Document", + "changeNote": "", + "contributors": [], + "creators": ["test_user_1_"], + "description": "", + "effective": None, + "exclude_from_nav": False, + "expires": None, + "id": "my-document", + "is_folderish": False, + "language": "", + "layout": "document_view", + "parent": { + "@id": "http://nohost/plone/my-folder", + "@type": "Folder", + "type_title": "Folder", "description": "", - "effective": None, - "exclude_from_nav": False, - "expires": None, - "id": "my-document", - "is_folderish": False, - "language": "", - "layout": "document_view", - "parent": { - "@id": "http://nohost/plone/my-folder", - "@type": "Folder", - "type_title": "Folder", - "description": "", - "review_state": "private", - "title": "My Folder", - }, - "relatedItems": [], "review_state": "private", - "rights": "", - "subjects": [], - "table_of_contents": None, - "text": None, - "title": "My Document", - "version": "current", + "title": "My Folder", }, - result_item, - ) + "relatedItems": [], + "review_state": "private", + "rights": "", + "subjects": [], + "table_of_contents": None, + "text": None, + "title": "My Document", + "version": "current", + } + self.assertEqual(result_item, {**result_item, **expected}) def test_brain_summary_representation(self): lazy_map = self.catalog(path="/plone/my-folder/my-document") diff --git a/src/plone/restapi/tests/test_services.py b/src/plone/restapi/tests/test_services.py index dac51df7ca..1a0944efd3 100644 --- a/src/plone/restapi/tests/test_services.py +++ b/src/plone/restapi/tests/test_services.py @@ -111,10 +111,8 @@ def test_get_news_item(self): self.assertEqual( "This is an image caption.", response.json()["image_caption"] ) - self.assertDictContainsSubset( - {"download": self.portal_url + f"/news1/@@images/{scale_url_uuid}.png"}, - response.json()["image"], - ) + url = self.portal_url + f"/news1/@@images/{scale_url_uuid}.png" + self.assertEqual(response.json()["image"]["download"], url) def test_get_folder(self): self.portal.invokeFactory("Folder", id="folder1", title="My Folder") diff --git a/src/plone/restapi/tests/test_services_querystring.py b/src/plone/restapi/tests/test_services_querystring.py index 1203ab6336..8ccb0f7ebb 100644 --- a/src/plone/restapi/tests/test_services_querystring.py +++ b/src/plone/restapi/tests/test_services_querystring.py @@ -80,13 +80,8 @@ def test_endpoint_inlines_vocabularies(self): indexes = response.json()["indexes"] idx = indexes["review_state"] - self.assertDictContainsSubset( - { - "title": "Review state", - "vocabulary": "plone.app.vocabularies.WorkflowStates", - }, - idx, - ) + self.assertEqual(idx["title"], "Review state") + self.assertEqual(idx["vocabulary"], "plone.app.vocabularies.WorkflowStates") expected_vocab_values = { "external": {"title": "Externally visible [external]"}, @@ -110,15 +105,13 @@ def test_endpoint_inlines_operators(self): indexes = response.json()["indexes"] idx = indexes["isDefaultPage"] - self.assertDictContainsSubset( - { - "title": "Default Page", - "operations": [ - "plone.app.querystring.operation.boolean.isTrue", - "plone.app.querystring.operation.boolean.isFalse", - ], - }, - idx, + self.assertEqual(idx["title"], "Default Page") + self.assertEqual( + idx["operations"], + [ + "plone.app.querystring.operation.boolean.isTrue", + "plone.app.querystring.operation.boolean.isFalse", + ], ) expected_operators = { @@ -144,16 +137,14 @@ def test_endpoint_includes_widgets_for_operators(self): indexes = response.json()["indexes"] idx = indexes["getObjPositionInParent"] - self.assertDictContainsSubset( - { - "title": "Order in folder", - "operations": [ - "plone.app.querystring.operation.int.is", - "plone.app.querystring.operation.int.lessThan", - "plone.app.querystring.operation.int.largerThan", - ], - }, - idx, + self.assertEqual(idx["title"], "Order in folder") + self.assertEqual( + idx["operations"], + [ + "plone.app.querystring.operation.int.is", + "plone.app.querystring.operation.int.lessThan", + "plone.app.querystring.operation.int.largerThan", + ], ) ops = idx["operators"] From 4dfa9e907454d7c4b38da3bd4abacb0b1d1bccc6 Mon Sep 17 00:00:00 2001 From: Gil Forcada Codinachs Date: Thu, 5 Oct 2023 14:45:33 +0200 Subject: [PATCH 2/3] Add news entry --- news/1.bugfix | 2 ++ 1 file changed, 2 insertions(+) create mode 100644 news/1.bugfix diff --git a/news/1.bugfix b/news/1.bugfix new file mode 100644 index 0000000000..1494e057ff --- /dev/null +++ b/news/1.bugfix @@ -0,0 +1,2 @@ +Replace deprecated assert methods. +[gforcada] From 24f943930ec8038417f9b08a0b62a168326237d9 Mon Sep 17 00:00:00 2001 From: David Glick Date: Sat, 7 Oct 2023 16:05:24 +0200 Subject: [PATCH 3/3] Apply suggestions from code review --- src/plone/restapi/tests/test_serializer_catalog.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/plone/restapi/tests/test_serializer_catalog.py b/src/plone/restapi/tests/test_serializer_catalog.py index 90abb5a0ae..67b0604aea 100644 --- a/src/plone/restapi/tests/test_serializer_catalog.py +++ b/src/plone/restapi/tests/test_serializer_catalog.py @@ -56,7 +56,7 @@ def test_lazy_map_serialization(self): results = getMultiAdapter((lazy_map, self.request), ISerializeToJson)() self.assertEqual(results["@id"], "http://nohost") - self.assertEqual(results["@items_total"], 3) + self.assertEqual(results["items_total"], 3) self.assertEqual(3, len(results["items"])) @unittest.skipIf(HAS_PLONE_6, "... before it was not") @@ -66,7 +66,7 @@ def test_lazy_map_serialization_plone5(self): results = getMultiAdapter((lazy_map, self.request), ISerializeToJson)() self.assertEqual(results["@id"], "http://nohost") - self.assertEqual(results["@items_total"], 2) + self.assertEqual(results["items_total"], 2) self.assertEqual(2, len(results["items"])) def test_lazy_map_serialization_with_fullobjects(self): @@ -77,7 +77,7 @@ def test_lazy_map_serialization_with_fullobjects(self): ) self.assertEqual(results["@id"], "http://nohost") - self.assertEqual(results["@items_total"], 1) + self.assertEqual(results["items_total"], 1) self.assertEqual(1, len(results["items"])) result_item = results["items"][0]