Resetting Primary Key (pk) Sequence in Django Factory Boy for Each Test

72 Views Asked by At

I'm trying to figure out how to reset the factory model's primary key (pk) sequence in Django when using FactoryBoy for testing. Consider the following sample model and tests:

class Profile(DjangoModelFactory):
    class Meta:
        model = Profile

Now I want to test it:

class FirstTest(TestCase):
    @classmethod
    def setUpClass(cls):
        super(UserScoresTest, cls).setUpClass()
        Profile.create()
        Profile.create()

class SecondTest(TestCase):
    @classmethod
    def setUpClass(cls):
        super(UserScoresTest, cls).setUpClass()
        Profile.create()
        Profile.create()

After running these tests, the Profiles' primary keys (pk) in SecondTest have values 3 and 4, respectively. I want to reset the pk sequence before each test. What should I do?

0

There are 0 best solutions below