Skip to content

Commit

Permalink
Fix regex asserts (#1719)
Browse files Browse the repository at this point in the history
* 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.

* Add news entry

* Apply suggestions from code review

---------

Co-authored-by: David Glick <david@glicksoftware.com>
  • Loading branch information
gforcada and davisagli committed Oct 9, 2023
1 parent 3927a89 commit 063e4e0
Show file tree
Hide file tree
Showing 7 changed files with 69 additions and 86 deletions.
2 changes: 2 additions & 0 deletions news/1.bugfix
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Replace deprecated assert methods.
[gforcada]
8 changes: 4 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,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))
Expand All @@ -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))
Expand All @@ -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))
Expand Down
10 changes: 4 additions & 6 deletions src/plone/restapi/tests/test_permissions.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
14 changes: 5 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,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"}
Expand Down
72 changes: 35 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,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")
Expand All @@ -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):
Expand All @@ -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")
Expand Down
6 changes: 2 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,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")
Expand Down
43 changes: 17 additions & 26 deletions src/plone/restapi/tests/test_services_querystring.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]"},
Expand All @@ -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 = {
Expand All @@ -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"]
Expand Down

0 comments on commit 063e4e0

Please sign in to comment.