0

I am writing a unit test for a model method of ModelA which queries for the most recent related ModelB and validates it based on the current time. Ideally for this test I would just like to create one ModelA and a few ModelBs make some assertions and be done.

My issue is that both ModelA and ModelB have foreign keys that point to other models which in turn have foreign keys that point to even more models. Is there a way in the scope of my test to ignore the IntegrityError that is thrown by not assigning these other foreign keys?

My models.py looks something like this

class ModelA(models.Model):
    modelc = models.ForeignKey('ModelC')
    ...
    def method1(self):
        most_recent_modelb = ModelB.objects.filter(modela = self).latest('created')
        #some other stuff

class ModelB(models.Model):
    modela = models.ForeignKey('ModelA')
    modelz = models.ForeignKey('ModelZ')

class ModelC(models.Model):
    modeld = models.ForeignKey('ModelD')
grrrrrr
  • 1,395
  • 12
  • 29
  • I haven't tried it myself, but maybe you can try the solution posted here: https://stackoverflow.com/questions/52767335/how-do-i-temporarily-disable-db-integrity-constraints-in-django-postgresql , disable triggers on test setup, then enable them again on tearDown. But I am not sure if this is a good idea for testing, you would be running the tests on different conditions than real application. – Ozgur Akcali Mar 15 '19 at 21:09
  • 1
    You can't and probably should not. – William R. Marchand Mar 15 '19 at 21:24

0 Answers0