diff options
author | Raphaël Barrois <raphael.barrois@polytechnique.org> | 2013-10-29 00:40:42 +0100 |
---|---|---|
committer | Raphaël Barrois <raphael.barrois@polytechnique.org> | 2013-10-29 00:40:42 +0100 |
commit | 5fc87aaa492abe596a8ced0e23ba23b8c97252e8 (patch) | |
tree | deea7d1abc302a4df2041a0f326c7b14e3b84d5b | |
parent | 382e319a934d27e16b9d0ae8597923ab85976694 (diff) | |
download | factory-boy-5fc87aaa492abe596a8ced0e23ba23b8c97252e8.tar factory-boy-5fc87aaa492abe596a8ced0e23ba23b8c97252e8.tar.gz |
django: Add tests for 'pk=None' with get_or_create.
See issue #61.
-rw-r--r-- | tests/test_django.py | 33 |
1 files changed, 33 insertions, 0 deletions
diff --git a/tests/test_django.py b/tests/test_django.py index 7cebf8d..94101e9 100644 --- a/tests/test_django.py +++ b/tests/test_django.py @@ -101,6 +101,14 @@ class StandardFactory(factory.django.DjangoModelFactory): foo = factory.Sequence(lambda n: "foo%d" % n) +class StandardFactoryWithPKField(factory.django.DjangoModelFactory): + FACTORY_FOR = models.StandardModel + FACTORY_DJANGO_GET_OR_CREATE = ('pk',) + + foo = factory.Sequence(lambda n: "foo%d" % n) + pk = None + + class NonIntegerPkFactory(factory.django.DjangoModelFactory): FACTORY_FOR = models.NonIntegerPk @@ -171,6 +179,31 @@ class DjangoPkSequenceTestCase(django_test.TestCase): @unittest.skipIf(django is None, "Django not installed.") +class DjangoPkForceTestCase(django_test.TestCase): + def setUp(self): + super(DjangoPkForceTestCase, self).setUp() + StandardFactoryWithPKField.reset_sequence() + + def test_no_pk(self): + std = StandardFactoryWithPKField() + self.assertIsNotNone(std.pk) + self.assertEqual('foo1', std.foo) + + def test_force_pk(self): + std = StandardFactoryWithPKField(pk=42) + self.assertIsNotNone(std.pk) + self.assertEqual('foo1', std.foo) + + def test_reuse_pk(self): + std1 = StandardFactoryWithPKField(foo='bar') + self.assertIsNotNone(std1.pk) + + std2 = StandardFactoryWithPKField(pk=std1.pk, foo='blah') + self.assertEqual(std1.pk, std2.pk) + self.assertEqual('bar', std2.foo) + + +@unittest.skipIf(django is None, "Django not installed.") class DjangoModelLoadingTestCase(django_test.TestCase): """Tests FACTORY_FOR = 'app.Model' pattern.""" |