db_alias
feature. You can configure a separate database alias for read-only
access to the reference data.
Here's how you can set it up:
Configure a Separate Database for Reference Data:
In your Django settings (settings.py
), define a
new database configuration for the reference data. You can
specify this in the DATABASES
setting using a
different alias:
DATABASES = { 'default': { # Your regular database configuration for testing 'ENGINE': 'django.db.backends.postgresql', 'NAME': 'test_db', # Other settings... }, 'reference_db': { # Configuration for the reference data database (read-only) 'ENGINE': 'django.db.backends.postgresql', 'NAME': 'reference_db', # Other settings... }, }
Replace 'reference_db'
with an appropriate
alias for your reference data.
Create a Router for Reference Data:
Next, you can create a database router to determine which
database to use based on the model being accessed. In your
Django project, create a Python module (e.g., routers.py
)
and define a database router class:
class ReferenceDataRouter: def db_for_read(self, model, **hints): # Use the 'reference_db' alias for models that access reference data if model._meta.app_label == 'your_app_label': return 'reference_db' return None def db_for_write(self, model, **hints): # Prevent writes to the 'reference_db' database if model._meta.app_label == 'your_app_label': return None return 'default' def allow_migrate(self, db, app_label, model_name=None, **hints): # Allow migrations only on the default database if db == 'default': return True return False
Replace 'your_app_label'
with the actual app
label containing your reference data models.
Add the Router to Your Settings:
In your Django settings (settings.py
), add the
reference data router to the DATABASE_ROUTERS
setting:
DATABASE_ROUTERS = ['your_project.routers.ReferenceDataRouter']
Write Tests That Access Reference Data:
In your test cases, you can access the reference data by
using the using
method to specify the database
alias:
from django.test import TestCase from your_app.models import YourReferenceModel class YourTestCase(TestCase): def test_access_reference_data(self): reference_objects = YourReferenceModel.objects.using('reference_db').all() # Perform assertions or test your application's behavior using the reference data
With this setup, you can read from the 'reference_db' database for models that access reference data, and writes will still occur in the regular 'default' database. This allows you to access the read-only reference data while maintaining the isolation and reset of the 'default' database between tests, as per Django's default testing behavior.
Do you need tests for every chemical? Is there a subset of tests and data that would cover all the cases?
--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/0FA932EA-13A3-465A-AE92-F073AEC51F41%40fattuba.com.
-- Signed email is an absolute defence against phishing. This email has been signed with my private key. If you import my public key you can automatically decrypt my signature and be sure it came from me. Your email software can handle signing.