pythondjangounit-testingdjango-reversion

How to create initial revision for test objects when using django-reversion in test case


I'm creating some initial tests as I play with django-revisions. I'd like to be able to test that some of my api and view code correctly saves revisions. However, I can't get even a basic test to save a deleted version.

import reversion
from django.db import transaction
from django import test
from myapp import models

class TestRevisioning(test.TestCase):
    fixtures = ['MyModel']
    def testDelete(self):
        object1 = models.MyModel.objects.first()
        with transaction.atomic():
             with reversion.create_revision():
                 object1.delete()
        self.assertEquals(reversion.get_deleted(models.MyModel).count(), 1)

This fails when checking the length of the deleted QuerySet with:

AssertionError: 0 != 1

My hypothesis is that I need to create the initial revisions of my model (do the equivalent of ./manage.py createinitialrevisions). If this is the issue, how do I create the initial revisions in my test? If that isn't the issue, what else can I try?


Solution

  • So, the solution is pretty simple. I saved my object under revision control.

    # imports same as question
    
    class TestRevisioning(test.TestCase):
        fixtures = ['MyModel']
    
        def testDelete(self):
            object1 = models.MyModel.objects.first()
            # set up initial revision
            with reversion.create_revision():
                object1.save()
            # continue with remainder of the test as per the question.
            # ... etc.
    

    I tried to override _fixture_setup(), but that didn't work. Another option would be to loop over the MyModel objects in the __init__(), saving them under reversion control.