Specifying Readonly access for Django.db connection object
<p>I have a series of integration-level tests that are being run as a management command in my Django project. These tests are verifying the integrity of a large amount of weather data ingested from external sources into my database. Because I have such a large amount of data, I really have to test against my production database for the tests to be meaningful. What I'm trying to figure out is how I can define a read-only database connection that is specific to that command or connection object. I should also add that these tests can't go through the ORM, so I need to execute raw SQL. </p>

<p>The structure of my test looks like this</p>

<pre><code>class Command(BaseCommand):
    help = 'Runs Integration Tests and Query Tests against Prod Database'

    def handle(self,*args, **options):
        suite = unittest.TestLoader().loadTestsFromTestCase(TestWeatherModel)
        ret = unittest.TextTestRunner().run(suite)
        if(len(ret.failures) != 0):
            sys.exit(1)
        else:
            sys.exit(0)

class TestWeatherModel(unittest.TestCase):
    def testCollectWeatherDataHist(self):
        wm = WeatherManager()
        wm.CollectWeatherData()
        self.assertTrue(wm.weatherData is not None)
</code></pre>

<p>And the WeatherManager.CollectWeatherData() method would look like this:</p>

<pre><code>def CollecWeatherData(self):
    cur = connection.cursor()
    cur.execute(&lt;Raw SQL Query&gt;)
    wm.WeatherData = cur.fetchall()
    cur.close()
</code></pre>

<p>I want to somehow idiot-proof this, so that someone else (or me) can't come along later and accidentally write a test that would modify the production database.</p>

<p>Man, once again, I should read the docs more carefully before I post questions here. I can define a readonly connection to my production database in the settings file, and then straight from the <a href="https://docs.djangoproject.com/en/1.10/topics/db/sql/" rel="nofollow">docs</a>:</p>

<p>If you are using more than one database, you can use django.db.connections to obtain the connection (and cursor) for a specific database. django.db.connections is a dictionary-like object that allows you to retrieve a specific connection using its alias:</p>

<pre><code>from django.db import connections
cursor = connections['my_db_alias'].cursor()
# Your code here...
</code></pre>

python django django-testing django-database django-postgresql