Answer the question
In order to leave comments, you need to log in
Is it possible to disable foreign keys in the test database (isn't that an anti-pattern)?
I have tests in my project that test individual classes and their interaction with the database. For this purpose, I provide the test with a clean test base before running each test.
Each class uses only its limited set of models. In the database, these models correspond to tables, but tables of different models used in different classes can be linked to each other using foreign keys.
What is the problem: for example, I have a class that saves some model in the database, and I want to test this save method. I create an instance of the model, try to save it in the test and realize that this will not work, because the model has 4 foreign keys, and I have to take and insert 4 rows into the database first into different tables. Each of these 4 rows has 2 more foreign keys, and so on. Thus, it turns out that in order to check the preservation of one model, I have to save a whole graph of objects in the database, some of which are not affected in any way within this test.
Of course, there is such a thing as fixtures, when I can store XML or JSON files and populate the database with predefined data before each test. But what if we just disable all foreign keys during tests and write to the database only those models that are currently being tested? Can this be considered as an option? And are there any critical disadvantages of this solution that are not visible at first glance?
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question