From 82988e154391cc37f81eb398bbfa91f30f524349 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rapha=C3=ABl=20Barrois?= Date: Thu, 21 Aug 2014 09:28:51 +0200 Subject: tests: Update to Django new 'duplicate file' mechanism. --- tests/test_django.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) (limited to 'tests/test_django.py') diff --git a/tests/test_django.py b/tests/test_django.py index 41a26cf..fd9c876 100644 --- a/tests/test_django.py +++ b/tests/test_django.py @@ -430,7 +430,8 @@ class DjangoFileFieldTestCase(unittest.TestCase): o2 = WithFileFactory.build(afile=o1.afile) self.assertIsNone(o2.pk) self.assertEqual(b'example_data\n', o2.afile.read()) - self.assertEqual('django/example_1.data', o2.afile.name) + self.assertNotEqual('django/example.data', o2.afile.name) + self.assertRegexpMatches(o2.afile.name, r'django/example_\w+.data') def test_no_file(self): o = WithFileFactory.build(afile=None) @@ -547,7 +548,8 @@ class DjangoImageFieldTestCase(unittest.TestCase): self.assertIsNone(o2.pk) # Image file for a 42x42 green jpeg: 301 bytes long. self.assertEqual(301, len(o2.animage.read())) - self.assertEqual('django/example_1.jpeg', o2.animage.name) + self.assertNotEqual('django/example.jpeg', o2.animage.name) + self.assertRegexpMatches(o2.animage.name, r'django/example_\w+.jpeg') def test_no_file(self): o = WithImageFactory.build(animage=None) -- cgit v1.2.3 From 1a00eef263f787a9f3d98fbaa43ec30ad9ac4071 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rapha=C3=ABl=20Barrois?= Date: Wed, 3 Sep 2014 22:53:02 +0200 Subject: Fix test running without django (Closes #161). --- tests/test_django.py | 101 +++++++++++++++++++++++---------------------------- 1 file changed, 45 insertions(+), 56 deletions(-) (limited to 'tests/test_django.py') diff --git a/tests/test_django.py b/tests/test_django.py index fd9c876..95e0256 100644 --- a/tests/test_django.py +++ b/tests/test_django.py @@ -57,21 +57,9 @@ if django is not None: from django.test import utils as django_test_utils from django.db.models import signals from .djapp import models -else: # pragma: no cover - django_test = unittest - - class Fake(object): - pass - models = Fake() - models.StandardModel = Fake - models.StandardSon = None - models.AbstractBase = Fake - models.ConcreteSon = Fake - models.NonIntegerPk = Fake - models.WithFile = Fake - models.WithImage = Fake - models.WithSignals = Fake +else: + django_test = unittest test_state = {} @@ -98,72 +86,73 @@ def tearDownModule(): django_test_utils.teardown_test_environment() -class StandardFactory(factory.django.DjangoModelFactory): - class Meta: - model = models.StandardModel +if django is not None: + class StandardFactory(factory.django.DjangoModelFactory): + class Meta: + model = models.StandardModel - foo = factory.Sequence(lambda n: "foo%d" % n) + foo = factory.Sequence(lambda n: "foo%d" % n) -class StandardFactoryWithPKField(factory.django.DjangoModelFactory): - class Meta: - model = models.StandardModel - django_get_or_create = ('pk',) + class StandardFactoryWithPKField(factory.django.DjangoModelFactory): + class Meta: + model = models.StandardModel + django_get_or_create = ('pk',) - foo = factory.Sequence(lambda n: "foo%d" % n) - pk = None + foo = factory.Sequence(lambda n: "foo%d" % n) + pk = None -class NonIntegerPkFactory(factory.django.DjangoModelFactory): - class Meta: - model = models.NonIntegerPk + class NonIntegerPkFactory(factory.django.DjangoModelFactory): + class Meta: + model = models.NonIntegerPk - foo = factory.Sequence(lambda n: "foo%d" % n) - bar = '' + foo = factory.Sequence(lambda n: "foo%d" % n) + bar = '' -class AbstractBaseFactory(factory.django.DjangoModelFactory): - class Meta: - model = models.AbstractBase - abstract = True + class AbstractBaseFactory(factory.django.DjangoModelFactory): + class Meta: + model = models.AbstractBase + abstract = True - foo = factory.Sequence(lambda n: "foo%d" % n) + foo = factory.Sequence(lambda n: "foo%d" % n) -class ConcreteSonFactory(AbstractBaseFactory): - class Meta: - model = models.ConcreteSon + class ConcreteSonFactory(AbstractBaseFactory): + class Meta: + model = models.ConcreteSon -class AbstractSonFactory(AbstractBaseFactory): - class Meta: - model = models.AbstractSon + class AbstractSonFactory(AbstractBaseFactory): + class Meta: + model = models.AbstractSon -class ConcreteGrandSonFactory(AbstractBaseFactory): - class Meta: - model = models.ConcreteGrandSon + class ConcreteGrandSonFactory(AbstractBaseFactory): + class Meta: + model = models.ConcreteGrandSon -class WithFileFactory(factory.django.DjangoModelFactory): - class Meta: - model = models.WithFile + class WithFileFactory(factory.django.DjangoModelFactory): + class Meta: + model = models.WithFile - if django is not None: - afile = factory.django.FileField() + if django is not None: + afile = factory.django.FileField() -class WithImageFactory(factory.django.DjangoModelFactory): - class Meta: - model = models.WithImage + class WithImageFactory(factory.django.DjangoModelFactory): + class Meta: + model = models.WithImage - if django is not None: - animage = factory.django.ImageField() + if django is not None: + animage = factory.django.ImageField() -class WithSignalsFactory(factory.django.DjangoModelFactory): - class Meta: - model = models.WithSignals + class WithSignalsFactory(factory.django.DjangoModelFactory): + class Meta: + model = models.WithSignals @unittest.skipIf(django is None, "Django not installed.") -- cgit v1.2.3 From 25bd44c30007d5babecefed651827431569ee1ee Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rapha=C3=ABl=20Barrois?= Date: Wed, 3 Sep 2014 23:49:47 +0200 Subject: Fix support for Django 1.7. --- tests/test_django.py | 2 ++ 1 file changed, 2 insertions(+) (limited to 'tests/test_django.py') diff --git a/tests/test_django.py b/tests/test_django.py index 95e0256..874c272 100644 --- a/tests/test_django.py +++ b/tests/test_django.py @@ -61,6 +61,8 @@ if django is not None: else: django_test = unittest +if django is not None and django.VERSION >= (1, 7, 0): + django.setup() test_state = {} -- cgit v1.2.3 From 13d310fa14f4e4b9a559f8b7887f2a2492357013 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rapha=C3=ABl=20Barrois?= Date: Sun, 16 Nov 2014 22:34:29 +0100 Subject: Remove automagic pk-based sequence setup Related to issues #78, #92, #103, #111, #153, #170 The default value of all sequences is now 0; the automagic ``_setup_next_sequence`` behavior of Django/SQLAlchemy has been removed. This feature's only goal was to allow the following scenario: 1. Run a Python script that uses MyFactory.create() a couple of times (with a unique field based on the sequence counter) 2. Run the same Python script a second time Without the magical ``_setup_next_sequence``, the Sequence counter would be set to 0 at the beginning of each script run, so both runs would generate objects with the same values for the unique field ; thus conflicting and crashing. The above behavior having only a very limited use and bringing various issues (hitting the database on ``build()``, problems with non-integer or composite primary key columns, ...), it has been removed. It could still be emulated through custom ``_setup_next_sequence`` methods, or by calling ``MyFactory.reset_sequence()``. --- tests/test_django.py | 40 ++++++++++++++++++++-------------------- 1 file changed, 20 insertions(+), 20 deletions(-) (limited to 'tests/test_django.py') diff --git a/tests/test_django.py b/tests/test_django.py index 874c272..0cbef19 100644 --- a/tests/test_django.py +++ b/tests/test_django.py @@ -174,32 +174,32 @@ class DjangoPkSequenceTestCase(django_test.TestCase): def test_pk_first(self): std = StandardFactory.build() - self.assertEqual('foo1', std.foo) + self.assertEqual('foo0', std.foo) def test_pk_many(self): std1 = StandardFactory.build() std2 = StandardFactory.build() - self.assertEqual('foo1', std1.foo) - self.assertEqual('foo2', std2.foo) + self.assertEqual('foo0', std1.foo) + self.assertEqual('foo1', std2.foo) def test_pk_creation(self): std1 = StandardFactory.create() - self.assertEqual('foo1', std1.foo) + self.assertEqual('foo0', std1.foo) self.assertEqual(1, std1.pk) StandardFactory.reset_sequence() std2 = StandardFactory.create() - self.assertEqual('foo2', std2.foo) + self.assertEqual('foo0', std2.foo) self.assertEqual(2, std2.pk) def test_pk_force_value(self): std1 = StandardFactory.create(pk=10) - self.assertEqual('foo1', std1.foo) # sequence was set before pk + self.assertEqual('foo0', std1.foo) # sequence is unrelated to pk self.assertEqual(10, std1.pk) StandardFactory.reset_sequence() std2 = StandardFactory.create() - self.assertEqual('foo11', std2.foo) + self.assertEqual('foo0', std2.foo) self.assertEqual(11, std2.pk) @@ -212,12 +212,12 @@ class DjangoPkForceTestCase(django_test.TestCase): def test_no_pk(self): std = StandardFactoryWithPKField() self.assertIsNotNone(std.pk) - self.assertEqual('foo1', std.foo) + self.assertEqual('foo0', std.foo) def test_force_pk(self): std = StandardFactoryWithPKField(pk=42) self.assertIsNotNone(std.pk) - self.assertEqual('foo1', std.foo) + self.assertEqual('foo0', std.foo) def test_reuse_pk(self): std1 = StandardFactoryWithPKField(foo='bar') @@ -286,9 +286,9 @@ class DjangoModelLoadingTestCase(django_test.TestCase): self.assertEqual(models.StandardModel, e1.__class__) self.assertEqual(models.StandardSon, e2.__class__) self.assertEqual(models.StandardModel, e3.__class__) - self.assertEqual(1, e1.foo) - self.assertEqual(2, e2.foo) - self.assertEqual(3, e3.foo) + self.assertEqual(0, e1.foo) + self.assertEqual(1, e2.foo) + self.assertEqual(2, e3.foo) @unittest.skipIf(django is None, "Django not installed.") @@ -299,23 +299,23 @@ class DjangoNonIntegerPkTestCase(django_test.TestCase): def test_first(self): nonint = NonIntegerPkFactory.build() - self.assertEqual('foo1', nonint.foo) + self.assertEqual('foo0', nonint.foo) def test_many(self): nonint1 = NonIntegerPkFactory.build() nonint2 = NonIntegerPkFactory.build() - self.assertEqual('foo1', nonint1.foo) - self.assertEqual('foo2', nonint2.foo) + self.assertEqual('foo0', nonint1.foo) + self.assertEqual('foo1', nonint2.foo) def test_creation(self): nonint1 = NonIntegerPkFactory.create() - self.assertEqual('foo1', nonint1.foo) - self.assertEqual('foo1', nonint1.pk) + self.assertEqual('foo0', nonint1.foo) + self.assertEqual('foo0', nonint1.pk) NonIntegerPkFactory.reset_sequence() nonint2 = NonIntegerPkFactory.build() - self.assertEqual('foo1', nonint2.foo) + self.assertEqual('foo0', nonint2.foo) def test_force_pk(self): nonint1 = NonIntegerPkFactory.create(pk='foo10') @@ -324,8 +324,8 @@ class DjangoNonIntegerPkTestCase(django_test.TestCase): NonIntegerPkFactory.reset_sequence() nonint2 = NonIntegerPkFactory.create() - self.assertEqual('foo1', nonint2.foo) - self.assertEqual('foo1', nonint2.pk) + self.assertEqual('foo0', nonint2.foo) + self.assertEqual('foo0', nonint2.pk) @unittest.skipIf(django is None, "Django not installed.") -- cgit v1.2.3 From 72fd943513b0e516f06c53b13ff35ca814b0a4a0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rapha=C3=ABl=20Barrois?= Date: Thu, 26 Mar 2015 22:37:54 +0100 Subject: Fix issues between mute_signals() and factory inheritance (Closes #183). Previously, if a factory was decorated with ``@mute_signals`` and one of its descendant called another one of its descendant, signals weren't unmuted properly. --- tests/test_django.py | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) (limited to 'tests/test_django.py') diff --git a/tests/test_django.py b/tests/test_django.py index 0cbef19..4653305 100644 --- a/tests/test_django.py +++ b/tests/test_django.py @@ -592,6 +592,27 @@ class PreventSignalsTestCase(unittest.TestCase): self.assertSignalsReactivated() + def test_class_decorator_with_subfactory(self): + @factory.django.mute_signals(signals.pre_save, signals.post_save) + class WithSignalsDecoratedFactory(factory.django.DjangoModelFactory): + class Meta: + model = models.WithSignals + + @factory.post_generation + def post(obj, create, extracted, **kwargs): + if not extracted: + WithSignalsDecoratedFactory.create(post=42) + + # This will disable the signals (twice), create two objects, + # and reactivate the signals. + WithSignalsDecoratedFactory() + + self.assertEqual(self.handlers.pre_init.call_count, 2) + self.assertFalse(self.handlers.pre_save.called) + self.assertFalse(self.handlers.post_save.called) + + self.assertSignalsReactivated() + def test_class_decorator_build(self): @factory.django.mute_signals(signals.pre_save, signals.post_save) class WithSignalsDecoratedFactory(factory.django.DjangoModelFactory): -- cgit v1.2.3 From 636ca46951d710a4b9d9fd61ec1da02294806d3d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rapha=C3=ABl=20Barrois?= Date: Thu, 26 Mar 2015 22:53:15 +0100 Subject: Add support for multidb with Django (Closes #171). The ``factory.django.DjangoModelFactory`` now takes an extra option: ``` class MyFactory(factory.django.DjangoModelFactory): class Meta: model = models.MyModel database = 'replica' ``` This will create all instances of ``models.Model`` in the ``'replica'`` database. --- tests/test_django.py | 10 ++++++++++ 1 file changed, 10 insertions(+) (limited to 'tests/test_django.py') diff --git a/tests/test_django.py b/tests/test_django.py index 4653305..a8f1f77 100644 --- a/tests/test_django.py +++ b/tests/test_django.py @@ -165,6 +165,16 @@ class ModelTests(django_test.TestCase): self.assertRaises(factory.FactoryError, UnsetModelFactory.create) + def test_cross_database(self): + class OtherDBFactory(factory.django.DjangoModelFactory): + class Meta: + model = models.StandardModel + database = 'replica' + + obj = OtherDBFactory() + self.assertFalse(models.StandardModel.objects.exists()) + self.assertEqual(obj, models.StandardModel.objects.using('replica').get()) + @unittest.skipIf(django is None, "Django not installed.") class DjangoPkSequenceTestCase(django_test.TestCase): -- cgit v1.2.3 From 4e0e563c1c0d823d2869d340e2fa31ca8630d854 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rapha=C3=ABl=20Barrois?= Date: Thu, 26 Mar 2015 23:31:56 +0100 Subject: Turn FileField/ImageField into normal fields (Closes #141). Previously, they ran as post_generation hooks, meaning that they couldn't be checked in a model's ``save()`` method, for instance. --- tests/test_django.py | 53 ++++++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 47 insertions(+), 6 deletions(-) (limited to 'tests/test_django.py') diff --git a/tests/test_django.py b/tests/test_django.py index a8f1f77..cf80edb 100644 --- a/tests/test_django.py +++ b/tests/test_django.py @@ -364,6 +364,9 @@ class DjangoFileFieldTestCase(unittest.TestCase): o = WithFileFactory.build() self.assertIsNone(o.pk) self.assertEqual(b'', o.afile.read()) + self.assertEqual('example.dat', o.afile.name) + + o.save() self.assertEqual('django/example.dat', o.afile.name) def test_default_create(self): @@ -375,19 +378,26 @@ class DjangoFileFieldTestCase(unittest.TestCase): def test_with_content(self): o = WithFileFactory.build(afile__data='foo') self.assertIsNone(o.pk) + + # Django only allocates the full path on save() + o.save() self.assertEqual(b'foo', o.afile.read()) self.assertEqual('django/example.dat', o.afile.name) def test_with_file(self): with open(testdata.TESTFILE_PATH, 'rb') as f: o = WithFileFactory.build(afile__from_file=f) - self.assertIsNone(o.pk) + o.save() + self.assertEqual(b'example_data\n', o.afile.read()) self.assertEqual('django/example.data', o.afile.name) def test_with_path(self): o = WithFileFactory.build(afile__from_path=testdata.TESTFILE_PATH) self.assertIsNone(o.pk) + + # Django only allocates the full path on save() + o.save() self.assertEqual(b'example_data\n', o.afile.read()) self.assertEqual('django/example.data', o.afile.name) @@ -397,7 +407,9 @@ class DjangoFileFieldTestCase(unittest.TestCase): afile__from_file=f, afile__from_path='' ) - self.assertIsNone(o.pk) + # Django only allocates the full path on save() + o.save() + self.assertEqual(b'example_data\n', o.afile.read()) self.assertEqual('django/example.data', o.afile.name) @@ -407,6 +419,9 @@ class DjangoFileFieldTestCase(unittest.TestCase): afile__from_file=None, ) self.assertIsNone(o.pk) + + # Django only allocates the full path on save() + o.save() self.assertEqual(b'example_data\n', o.afile.read()) self.assertEqual('django/example.data', o.afile.name) @@ -422,14 +437,21 @@ class DjangoFileFieldTestCase(unittest.TestCase): afile__filename='example.foo', ) self.assertIsNone(o.pk) + + # Django only allocates the full path on save() + o.save() self.assertEqual(b'example_data\n', o.afile.read()) self.assertEqual('django/example.foo', o.afile.name) def test_existing_file(self): o1 = WithFileFactory.build(afile__from_path=testdata.TESTFILE_PATH) + o1.save() + self.assertEqual('django/example.data', o1.afile.name) - o2 = WithFileFactory.build(afile=o1.afile) + o2 = WithFileFactory.build(afile__from_file=o1.afile) self.assertIsNone(o2.pk) + o2.save() + self.assertEqual(b'example_data\n', o2.afile.read()) self.assertNotEqual('django/example.data', o2.afile.name) self.assertRegexpMatches(o2.afile.name, r'django/example_\w+.data') @@ -453,6 +475,8 @@ class DjangoImageFieldTestCase(unittest.TestCase): def test_default_build(self): o = WithImageFactory.build() self.assertIsNone(o.pk) + o.save() + self.assertEqual(100, o.animage.width) self.assertEqual(100, o.animage.height) self.assertEqual('django/example.jpg', o.animage.name) @@ -460,6 +484,8 @@ class DjangoImageFieldTestCase(unittest.TestCase): def test_default_create(self): o = WithImageFactory.create() self.assertIsNotNone(o.pk) + o.save() + self.assertEqual(100, o.animage.width) self.assertEqual(100, o.animage.height) self.assertEqual('django/example.jpg', o.animage.name) @@ -467,6 +493,8 @@ class DjangoImageFieldTestCase(unittest.TestCase): def test_with_content(self): o = WithImageFactory.build(animage__width=13, animage__color='red') self.assertIsNone(o.pk) + o.save() + self.assertEqual(13, o.animage.width) self.assertEqual(13, o.animage.height) self.assertEqual('django/example.jpg', o.animage.name) @@ -480,6 +508,8 @@ class DjangoImageFieldTestCase(unittest.TestCase): def test_gif(self): o = WithImageFactory.build(animage__width=13, animage__color='blue', animage__format='GIF') self.assertIsNone(o.pk) + o.save() + self.assertEqual(13, o.animage.width) self.assertEqual(13, o.animage.height) self.assertEqual('django/example.jpg', o.animage.name) @@ -493,7 +523,8 @@ class DjangoImageFieldTestCase(unittest.TestCase): def test_with_file(self): with open(testdata.TESTIMAGE_PATH, 'rb') as f: o = WithImageFactory.build(animage__from_file=f) - self.assertIsNone(o.pk) + o.save() + # Image file for a 42x42 green jpeg: 301 bytes long. self.assertEqual(301, len(o.animage.read())) self.assertEqual('django/example.jpeg', o.animage.name) @@ -501,6 +532,8 @@ class DjangoImageFieldTestCase(unittest.TestCase): def test_with_path(self): o = WithImageFactory.build(animage__from_path=testdata.TESTIMAGE_PATH) self.assertIsNone(o.pk) + o.save() + # Image file for a 42x42 green jpeg: 301 bytes long. self.assertEqual(301, len(o.animage.read())) self.assertEqual('django/example.jpeg', o.animage.name) @@ -511,7 +544,8 @@ class DjangoImageFieldTestCase(unittest.TestCase): animage__from_file=f, animage__from_path='' ) - self.assertIsNone(o.pk) + o.save() + # Image file for a 42x42 green jpeg: 301 bytes long. self.assertEqual(301, len(o.animage.read())) self.assertEqual('django/example.jpeg', o.animage.name) @@ -522,6 +556,8 @@ class DjangoImageFieldTestCase(unittest.TestCase): animage__from_file=None, ) self.assertIsNone(o.pk) + o.save() + # Image file for a 42x42 green jpeg: 301 bytes long. self.assertEqual(301, len(o.animage.read())) self.assertEqual('django/example.jpeg', o.animage.name) @@ -538,15 +574,20 @@ class DjangoImageFieldTestCase(unittest.TestCase): animage__filename='example.foo', ) self.assertIsNone(o.pk) + o.save() + # Image file for a 42x42 green jpeg: 301 bytes long. self.assertEqual(301, len(o.animage.read())) self.assertEqual('django/example.foo', o.animage.name) def test_existing_file(self): o1 = WithImageFactory.build(animage__from_path=testdata.TESTIMAGE_PATH) + o1.save() - o2 = WithImageFactory.build(animage=o1.animage) + o2 = WithImageFactory.build(animage__from_file=o1.animage) self.assertIsNone(o2.pk) + o2.save() + # Image file for a 42x42 green jpeg: 301 bytes long. self.assertEqual(301, len(o2.animage.read())) self.assertNotEqual('django/example.jpeg', o2.animage.name) -- cgit v1.2.3 From a1e5ff13c0573feb95c810e7e27cd30de97b8f21 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rapha=C3=ABl=20Barrois?= Date: Thu, 26 Mar 2015 23:45:29 +0100 Subject: Update header years. --- tests/test_django.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'tests/test_django.py') diff --git a/tests/test_django.py b/tests/test_django.py index cf80edb..2744032 100644 --- a/tests/test_django.py +++ b/tests/test_django.py @@ -1,5 +1,5 @@ # -*- coding: utf-8 -*- -# Copyright (c) 2011-2013 Raphaël Barrois +# Copyright (c) 2011-2015 Raphaël Barrois # # Permission is hereby granted, free of charge, to any person obtaining a copy # of this software and associated documentation files (the "Software"), to deal -- cgit v1.2.3 From bdc1b815cfdf3028379c6c3f18c9c47ee8298a70 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rapha=C3=ABl=20Barrois?= Date: Fri, 27 Mar 2015 16:27:05 +0100 Subject: Respect default manager in DjangoModelFactory (Closes #192). The previous version tries to use ``cls._default_manager`` all the time, which breaks with ``manager.using(db_name)``. --- tests/test_django.py | 12 ++++++++++++ 1 file changed, 12 insertions(+) (limited to 'tests/test_django.py') diff --git a/tests/test_django.py b/tests/test_django.py index 2744032..9ac8f5c 100644 --- a/tests/test_django.py +++ b/tests/test_django.py @@ -157,6 +157,13 @@ if django is not None: model = models.WithSignals + class WithCustomManagerFactory(factory.django.DjangoModelFactory): + class Meta: + model = models.WithCustomManager + + foo = factory.Sequence(lambda n: "foo%d" % n) + + @unittest.skipIf(django is None, "Django not installed.") class ModelTests(django_test.TestCase): def test_unset_model(self): @@ -706,5 +713,10 @@ class PreventSignalsTestCase(unittest.TestCase): self.assertSignalsReactivated() +class DjangoCustomManagerTestCase(django_test.TestCase): + + def test_extra_args(self): + model = WithCustomManagerFactory(arg='foo') + if __name__ == '__main__': # pragma: no cover unittest.main() -- cgit v1.2.3 From 5363951bb62ca90d971bf036851dea564204ed2e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rapha=C3=ABl=20Barrois?= Date: Fri, 27 Mar 2015 17:00:32 +0100 Subject: Support declarations in FileField/ImageField. Previously, the declarations (``factory.Sequence`` & co) weren't properly computed. --- tests/test_django.py | 11 +++++++++++ 1 file changed, 11 insertions(+) (limited to 'tests/test_django.py') diff --git a/tests/test_django.py b/tests/test_django.py index 9ac8f5c..ac52769 100644 --- a/tests/test_django.py +++ b/tests/test_django.py @@ -497,6 +497,17 @@ class DjangoImageFieldTestCase(unittest.TestCase): self.assertEqual(100, o.animage.height) self.assertEqual('django/example.jpg', o.animage.name) + def test_complex_create(self): + o = WithImageFactory.create( + size=10, + animage__filename=factory.Sequence(lambda n: 'img%d.jpg' % n), + __sequence=42, + animage__width=factory.SelfAttribute('..size'), + animage__height=factory.SelfAttribute('width'), + ) + self.assertIsNotNone(o.pk) + self.assertEqual('django/img42.jpg', o.animage.name) + def test_with_content(self): o = WithImageFactory.build(animage__width=13, animage__color='red') self.assertIsNone(o.pk) -- cgit v1.2.3 From c77d97d950bcf6fab0061519922c4800e06ff711 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rapha=C3=ABl=20Barrois?= Date: Thu, 2 Apr 2015 09:48:53 +0200 Subject: Fix imports for Django 1.8 --- tests/test_django.py | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) (limited to 'tests/test_django.py') diff --git a/tests/test_django.py b/tests/test_django.py index ac52769..9da99cc 100644 --- a/tests/test_django.py +++ b/tests/test_django.py @@ -53,7 +53,10 @@ if django is not None: from django import test as django_test from django.conf import settings from django.db import models as django_models - from django.test import simple as django_test_simple + if django.VERSION <= (1, 8, 0): + from django.test.simple import DjangoTestSuiteRunner + else: + from django.test.runner import DiscoverRunner as DjangoTestSuiteRunner from django.test import utils as django_test_utils from django.db.models import signals from .djapp import models @@ -71,7 +74,7 @@ def setUpModule(): if django is None: # pragma: no cover raise unittest.SkipTest("Django not installed") django_test_utils.setup_test_environment() - runner = django_test_simple.DjangoTestSuiteRunner() + runner = DjangoTestSuiteRunner() runner_state = runner.setup_databases() test_state.update({ 'runner': runner, -- cgit v1.2.3 From e357919cdb52af96eb67148fd38dced34981821a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rapha=C3=ABl=20Barrois?= Date: Tue, 14 Apr 2015 00:20:33 +0200 Subject: Remove warnings with Django 1.7 (Closes #195). Builds upon pull request by @shinuza: - Properly import ``get_model`` - Run ``django.setup()`` before importing any models. --- tests/test_django.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'tests/test_django.py') diff --git a/tests/test_django.py b/tests/test_django.py index 9da99cc..113caeb 100644 --- a/tests/test_django.py +++ b/tests/test_django.py @@ -50,6 +50,8 @@ from . import tools if django is not None: os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'tests.djapp.settings') + if django.VERSION >= (1, 7, 0): + django.setup() from django import test as django_test from django.conf import settings from django.db import models as django_models @@ -64,8 +66,6 @@ if django is not None: else: django_test = unittest -if django is not None and django.VERSION >= (1, 7, 0): - django.setup() test_state = {} -- cgit v1.2.3 From 0e3cdffac41250cddfe93388b1c9fc1547e77a67 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rapha=C3=ABl=20Barrois?= Date: Sat, 25 Apr 2015 17:50:50 +0200 Subject: Clarify .build() issue with Django>1.8 (Ref #198). From 1.8 onwards, this crashes: >>> a = MyModel() # Don't save >>> b = MyOtherModel(fkey_to_mymodel=a) In turn, it breaks: class MyModelFactory(factory.django.DjangoModelFactory): class Meta: model = MyModel class MyOtherModelFactory(factory.django.DjangoModelFactory): class Meta: model = MyOtherModel fkey_to_mymodel = factory.SubFactory(MyModelFactory) MyOtherModelFactory.build() # Breaks The error message is: Cannot assign "MyModel()": "MyModel" instance isn't saved in the database. See https://code.djangoproject.com/ticket/10811 for details. --- tests/test_django.py | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) (limited to 'tests/test_django.py') diff --git a/tests/test_django.py b/tests/test_django.py index 113caeb..33d159d 100644 --- a/tests/test_django.py +++ b/tests/test_django.py @@ -361,6 +361,32 @@ class DjangoAbstractBaseSequenceTestCase(django_test.TestCase): self.assertEqual(1, obj.pk) +@unittest.skipIf(django is None, "Django not installed.") +class DjangoRelatedFieldTestCase(django_test.TestCase): + + @classmethod + def setUpClass(cls): + super(DjangoRelatedFieldTestCase, cls).setUpClass() + class PointedFactory(factory.django.DjangoModelFactory): + class Meta: + model = models.PointedModel + foo = 'ahah' + + class PointerFactory(factory.django.DjangoModelFactory): + class Meta: + model = models.PointingModel + pointed = factory.SubFactory(PointedFactory, foo='hihi') + foo = 'bar' + + cls.PointedFactory = PointedFactory + cls.PointerFactory = PointerFactory + + def test_direct_related_create(self): + ptr = self.PointerFactory() + self.assertEqual('hihi', ptr.pointed.foo) + self.assertEqual(ptr.pointed, models.PointedModel.objects.get()) + + @unittest.skipIf(django is None, "Django not installed.") class DjangoFileFieldTestCase(unittest.TestCase): -- cgit v1.2.3 From 526293fccdc2661d6b0d68e524dc32aa858a3435 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rapha=C3=ABl=20Barrois?= Date: Mon, 27 Apr 2015 15:30:14 +0200 Subject: Fix test startup for Django==1.6 --- tests/test_django.py | 40 +++++++++++++++++++++------------------- 1 file changed, 21 insertions(+), 19 deletions(-) (limited to 'tests/test_django.py') diff --git a/tests/test_django.py b/tests/test_django.py index 33d159d..2cfb55c 100644 --- a/tests/test_django.py +++ b/tests/test_django.py @@ -22,31 +22,13 @@ import os -import factory -import factory.django - try: import django except ImportError: # pragma: no cover django = None -try: - from PIL import Image -except ImportError: # pragma: no cover - # Try PIL alternate name - try: - import Image - except ImportError: - # OK, not installed - Image = None - - -from .compat import is_python2, unittest, mock -from . import testdata -from . import tools - - +# Setup Django as soon as possible if django is not None: os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'tests.djapp.settings') @@ -67,6 +49,26 @@ else: django_test = unittest + +try: + from PIL import Image +except ImportError: # pragma: no cover + # Try PIL alternate name + try: + import Image + except ImportError: + # OK, not installed + Image = None + + +import factory +import factory.django + +from .compat import is_python2, unittest, mock +from . import testdata +from . import tools + + test_state = {} -- cgit v1.2.3 From ebc89520d3f7589da35d4e7b78637fbe7d4d664a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rapha=C3=ABl=20Barrois?= Date: Sun, 24 May 2015 18:21:04 +0200 Subject: Add lazy loading to factory.Iterator. factory.Iterator no longers begins iteration of its argument on declaration, since this behavior may trigger database query when that argument is, for instance, a Django queryset. The ``factory.Iterator``'s argument will only be called when the containing ``Factory`` is first evaluated; this means that factories using ``factory.Iterator(models.MyThingy.objects.all())`` will no longer call the database at import time. --- tests/test_django.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'tests/test_django.py') diff --git a/tests/test_django.py b/tests/test_django.py index 2cfb55c..bde8efe 100644 --- a/tests/test_django.py +++ b/tests/test_django.py @@ -21,6 +21,7 @@ """Tests for factory_boy/Django interactions.""" import os +from .compat import is_python2, unittest, mock try: @@ -64,7 +65,6 @@ except ImportError: # pragma: no cover import factory import factory.django -from .compat import is_python2, unittest, mock from . import testdata from . import tools -- cgit v1.2.3 From 0b5270eab393fad20faa7a6a9720af18c97b1773 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rapha=C3=ABl=20Barrois?= Date: Sun, 31 May 2015 10:47:28 +0100 Subject: Properly handle custom Django managers (Closes #201). The actual behavior of Django with custom managers and inherited abstract models is rather complex, so this had to be adapted to the actual Django source code. --- tests/test_django.py | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) (limited to 'tests/test_django.py') diff --git a/tests/test_django.py b/tests/test_django.py index bde8efe..b8e7ccb 100644 --- a/tests/test_django.py +++ b/tests/test_django.py @@ -755,10 +755,21 @@ class PreventSignalsTestCase(unittest.TestCase): self.assertSignalsReactivated() -class DjangoCustomManagerTestCase(django_test.TestCase): +@unittest.skipIf(django is None, "Django not installed.") +class DjangoCustomManagerTestCase(unittest.TestCase): def test_extra_args(self): + # Our CustomManager will remove the 'arg=' argument. model = WithCustomManagerFactory(arg='foo') + def test_with_manager_on_abstract(self): + class ObjFactory(factory.django.DjangoModelFactory): + class Meta: + model = models.FromAbstractWithCustomManager + + # Our CustomManager will remove the 'arg=' argument, + # invalid for the actual model. + ObjFactory.create(arg='invalid') + if __name__ == '__main__': # pragma: no cover unittest.main() -- cgit v1.2.3 From d471c1b4b0d4b06d557b5b6a9349a7dc55515d69 Mon Sep 17 00:00:00 2001 From: Ilya Baryshev Date: Thu, 2 Jul 2015 23:44:37 +0300 Subject: Fix mute_signals behavior for signals with caching Connecting signals (with use_caching=True) inside mute_signals was breaking unmute on exit. Paused receivers were not running. This was caused by signal cache not being restored after unpatching. Workaround is to clear signal cache on exit. Fixes #212 --- tests/test_django.py | 13 +++++++++++++ 1 file changed, 13 insertions(+) (limited to 'tests/test_django.py') diff --git a/tests/test_django.py b/tests/test_django.py index b8e7ccb..103df91 100644 --- a/tests/test_django.py +++ b/tests/test_django.py @@ -678,6 +678,19 @@ class PreventSignalsTestCase(unittest.TestCase): self.assertSignalsReactivated() + def test_signal_cache(self): + with factory.django.mute_signals(signals.pre_save, signals.post_save): + signals.post_save.connect(self.handlers.mute_block_receiver) + WithSignalsFactory() + + self.assertTrue(self.handlers.mute_block_receiver.call_count, 1) + self.assertEqual(self.handlers.pre_init.call_count, 1) + self.assertFalse(self.handlers.pre_save.called) + self.assertFalse(self.handlers.post_save.called) + + self.assertSignalsReactivated() + self.assertTrue(self.handlers.mute_block_receiver.call_count, 1) + def test_class_decorator(self): @factory.django.mute_signals(signals.pre_save, signals.post_save) class WithSignalsDecoratedFactory(factory.django.DjangoModelFactory): -- cgit v1.2.3