【问题标题】:django postgresql tests.py RuntimeWarning errordjango postgresql tests.py RuntimeWarning 错误
【发布时间】:2019-10-12 13:44:15
【问题描述】:

我有一个 Django 应用程序,它可以在线向我的 postgresql 数据库添加和显示内容,网址为 evilsql.com。我的项目文件设置如下所示:

website/
    website/
    music/
    playlist/  
            __pycache__/
            migrations/
            static/
            templates/
            __init__.py
            admin.py
            apps.py
            models.py
            tests.py
            urls.py
            views.py
    .coverage
    db.sqlite3
    manage.py              

我的项目现在可以运行,当我运行服务器并转到 /playlist/ 时,它会正确显示内容并很好地连接到我的 postgresql 数据库。

我的 settings.py DATABASES 对象如下所示:

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql',
        'NAME': 'kxvmghva',
        'USER': 'kxvmghva',
        'PASSWORD': '--an actual password---',
        'HOST': 'raja.db.elephantsql.com', 
        'PORT': '5432',
    }
}

现在我正在尝试在我的 playlist/tests.py 文件中编写测试用例,但是当我尝试运行这些测试时出现错误。

我正在尝试运行的测试文件 /playlist/tests.py

from django.test import TestCase
from .models import plays
from django.utils import timezone


class AnimalTestCase(TestCase):
    def setUp(self):
        print("setup")
        #Animal.objects.create(name="lion", sound="roar")
        #Animal.objects.create(name="cat", sound="meow")

    def test_animals_can_speak(self):
        """Animals that can speak are correctly identified"""
        print("test")
        #lion = Animal.objects.get(name="lion")
        #cat = Animal.objects.get(name="cat")
        #self.assertEqual(lion.speak(), 'The lion says "roar"')
        #self.assertEqual(cat.speak(), 'The cat says "meow"')

当我运行命令“python manage.py test playlist”时,出现以下错误:

C:\Users\marti\Documents\kexp\website>python manage.py test playlist
Creating test database for alias 'default'...
C:\Python36-32\lib\site-packages\django\db\backends\postgresql\base.py:267: RuntimeWarning: Normally Django will use a connection to the 'postgres' database to avoid running initialization queries against the 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 default database instead.
  RuntimeWarning
Got an error creating the test database: permission denied to create database

Type 'yes' if you would like to try deleting the test database 'test_kxvmghva', or 'no' to cancel:

如果我输入“是”,则会导致此错误:

Destroying old test database for alias 'default'...
Got an error recreating the test database: database "test_kxvmghva" does not exist

我一直在尝试通过在线搜索来解决此错误,并尝试了诸如授予我的用户“kxvmghva”CREATEDB 权限以及在我的大象数据库中运行此行:

将 SCHEMA public 中所有表的所有权限授予 kxvmghva;

但是当我尝试为我的播放列表/应用程序运行 tests.py 文件时,我仍然遇到这些错误。这是我第一次在 django 中为 postgresql 数据库设置测试用例,我非常感谢任何帮助或指导。谢谢。

【问题讨论】:

  • 也许你可以试试:'ENGINE': 'django.db.backends.postgresql_psycopg2'
  • python manage.py test playlist --keepdb 至少摆脱了提示。

标签: python django postgresql unit-testing


【解决方案1】:

我想您用于数据库的计划是免费/共享的,以防您无权创建其他数据库。

这不是 Django 问题,而是您使用的服务的限制。

【讨论】:

    【解决方案2】:

    正如first answer 中所指出的,ElephantSQL 免费套餐(我想您正在使用这个)是共享的。在该服务器中,它们有多个 DB,因此您可以访问其中一个而无法创建另一个。

    我尝试创建instance just for tests,但它也失败了,因为“在 PostgreSQL 上,用户还需要对内置 postgres 数据库的读取访问权限。” (来自django docs)。

    所以,我的解决方案,并且理解它不是最佳实践(因为您将有不同的测试环境)正在更换引擎进行测试:

    DATABASES = {
        'default': {
            'ENGINE': 'django.db.backends.sqlite3',
        },
    }
    

    此配置有效,因为(来自django docs):

    使用 SQLite 时,测试默认使用内存数据库(即,数据库将在内存中创建,完全绕过文件系统!)

    两个音符:

    • 使用 GitHub 操作或类似解决方案在每次提交中针对 PostgreSQL 进行测试(这样可以降低使用不同测试和产品数据库的危险)
    • 拥有不同设置配置的最简单方法是使用单独的文件。我遵循cookiecutter-django中的逻辑

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-04-03
      • 2021-06-18
      • 1970-01-01
      • 1970-01-01
      • 2019-06-11
      • 2016-03-14
      • 1970-01-01
      • 2012-05-07
      相关资源
      最近更新 更多