Skip to content

Commit

Permalink
Merge pull request #32 from matagus/more-tests
Browse files Browse the repository at this point in the history
Added more tests to take coverage close to 100%
  • Loading branch information
matagus committed Feb 9, 2024
2 parents fba9cd9 + c560cad commit f126f78
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 16 deletions.
4 changes: 2 additions & 2 deletions generic_links/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,5 +30,5 @@ class Meta:
verbose_name = _("Generic Link")
verbose_name_plural = _("Generic Links")

def __unicode__(self):
return self.url
def __str__(self):
return f"{self.title} :: {self.url}"
20 changes: 20 additions & 0 deletions tests/test_forms.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
from django.test import TestCase

from generic_links.forms import AddLinkForm


class AddFormTest(TestCase):
def setUp(self):
self.initial_kwargs = dict(user=None, object_id="1", content_type="1")

def test_add_form_without_data(self):
form = AddLinkForm(**self.initial_kwargs)
self.assertFalse(form.is_valid())

def test_add_form_with_incomplete_data(self):
form = AddLinkForm(data={"url": "http://www.example.com"}, **self.initial_kwargs)
self.assertFalse(form.is_valid())

def test_add_form_with_complete_data(self):
form = AddLinkForm(data={"url": "http://www.example.com", "title": "Example"}, **self.initial_kwargs)
self.assertTrue(form.is_valid())
30 changes: 16 additions & 14 deletions tests/test_models.py
Original file line number Diff line number Diff line change
@@ -1,21 +1,23 @@
"""
test_generic_links
------------
Tests for `generic_links` models module.
"""

from __future__ import annotations

from django.contrib.auth.models import User
from django.contrib.contenttypes.models import ContentType
from django.test import TestCase

from generic_links.models import GenericLink


class TestGeneric_links(TestCase):
def setUp(self):
pass
class ModelTest(TestCase):
def test_str(self):
user = User.objects.create(username="Test User")
content_type = ContentType.objects.get_for_model(User)

def test_something(self):
pass
link = GenericLink.objects.create(
title="Test Title",
url="https://www.test.com",
description="Test Description",
content_type=content_type,
object_id=user.id,
)

def tearDown(self):
pass
self.assertEqual(str(link), f"{link.title} :: {link.url}")
25 changes: 25 additions & 0 deletions tests/test_templatetags.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,3 +60,28 @@ def test_get_links_for(self):
rendered_string2 = template.render(Context({"my_obj": self.user2}))

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

def test_get_external_links_for(self):
"""
Test the `get_links_for` template tag with is_external argument.
"""

template_string = """
{% load generic_links_tags %}
{% get_links_for my_obj is_external=True 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 f126f78

Please sign in to comment.