I have created a REST-API application using Django Rest Framework.
The API just converts the data from an existing Read-only Postgres DB to a REST- API to be consumed by the front-end.
Now I need to write some unit tests to test the application. Since there are lots of tables and views involved, I cannot create mock data and test.
Is there a way to run the tests in Django using the existing DB (Postgres) without creating any mock data ?
Note: I have read a lot of SO posts related to my problem but none of them worked as they were for old versions of Django.
CodePudding user response:
You can't use the Django test command for looking at production data. It always creates an empty database that is populated from fixtures in the TestCase.
This is not a good practice and should not be done. But if you want to achieve what you stated, you can try this -
DATABASES = {
'default': {
...
'TEST': {
'NAME': 'your prod db'
}
}
in your settings.py file. (This may interfere with your production database)
I will suggest you use hypothesis and factories for data production to writing tests as it will help you in the long run and will be foolproof.
What you are suggesting will not be called a test though. So it will be helpful for you to reconsider writing tests for your application.
CodePudding user response:
You could do something like this in your settings.py
import sys
if 'test' in sys.argv:
DATABASES['default'] = {
# your default database credentials
}