简体   繁体   中英

Permission denied to create database testing views in Django

I've tried to test the views of my application in django but with no success.

test_views.py

class TestViews(TestCase):
#test_main_view_GET test is displayed on the main url in main_tour_folder

    def test_destinations_GET(self):
        client = Client()
        response = client.get(reverse('destination'))

        self.assertEquals(response.status_code, 200)
        self.assertTemplateUsed(response, 'tour_store/destinations.html')

I got the message after running the command python manage.py test <app_name> :

RuntimeWarning: Normally Django will use a connection to the 'postgres' database to avoid running initialization queries against t
he production database when it's not needed (for example, when running tests). Django was unable to create a connection to the
 'postgres' database and will use the first PostgreSQL database instead.
  warnings.warn(
Got an error creating the test database: permission denied to create database

Where I don't understand how do I implement this test in my DATABASE in settings.py file.

settings.py:

#imports

import os

//To import the env.py secret_keys
if os.path.exists('env.py'):
    import env

import dj_database_url

... . .

//postgress
if 'DATABASE_URL' in os.environ:
    DATABASES = {
        'default': dj_database_url.parse(os.environ.get('DATABASE_URL'))
    }
else:
#local
    print("Postgres URL not found, using sqlite instead")
    DATABASES = {
        'default': {
            'ENGINE': 'django.db.backends.sqlite3',
            'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
        }
    }

I've seen in one article that I need to create a TEST with NAME dictionary within the DATABASES config but with no success.

I've tried to test the views of my application in django but with no success.

test_views.py

class TestViews(TestCase):
#test_main_view_GET test is displayed on the main url in main_tour_folder

    def test_destinations_GET(self):
        client = Client()
        response = client.get(reverse('destination'))

        self.assertEquals(response.status_code, 200)
        self.assertTemplateUsed(response, 'tour_store/destinations.html')

I got the message after running the command python manage.py test <app_name> :

RuntimeWarning: Normally Django will use a connection to the 'postgres' database to avoid running initialization queries against t
he production database when it's not needed (for example, when running tests). Django was unable to create a connection to the
 'postgres' database and will use the first PostgreSQL database instead.
  warnings.warn(
Got an error creating the test database: permission denied to create database

Where I don't understand how do I implement this test in my DATABASE in settings.py file.

settings.py:

#imports

import os

//To import the env.py secret_keys
if os.path.exists('env.py'):
    import env

import dj_database_url

... . .

//postgress
if 'DATABASE_URL' in os.environ:
    DATABASES = {
        'default': dj_database_url.parse(os.environ.get('DATABASE_URL'))
    }
else:
#local
    print("Postgres URL not found, using sqlite instead")
    DATABASES = {
        'default': {
            'ENGINE': 'django.db.backends.sqlite3',
            'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
        }
    }

I've seen in one article that I need to create a TEST with NAME dictionary within the DATABASES config but with no success.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM