Opened 19 months ago
Closed 19 months ago
#34490 closed Bug (invalid)
Tests with mirror database not working as expected
Reported by: | Andrés Fernández | Owned by: | nobody |
---|---|---|---|
Component: | Testing framework | Version: | 4.2 |
Severity: | Normal | Keywords: | |
Cc: | Triage Stage: | Unreviewed | |
Has patch: | no | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | no |
Easy pickings: | no | UI/UX: | no |
Description
Hello. I am using TransactionTestCases to perform tests using a primary-replica database architecture. I followed the instructions here https://docs.djangoproject.com/en/4.2/topics/testing/advanced/#testing-primary-replica-configurations to create this architecture for testing. However, I encounter some issues:
- Fixtures cannot be loaded: foreign keys are not found. From what I see, loaddata command is placed inside a transaction.atomic block, so that would make sense. However, in a different, more simple project, with a couple of models and fixtures with foreign keys, fixtures DO work, so I'm not sure what the issue is.
- If I try to manually load the fixtures in setUp (calling loaddata command manually), something strange happens: for the first test of a given TransactionTestCase, I have the same issue that above: any fixture referencing an afore loaded object loaded through a FK fails because said object is not found. However, the manual fixture loading does work for the rest of the tests. It's hard to understand why.
- Some tests fail because the replica database does not find an object. For example, I create an object and then try to retrieve it, and the object is not found. Through debugging, the object is correctly created in the primary db, but for some reason not replicated in the other one. A very strange thing is that this happens when I run this TestCase in particular, when I run all my tests, that same test passes.
There is something not deterministic about tests failing when using this architecture, and I believe it mainly has to do with data not being replicated for some reason. I believed I followed the instructions correctly, using TransactionTestCase instead of TestCase, and setting up the replica to mirror the default database. Like I said, in my other simple project, things do work as expected: non of the above issues is raised (using the same Python, PostgreSQL and libraries' versions). Any help would be appreciated.
Hello Andres!
Thank you for your report, though I think this request for help would be best handled in any of the django users groups. You could either join the Django Forum or the django-users mailing list, and send details about your issue (see this link for more details).
I'll be closing this issue as Invalid, but if you find out that this is really a bug in Django itself, feel free to re-open this ticket with steps to reproduce the problem.