Skip to content

Commit

Permalink
Add tests for custom tag is_wagtail_version_gte
Browse files Browse the repository at this point in the history
  • Loading branch information
katdom13 committed May 28, 2024
1 parent 3026425 commit ee70c7b
Showing 1 changed file with 72 additions and 0 deletions.
72 changes: 72 additions & 0 deletions tests/test_tags.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
import unittest

from unittest.mock import patch

from django import forms
from django.http import HttpResponse
from django.template import Context, Template
from django.test import TestCase, override_settings
from django.urls import path
from django.views import View
from wagtail import VERSION as WAGTAIL_VERSION


class DummyForm(forms.Form):
media_file = forms.FileField(label="Change media file:", required=False)


class DummyFileFieldView(View):
template_string = """
{% load media_tags %}
{% is_wagtail_version_gte 6 0 as wagtail60 %} # Update version as needed
{% if wagtail60 %}
{% include "wagtailmedia/media/_file_field.html" %}
{% else %}
{% include "wagtailmedia/media/_file_field_legacy.html" %}
{% endif %}
"""

def get(self, request, *args, **kwargs):
template = Template(self.template_string)
form = DummyForm() # Create an instance of the form
context_data = self.get_context_data(form=form, **kwargs)
return HttpResponse(template.render(Context(context_data)))

def get_context_data(self, **kwargs):
# Mock 'media' and 'field' as they are used in the templates
return {
"media": {
"url": "http://example.com/media/file.mp3",
"filename": "file.mp3",
},
"field": kwargs["form"]["media_file"], # Passing the actual form field
**kwargs,
}


urlpatterns = [
# Temporary URL for testing
path(
"test-filefield-template/",
DummyFileFieldView.as_view(),
name="test-filefield-template",
),
]


@override_settings(ROOT_URLCONF=__name__) # Override ROOT_URLCONF during this test
class WagtailVersionFileFieldTemplateTests(TestCase):
@patch(
"wagtailmedia.templatetags.media_tags.WAGTAIL_VERSION", new=(5, 2)
) # Version for legacy template
def test_legacy_template_loaded(self):
response = self.client.get("/test-filefield-template/")
self.assertTemplateUsed(response, "wagtailmedia/media/_file_field_legacy.html")

@unittest.skipIf(WAGTAIL_VERSION < (6.0), "Requires Wagtail 6.0 or higher")
@patch(
"wagtailmedia.templatetags.media_tags.WAGTAIL_VERSION", new=(6, 0)
) # Version for new template
def test_new_template_loaded(self):
response = self.client.get("/test-filefield-template/")
self.assertTemplateUsed(response, "wagtailmedia/media/_file_field.html")

0 comments on commit ee70c7b

Please sign in to comment.