From 4117a2f467d6cc3c8e41ede0b2f9dd43c431ca1e Mon Sep 17 00:00:00 2001 From: Dmitry Afanasyev <71835315+Balshgit@users.noreply.github.com> Date: Sun, 17 Dec 2023 15:20:02 +0300 Subject: [PATCH] fix setuptools issue for python3.12 (#67) * fix setuptools issue for python3.12 * one test file fot tests and ci --- .github/workflows/poetry-test.yml | 2 +- README.md | 2 +- bot_microservice/settings/.env.local.runtests | 23 ------------------- .../{.env.ci.runtests => .env.runtests} | 0 bot_microservice/settings/config.py | 5 +--- .../tests/integration/conftest.py | 14 +++++------ deploy/Dockerfile | 6 +++-- poetry.lock | 17 +++++++------- pyproject.toml | 2 +- 9 files changed, 22 insertions(+), 49 deletions(-) delete mode 100644 bot_microservice/settings/.env.local.runtests rename bot_microservice/settings/{.env.ci.runtests => .env.runtests} (100%) diff --git a/.github/workflows/poetry-test.yml b/.github/workflows/poetry-test.yml index 7dcdb21..ee01a17 100644 --- a/.github/workflows/poetry-test.yml +++ b/.github/workflows/poetry-test.yml @@ -65,7 +65,7 @@ jobs: #---------------------------------------------- - name: Coverage report run: | - cp bot_microservice/settings/.env.ci.runtests bot_microservice/settings/.env + cp bot_microservice/settings/.env.runtests bot_microservice/settings/.env docker compose run bot poetry run bash -c "coverage run -m pytest -vv --exitfirst && poetry run coverage report" #---------------------------------------------- # check dependencies diff --git a/README.md b/README.md index 4069db2..b89a205 100644 --- a/README.md +++ b/README.md @@ -79,7 +79,7 @@ gunicorn main:create_app --workers 10 --bind 0.0.0.0:8000 --worker-class uvicorn ### Run local tests: ```bash cd bot_microservice -LOCALTEST=1 STAGE=runtests poetry run pytest +STAGE=runtests poetry run pytest ``` ### Run tests in docker compose: diff --git a/bot_microservice/settings/.env.local.runtests b/bot_microservice/settings/.env.local.runtests deleted file mode 100644 index 9830aa1..0000000 --- a/bot_microservice/settings/.env.local.runtests +++ /dev/null @@ -1,23 +0,0 @@ -STAGE="runtests" - -# ==== start app settings ==== -APP_HOST="0.0.0.0" -APP_PORT="8000" - -DB_NAME="test_chatgpt.db" - -# ==== telegram settings ==== -TELEGRAM_API_TOKEN="123456789:AABBCCDDEEFFaabbccddeeff-1234567890" -# set to true to start with webhook. Else bot will start on polling method -START_WITH_WEBHOOK="false" - -# ==== domain settings ==== -DOMAIN="http://localhost" -URL_PREFIX= -CHAT_PREFIX="/chat" - -# ==== gpt settings ==== -GPT_BASE_HOST="http://localhost" - -# ==== other settings ==== -USER="web" \ No newline at end of file diff --git a/bot_microservice/settings/.env.ci.runtests b/bot_microservice/settings/.env.runtests similarity index 100% rename from bot_microservice/settings/.env.ci.runtests rename to bot_microservice/settings/.env.runtests diff --git a/bot_microservice/settings/config.py b/bot_microservice/settings/config.py index 8659927..22b172c 100644 --- a/bot_microservice/settings/config.py +++ b/bot_microservice/settings/config.py @@ -20,10 +20,7 @@ DIR_LOGS.mkdir(exist_ok=True) env_path = f"{BASE_DIR}/settings/.env" if environ.get("STAGE") == "runtests": - if "LOCALTEST" in environ: - env_path = f"{BASE_DIR}/settings/.env.local.runtests" - else: - env_path = f"{BASE_DIR}/settings/.env.ci.runtests" + env_path = f"{BASE_DIR}/settings/.env.runtests" load_dotenv(env_path, override=True) diff --git a/bot_microservice/tests/integration/conftest.py b/bot_microservice/tests/integration/conftest.py index 7b87c44..94182e1 100644 --- a/bot_microservice/tests/integration/conftest.py +++ b/bot_microservice/tests/integration/conftest.py @@ -3,9 +3,7 @@ from asyncio import AbstractEventLoop from typing import Any, AsyncGenerator, Generator import pytest -import pytest_asyncio from httpx import AsyncClient -from pytest_asyncio.plugin import SubRequest from sqlalchemy import Engine, create_engine from sqlalchemy.orm import Session, sessionmaker from telegram import Bot, User @@ -45,7 +43,7 @@ def engine(test_settings: AppSettings) -> Generator[Engine, None, None]: engine.dispose() -@pytest_asyncio.fixture(scope="function") +@pytest.fixture() def dbsession(engine: Engine) -> Generator[Session, None, None]: """ Get session to database. @@ -150,7 +148,7 @@ def _get_bot_user(token: str) -> User: # Redefine the event_loop fixture to have a session scope. Otherwise `bot` fixture can't be # session. See https://github.com/pytest-dev/pytest-asyncio/issues/68 for more details. @pytest.fixture(scope="session", autouse=True) -def event_loop(request: SubRequest) -> AbstractEventLoop: +def event_loop() -> AbstractEventLoop: """ Пересоздаем луп для изоляции тестов. В основном нужно для запуска юнит тестов в связке с интеграционными, т.к. без этого pytest зависает. @@ -166,7 +164,7 @@ def bot_info() -> dict[str, Any]: return BotInfoFactory() -@pytest_asyncio.fixture(scope="session") +@pytest.fixture(scope="session") async def bot_application(bot_info: dict[str, Any]) -> AsyncGenerator[Any, None]: # We build a new bot each time so that we use `app` in a context manager without problems application = ApplicationBuilder().bot(make_bot(bot_info)).application_class(PytestApplication).build() @@ -177,7 +175,7 @@ async def bot_application(bot_info: dict[str, Any]) -> AsyncGenerator[Any, None] await application.shutdown() -@pytest_asyncio.fixture(scope="session") +@pytest.fixture(scope="session") async def bot(bot_info: dict[str, Any], bot_application: Any) -> AsyncGenerator[PytestExtBot, None]: """Makes an ExtBot instance with the given bot_info""" async with make_bot(bot_info) as _bot: @@ -185,7 +183,7 @@ async def bot(bot_info: dict[str, Any], bot_application: Any) -> AsyncGenerator[ yield _bot -@pytest_asyncio.fixture(scope="session") +@pytest.fixture(scope="session") async def main_application( bot_application: PytestApplication, test_settings: AppSettings ) -> AsyncGenerator[AppApplication, None]: @@ -203,7 +201,7 @@ async def main_application( await database.drop_database() -@pytest_asyncio.fixture() +@pytest.fixture() async def rest_client(main_application: AppApplication) -> AsyncGenerator[AsyncClient, None]: """ Default http client. Use to test unauthorized requests, public endpoints diff --git a/deploy/Dockerfile b/deploy/Dockerfile index 0dc3c9f..49b9817 100644 --- a/deploy/Dockerfile +++ b/deploy/Dockerfile @@ -59,7 +59,9 @@ COPY ./scripts/start-bot.sh /app/ RUN chmod +x ./start-bot.sh COPY --from=compile-image /app/.venv /app/.venv +ENV PATH="/app/.venv/bin:$PATH" + +# workarroud fo python3.12 and setuptools not found for fastapi users +RUN pip3 uninstall -y setuptools && pip3 install --upgrade setuptools wheel USER ${USER} - -ENV PATH="/app/.venv/bin:$PATH" diff --git a/poetry.lock b/poetry.lock index 3ac8cf0..b3ad630 100644 --- a/poetry.lock +++ b/poetry.lock @@ -2748,21 +2748,20 @@ testing = ["argcomplete", "attrs (>=19.2.0)", "hypothesis (>=3.56)", "mock", "no [[package]] name = "pytest-asyncio" -version = "0.23.2" -description = "Pytest support for asyncio" +version = "0.15.1" +description = "Pytest support for asyncio." optional = false -python-versions = ">=3.8" +python-versions = ">= 3.6" files = [ - {file = "pytest-asyncio-0.23.2.tar.gz", hash = "sha256:c16052382554c7b22d48782ab3438d5b10f8cf7a4bdcae7f0f67f097d95beecc"}, - {file = "pytest_asyncio-0.23.2-py3-none-any.whl", hash = "sha256:ea9021364e32d58f0be43b91c6233fb8d2224ccef2398d6837559e587682808f"}, + {file = "pytest-asyncio-0.15.1.tar.gz", hash = "sha256:2564ceb9612bbd560d19ca4b41347b54e7835c2f792c504f698e05395ed63f6f"}, + {file = "pytest_asyncio-0.15.1-py3-none-any.whl", hash = "sha256:3042bcdf1c5d978f6b74d96a151c4cfb9dcece65006198389ccd7e6c60eb1eea"}, ] [package.dependencies] -pytest = ">=7.0.0" +pytest = ">=5.4.0" [package.extras] -docs = ["sphinx (>=5.3)", "sphinx-rtd-theme (>=1.0)"] -testing = ["coverage (>=6.2)", "hypothesis (>=5.7.1)"] +testing = ["coverage", "hypothesis (>=5.7.1)"] [[package]] name = "pytest-clarity" @@ -4003,4 +4002,4 @@ multidict = ">=4.0" [metadata] lock-version = "2.0" python-versions = "^3.12" -content-hash = "0da89addadf785cd04e9b7bd9c16355c8fa3f6d2f9b0336e100db5717b1c0646" +content-hash = "382d826bbe0346cfd327c9729de156623d7efc916dbf4af18bd51a2cfc0d5e13" diff --git a/pyproject.toml b/pyproject.toml index 7ecdfdd..91a2665 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -58,7 +58,7 @@ types-PyMySQL = "^1.0" types-python-dateutil = "^2.8" pytest = "^7.4" -pytest-asyncio = "^0.23" +pytest-asyncio = "^0.15.1" pytest-deadfixtures = "^2.2" pytest-repeat = "^0.9" pytest-testmon = "^2.1"