postgresql - 使用 FastAPI 在 github 操作上运行测试失败,因为它首先尝试连接到托管数据库
问题描述
我正在尝试使用 github 操作设置 CI 管道,但它失败了。我认为它失败的原因是因为 FastAPI 尝试首先与生产数据库建立连接,然后运行数据库覆盖。另一种理论是容器可以被“沙盒化”,这会阻止与生产数据库的连接。
这是我的工作流程代码:
name: Python application
on:
push:
branches: [ main ]
pull_request:
branches: [ main ]
jobs:
build:
runs-on: ubuntu-latest
# Service containers to run with `container-job`
services:
# Label used to access the service container
postgres:
# Docker Hub image
image: postgres
# Provide the password for postgres
env:
POSTGRES_USER: test
POSTGRES_DB: test
POSTGRES_PASSWORD: testing
ports:
- 5432:5432
# Set health checks to wait until postgres has started
options: >-
--health-cmd pg_isready
--health-interval 10s
--health-timeout 5s
--health-retries 5
steps:
- uses: actions/checkout@v2
- name: Set up Python 3.9
uses: actions/setup-python@v2
with:
python-version: 3.9
- name: Install dependencies
run: |
cd backend
python -m pip install --upgrade pip
pip install flake8 pytest
if [ -f requirements.txt ]; then pip install -r requirements.txt; fi
- name: Lint with flake8
run: |
# stop the build if there are Python syntax errors or undefined names
flake8 . --count --select=E9,F63,F7,F82 --show-source --statistics
# exit-zero treats all errors as warnings. The GitHub editor is 127 chars wide
flake8 . --count --exit-zero --max-complexity=10 --max-line-length=127 --statistics
- name: Test with pytest
env:
NAME: ${{ secrets.NAME }}
USERNAME: ${{ secrets.USERNAME }}
PASSWORD: ${{ secrets.PASSWORD }}
PORT: ${{ secrets.PORT }}
URL: ${{ secrets.URL }}
run: |
cd backend
python -m pytest tests/
这是在 FastAPI 中设置测试环境的代码:
from fastapi.testclient import TestClient
from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker
from database import Base, get_db
from main import app
import json
SQLALCHEMY_DATABASE_URL = 'postgresql://test:testing@localhost:5432/test'
engine = create_engine(
SQLALCHEMY_DATABASE_URL, connect_args={}
)
TestingSessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine)
Base.metadata.drop_all(bind=engine)
Base.metadata.create_all(bind=engine)
def override_get_db():
try:
db = TestingSessionLocal()
yield db
finally:
db.close()
app.dependency_overrides[get_db] = override_get_db
client = TestClient(app)
这是错误:
E sqlalchemy.exc.OperationalError: (psycopg2.OperationalError) could not connect to server: No such file or directory
E Is the server running locally and accepting
E connections on Unix domain socket "/var/run/postgresql/.s.PGSQL.5432"?
从错误堆栈跟踪中我可以看到错误以我的代码中的这一行开头,女巫是与生产数据库连接的代码的一部分。这位于 main.py
models.Base.metadata.create_all(bind=engine)
运行服务器工作正常并且在本地运行测试也工作正常,只有通过 github 操作运行测试时才会出现问题。FastAPI 不应该先运行覆盖吗?
解决方案
推荐阅读
- r - 将列变量的 NA 值替换为每个变量的平均值
- hadoop-yarn - 有没有办法为 yarn2 monorepo 中的一个包安装依赖项?
- azure-active-directory - 是否可以使用 Azure AD 作为身份提供者从 Azure AD B2C 登录获取 MSGraph 访问令牌?
- c++ - C++20 #import ""-statement:是否可以使用多个预处理的头文件
- awk - 使用 awk 过滤返回空文件
- go - 为什么我无法在服务器端获取上下文值
- php - Foreach 只返回第一条记录,停止
- c - 基于 TLS 的套接字
- javascript - 如何使用 setState 回调来更新 TextInput 的 value 属性
- node.js - 将猫鼬字段设置为字符串或数字类型