Skip to content
This repository has been archived by the owner on Oct 22, 2019. It is now read-only.

Commit

Permalink
Fix generating models which have a 'next' method, but aren't iterators
Browse files Browse the repository at this point in the history
  • Loading branch information
Denis Cornehl committed Jun 9, 2015
1 parent a1d86ff commit f2e9568
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 4 deletions.
9 changes: 5 additions & 4 deletions model_mommy/mommy.py
Expand Up @@ -241,10 +241,11 @@ def _populate(self):


def is_iterator(value):
if PY3:
return hasattr(value, '__next__')
else:
return hasattr(value, 'next')
try:
iter(value)
return not isinstance(value, string_types)
except TypeError:
return False


class Mommy(object):
Expand Down
11 changes: 11 additions & 0 deletions test/generic/models.py
Expand Up @@ -235,6 +235,17 @@ class DummyUniqueIntegerFieldModel(models.Model):
value = models.IntegerField(unique=True)


class ModelWithNext(models.Model):
attr = models.CharField(max_length=10)

def next(self):
return 'foo'


class BaseModelForNext(models.Model):
fk = models.ForeignKey(ModelWithNext)


if VERSION < (1, 4):
class DummyIPAddressFieldModel(models.Model):
ipv4_field = models.IPAddressField() # Deprecated in Django 1.7
Expand Down
14 changes: 14 additions & 0 deletions test/generic/tests/test_mommy.py
Expand Up @@ -19,6 +19,7 @@
from test.generic.models import DummyDefaultFieldsModel, DummyMultipleInheritanceModel
from test.generic.models import DummyGenericForeignKeyModel, NonAbstractPerson
from test.generic.models import DummyEmptyModel
from test.generic.models import ModelWithNext, BaseModelForNext


class ModelFinderTest(TestCase):
Expand Down Expand Up @@ -429,6 +430,19 @@ def test_skip_fields_with_default(self):
self.assertEqual(dummy.default_slug_field, 'a-slug')


class MommyHandlesModelWithNext(TestCase):
def test_creates_instance_for_model_with_next(self):
instance = mommy.make(
BaseModelForNext,
fk=mommy.make(ModelWithNext),
)

self.assertTrue(instance.id)
self.assertTrue(instance.fk.id)
self.assertTrue(instance.fk.attr)
self.assertEqual('foo', instance.fk.next())


if VERSION < (1, 4):
from test.generic.forms import DummyIPAddressFieldForm

Expand Down

0 comments on commit f2e9568

Please sign in to comment.