mirror of
https://github.com/django/django.git
synced 2024-11-25 16:09:27 +01:00
36 lines
1.2 KiB
Python
36 lines
1.2 KiB
Python
|
from django.core.exceptions import ValidationError
|
||
|
from django.db import models
|
||
|
from django.test import TestCase
|
||
|
|
||
|
from .models import GenericIPAddress
|
||
|
|
||
|
|
||
|
class GenericIPAddressFieldTests(TestCase):
|
||
|
|
||
|
def test_genericipaddressfield_formfield_protocol(self):
|
||
|
"""
|
||
|
GenericIPAddressField with a specified protocol does not generate a
|
||
|
formfield without a protocol.
|
||
|
"""
|
||
|
model_field = models.GenericIPAddressField(protocol='IPv4')
|
||
|
form_field = model_field.formfield()
|
||
|
with self.assertRaises(ValidationError):
|
||
|
form_field.clean('::1')
|
||
|
model_field = models.GenericIPAddressField(protocol='IPv6')
|
||
|
form_field = model_field.formfield()
|
||
|
with self.assertRaises(ValidationError):
|
||
|
form_field.clean('127.0.0.1')
|
||
|
|
||
|
def test_null_value(self):
|
||
|
"""
|
||
|
Null values should be resolved to None.
|
||
|
"""
|
||
|
GenericIPAddress.objects.create()
|
||
|
o = GenericIPAddress.objects.get()
|
||
|
self.assertIsNone(o.ip)
|
||
|
|
||
|
def test_save_load(self):
|
||
|
instance = GenericIPAddress.objects.create(ip='::1')
|
||
|
loaded = GenericIPAddress.objects.get()
|
||
|
self.assertEqual(loaded.ip, instance.ip)
|