Repository: uccser/cs-unplugged

Issue:
Create example test for templatetags

Create example test for templatetags



Discussion:

Resolving patch:
diff --git a/csunplugged/general/templatetags/render_html_field.py b/csunplugged/general/templatetags/render_html_field.py
--- a/csunplugged/general/templatetags/render_html_field.py
+++ b/csunplugged/general/templatetags/render_html_field.py
@@ -3,6 +3,12 @@
 from django import template
 from django.template import Template, Variable, TemplateSyntaxError
 
+INVALID_ATTRIBUTE_MESSAGE = "The 'render_html_field' tag was given an " \
+                            "attribute that could not be converted to a string."
+
+MISSING_ATTRIBUTE_MESSAGE = "The 'render_html_field' tag was given an " \
+                            "attribute that does not exist."
+
 
 class RenderHTMLFieldNode(template.Node):
     """Class used for the custom render_html_field template tag."""
@@ -21,8 +27,10 @@ def render(self, context):
         try:
             actual_item = '{% load static %}\n' + self.item_to_be_rendered.resolve(context)
             return Template(actual_item).render(context)
+        except TypeError:
+            raise TemplateSyntaxError(INVALID_ATTRIBUTE_MESSAGE)
         except template.VariableDoesNotExist:
-            return ''
+            raise TemplateSyntaxError(MISSING_ATTRIBUTE_MESSAGE)
 
 
 def render_html_field(parser, token):


Test patch:
diff --git a/csunplugged/tests/general/templatetags/test_render_html_field.py b/csunplugged/tests/general/templatetags/test_render_html_field.py
--- a/csunplugged/tests/general/templatetags/test_render_html_field.py
+++ b/csunplugged/tests/general/templatetags/test_render_html_field.py
@@ -0,0 +1,112 @@
+from tests.BaseTestWithDB import BaseTestWithDB
+from django import template
+from topics.models import CurriculumIntegration
+from topics.models import Topic
+from model_mommy import mommy
+
+
+class RenderHTMLFieldTest(BaseTestWithDB):
+
+    def __init__(self, *args, **kwargs):
+        super().__init__(*args, **kwargs)
+
+    def render_template(self, string, context=None):
+        context = context or {}
+        context = template.Context(context)
+        return template.Template(string).render(context)
+
+    def test_render_html_field_without_static(self):
+        topic = mommy.make(Topic)
+        curriculum_integration_without_static = CurriculumIntegration.objects.create(
+            topic=topic,
+            slug="slug_1",
+            number=1,
+            name="1",
+            content="<p>Example text.</p>"
+        )
+        context = {"integration": curriculum_integration_without_static}
+        rendered = self.render_template(
+            "{% load render_html_field %}\n{% render_html_field integration.content %}",
+            context
+        )
+        self.assertHTMLEqual(rendered, "<p>Example text.</p>")
+
+    def test_render_html_field_with_static(self):
+        topic = mommy.make(Topic)
+        curriculum_integration_with_static = CurriculumIntegration.objects.create(
+            topic=topic,
+            slug="slug_2",
+            number=2,
+            name="2",
+            content="<img src='{% static 'img/logo-small.png' %}'>"
+        )
+        context = {"integration": curriculum_integration_with_static}
+        rendered = self.render_template(
+            "{% load render_html_field %}\n{% render_html_field integration.content %}",
+            context
+        )
+        self.assertHTMLEqual(rendered, "<img src='/staticfiles/img/logo-small.png'>")
+
+    def test_render_html_field_empty(self):
+        topic = mommy.make(Topic)
+        curriculum_integration_empty = CurriculumIntegration.objects.create(
+            topic=topic,
+            slug="slug_3",
+            number=3,
+            name="3",
+            content=""
+        )
+        context = {"integration": curriculum_integration_empty}
+        rendered = self.render_template(
+            "{% load render_html_field %}\n{% render_html_field integration.content %}",
+            context
+        )
+        self.assertEqual(rendered.strip(), "")
+
+    def test_render_html_field_zero_parameters(self):
+        self.assertRaises(
+            template.TemplateSyntaxError,
+            self.render_template,
+            "{% load render_html_field %}\n{% render_html_field %}"
+        )
+
+    def test_render_html_field_two_parameters(self):
+        self.assertRaises(
+            template.TemplateSyntaxError,
+            self.render_template,
+            "{% load render_html_field %}\n{% render_html_field param1 param2 %}"
+        )
+
+    def test_render_html_field_invalid_parameter(self):
+        topic = mommy.make(Topic)
+        curriculum_integration_with_static = CurriculumIntegration.objects.create(
+            topic=topic,
+            slug="slug_2",
+            number=2,
+            name="2",
+            content="<img src='{% static 'img/logo-small.png' %}'>"
+        )
+        context = {"integration": curriculum_integration_with_static}
+        self.assertRaises(
+            template.TemplateSyntaxError,
+            self.render_template,
+            "{% load render_html_field %}\n{% render_html_field integration.topic %}",
+            context
+        )
+
+    def test_render_html_field_missing_parameter(self):
+        topic = mommy.make(Topic)
+        curriculum_integration_with_static = CurriculumIntegration.objects.create(
+            topic=topic,
+            slug="slug_2",
+            number=2,
+            name="2",
+            content="<img src='{% static 'img/logo-small.png' %}'>"
+        )
+        context = {"integration": curriculum_integration_with_static}
+        self.assertRaises(
+            template.TemplateSyntaxError,
+            self.render_template,
+            "{% load render_html_field %}\n{% render_html_field integration.invalid %}",
+            context
+        )
diff --git a/csunplugged/tests/general/urls/test_about.py b/csunplugged/tests/general/urls/test_about.py
deleted file mode 100644
diff --git a/csunplugged/tests/general/urls/test_about_url.py b/csunplugged/tests/general/urls/test_about_url.py
new file mode 100644
--- /dev/null
+++ b/csunplugged/tests/general/urls/test_about_url.py
@@ -0,0 +1,16 @@
+from tests.BaseTestWithDB import BaseTestWithDB
+from django.urls import reverse
+
+
+class AboutURLTest(BaseTestWithDB):
+
+    def __init__(self, *args, **kwargs):
+        super().__init__(*args, **kwargs)
+        self.language = 'en'
+
+    def test_valid_about_url(self):
+        url = reverse('general:about')
+        self.assertEqual(url, '/en/about/')
+
+        response = self.client.get(url)
+        self.assertEqual(200, response.status_code)
diff --git a/csunplugged/tests/general/urls/test_contact_url.py b/csunplugged/tests/general/urls/test_contact_url.py
new file mode 100644
--- /dev/null
+++ b/csunplugged/tests/general/urls/test_contact_url.py
@@ -0,0 +1,16 @@
+from tests.BaseTestWithDB import BaseTestWithDB
+from django.urls import reverse
+
+
+class ContactURLTest(BaseTestWithDB):
+
+    def __init__(self, *args, **kwargs):
+        super().__init__(*args, **kwargs)
+        self.language = 'en'
+
+    def test_valid_contact_url(self):
+        url = reverse('general:contact')
+        self.assertEqual(url, '/en/contact/')
+
+        response = self.client.get(url)
+        self.assertEqual(200, response.status_code)
diff --git a/csunplugged/tests/general/urls/test_health_check_url.py b/csunplugged/tests/general/urls/test_health_check_url.py
new file mode 100644
--- /dev/null
+++ b/csunplugged/tests/general/urls/test_health_check_url.py
@@ -0,0 +1,12 @@
+from tests.BaseTestWithDB import BaseTestWithDB
+
+
+class HealthCheckURLTest(BaseTestWithDB):
+
+    def __init__(self, *args, **kwargs):
+        super().__init__(*args, **kwargs)
+        self.language = 'en'
+
+    def test_valid_health_check_request(self):
+        response = self.client.get('/_ah/health')
+        self.assertEqual(200, response.status_code)
diff --git a/csunplugged/tests/general/urls/test_home.py b/csunplugged/tests/general/urls/test_home.py
deleted file mode 100644
diff --git a/csunplugged/tests/general/urls/test_index_url.py b/csunplugged/tests/general/urls/test_index_url.py
new file mode 100644
--- /dev/null
+++ b/csunplugged/tests/general/urls/test_index_url.py
@@ -0,0 +1,16 @@
+from tests.BaseTestWithDB import BaseTestWithDB
+from django.urls import reverse
+
+
+class IndexURLTest(BaseTestWithDB):
+
+    def __init__(self, *args, **kwargs):
+        super().__init__(*args, **kwargs)
+        self.language = 'en'
+
+    def test_valid_index_request(self):
+        url = reverse('general:home')
+        self.assertEqual(url, '/en/')
+
+        response = self.client.get(url)
+        self.assertEqual(200, response.status_code)
diff --git a/csunplugged/tests/general/urls/test_people_url.py b/csunplugged/tests/general/urls/test_people_url.py
new file mode 100644
--- /dev/null
+++ b/csunplugged/tests/general/urls/test_people_url.py
@@ -0,0 +1,16 @@
+from tests.BaseTestWithDB import BaseTestWithDB
+from django.urls import reverse
+
+
+class PeopleURLTest(BaseTestWithDB):
+
+    def __init__(self, *args, **kwargs):
+        super().__init__(*args, **kwargs)
+        self.language = 'en'
+
+    def test_valid_people_url(self):
+        url = reverse('general:people')
+        self.assertEqual(url, '/en/people/')
+
+        response = self.client.get(url)
+        self.assertEqual(200, response.status_code)
diff --git a/csunplugged/tests/general/urls/test_principles_url.py b/csunplugged/tests/general/urls/test_principles_url.py
new file mode 100644
--- /dev/null
+++ b/csunplugged/tests/general/urls/test_principles_url.py
@@ -0,0 +1,16 @@
+from tests.BaseTestWithDB import BaseTestWithDB
+from django.urls import reverse
+
+
+class PrinciplesURLTest(BaseTestWithDB):
+
+    def __init__(self, *args, **kwargs):
+        super().__init__(*args, **kwargs)
+        self.language = 'en'
+
+    def test_valid_principles_url(self):
+        url = reverse('general:principles')
+        self.assertEqual(url, '/en/principles/')
+
+        response = self.client.get(url)
+        self.assertEqual(200, response.status_code)
diff --git a/csunplugged/tests/general/views/test_general_about_view.py b/csunplugged/tests/general/views/test_general_about_view.py
--- a/csunplugged/tests/general/views/test_general_about_view.py
+++ b/csunplugged/tests/general/views/test_general_about_view.py
@@ -0,0 +1,14 @@
+from tests.BaseTestWithDB import BaseTestWithDB
+from django.urls import reverse
+
+
+class AboutViewTest(BaseTestWithDB):
+
+    def __init__(self, *args, **kwargs):
+        super().__init__(*args, **kwargs)
+        self.language = 'en'
+
+    def test_about_view(self):
+        response = self.client.get(reverse('general:about'))
+        self.assertEqual(response.status_code, 200)
+        self.assertContains(response, "About")
diff --git a/csunplugged/tests/general/views/test_general_contact_view.py b/csunplugged/tests/general/views/test_general_contact_view.py
new file mode 100644
--- /dev/null
+++ b/csunplugged/tests/general/views/test_general_contact_view.py
@@ -0,0 +1,14 @@
+from tests.BaseTestWithDB import BaseTestWithDB
+from django.urls import reverse
+
+
+class ContactViewTest(BaseTestWithDB):
+
+    def __init__(self, *args, **kwargs):
+        super().__init__(*args, **kwargs)
+        self.language = 'en'
+
+    def test_contact_view(self):
+        response = self.client.get(reverse('general:contact'))
+        self.assertEqual(response.status_code, 200)
+        self.assertContains(response, "Contact Us")
diff --git a/csunplugged/tests/general/views/test_general_index_view.py b/csunplugged/tests/general/views/test_general_index_view.py
--- a/csunplugged/tests/general/views/test_general_index_view.py
+++ b/csunplugged/tests/general/views/test_general_index_view.py
@@ -0,0 +1,14 @@
+from tests.BaseTestWithDB import BaseTestWithDB
+from django.urls import reverse
+
+
+class IndexViewTest(BaseTestWithDB):
+
+    def __init__(self, *args, **kwargs):
+        super().__init__(*args, **kwargs)
+        self.language = 'en'
+
+    def test_index_view(self):
+        response = self.client.get(reverse('general:home'))
+        self.assertEqual(response.status_code, 200)
+        self.assertContains(response, "Computer Science")
diff --git a/csunplugged/tests/general/views/test_general_people_view.py b/csunplugged/tests/general/views/test_general_people_view.py
new file mode 100644
--- /dev/null
+++ b/csunplugged/tests/general/views/test_general_people_view.py
@@ -0,0 +1,14 @@
+from tests.BaseTestWithDB import BaseTestWithDB
+from django.urls import reverse
+
+
+class PeopleViewTest(BaseTestWithDB):
+
+    def __init__(self, *args, **kwargs):
+        super().__init__(*args, **kwargs)
+        self.language = 'en'
+
+    def test_people_view(self):
+        response = self.client.get(reverse('general:people'))
+        self.assertEqual(response.status_code, 200)
+        self.assertContains(response, "People")
diff --git a/csunplugged/tests/general/views/test_general_principles_view.py b/csunplugged/tests/general/views/test_general_principles_view.py
new file mode 100644
--- /dev/null
+++ b/csunplugged/tests/general/views/test_general_principles_view.py
@@ -0,0 +1,14 @@
+from tests.BaseTestWithDB import BaseTestWithDB
+from django.urls import reverse
+
+
+class PrinciplesViewTest(BaseTestWithDB):
+
+    def __init__(self, *args, **kwargs):
+        super().__init__(*args, **kwargs)
+        self.language = 'en'
+
+    def test_principles_view(self):
+        response = self.client.get(reverse('general:principles'))
+        self.assertEqual(response.status_code, 200)
+        self.assertContains(response, "Principles")