lundi 30 janvier 2017

Mock model to use a different database in django

In my settings I defined 2 databases:

DATABASES = { "default": default_db, "test": test_db }

In my app code, I have the following:

def my_method()
    records = do_something()
    MyModel.objects.filter(
       start=truncate_minutes(utc_now()),
       country=country
    ).delete()

    MyModel.objects.bulk_create(records)
    do_something()

I want to mock the MyModel.objects, than in my test it will be using a different database like this:

MyModel.objects.using("test").bulk_create(records) or
MyModel.objects.using("test").filter(
           start=truncate_minutes(utc_now()),
           country=country
        ).delete()

How can I do it from my test method without changing the existing code?

@mock("MyModel)
def test_my_method(self, mock_my_model):
   ...

Aucun commentaire:

Enregistrer un commentaire