1

I am trying to run a simple unit test in Django where I verify the template used. It is very similar to the test in TDD with Django:

from django.test import TestCase

class HomePageTest(TestCase):

    def test_uses_home_template(self):
        response = self.client.get('/')
        self.assertTemplateUsed(response, 'home.html')

The problem is that the view uses a second database and it fails with the error:

django.db.utils.ProgrammingError: relation "public.secondary_db" does not exist

Is it possible to run this test without hitting the database at all? I know that multiple databases in Django is a known issue, but is there a way to avoid this issue all together?

Scott Skiles
  • 3,647
  • 6
  • 40
  • 64
  • Isn't it easier to just configure your test settings to use secondary database? Your question implies that you want overly complicated solution/hack etc to somehow omit database. Do it in simple and obvious way. – Kamil Niski Oct 16 '18 at 16:44
  • Ideally I don't hit the DB at all for a test that does not need the DB. Does that make sense? Do you have any documentation on setting up test settings for secondary DB? I thought unit tests in Django simply did not work with a secondary DB. – Scott Skiles Oct 16 '18 at 16:57
  • Here is relevant paragraph https://docs.djangoproject.com/en/2.1/topics/testing/advanced/#tests-and-multiple-databases – Kamil Niski Oct 16 '18 at 17:00
  • Thank you, @KamilNiski. However, that is only relevant if it is a replica database, is that correct? – Scott Skiles Oct 16 '18 at 17:10
  • No, you can configure django to have multiple writable databases. – Kamil Niski Oct 16 '18 at 17:11
  • Do you have any documentation on that? Or is it similar logic to setting up a replica db in Django? – Scott Skiles Oct 16 '18 at 18:40

0 Answers0