django-rest-framework/tests/test_lazy_hyperlinks.py
Adam Johnson 410575dace
Replace all url() calls with path() or re_path() (#7512)
* url() is deprecated in Django 3.1

* update given feedbacks on url() is deprecated in Django 3.1

* Fix test_urlpatterns.py to continue testing mixed re_path() and path()

* Fix one missed reference

Co-authored-by: sanjusci <sanju.sci9@gmail.com>
2020-09-08 15:32:27 +01:00

50 lines
1.3 KiB
Python

from django.db import models
from django.test import TestCase, override_settings
from django.urls import path
from rest_framework import serializers
from rest_framework.renderers import JSONRenderer
from rest_framework.templatetags.rest_framework import format_value
str_called = False
class Example(models.Model):
text = models.CharField(max_length=100)
def __str__(self):
global str_called
str_called = True
return 'An example'
class ExampleSerializer(serializers.HyperlinkedModelSerializer):
class Meta:
model = Example
fields = ('url', 'id', 'text')
def dummy_view(request):
pass
urlpatterns = [
path('example/<int:pk>/', dummy_view, name='example-detail'),
]
@override_settings(ROOT_URLCONF='tests.test_lazy_hyperlinks')
class TestLazyHyperlinkNames(TestCase):
def setUp(self):
self.example = Example.objects.create(text='foo')
def test_lazy_hyperlink_names(self):
global str_called
context = {'request': None}
serializer = ExampleSerializer(self.example, context=context)
JSONRenderer().render(serializer.data)
assert not str_called
hyperlink_string = format_value(serializer.data['url'])
assert hyperlink_string == '<a href=/example/1/>An example</a>'
assert str_called