Skip to content

Commit

Permalink
Update tests
Browse files Browse the repository at this point in the history
  • Loading branch information
fsbraun committed Oct 26, 2024
1 parent c452218 commit aa8d960
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 28 deletions.
6 changes: 3 additions & 3 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -139,9 +139,9 @@ You can run tests by executing::
.. |pypi| image:: https://badge.fury.io/py/djangocms-link.svg
:target: http://badge.fury.io/py/djangocms-link
.. |build| image:: https://travis-ci.org/divio/djangocms-link.svg?branch=master
:target: https://travis-ci.org/divio/djangocms-link
.. |coverage| image:: https://codecov.io/gh/divio/djangocms-link/branch/master/graph/badge.svg
:target: https://codecov.io/gh/divio/djangocms-link
:target: https://travis-ci.org/django-cms/djangocms-link
.. |coverage| image:: https://codecov.io/gh/django-cms/djangocms-link/branch/master/graph/badge.svg
:target: https://codecov.io/gh/django-cms/djangocms-link

.. |python| image:: https://img.shields.io/badge/python-3.10+-blue.svg
:target: https://pypi.org/project/djangocms-link/
Expand Down
16 changes: 7 additions & 9 deletions djangocms_link/admin.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import traceback

from django.apps import apps
from django.contrib import admin
from django.core.exceptions import FieldError, PermissionDenied
Expand Down Expand Up @@ -118,7 +116,7 @@ def get_queryset(self):
).current_content().values_list("page_id", flat=True)
qs = Page.objects.filter(pk__in=qs).order_by("path")
if self.site:
qs = qs.filter(page__site_id=self.site)
qs = qs.filter(site_id=self.site)

Check warning on line 119 in djangocms_link/admin.py

View check run for this annotation

Codecov / codecov/patch

djangocms_link/admin.py#L119

Added line #L119 was not covered by tests
except (AttributeError, FieldError):
# django CMS 3.11 - 4.1
if hasattr(PageContent, "admin_manager"): # V4
Expand All @@ -131,7 +129,7 @@ def get_queryset(self):
).values_list("page_id", flat=True)
qs = Page.objects.filter(pk__in=qs).order_by("node__path")
if self.site:
qs = qs.filter(page__site_id=self.site)
qs = qs.filter(node__site_id=self.site)
return list(qs)

def add_admin_querysets(self, qs):
Expand All @@ -148,10 +146,10 @@ def add_admin_querysets(self, qs):
new_qs = new_qs.distinct()

Check warning on line 146 in djangocms_link/admin.py

View check run for this annotation

Codecov / codecov/patch

djangocms_link/admin.py#L146

Added line #L146 was not covered by tests

qs += list(new_qs)
except Exception as e:
print("==>", e)
traceback.print_tb(e.__traceback__)
# raise e
except Exception:

Check warning on line 149 in djangocms_link/admin.py

View check run for this annotation

Codecov / codecov/patch

djangocms_link/admin.py#L148-L149

Added lines #L148 - L149 were not covered by tests
# Still report back remaining urls even if one model fails
pass

Check warning on line 151 in djangocms_link/admin.py

View check run for this annotation

Codecov / codecov/patch

djangocms_link/admin.py#L151

Added line #L151 was not covered by tests

return qs

def process_request(self, request):
Expand Down Expand Up @@ -185,7 +183,7 @@ def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.link_url_name = f"{self.opts.app_label}_{self.opts.model_name}_urls"

def has_module_permission(self, request):
def has_module_permission(self, request): # pragma: no cover
# Remove from admin
return False

Expand Down
1 change: 0 additions & 1 deletion djangocms_link/validators.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,6 @@ def __call__(self, value):
value = value.lstrip("#")

Check warning on line 54 in djangocms_link/validators.py

View check run for this annotation

Codecov / codecov/patch

djangocms_link/validators.py#L54

Added line #L54 was not covered by tests
if not value:
return value

Check warning on line 56 in djangocms_link/validators.py

View check run for this annotation

Codecov / codecov/patch

djangocms_link/validators.py#L56

Added line #L56 was not covered by tests
print("AnchorValidator", value)
if not isinstance(value, str) or len(value) > 100:
raise ValidationError(self.message, code=self.code, params={"value": value})

Check warning on line 58 in djangocms_link/validators.py

View check run for this annotation

Codecov / codecov/patch

djangocms_link/validators.py#L58

Added line #L58 was not covered by tests

Expand Down
46 changes: 31 additions & 15 deletions tests/test_endpoint.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,32 @@ def tearDown(self):
self.subling.delete()

def test_api_endpoint(self):

for query_params in ("", "?app_label=1"):
with self.subTest(query_params=query_params):
with self.login_user_context(self.get_superuser()):
response = self.client.get(self.endpoint + query_params)
self.assertEqual(response.status_code, 200)
data = response.json()

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

pages = data["results"][0]
self.assertEqual(pages["text"], "Pages")
for page in pages["children"]:
self.assertIn("id", page)
self.assertIn("text", page)
self.assertIn("url", page)
_, pk = page["id"].split(":")
db_page = Page.objects.get(pk=pk)
self.assertEqual(page["text"], str(db_page))

def test_filter(self):
with self.login_user_context(self.get_superuser()):
response = self.client.get(self.endpoint)
response = self.client.get(self.endpoint + "?term=child")
self.assertEqual(response.status_code, 200)
data = response.json()

Expand All @@ -51,32 +75,24 @@ def test_api_endpoint(self):
self.assertEqual(data["pagination"]["more"], False)

pages = data["results"][0]
self.assertEqual(pages["text"], "Pages")
for page in pages["children"]:
self.assertIn("id", page)
self.assertIn("text", page)
self.assertIn("url", page)
_, pk = page["id"].split(":")
db_page = Page.objects.get(pk=pk)
self.assertEqual(page["text"], str(db_page))
self.assertEqual(len(pages["children"]), 2)

def test_filter(self):
def test_filter_with_empty_result(self):
with self.login_user_context(self.get_superuser()):
response = self.client.get(self.endpoint + "?term=child")
response = self.client.get(self.endpoint + "?term=DJANGOCMS")
self.assertEqual(response.status_code, 200)
data = response.json()

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

pages = data["results"][0]
self.assertEqual(len(pages["children"]), 2)
self.assertEqual(pages, {})

def test_filter_with_empty_result(self):
def test_site_selector(self):
with self.login_user_context(self.get_superuser()):
response = self.client.get(self.endpoint + "?term=DJANGOCMS")
response = self.client.get(self.endpoint + "?app_label=2")
self.assertEqual(response.status_code, 200)
data = response.json()

Expand Down

0 comments on commit aa8d960

Please sign in to comment.