django-ipyfield / ipyfield /

from django.test import TestCase
from django.db.models import Model
from django.db import IntegrityError
from ipyfield import IPyField
from IPy import IP

class DummyModel(Model):
    field = IPyField()
    null_field = IPyField(null=True)

    def __str__(self):
        return '<DummyModel %s>' %

    __repr__ = __str__

class IPyFieldTests(TestCase):

    def setUp(self):
        self.obj = DummyModel()

    def test_empty(self):
        self.assertEqual(None, self.obj.field)

    def test_iptype(self):
        self.obj.field = ''
        self.obj.field = ''
        self.assertEqual(self.obj.field.iptype(), 'PUBLIC')

    def test_lookups_in_CIDR(self):
        ip_block = IP('')
        [DummyModel.objects.create(field=ip) for ip in ip_block]
        self.assertEqual(16, len(ip_block), DummyModel.objects.count())
        self.assertTrue(DummyModel.objects.count() > 
        self.assertEqual(4, DummyModel.objects.filter(
        # testing lookups work with str as __in param rather than 
        # requiring an IP instance
               [repr(o) for o in DummyModel.objects.filter(

    def test_null_values(self):
        with self.assertRaises(IntegrityError):
            # non-null field should require value
        # null field is fine unspecified

    def test_ipv6_support(self):
        obj = DummyModel.objects.create(field='2001:dead:beef::1')
        self.assertEqual(obj.field.version(), 6)
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.