title: Can I configure Django for Unit tests with restricted view on unmanaged databases?
body: <p>We are a small team, trying to work with <em>Django</em> with a <strong>restricted access</strong> to a <strong>futurely unmanaged</strong> <em>PostgreSQL</em> database (i.e: only views and stored procedures ; no access to any tables) for security reasons.</p>
  
  <ol>
  <li>We tried to give (within Postgre) the user <strong>external_test</strong> the rights to create any tables inside his own schema on <strong>external</strong>, and to use the following settings (<code>settings.py</code>):</li>
  </ol>
  
  <pre class="lang-py prettyprint-override"><code>...
  DATABASES = {
      'default': {
          'ENGINE': 'django.db.backends.sqlite3',
          'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
      },
      'external': {
          'ENGINE': 'django.db.backends.postgresql',
          'NAME': 'postgre_db',
          'USER': 'external_user',
          'PASSWORD': 'password',
          'HOST': 'integration.project.net',
          'PORT': '5432',
          'TEST': {
              'NAME': 'test_db',
              'USER': 'external_test',
              'PASSWORD': 'password',
  ...
  </code></pre>
  
  <ol start="2">
  <li>Using simple passing unit tests (<code>project/app/tests/test_views.py</code>):</li>
  </ol>
  
  <pre class="lang-py prettyprint-override"><code>...
  class InternalTest(TestCase):
      database = ['default']
  
      def test_something(self):
          pass
  
  class StoredProcedureTest(TestCase):
      databases = ['external']    
  
      def test_one_procedure(self):
          with connections["external"].cursor() as cursor:
              cursor.callproc("some_procedure", [42, ])
          pass
  ...
  </code></pre>
  
  <ol start="3">
  <li><p>If we try the first one with <code>./manage.py test app.tests.test_views.InternalTest</code><br>
  &rarr; <strong>ok</strong>  </p></li>
  <li><p>If we try the other one with <code>./manage.py test app.tests.test_views.StoredProcedureTest</code><br>
  &rarr; <strong>circular dependency issue</strong><br>
  (<code>ImproperlyConfigured: Circular dependency in TEST[DEPENDENCIES]</code>)<br>
  probably because it's skipping the configuration of <strong>default</strong>  </p></li>
  <li>If we try both tests with <code>./manage.py test app.tests.test_views</code>:<br>
  &rarr; <strong>permission denied</strong>  
  
  <pre class="lang-sh prettyprint-override"><code>Creating test database for alias 'default'...
  Creating test database for alias 'external'...
  Got an error creating the test database: permission denied to create database
  </code></pre>
  
  (Django try to create a database <strong>test_db</strong> as the user <strong>external_user</strong>)</li>
  </ol>
  
  <p>We don't really get what Django is trying to do and how to properly configure it.<br>
  If we give the rights to <strong>external_user</strong> to create their own databases:  </p>
  
  <ul>
  <li>the database <strong>test_db</strong> is created by <strong>external_user</strong></li>
  <li>the schema of <strong>default</strong> (sqlite) is created in <strong>test_db</strong> of <strong>external</strong> (postgre)</li>
  <li>the schema of <strong>external</strong> (postgre) is not created in <strong>test_db</strong></li>
  </ul>
  
  <p><strong>Questions</strong>  </p>
  
  <ol>
  <li>Is django able to handle this ?  </li>
  <li>What are we doing wrong ?  </li>
  <li>What is the point of specifying a user <strong>external_user</strong> for <em>TEST</em> if in the end django is using the normal user <strong>external_user</strong> ?   </li>
  <li>Why does it write the schema of <strong>default</strong> in <strong>test_db</strong> ? Is there a way to create only models of some apps in it ?</li>
  <li>Why isn't it able to create the schema of <strong>external</strong> in <strong>test_db</strong> ?</li>
  </ol>
  
  <p>I hope it was described enough. Thank you in advance for your responses =)</p>