File: test_fields.py

package info (click to toggle)
python-django 3%3A3.2.19-1%2Bdeb12u2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm-proposed-updates
  • size: 56,696 kB
  • sloc: python: 264,418; javascript: 18,362; xml: 193; makefile: 178; sh: 43
file content (35 lines) | stat: -rw-r--r-- 1,349 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
import json

from django.contrib.contenttypes.fields import GenericForeignKey
from django.db import models
from django.test import SimpleTestCase, TestCase
from django.test.utils import isolate_apps

from .models import Answer, Question


@isolate_apps('contenttypes_tests')
class GenericForeignKeyTests(SimpleTestCase):

    def test_str(self):
        class Model(models.Model):
            field = GenericForeignKey()
        self.assertEqual(str(Model.field), 'contenttypes_tests.Model.field')

    def test_get_content_type_no_arguments(self):
        with self.assertRaisesMessage(Exception, 'Impossible arguments to GFK.get_content_type!'):
            Answer.question.get_content_type()

    def test_incorrect_get_prefetch_queryset_arguments(self):
        with self.assertRaisesMessage(ValueError, "Custom queryset can't be used for this lookup."):
            Answer.question.get_prefetch_queryset(Answer.objects.all(), Answer.objects.all())


class GenericRelationTests(TestCase):

    def test_value_to_string(self):
        question = Question.objects.create(text='test')
        answer1 = Answer.objects.create(question=question)
        answer2 = Answer.objects.create(question=question)
        result = json.loads(Question.answer_set.field.value_to_string(question))
        self.assertCountEqual(result, [answer1.pk, answer2.pk])