I am writings tests for a django application and I've run into a problem were deleted objects still exist in the test database after I've supposedly deleted them.
I'm using the following factory
class CMSPageFactory(factory.DjangoModelFactory):
class Meta:
model = CMSPage
title = factory.Faker('company')
tenant = factory.SubFactory(TenantFactory)
key = factory.Faker('slug')
protected = False
in_navigation = False
active = True
This is the test I am running
def test_example_for_so(self):
page = CMSPageFactory()
page.delete()
self.assertFalse(page)
And it raises the following error: AssertionError: <CMSPage: Fletcher LLC> is not false
I must be missing something very obvious but for the life of me I cannot figure out what. Does anyone know what I am doing wrong?
Are you sure, page
still exists in the db?
Calling delete()
on a django model instance (which your factory supposedly creates) will delete the database row, but nor your local python representation:
https://docs.djangoproject.com/en/2.1/ref/models/instances/#django.db.models.Model.delete
Issues an SQL DELETE for the object. This only deletes the object in the database; the Python instance will still exist and will still have data in its fields.
The object was deleted from the database but still exists in memory. From the Model delete docs :
Issues an SQL DELETE for the object. This only deletes the object in the database; the Python instance will still exist and will still have data in its fields. This method returns the number of objects deleted and a dictionary with the number of deletions per object type
What you can do in the test is get the id and then try to get the object from the database, or count the objects in the database and expect 0.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.