Debian Codegraph @

tests/model_fields/test_genericipaddressfield.py

raw
1 from django.core.exceptions import ValidationError
2 from django.db import models
3 from django.test import TestCase
4
5 from .models import GenericIPAddress
6
7
8 class GenericIPAddressFieldTests(TestCase):
9 def test_genericipaddressfield_formfield_protocol(self):
10 """
11 GenericIPAddressField with a specified protocol does not generate a
12 formfield without a protocol.
13 """
14 model_field = models.GenericIPAddressField(protocol="IPv4")
15 form_field = model_field.formfield()
16 with self.assertRaises(ValidationError):
17 form_field.clean("::1")
18 model_field = models.GenericIPAddressField(protocol="IPv6")
19 form_field = model_field.formfield()
20 with self.assertRaises(ValidationError):
21 form_field.clean("127.0.0.1")
22
23 def test_null_value(self):
24 """
25 Null values should be resolved to None.
26 """
27 GenericIPAddress.objects.create()
28 o = GenericIPAddress.objects.get()
29 self.assertIsNone(o.ip)
30
31 def test_blank_string_saved_as_null(self):
32 o = GenericIPAddress.objects.create(ip="")
33 o.refresh_from_db()
34 self.assertIsNone(o.ip)
35 GenericIPAddress.objects.update(ip="")
36 o.refresh_from_db()
37 self.assertIsNone(o.ip)
38
39 def test_save_load(self):
40 instance = GenericIPAddress.objects.create(ip="::1")
41 loaded = GenericIPAddress.objects.get()
42 self.assertEqual(loaded.ip, instance.ip)