From 636ca46951d710a4b9d9fd61ec1da02294806d3d Mon Sep 17 00:00:00 2001 From: Raphaƫl Barrois 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/djapp/settings.py | 3 +++ tests/test_django.py | 10 ++++++++++ tests/test_using.py | 6 ++++++ 3 files changed, 19 insertions(+) (limited to 'tests') diff --git a/tests/djapp/settings.py b/tests/djapp/settings.py index c051faf..18e43dd 100644 --- a/tests/djapp/settings.py +++ b/tests/djapp/settings.py @@ -34,6 +34,9 @@ DATABASES = { 'default': { 'ENGINE': 'django.db.backends.sqlite3', }, + 'replica': { + 'ENGINE': 'django.db.backends.sqlite3', + }, } 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): diff --git a/tests/test_using.py b/tests/test_using.py index 7318f2e..1d7977f 100644 --- a/tests/test_using.py +++ b/tests/test_using.py @@ -69,6 +69,9 @@ class FakeModel(object): def order_by(self, *args, **kwargs): return [1] + def using(self, db): + return self + objects = FakeModelManager() def __init__(self, **kwargs): @@ -1490,6 +1493,9 @@ class BetterFakeModelManager(object): instance.id = 2 return instance, True + def using(self, db): + return self + class BetterFakeModel(object): @classmethod -- cgit v1.2.3