2015-08-06 21:43:55 +00:00
|
|
|
from django import forms
|
2016-10-31 19:21:05 +00:00
|
|
|
from django.core.exceptions import ValidationError
|
2015-08-06 21:43:55 +00:00
|
|
|
from django.test import TestCase
|
|
|
|
|
|
|
|
from .models import UUIDPK
|
|
|
|
|
|
|
|
|
|
|
|
class UUIDPKForm(forms.ModelForm):
|
|
|
|
class Meta:
|
|
|
|
model = UUIDPK
|
2022-02-03 19:24:19 +00:00
|
|
|
fields = "__all__"
|
2015-08-06 21:43:55 +00:00
|
|
|
|
|
|
|
|
|
|
|
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):
|
2022-02-03 19:24:19 +00:00
|
|
|
obj = UUIDPK.objects.create(name="foo")
|
2015-08-06 21:43:55 +00:00
|
|
|
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()
|
2016-10-31 19:21:05 +00:00
|
|
|
|
|
|
|
def test_model_multiple_choice_field_uuid_pk(self):
|
|
|
|
f = forms.ModelMultipleChoiceField(UUIDPK.objects.all())
|
2022-02-03 19:24:19 +00:00
|
|
|
with self.assertRaisesMessage(
|
|
|
|
ValidationError, "“invalid_uuid” is not a valid UUID."
|
|
|
|
):
|
|
|
|
f.clean(["invalid_uuid"])
|