【问题标题】:Getting Error when running Django Test on Heroku CI在 Heroku CI 上运行 Django 测试时出错
【发布时间】:2020-06-30 06:18:44
【问题描述】:

我正在使用 DRF,并且测试是为 API 编写的。我想在 heroku 上运行这些测试,并在我的开发环境管道中使用 CI。

在配置中使用默认 SQLlite db 时,我得到的错误 -

通常,Django 将使用与“postgres”数据库的连接来避免在不需要时(例如,在运行测试时)对生产数据库运行初始化查询。 Django 无法创建到“postgres”数据库的连接,将使用第一个 PostgreSQL 数据库。

我的代码和配置

test_*.py

class TestUser(APITestCase):
    def setUp(self):
        ...

    def test_user(self):
    ...

base.py 文件

db_config = dj_database_url.config(conn_max_age=600, ssl_require=False)
DATABASES_AVAILABLE = {
    'test': db_config,
    'sqlite': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
    },
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
    },
}

database = os.environ.get('DJANGO_DATABASE_TEST', 'sqlite')
DATABASES = {
    'default': DATABASES_AVAILABLE[database]
}

# Database Configuration Ends
django_heroku.settings(locals())

在 Heroku CI 配置中,我有

DJANGO_DATABASE_TEST : test

app.json

{
  "buildpacks": [{ "url": "heroku/python" }],
  "environments": {
    "test": {
      "env": { "POSTGRESQL_VERSION": "10" },
      "addons": ["heroku-postgresql:in-dyno"],
      "scripts": {
        "test": "./manage.py migrate && ./manage.py  test"
      }
    }
  }
}

我在 heroku ci 中遇到的错误

django.db.utils.OperationalError: server does not support SSL, but SSL was required

更新 1:

django_heroku.settings(locals())


db_config = dj_database_url.config(conn_max_age=600, ssl_require=False)
DATABASES_AVAILABLE = {
    'test': db_config,
    'sqlite': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
    },
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
    },
}

database = os.environ.get('DJANGO_DATABASE_TEST', 'sqlite')
DATABASES = {
    'default': DATABASES_AVAILABLE[database]
}

如果我将 django_heroku.settings(locals(), databases=True) 移动到 DATABASE 之前,则会收到此错误

psycopg2.errors.UndefinedObject: role "postgres" does not exist

我提到的资源 -

  1. https://github.com/heroku/django-heroku/issues/17, 2) https://devcenter.heroku.com/articles/heroku-postgresql, 3) https://devcenter.heroku.com/articles/sqlite3

【问题讨论】:

  • 您的部分问题可能是您使用的是django-heroku,它已被废弃、存档且2 年内未更新。
  • @TomCarrick 您是否推荐任何其他方式来运行测试?
  • 我会先拿出 django-heroku 看看它是否有效。我认为您应该尝试一下,如果仍然无法正常工作,请返回,因为人们不想帮助您支持不受支持的库。

标签: django postgresql heroku heroku-ci


【解决方案1】:

经过大量的实验和阅读,我有一个解决方案

在 Django 中创建了不同的环境文件 - developproductionsci

ci环境中我没有包含django_heroku.settings(locals()),只添加了以下配置

DEBUG = True
APPEND_SLASH = True
SECURE_SSL_REDIRECT = False

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql_psycopg2',
        'NAME': 'postgres_buildpack_db',
    }
}

在 app.json 文件中

{
  "buildpacks": [{ "url": "heroku/python" }],
  "environments": {
    "test": {
      "env": { "POSTGRESQL_VERSION": "10" },
      "addons": ["heroku-postgresql:in-dyno"],
      "scripts": {
        "test-setup": "./manage.py migrate && ./manage.py createsuperuser --noinput --username='admin' --email='admin@admin.com'",
        "test": "./manage.py migrate"
      }
    }
  }
}

django_heroku.settings(locals()) 将 Db SLL 连接设置为 true,而 indyno db 不支持 SSL,因此在 CI env 中将其删除。

最后所有测试都在 Heroku-CI 中运行。呸!

【讨论】:

    猜你喜欢
    • 2014-07-07
    • 2020-07-27
    • 2012-11-22
    • 2016-06-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-04-09
    相关资源
    最近更新 更多