Skip to content

Commit

Permalink
fix(py3.12): deprecated methods
Browse files Browse the repository at this point in the history
I used the approach suggested on https://stackoverflow.com/questions/20050913

This makes plone.restapi tests pass on Python 3.12.
  • Loading branch information
gforcada committed Oct 5, 2023
1 parent 3927a89 commit 65c6947
Show file tree
Hide file tree
Showing 6 changed files with 88 additions and 89 deletions.
12 changes: 8 additions & 4 deletions src/plone/restapi/tests/test_batching.py
Original file line number Diff line number Diff line change
Expand Up @@ -455,7 +455,8 @@ 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)
expected = {"first": "http://nohost?b_start=0"}
self.assertEqual(batch.links, {**batch_links, **expected})

def test_first_link_preserves_list_like_querystring_params(self):
items = list(range(1, 26))
Expand All @@ -478,14 +479,16 @@ 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)
expected = {"last": "http://nohost?b_start=20"}
self.assertEqual(batch.links, {**batch_links, **expected})

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)
expected = {"next": "http://nohost?b_start=10"}
self.assertEqual(batch.links, {**batch_links, **expected})

def test_next_link_omitted_on_last_page(self):
items = list(range(1, 26))
Expand All @@ -503,7 +506,8 @@ 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)
expected = {"prev": "http://nohost?b_start=10"}
self.assertEqual(batch.links, {**batch_links, **expected})

def test_prev_link_omitted_on_first_page(self):
items = list(range(1, 26))
Expand Down
13 changes: 6 additions & 7 deletions src/plone/restapi/tests/test_permissions.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,9 @@ 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(),
)
expected = {
"type": "Unauthorized",
"message": "Missing 'plone.restapi: Use REST API' permission",
}
data = response.json()
self.assertEqual(data, {**data, **expected})
17 changes: 8 additions & 9 deletions src/plone/restapi/tests/test_search.py
Original file line number Diff line number Diff line change
Expand Up @@ -247,15 +247,14 @@ 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],
)
expected = {
"@id": self.portal_url + "/folder/doc",
"title": "Lorem Ipsum",
"portal_type": "DXTestDocument",
"review_state": "private",
}
item = response.json()["items"][0]
self.assertEqual(item, {**item, **expected})

def test_full_metadata_retrieval(self):
query = {"SearchableText": "lorem", "metadata_fields": "_all"}
Expand Down
78 changes: 41 additions & 37 deletions src/plone/restapi/tests/test_serializer_catalog.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,10 @@ 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)
expected = {"@id": "http://nohost"}
self.assertEqual(results, {**results, **expected})
expected = {"items_total": 3}
self.assertEqual(results, {**results, **expected})
self.assertEqual(3, len(results["items"]))

@unittest.skipIf(HAS_PLONE_6, "... before it was not")
Expand All @@ -65,8 +67,10 @@ 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)
expected = {"@id": "http://nohost"}
self.assertEqual(results, {**results, **expected})
expected = {"items_total": 2}
self.assertEqual(results, {**results, **expected})
self.assertEqual(2, len(results["items"]))

def test_lazy_map_serialization_with_fullobjects(self):
Expand All @@ -76,45 +80,45 @@ def test_lazy_map_serialization_with_fullobjects(self):
fullobjects=True
)

self.assertDictContainsSubset({"@id": "http://nohost"}, results)
self.assertDictContainsSubset({"items_total": 1}, results)
expected = {"@id": "http://nohost"}
self.assertEqual(results, {**results, **expected})
expected = {"items_total": 1}
self.assertEqual(results, {**results, **expected})
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")
Expand Down
7 changes: 3 additions & 4 deletions src/plone/restapi/tests/test_services.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,10 +111,9 @@ 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"],
)
expected = {"download": self.portal_url + f"/news1/@@images/{scale_url_uuid}.png"}
data = response.json()["image"]
self.assertEqual(data, {**data, **expected})

def test_get_folder(self):
self.portal.invokeFactory("Folder", id="folder1", title="My Folder")
Expand Down
50 changes: 22 additions & 28 deletions src/plone/restapi/tests/test_services_querystring.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,13 +80,11 @@ 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,
)
expected = {
"title": "Review state",
"vocabulary": "plone.app.vocabularies.WorkflowStates",
}
sef.assertEqual(idx, {**idx, **expected})

expected_vocab_values = {
"external": {"title": "Externally visible [external]"},
Expand All @@ -110,16 +108,14 @@ 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,
)
expected = {
"title": "Default Page",
"operations": [
"plone.app.querystring.operation.boolean.isTrue",
"plone.app.querystring.operation.boolean.isFalse",
],
}
sef.assertEqual(idx, {**idx, **expected})

expected_operators = {
"plone.app.querystring.operation.boolean.isFalse": {
Expand All @@ -144,17 +140,15 @@ 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,
)
expected = {
"title": "Order in folder",
"operations": [
"plone.app.querystring.operation.int.is",
"plone.app.querystring.operation.int.lessThan",
"plone.app.querystring.operation.int.largerThan",
],
}
sef.assertEqual(idx, {**idx, **expected})

ops = idx["operators"]

Expand Down

0 comments on commit 65c6947

Please sign in to comment.