diff options
-rw-r--r-- | docs/changelog.rst | 8 | ||||
-rw-r--r-- | factory/__init__.py | 2 | ||||
-rw-r--r-- | factory/base.py | 11 | ||||
-rw-r--r-- | tests/test_using.py | 33 |
4 files changed, 48 insertions, 6 deletions
diff --git a/docs/changelog.rst b/docs/changelog.rst index d6d4bd8..76e2d43 100644 --- a/docs/changelog.rst +++ b/docs/changelog.rst @@ -1,6 +1,14 @@ ChangeLog ========= +2.0.1 (2013-04-16) +------------------ + +*New:* + + - Don't push ``defaults`` to ``get_or_create`` when + :attr:`~factory.DjangoModelFactory.FACTORY_DJANGO_GET_OR_CREATE` is not set. + 2.0.0 (2013-04-15) ------------------ diff --git a/factory/__init__.py b/factory/__init__.py index ef5d40e..939500c 100644 --- a/factory/__init__.py +++ b/factory/__init__.py @@ -20,7 +20,7 @@ # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN # THE SOFTWARE. -__version__ = '2.0.0' +__version__ = '2.0.1' __author__ = 'Raphaƫl Barrois <raphael.barrois+fboy@polytechnique.org>' from .base import ( diff --git a/factory/base.py b/factory/base.py index 928ea7a..0d03838 100644 --- a/factory/base.py +++ b/factory/base.py @@ -596,10 +596,13 @@ class DjangoModelFactory(Factory): "(in %s.FACTORY_DJANGO_GET_OR_CREATE=%r)" % (cls, cls.FACTORY_DJANGO_GET_OR_CREATE)) - key_fields = {} - for field in cls.FACTORY_DJANGO_GET_OR_CREATE: - key_fields[field] = kwargs.pop(field) - key_fields['defaults'] = kwargs + if cls.FACTORY_DJANGO_GET_OR_CREATE: + key_fields = {} + for field in cls.FACTORY_DJANGO_GET_OR_CREATE: + key_fields[field] = kwargs.pop(field) + key_fields['defaults'] = kwargs + else: + key_fields = kwargs obj, _created = manager.get_or_create(*args, **key_fields) return obj diff --git a/tests/test_using.py b/tests/test_using.py index 9e9e6aa..d366c8c 100644 --- a/tests/test_using.py +++ b/tests/test_using.py @@ -50,9 +50,11 @@ class FakeModel(object): class FakeModelManager(object): def get_or_create(self, **kwargs): - kwargs.update(kwargs.pop('defaults', {})) + defaults = kwargs.pop('defaults', {}) + kwargs.update(defaults) instance = FakeModel.create(**kwargs) instance.id = 2 + instance._defaults = defaults return instance, True def values_list(self, *args, **kwargs): @@ -1778,6 +1780,35 @@ class DjangoModelFactoryTestCase(unittest.TestCase): self.assertEqual('foo_4', o3.a) self.assertEqual('foo_5', o4.a) + def test_no_get_or_create(self): + class TestModelFactory(factory.DjangoModelFactory): + FACTORY_FOR = TestModel + + a = factory.Sequence(lambda n: 'foo_%s' % n) + + o = TestModelFactory() + self.assertEqual({}, o._defaults) + self.assertEqual('foo_2', o.a) + self.assertEqual(2, o.id) + + def test_get_or_create(self): + class TestModelFactory(factory.DjangoModelFactory): + FACTORY_FOR = TestModel + FACTORY_DJANGO_GET_OR_CREATE = ('a', 'b') + + a = factory.Sequence(lambda n: 'foo_%s' % n) + b = 2 + c = 3 + d = 4 + + o = TestModelFactory() + self.assertEqual({'c': 3, 'd': 4}, o._defaults) + self.assertEqual('foo_2', o.a) + self.assertEqual(2, o.b) + self.assertEqual(3, o.c) + self.assertEqual(4, o.d) + self.assertEqual(2, o.id) + if __name__ == '__main__': unittest.main() |