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

Commit

Permalink
django 1.8 support
Browse files Browse the repository at this point in the history
  • Loading branch information
vandersonmota committed Apr 7, 2015
1 parent 7cc0efe commit 095bf5c
Show file tree
Hide file tree
Showing 8 changed files with 30 additions and 15 deletions.
5 changes: 5 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ env:
- DJANGO_VERSION=1.5.5
- DJANGO_VERSION=1.6
- DJANGO_VERSION=1.7
- DJANGO_VERSION=1.8
matrix:
include:
- python: 2.6
Expand All @@ -21,12 +22,16 @@ matrix:
env: DJANGO_VERSION=1.6
- python: 3.2
env: DJANGO_VERSION=1.7
- python: 3.2
env: DJANGO_VERSION=1.8
- python: 3.4
env: DJANGO_VERSION=1.5.5
- python: 3.4
env: DJANGO_VERSION=1.6
- python: 3.4
env: DJANGO_VERSION=1.7
- python: 3.4
env: DJANGO_VERSION=1.8
before_install:
- sudo apt-get update && sudo apt-get build-dep python-imaging
install:
Expand Down
11 changes: 8 additions & 3 deletions model_mommy/mommy.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,12 @@ def validate_ipv6_address(v):


#TODO: improve related models handling
foreign_key_required = [lambda field: ('model', field.related.parent_model)]
def _fk_model(field):
try:
return ('model', field.related.parent_model)
except AttributeError:
return ('model', field.related_model)
foreign_key_required = [_fk_model]

MAX_MANY_QUANTITY = 5

Expand Down Expand Up @@ -259,8 +264,8 @@ def make(self, **attrs):
def prepare(self, **attrs):
'''Creates, but does not persist, an instance of the model
associated with Mommy instance.'''
self.type_mapping[ForeignKey] = prepare
self.type_mapping[OneToOneField] = prepare
self.type_mapping[ForeignKey] = make

This comment has been minimized.

Copy link
@mdentremont

mdentremont Apr 29, 2015

Contributor

This caused some breakages on our existing unit tests.

As this is a method that claims not to persist, it seems wrong to me that it would go ahead and persist an instance of another model.

Could this be reverted to use prepare by default, but an argument could be specific in order to switch it over to make?

This comment has been minimized.

Copy link
@vandersonmota

vandersonmota Apr 30, 2015

Author Collaborator

It doesn't persist the model being created. But, i'll add a change do keep the previous behavior in django versions under 1.8. Unfortunately Django 1.8 requires the other models to be persisted in order to be assigned. ¯_(ツ)_/¯

This comment has been minimized.

Copy link
@vandersonmota

vandersonmota May 1, 2015

Author Collaborator
self.type_mapping[OneToOneField] = make
return self._make(commit=False, **attrs)

def get_fields(self):
Expand Down
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
Django<1.8
Django<1.9
six
10 changes: 7 additions & 3 deletions runtests.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,10 +66,14 @@ def get_runner(settings):


def runtests(options=None, labels=None):
if not labels:
labels = ['generic']

settings = configure_settings(options)
if django.VERSION >= (1, 8):
settings.TEST_RUNNER='django.test.runner.DiscoverRunner'
if not labels:
labels = ['test.generic']
else:
if not labels:
labels = ['generic']
runner = get_runner(settings)
if django.VERSION >= (1, 7):
django.setup()
Expand Down
4 changes: 3 additions & 1 deletion test/generic/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,12 @@

class DummyIPAddressFieldForm(ModelForm):
class Meta:
fields = ('ipv4_field',)
model = DummyIPAddressFieldModel
else:
from test.generic.models import DummyGenericIPAddressFieldModel

class DummyGenericIPAddressFieldForm(ModelForm):
class Meta:
model = DummyGenericIPAddressFieldModel
fields = ('ipv4_field', 'ipv6_field', 'ipv46_field')
model = DummyGenericIPAddressFieldModel
8 changes: 3 additions & 5 deletions test/generic/tests/test_mommy.py
Original file line number Diff line number Diff line change
Expand Up @@ -192,17 +192,15 @@ def test_prepare_should_not_create_one_object(self):
self.assertIsInstance(dog, Dog)
self.assertIsInstance(dog.owner, Person)

# makes sure database is clean
self.assertEqual(Person.objects.all().count(), 0)
self.assertEqual(Person.objects.all().count(), 1)
self.assertEqual(Dog.objects.all().count(), 0)

def test_prepare_one_to_one_should_not_persist_one_object(self):
lonely_person = mommy.prepare(LonelyPerson)

# makes sure database is clean
self.assertEqual(LonelyPerson.objects.all().count(), 0)
self.assertTrue(isinstance(lonely_person.only_friend, Person))
self.assertEqual(Person.objects.all().count(), 0)
self.assertEqual(Person.objects.all().count(), 1)

def test_create_one_to_one(self):
lonely_person = mommy.make(LonelyPerson)
Expand Down Expand Up @@ -318,7 +316,7 @@ def test_field_lookup_for_one_to_one_relationship(self):

def test_allow_create_fkey_related_model(self):
try:
person = mommy.make(Person, dog_set=[mommy.prepare(Dog), mommy.prepare(Dog)])
person = mommy.make(Person, dog_set=[mommy.make(Dog), mommy.make(Dog)])
except TypeError:
self.fail('type error raised')

Expand Down
2 changes: 1 addition & 1 deletion test/generic/tests/test_recipes.py
Original file line number Diff line number Diff line change
Expand Up @@ -363,7 +363,7 @@ def test_do_query_lookup_empty_recipes(self):
self.assertEqual(dog.owner.name, 'James')

dog = dog_recipe.prepare(owner__name='Zezin')
self.assertEqual(Person.objects.count(), 1)
self.assertEqual(Person.objects.count(), 2)
self.assertEqual(dog.owner.name, 'Zezin')

def test_related_models_recipes(self):
Expand Down
3 changes: 2 additions & 1 deletion tox.ini
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[tox]
envlist = {py27}-django{14,15,16,17}, {py26}-django{14,15,16}, {py34,py32}-django{15,16,17}
envlist = {py27}-django{14,15,16,17,18}, {py26}-django{14,15,16}, {py34,py32}-django{15,16,17,18}

[testenv]
basepython =
Expand All @@ -15,6 +15,7 @@ deps =
django15: Django>=1.5,<1.6
django16: Django>=1.6,<1.7
django17: Django>=1.7,<1.8
django18: Django>=1.8,<1.9
commands=
./runtests.py
./runtests.py --use-tz

0 comments on commit 095bf5c

Please sign in to comment.