0

Recently I've changed db engine from SQLite to PostgreSQL. I have successfully migrated whole db design to PostgreSQL (just simple makemigaretions, migrate). Once I ran tests some have failed for some unknown reason (errors suggest that some objects were not created). Failure doesn't apply to all tests, just selected few. Everything has been working before.

I'd start investigating what's going on on test-by-test basis, but some bizarre behavior has appeared. Let's say my test is in class MyTestClass and test is called test_do_something and in MyTestClass there are other tests as well.

  1. When I'm running python manage.py test MyTestClass I'm getting info that test_do_something has failed.
  2. When I'm running python manage.py test MyTestClass.test_do_something everything passes.
  3. On SQLite both ways pass.

I'm assuming that setUpTestData() and setUp() methods work same way in SQLite and PostgreSQL. Or they don't?

Any clue why such discrepancy might be happening?

EDIT

I think I've noticed what might be wrong, but I don't understand why. The problem is because my function I've used to call to create object which is later used only once. Which differs from SQLite execution.

What I mean in my test I have something like this:

def create_object(self):
    self.client.post(reverse('myurl'), kwargs={'myargs':arg})

def test_mytest1(self):
    # Do something
    self.create_object()
    # Do something

def test_mytest2(self):
    # Do something
    self.create_object()
    # Do something

def test_mytest3(self):
    # Do something
    self.create_object()
    # Do something

Only for one test create_object() will be executed.

sebap123
  • 2,541
  • 6
  • 45
  • 81
  • Are you sure the *test database* is created during each run? – JPG Aug 15 '20 at 12:46
  • @ArakkalAbu yes, I have considered that. But when I run e.g. #2 then #1 then #2 or any other order of execution schema always repeats. What's more I'm not using --keepdb. According to documentation between each run `flush` should be executed automatically. – sebap123 Aug 15 '20 at 12:50
  • I think you should add more information regarding the issue, like what error you have got, is it reproducible etc. maybe, someone else could help, if you do so – JPG Aug 15 '20 at 13:00
  • @ArakkalAbu I've updated question with more details. – sebap123 Aug 15 '20 at 14:23

1 Answers1

0

I believe I've fond cause of those failures. As a matter of fact it hasn't been an issue with one-time execution of support function as I expected. The problem has been with hardcoded ids I've used for various reasons. It appears that object I've been hoping to see didn't exist.

Let me explain a bit more what I've experienced. E.g. I had test where I've been referring to particular object passing this object id in URL kwargs. Before this operation I've created object and passed id=1 as kwargs, because I assumed that if this is only place within this test and setUp() it will be 1. It appears that with PostgreSQL it's not so clear. It seems that ids are incremented despite DB flush. Which is completely different behavior then SQLite has been providing.

I'd very much appreciate if someone could provide some more detailed answer why is this happening. Is ID counter not zeroed in PostgreSQL on flush? It would look so.

sebap123
  • 2,541
  • 6
  • 45
  • 81