- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
如何为测试目的准确配置数据库。我做错了什么,我已经阅读了网站上的 starlette 配置,但我认为我没有正确理解它。我尝试构建它,但收到以下错误消息:
tests\test_main.py . [ 33%]
tests\test_players\test_players.py FF [100%]
==================================================================== FAILURES ====================================================================
________________________________________________________________ test_post_player ________________________________________________________________
test_app = <httpx.AsyncClient object at 0x0000021B01F8C2E0>
@pytest.mark.asyncio
async def test_post_player(test_app):
test_request_payload = {"first_name": "Jane", "last_name": "Doe", "nationality": "American", "date_of_birth": "1985-04-15"}
test_response_payload = {"first_name": "Jane", "last_name": "Doe", "nationality": "American"}
> response = await test_app.post("/api/players/", data=json.dumps(test_request_payload),)
tests\test_players\test_players.py:12:
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
venv\lib\site-packages\httpx\_client.py:1633: in post
return await self.request(
venv\lib\site-packages\httpx\_client.py:1371: in request
response = await self.send(
venv\lib\site-packages\httpx\_client.py:1406: in send
response = await self._send_handling_auth(
venv\lib\site-packages\httpx\_client.py:1444: in _send_handling_auth
response = await self._send_handling_redirects(
venv\lib\site-packages\httpx\_client.py:1476: in _send_handling_redirects
response = await self._send_single_request(request, timeout)
venv\lib\site-packages\httpx\_client.py:1502: in _send_single_request
(status_code, headers, stream, ext,) = await transport.arequest(
venv\lib\site-packages\httpx\_transports\asgi.py:148: in arequest
await self.app(scope, receive, send)
venv\lib\site-packages\fastapi\applications.py:199: in __call__
await super().__call__(scope, receive, send)
venv\lib\site-packages\starlette\applications.py:111: in __call__
await self.middleware_stack(scope, receive, send)
venv\lib\site-packages\starlette\middleware\errors.py:181: in __call__
raise exc from None
venv\lib\site-packages\starlette\middleware\errors.py:159: in __call__
await self.app(scope, receive, _send)
venv\lib\site-packages\starlette\middleware\cors.py:78: in __call__
await self.app(scope, receive, send)
venv\lib\site-packages\starlette\exceptions.py:82: in __call__
raise exc from None
venv\lib\site-packages\starlette\exceptions.py:71: in __call__
await self.app(scope, receive, sender)
venv\lib\site-packages\starlette\routing.py:566: in __call__
await route.handle(scope, receive, send)
venv\lib\site-packages\starlette\routing.py:227: in handle
await self.app(scope, receive, send)
venv\lib\site-packages\starlette\routing.py:41: in app
response = await func(request)
venv\lib\site-packages\fastapi\routing.py:201: in app
raw_response = await run_endpoint_function(
venv\lib\site-packages\fastapi\routing.py:148: in run_endpoint_function
return await dependant.call(**values)
src\app\api\routers\players\players.py:11: in post_player
player = await crudplayers.create_player(payload)
src\app\crud\crudplayers\crudplayers.py:13: in create_player
await database.execute(query=query)
venv\lib\site-packages\databases\core.py:160: in execute
async with self.connection() as connection:
venv\lib\site-packages\databases\core.py:219: in __aenter__
await self._connection.acquire()
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
self = <databases.backends.postgres.PostgresConnection object at 0x0000021B01F980D0>
async def acquire(self) -> None:
assert self._connection is None, "Connection is already acquired"
> assert self._database._pool is not None, "DatabaseBackend is not running"
E AssertionError: DatabaseBackend is not running
venv\lib\site-packages\databases\backends\postgres.py:148: AssertionError
________________________________________________________________ test_read_player ________________________________________________________________
test_app = <httpx.AsyncClient object at 0x0000021B02374EB0>
@pytest.mark.asyncio
async def test_read_player(test_app):
test_response_payload = {"id": 1, "first_name": "Jane", "last_name": "Doe", "nationality": "American"}
> response = await test_app.get("/api/players/1")
tests\test_players\test_players.py:21:
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
venv\lib\site-packages\httpx\_client.py:1548: in get
return await self.request(
venv\lib\site-packages\httpx\_client.py:1371: in request
response = await self.send(
venv\lib\site-packages\httpx\_client.py:1406: in send
response = await self._send_handling_auth(
venv\lib\site-packages\httpx\_client.py:1444: in _send_handling_auth
response = await self._send_handling_redirects(
venv\lib\site-packages\httpx\_client.py:1476: in _send_handling_redirects
response = await self._send_single_request(request, timeout)
venv\lib\site-packages\httpx\_client.py:1502: in _send_single_request
(status_code, headers, stream, ext,) = await transport.arequest(
venv\lib\site-packages\httpx\_transports\asgi.py:148: in arequest
await self.app(scope, receive, send)
venv\lib\site-packages\fastapi\applications.py:199: in __call__
await super().__call__(scope, receive, send)
venv\lib\site-packages\starlette\applications.py:111: in __call__
await self.middleware_stack(scope, receive, send)
venv\lib\site-packages\starlette\middleware\errors.py:181: in __call__
raise exc from None
venv\lib\site-packages\starlette\middleware\errors.py:159: in __call__
await self.app(scope, receive, _send)
venv\lib\site-packages\starlette\middleware\cors.py:78: in __call__
await self.app(scope, receive, send)
venv\lib\site-packages\starlette\exceptions.py:82: in __call__
raise exc from None
venv\lib\site-packages\starlette\exceptions.py:71: in __call__
await self.app(scope, receive, sender)
venv\lib\site-packages\starlette\routing.py:566: in __call__
await route.handle(scope, receive, send)
venv\lib\site-packages\starlette\routing.py:227: in handle
await self.app(scope, receive, send)
venv\lib\site-packages\starlette\routing.py:41: in app
response = await func(request)
venv\lib\site-packages\fastapi\routing.py:201: in app
raw_response = await run_endpoint_function(
venv\lib\site-packages\fastapi\routing.py:148: in run_endpoint_function
return await dependant.call(**values)
src\app\api\routers\players\players.py:18: in read_player
player = await crudplayers.get_player(player_id)
src\app\crud\crudplayers\crudplayers.py:19: in get_player
player = await database.fetch_one(query=query)
venv\lib\site-packages\databases\core.py:145: in fetch_one
async with self.connection() as connection:
venv\lib\site-packages\databases\core.py:219: in __aenter__
await self._connection.acquire()
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
self = <databases.backends.postgres.PostgresConnection object at 0x0000021B023757F0>
async def acquire(self) -> None:
assert self._connection is None, "Connection is already acquired"
> assert self._database._pool is not None, "DatabaseBackend is not running"
E AssertionError: DatabaseBackend is not running
venv\lib\site-packages\databases\backends\postgres.py:148: AssertionError
================================================================ warnings summary ================================================================
venv\lib\site-packages\aiofiles\os.py:10
venv\lib\site-packages\aiofiles\os.py:10
venv\lib\site-packages\aiofiles\os.py:10
venv\lib\site-packages\aiofiles\os.py:10
venv\lib\site-packages\aiofiles\os.py:10
c:\users\mathiaskolie\pycharmprojects\microservices\livegolf\venv\lib\site-packages\aiofiles\os.py:10: DeprecationWarning: "@coroutine" decorator
is deprecated since Python 3.8, use "async def" instead
def run(*args, loop=None, executor=None, **kwargs):
-- Docs: https://docs.pytest.org/en/stable/warnings.html
============================================================ short test summary info =============================================================
FAILED tests/test_players/test_players.py::test_post_player - AssertionError: DatabaseBackend is not running
FAILED tests/test_players/test_players.py::test_read_player - AssertionError: DatabaseBackend is not running
==================================================== 2 failed, 1 passed, 5 warnings in 7.23s =====================================================
conftest.py 中的 Starlette 配置
import pytest
from httpx import AsyncClient
from starlette.config import environ
from sqlalchemy import create_engine
from sqlalchemy_utils import database_exists, create_database, drop_database
from src.app.domain.models.models import metadata
environ['TESTING'] = 'True'
from src.app.main import app
from src.app.core.config import TEST_DATABASE_URL
@pytest.fixture(scope="function", autouse=True)
async def create_test_database():
url = str(TEST_DATABASE_URL)
engine = create_engine(url)
create_database(url)
metadata.create_all(engine)
yield **#it returns None**
drop_database(url)
@pytest.fixture(scope="function")
async def test_app():
async with AsyncClient(app=app, base_url="http://localhost:8000", headers={"Content-Type": "application/json"},) as ac:
yield ac
数据库配置
from starlette.datastructures import Secret, CommaSeparatedStrings
from starlette.config import Config
from pydantic import BaseSettings
from typing import List
from databases import DatabaseURL
from sqlalchemy import create_engine
class Settings(BaseSettings):
POSTGRES_USER: str
POSTGRES_PASSWORD: str
POSTGRES_HOST:str
POSTGRES_PORT: int
DATABASE_NAME: str
ALLOWED_HOSTS: List[str]
TESTING: str
TESTING_DATABASE_NAME: str
class Config:
env_file = "src/app/core/.env"
config = Config(Settings.Config.env_file)
SECRET_KEY = config('SECRET_KEY', cast=Secret)
TESTING = config('TESTING', cast=bool, default=False)
POSTGRES_USER = config('POSTGRES_USER')
POSTGRES_PASSWORD = config('POSTGRES_PASSWORD', cast=Secret)
POSTGRES_HOST = config('POSTGRES_HOST')
POSTGRES_PORT = config('POSTGRES_PORT', cast=int, default=5432)
DATABASE_NAME = config('DATABASE_NAME')
ALLOWED_HOSTS = config('ALLOWED_HOSTS', cast=CommaSeparatedStrings)
TESTING_DATABASE_NAME = config('TESTING_DATABASE_NAME')
DATABASE_URL = config(
"DATABASE_URL",
default=f"postgresql://{POSTGRES_USER}:{POSTGRES_PASSWORD}@{POSTGRES_HOST}:{POSTGRES_PORT}/{DATABASE_NAME}",
)
TEST_DATABASE_URL = config(
"TEST_DATABASE_URL",
default=f"postgresql://{POSTGRES_USER}:{POSTGRES_PASSWORD}@{POSTGRES_HOST}:{POSTGRES_PORT}/{TESTING_DATABASE_NAME}",
)
engine = create_engine(DATABASE_URL, pool_size=3, max_overflow=0)
最佳答案
我找到了这个解决方案 here :
“在上下文管理器中使用您的 TestClient”
@pytest.fixture(scope="module")
def client() -> Generator:
with TestClient(api) as c:
yield c
好作品对我有同样的麻烦
关于python - Fastapi 数据库测试隔离与 starlette 配置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65470896/
我的公司有一个 Web 应用程序,其中包含纯 JavaScript,它以自己的方式使用 $ -“美元符号”,如下所示: function $(e) { return document.getE
doc说“这意味着对单个节点上单个分区内的行的写入仅对执行操作的客户端可见”。 如果有另一个 client2 在同一个分区和同一个节点上执行操作,那么文档中提到的“THE CLIENT”执行的写入是否
只是一个想法,但在 DIV 上使用 IFRAME 本质上会使该元素与窗口隔离,从而降低 IFRAME 中运行的脚本速度 不会影响其他框架/窗口吗? 最佳答案 是的,对于第一部分,iframe 会“某种
我有以下模型 Inventory [product_name, quantity, reserved_quantity] 有数据 [Shirt, 1, 0] [Shorts, 10, 0] 如果以下代
我面临的情况如下。因为ThreadPool是每个进程1个实例,所以我的问题是是否会在 3秒后取消方法2排队的任务? http request comes in *method 1 gets execu
我希望在 Dart 中创建一个 Isolate,我可以通过编程方式暂停和恢复。这是我使用的代码。 import 'dart:io'; import 'dart:isolate'; void main(
我想编写一个具有隔离作用域的指令,但也希望使该作用域可用于父作用域的 Controller 。我找到了这个解决方案: app.directive('popupbutton', [functi
我有一个像这样的 JSON 对象: [ {"Subject": "Physics", "Active": 48, "Date": "2020-01-22T00:00:00Z"}, {"Su
我正在使用 Elixir 自动执行用 Gherkin 编写的规范中定义的验收测试。一种方法是使用名为 Cabbage 的 ExUnit 插件。 . 现在,ExUnit 似乎提供了一个在任何单个测试之前
我被要求为多个用户配置一个带有 docker 的 ubuntu 18.04 服务器。 目的: 我们有多个编写测试用例的测试人员。但是我们的笔记本电脑速度不够快,无法在 docker 环境中构建项目和运
我一直在网上寻找完整的解决方案,但到目前为止,我只能找到不合适的部分。 我正在寻找一个可以查看图像文件、循环遍历文件并隔离 Sprite 然后保存它们的程序。之后,我需要一种方法来重新访问该 Spri
我想知道如何隔离 JavaScript 函数的执行以避免浏览器崩溃。 示例:如果我想在控制台中输出一个包含大约 10k 元素的关联数组,浏览器将停止响应。我怎样才能避免这种情况? 最佳答案 解决方案是
我必须向我的数据库添加大量信息。添加此信息大约需要 5-7 分钟。我需要添加交易。 我试过这个: try { db.Connection.Open(); db.Transaction
我有 6 个 iframe,它们来自同一个域,但具有不同的 url 和子目录。他们都使用 html header “set-cookie”设置了一个名称相同但值不同的 cookie。我需要将它们的 c
我正在编写一个代码,它基本上读取一个文本文件(表格格式)并检查该文件是否包含预期的数据类型。为此我写了下面的课。 示例文件应该是这样的。 name age abc 20 xyz
我有一个表,线程。我有一个表,thread_participants。我正在尝试隔离与特定 thread_id 和特定 thread_participants.user_id 标识的行。 例如,如果
我有一个非常实际的问题。我的数据库中有大约 400 篇文章,在这些文章中我有其他文章的链接。在转换过程中,链接被破坏。我们在 CMS 中手动插入新的菜单链接项。我想制作一个脚本来查找(文章)id 并将
关闭。这个问题是off-topic .它目前不接受答案。 想改进这个问题吗? Update the question所以它是on-topic用于堆栈溢出。 关闭 10 年前。 Improve thi
我在想是否可以在 postgres 数据库(高于 8.3 的版本)上创建一个只能访问特定指定模式的用户。问题是,在我的数据库中我有一些模式。如果我撤销某个用户对除一个模式之外的所有模式的所有特权,他仍
我有两组点,一组来自分析,另一组用于分析数据的后处理结果。 黑色的分析数据是散乱的。 用于结果的点是红色的。 这是同一地 block 上的两组: 我遇到的问题是:我将插值到红点上,但如您所见,有些红点
我是一名优秀的程序员,十分优秀!