Skip to content

Commit

Permalink
Merge pull request #20 from matagus/more-improvements-2
Browse files Browse the repository at this point in the history
Tests-related improvements
  • Loading branch information
matagus authored Feb 6, 2024
2 parents a9e3951 + 86f8865 commit 88c1bb3
Show file tree
Hide file tree
Showing 8 changed files with 69 additions and 11 deletions.
Empty file.
Empty file.
2 changes: 0 additions & 2 deletions generic_links/test_utils/test_app/admin.py

This file was deleted.

7 changes: 0 additions & 7 deletions generic_links/test_utils/test_app/apps.py

This file was deleted.

Empty file.
2 changes: 0 additions & 2 deletions generic_links/test_utils/test_app/models.py

This file was deleted.

6 changes: 6 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,12 @@ include = [
"generic_links/",
]

[tool.hatch.envs.default]
dependencies = [
"Django>=4.0", "ipython", "ipdb", "mypy", "typing-extensions",
]


# Test environment
[[tool.hatch.envs.test.matrix]]
python = ["3.9", "3.10", "3.11"]
Expand Down
63 changes: 63 additions & 0 deletions tests/test_templatetags.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
from django.contrib.auth.models import User
from django.contrib.contenttypes.models import ContentType
from django.template import Context
from django.template import Template
from django.test import TestCase

from generic_links.models import GenericLink


class TemplateTagTestCase(TestCase):
"""
Test case for the template tag `get_links_for <obj>` in the `generic_links_tags.py` file.
"""

def setUp(self):
content_type = ContentType.objects.get_for_model(User)

self.user1 = User.objects.create(username="test_user")

self.link1 = GenericLink.objects.create(
url="https://example.com/path/to/this-thing/",
content_type=content_type,
object_id=self.user1.id,
)

self.link2 = GenericLink.objects.create(
url="https://example.com/path/to/other-thing/",
content_type=content_type,
object_id=self.user1.id,
)

self.user2 = User.objects.create(username="test_user2")

self.link3 = GenericLink.objects.create(
url="https://example.com/foobar/",
content_type=content_type,
object_id=self.user2.id,
)

def test_get_links_for(self):
"""
Test the `get_links_for` template tag.
"""

template_string = """
{% load generic_links_tags %}
{% get_links_for my_obj as links_qs %}
{% for link in links_qs %}
{{ link.url }}
{% endfor %}
"""

template = Template(template_string)

rendered_string = template.render(Context({"my_obj": self.user1}))

self.assertIn(self.link1.url, rendered_string)
self.assertIn(self.link2.url, rendered_string)
self.assertNotIn(self.link3.url, rendered_string)

rendered_string2 = template.render(Context({"my_obj": self.user2}))

self.assertEqual(self.link3.url, rendered_string2.strip())

0 comments on commit 88c1bb3

Please sign in to comment.