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

Commit

Permalink
Merge pull request #221 from romulocollopy/master
Browse files Browse the repository at this point in the history
Issue #216
  • Loading branch information
vandersonmota committed Apr 16, 2015
2 parents 7bccad4 + 21b9cfe commit 79f4299
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 1 deletion.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,5 @@ share/
.idea
.tox
build/
lib64
pyvenv.cfg
2 changes: 1 addition & 1 deletion model_mommy/mommy.py
Original file line number Diff line number Diff line change
Expand Up @@ -326,7 +326,7 @@ def _make(self, commit=True, **attrs):
def m2m_value(self, field):
if field.name in self.rel_fields:
return self.generate_value(field)
if not self.make_m2m or field.null:
if not self.make_m2m or field.null and not field.fill_optional:
return []
return self.generate_value(field)

Expand Down
16 changes: 16 additions & 0 deletions test/generic/tests/test_mommy.py
Original file line number Diff line number Diff line change
Expand Up @@ -350,6 +350,22 @@ def test_skip_null(self):
self.assertEqual(dummy.null_integer_field, None)


class FillNullsTestCase(TestCase):
def test_create_nullable_many_to_many_if_flagged_and_fill_field_optional(
self):
classroom = mommy.make(Classroom, make_m2m=True, _fill_optional=[
'students'])
self.assertEqual(classroom.students.count(), 5)

def test_create_nullable_many_to_many_if_flagged_and_fill_optional(self):
classroom = mommy.make(Classroom, make_m2m=True, _fill_optional=True)
self.assertEqual(classroom.students.count(), 5)

def test_nullable_many_to_many_is_not_created_if_not_flagged_and_fill_optional(self):
classroom = mommy.make(Classroom, make_m2m=False, _fill_optional=True)
self.assertEqual(classroom.students.count(), 0)


class SkipBlanksTestCase(TestCase):
def test_skip_blank(self):
dummy = mommy.make(DummyBlankFieldsModel)
Expand Down

0 comments on commit 79f4299

Please sign in to comment.