Permalink
Browse files

Merge pull request #140 from amatellanes/master

Added gen_ipv4() and gen_ipv6() methods
  • Loading branch information...
2 parents 1b40bc9 + 9c908ad commit d5de4410ba08722880d9ae04725f47eae2b09713 @vandersonmota committed Jan 25, 2014
Showing with 62 additions and 0 deletions.
  1. +8 −0 model_mommy/generators.py
  2. +16 −0 test/generic/forms.py
  3. +10 −0 test/generic/models.py
  4. +28 −0 test/generic/tests/test_mommy.py
@@ -115,6 +115,14 @@ def gen_url():
def gen_email():
return u"%s@example.com" % gen_string(10)
+
+def gen_ipv6():
+ return ":".join(format(randint(1, 65535), 'x') for i in range(8))
+
+
+def gen_ipv4():
+ return ".".join(str(randint(1, 255)) for i in range(4))
+
def gen_content_type():
return ContentType.objects.get_for_model(choice(get_models()))
View
@@ -0,0 +1,16 @@
+from django import VERSION
+from django.forms import ModelForm
+
+
+if VERSION < (1, 4):
+ from test.generic.models import DummyIPAddressFieldModel
+
+ class DummyIPAddressFieldForm(ModelForm):
+ class Meta:
+ model = DummyIPAddressFieldModel
+else:
+ from test.generic.models import DummyGenericIPAddressFieldModel
+
+ class DummyGenericIPAddressFieldForm(ModelForm):
+ class Meta:
+ model = DummyGenericIPAddressFieldModel
View
@@ -7,6 +7,7 @@
from decimal import Decimal
from tempfile import gettempdir
+from django import VERSION
from django.db import models
from django.core.files.storage import FileSystemStorage
@@ -199,3 +200,12 @@ class CustomFieldWithoutGeneratorModel(models.Model):
class DummyUniqueIntegerFieldModel(models.Model):
value = models.IntegerField(unique=True)
+
+
+if VERSION < (1, 4):
+ class DummyIPAddressFieldModel(models.Model):
+ ip = models.IPAddressField() # Deprecated in Django 1.7
+else:
+ class DummyGenericIPAddressFieldModel(models.Model):
+ ip = models.GenericIPAddressField() # New in Django 1.4
+
@@ -2,12 +2,14 @@
from decimal import Decimal
from django.test import TestCase
+from django import VERSION
from django.db.models.options import Options
from django.db.models import Manager
from mock import patch, Mock
from model_mommy import mommy
+from model_mommy import generators
from model_mommy.exceptions import ModelNotFound, AmbiguousModelName, InvalidQuantityException
from model_mommy.timezone import smart_datetime as datetime
from test.generic.models import Person, Dog, Store, LonelyPerson, School, SchoolEnrollment, ModelWithImpostorField, Classroom, GuardDog
@@ -340,3 +342,29 @@ def test_skip_fields_with_default(self):
self.assertEqual(dummy.default_decimal_field, Decimal('0'))
self.assertEqual(dummy.default_email_field, 'foo@bar.org')
self.assertEqual(dummy.default_slug_field, 'a-slug')
+
+
+if VERSION < (1, 4):
+ from test.generic.forms import DummyIPAddressFieldForm
+
+ class MommyGeneratesIPAdresses(TestCase):
+ def test_create_model_with_valid_ipv4(self):
+ form_data = {
+ 'ip': generators.gen_ipv4(),
+ }
+ self.assertTrue(DummyIPAddressFieldForm(form_data).is_valid())
+else:
+ from test.generic.forms import DummyGenericIPAddressFieldForm
+
+ class MommyGeneratesIPAdresses(TestCase):
+ def test_create_model_with_valid_ipv4(self):
+ form_data = {
+ 'ip': generators.gen_ipv4(),
+ }
+ self.assertTrue(DummyGenericIPAddressFieldForm(form_data).is_valid())
+
+ def test_create_model_with_valid_ipv6(self):
+ form_data = {
+ 'ip': generators.gen_ipv6(),
+ }
+ self.assertTrue(DummyGenericIPAddressFieldForm(form_data).is_valid())

0 comments on commit d5de441

Please sign in to comment.