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

Commit

Permalink
assign unsaved objects when django < 1.8
Browse files Browse the repository at this point in the history
  • Loading branch information
vandersonmota committed May 1, 2015
1 parent a4f3cdd commit 08513f5
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 8 deletions.
8 changes: 6 additions & 2 deletions model_mommy/mommy.py
Original file line number Diff line number Diff line change
Expand Up @@ -267,8 +267,12 @@ 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] = make
self.type_mapping[OneToOneField] = make
if django.VERSION >= (1, 8):
self.type_mapping[ForeignKey] = make
self.type_mapping[OneToOneField] = make
else:
self.type_mapping[ForeignKey] = prepare
self.type_mapping[OneToOneField] = prepare
return self._make(commit=False, **attrs)

def get_fields(self):
Expand Down
17 changes: 12 additions & 5 deletions test/generic/tests/test_mommy.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# -*- coding:utf-8 -*-
from decimal import Decimal

import django
from decimal import Decimal
from django.test import TestCase
from django import VERSION
from django.db.models.options import Options
Expand Down Expand Up @@ -187,20 +188,26 @@ def test_attrs_on_related_model_through_parent(self):
for person in Person.objects.all():
self.assertEqual(person.name, 'john')

def test_prepare_should_not_create_one_object(self):
def test_prepare_fk(self):
dog = mommy.prepare(Dog)
self.assertIsInstance(dog, Dog)
self.assertIsInstance(dog.owner, Person)

self.assertEqual(Person.objects.all().count(), 1)
if django.VERSION >= (1, 8):
self.assertEqual(Person.objects.all().count(), 1)
else:
self.assertEqual(Person.objects.all().count(), 0)
self.assertEqual(Dog.objects.all().count(), 0)

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

self.assertEqual(LonelyPerson.objects.all().count(), 0)
self.assertTrue(isinstance(lonely_person.only_friend, Person))
self.assertEqual(Person.objects.all().count(), 1)
if django.VERSION >= (1, 8):
self.assertEqual(Person.objects.all().count(), 1)
else:
self.assertEqual(Person.objects.all().count(), 0)

def test_create_one_to_one(self):
lonely_person = mommy.make(LonelyPerson)
Expand Down
6 changes: 5 additions & 1 deletion test/generic/tests/test_recipes.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#coding: utf-8

import itertools
import django
from random import choice
from mock import patch
from decimal import Decimal
Expand Down Expand Up @@ -363,7 +364,10 @@ 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(), 2)
if django.VERSION >= (1, 8):
self.assertEqual(Person.objects.count(), 2)
else:
self.assertEqual(Person.objects.count(), 1)
self.assertEqual(dog.owner.name, 'Zezin')

def test_related_models_recipes(self):
Expand Down

0 comments on commit 08513f5

Please sign in to comment.