Skip to content

Commit

Permalink
Improved endpoint tests
Browse files Browse the repository at this point in the history
  • Loading branch information
fsbraun committed Oct 27, 2024
1 parent c3a3c1f commit 106f4ac
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 1 deletion.
2 changes: 1 addition & 1 deletion djangocms_link/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ def get_queryset_manager(base: models.QuerySet | models.Manager) -> models.Query


def get_manager(model: models.Model, current_content: bool = False) -> models.Manager:
if hasattr(model, "admin_manager"): # pragma: no cover
if hasattr(model, "admin_manager"):
return model.admin_manager.current_content() if current_content else model.admin_manager
return model.objects

Expand Down
34 changes: 34 additions & 0 deletions tests/test_endpoint.py
Original file line number Diff line number Diff line change
Expand Up @@ -242,3 +242,37 @@ def test_get_reference(self):
self.assertEqual(data["id"], "utils.thirdpartymodel:1")
self.assertEqual(data["text"], "First")
self.assertEqual(data["url"], self.items[0].get_absolute_url())


class LinkEndpointMultiModelTestCase(CMSTestCase):
def setUp(self):

LinkAdmin = admin.site._registry[Link]

self.endpoint = admin_reverse(LinkAdmin.link_url_name)
self.root_page = create_page(
title="root",
template="page.html",
language="en",
)
self.items = (
ThirdPartyModel.objects.create(name="First", path="/first"),
ThirdPartyModel.objects.create(name="Second", path="/second"),
)

def test_api_endpoint(self):
with self.login_user_context(self.get_superuser()):
response = self.client.get(self.endpoint)
self.assertEqual(response.status_code, 200)
data = response.json()

self.assertIn("results", data)
self.assertIn("pagination", data)
self.assertEqual(data["pagination"]["more"], False)

# Two optgroups:
# 1. Pages (always first)
# 2. Third party models
self.assertEqual(len(data["results"]), 2)
self.assertEqual(data["results"][0]["text"], "Pages")
self.assertEqual(data["results"][1]["text"], "Third party models")

0 comments on commit 106f4ac

Please sign in to comment.