File: test_uuid.py

package info (click to toggle)
python-django 3%3A6.0~beta1-1
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 62,252 kB
  • sloc: python: 371,056; javascript: 19,376; xml: 211; makefile: 187; sh: 28
file content (44 lines) | stat: -rw-r--r-- 1,505 bytes parent folder | download
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
36
37
38
39
40
41
42
43
44
from django import forms
from django.core.exceptions import ValidationError
from django.test import TestCase

from .models import UUIDPK


class UUIDPKForm(forms.ModelForm):
    class Meta:
        model = UUIDPK
        fields = "__all__"


class ModelFormBaseTest(TestCase):
    def test_create_save_error(self):
        form = UUIDPKForm({})
        self.assertFalse(form.is_valid())
        msg = "The UUIDPK could not be created because the data didn't validate."
        with self.assertRaisesMessage(ValueError, msg):
            form.save()

    def test_update_save_error(self):
        obj = UUIDPK.objects.create(name="foo")
        form = UUIDPKForm({}, instance=obj)
        self.assertFalse(form.is_valid())
        msg = "The UUIDPK could not be changed because the data didn't validate."
        with self.assertRaisesMessage(ValueError, msg):
            form.save()

    def test_model_multiple_choice_field_uuid_pk(self):
        f = forms.ModelMultipleChoiceField(UUIDPK.objects.all())
        with self.assertRaisesMessage(
            ValidationError, "“invalid_uuid” is not a valid value."
        ):
            f.clean(["invalid_uuid"])

    def test_model_choice_invalid_pk_value_error_messages(self):
        f = forms.ModelChoiceField(UUIDPK.objects.all())
        with self.assertRaisesMessage(
            ValidationError,
            "['Select a valid choice. "
            "That choice is not one of the available choices.']",
        ):
            f.clean("invalid")