django-model-utils/tests/test_fields/test_split_field.py
Maarten ter Huurne aeeb69a9dd Enable postponed evaluation of annotations for all test modules
This allows using the latest annotation syntax supported by the type
checker regardless of the runtime Python version.
2024-06-13 12:02:05 +02:00

73 lines
2.3 KiB
Python

from __future__ import annotations
from django.test import TestCase
from tests.models import Article, SplitFieldAbstractParent
class SplitFieldTests(TestCase):
full_text = 'summary\n\n<!-- split -->\n\nmore'
excerpt = 'summary\n'
def setUp(self):
self.post = Article.objects.create(
title='example post', body=self.full_text)
def test_unicode_content(self):
self.assertEqual(str(self.post.body), self.full_text)
def test_excerpt(self):
self.assertEqual(self.post.body.excerpt, self.excerpt)
def test_content(self):
self.assertEqual(self.post.body.content, self.full_text)
def test_has_more(self):
self.assertTrue(self.post.body.has_more)
def test_not_has_more(self):
post = Article.objects.create(title='example 2',
body='some text\n\nsome more\n')
self.assertFalse(post.body.has_more)
def test_load_back(self):
post = Article.objects.get(pk=self.post.pk)
self.assertEqual(post.body.content, self.post.body.content)
self.assertEqual(post.body.excerpt, self.post.body.excerpt)
def test_assign_to_body(self):
new_text = 'different\n\n<!-- split -->\n\nother'
self.post.body = new_text
self.post.save()
self.assertEqual(str(self.post.body), new_text)
def test_assign_to_content(self):
new_text = 'different\n\n<!-- split -->\n\nother'
self.post.body.content = new_text
self.post.save()
self.assertEqual(str(self.post.body), new_text)
def test_assign_to_excerpt(self):
with self.assertRaises(AttributeError):
self.post.body.excerpt = 'this should fail'
def test_access_via_class(self):
with self.assertRaises(AttributeError):
Article.body
def test_assign_splittext(self):
a = Article(title='Some Title')
a.body = self.post.body
self.assertEqual(a.body.excerpt, 'summary\n')
def test_value_to_string(self):
f = self.post._meta.get_field('body')
self.assertEqual(f.value_to_string(self.post), self.full_text)
def test_abstract_inheritance(self):
class Child(SplitFieldAbstractParent):
pass
self.assertEqual(
[f.name for f in Child._meta.fields],
["id", "content", "_content_excerpt"])