From 0a50914fa85e85a5e466708a3d0bda79e9fdc6e6 Mon Sep 17 00:00:00 2001 From: Yang Wang Date: Thu, 28 Aug 2025 11:12:13 -0700 Subject: [PATCH 1/3] web api forward fix --- .../api}/background_submission_manager.py | 0 src/kernelbot/api/main.py | 2 +- src/kernelbot/main.py | 2 +- src/libkernelbot/api_utils.py | 0 tests/conftest.py | 34 +++++++------------ tests/test_background_submission_manager.py | 4 ++- tests/test_leaderboard_db.py | 2 ++ 7 files changed, 19 insertions(+), 25 deletions(-) rename src/{libkernelbot => kernelbot/api}/background_submission_manager.py (100%) create mode 100644 src/libkernelbot/api_utils.py diff --git a/src/libkernelbot/background_submission_manager.py b/src/kernelbot/api/background_submission_manager.py similarity index 100% rename from src/libkernelbot/background_submission_manager.py rename to src/kernelbot/api/background_submission_manager.py diff --git a/src/kernelbot/api/main.py b/src/kernelbot/api/main.py index d9d0ae9b..4039fb51 100644 --- a/src/kernelbot/api/main.py +++ b/src/kernelbot/api/main.py @@ -10,8 +10,8 @@ from fastapi import Depends, FastAPI, Header, HTTPException, Request, UploadFile from fastapi.responses import JSONResponse, StreamingResponse +from kernelbot.api.background_submission_manager import BackgroundSubmissionManager from libkernelbot.backend import KernelBackend -from libkernelbot.background_submission_manager import BackgroundSubmissionManager from libkernelbot.consts import SubmissionMode from libkernelbot.db_types import IdentityType from libkernelbot.leaderboard_db import LeaderboardDB, LeaderboardRankedEntry diff --git a/src/kernelbot/main.py b/src/kernelbot/main.py index e0411096..32f63a8b 100644 --- a/src/kernelbot/main.py +++ b/src/kernelbot/main.py @@ -13,9 +13,9 @@ from discord.ext import commands from env import env, init_environment +from kernelbot.api.background_submission_manager import BackgroundSubmissionManager from libkernelbot import consts from libkernelbot.backend import KernelBackend -from libkernelbot.background_submission_manager import BackgroundSubmissionManager from libkernelbot.launchers import GitHubLauncher, ModalLauncher from libkernelbot.utils import setup_logging diff --git a/src/libkernelbot/api_utils.py b/src/libkernelbot/api_utils.py new file mode 100644 index 00000000..e69de29b diff --git a/tests/conftest.py b/tests/conftest.py index f296bbc9..e0fc0b0f 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -1,33 +1,23 @@ -import os import subprocess import time from pathlib import Path import pytest -REQUIRED = { - "DISCORD_TOKEN": "dummy", - "GITHUB_TOKEN": "dummy", - "GITHUB_REPO": "dummy", -} - -for k, v in REQUIRED.items(): - os.environ.setdefault(k, v) - -@pytest.fixture(scope="session", autouse=True) -def _restore_env(): - old = {k: os.environ.get(k) for k in REQUIRED} - try: - yield - finally: - for k, v in old.items(): - if v is None: - os.environ.pop(k, None) - else: - os.environ[k] = v - DATABASE_URL = "postgresql://postgres:postgres@localhost:5433/clusterdev" +@pytest.fixture +def with_required_env(monkeypatch): + REQUIRED = { + "DISCORD_TOKEN": "dummy", + "GITHUB_TOKEN": "dummy", + "GITHUB_REPO": "dummy", + } + + for k, v in REQUIRED.items(): + monkeypatch.setenv(k, v) + yield + @pytest.fixture(scope="module") def docker_compose(project_root: Path): diff --git a/tests/test_background_submission_manager.py b/tests/test_background_submission_manager.py index ac038972..7c81c839 100644 --- a/tests/test_background_submission_manager.py +++ b/tests/test_background_submission_manager.py @@ -4,10 +4,12 @@ import pytest -from libkernelbot.background_submission_manager import BackgroundSubmissionManager from libkernelbot.consts import SubmissionMode from libkernelbot.submission import ProcessedSubmissionRequest +from kernelbot.api.background_submission_manager import BackgroundSubmissionManager +# only replace env vars in this test file +pytestmark = pytest.mark.usefixtures("with_required_env") @pytest.fixture def mock_backend(): diff --git a/tests/test_leaderboard_db.py b/tests/test_leaderboard_db.py index 753c88f0..54023def 100644 --- a/tests/test_leaderboard_db.py +++ b/tests/test_leaderboard_db.py @@ -9,6 +9,8 @@ from libkernelbot.db_types import IdentityType from libkernelbot.utils import KernelBotError +# only replace env vars in this test file +pytestmark = pytest.mark.usefixtures("with_required_env") def _submit_leaderboard(database, task_directory): """ From b94e093c3e43764bea176437b222319bdfa7fb31 Mon Sep 17 00:00:00 2001 From: Yang Wang Date: Thu, 28 Aug 2025 11:30:34 -0700 Subject: [PATCH 2/3] fix unittest --- tests/test_background_submission_manager.py | 2 +- tests/test_validate_user_header.py | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/tests/test_background_submission_manager.py b/tests/test_background_submission_manager.py index 7c81c839..ff023594 100644 --- a/tests/test_background_submission_manager.py +++ b/tests/test_background_submission_manager.py @@ -4,9 +4,9 @@ import pytest +from kernelbot.api.background_submission_manager import BackgroundSubmissionManager from libkernelbot.consts import SubmissionMode from libkernelbot.submission import ProcessedSubmissionRequest -from kernelbot.api.background_submission_manager import BackgroundSubmissionManager # only replace env vars in this test file pytestmark = pytest.mark.usefixtures("with_required_env") diff --git a/tests/test_validate_user_header.py b/tests/test_validate_user_header.py index 54ab484d..70db9447 100644 --- a/tests/test_validate_user_header.py +++ b/tests/test_validate_user_header.py @@ -6,6 +6,7 @@ from kernelbot.api.main import validate_user_header from libkernelbot.db_types import IdentityType +pytestmark = pytest.mark.usefixtures("with_required_env") class DummyDBCtx: def __init__(self, to_return=None, to_raise: Optional[Exception] = None): From cf012c1378cd6a4b9a0c2df020e1e941b9afeeaf Mon Sep 17 00:00:00 2001 From: Yang Wang Date: Thu, 28 Aug 2025 11:41:28 -0700 Subject: [PATCH 3/3] fix unittest --- .coverage | Bin 0 -> 53248 bytes coverage.xml | 1135 +++++++++++++++++++ tests/conftest.py | 8 +- tests/test_background_submission_manager.py | 4 +- tests/test_leaderboard_db.py | 2 +- tests/test_validate_user_header.py | 2 +- 6 files changed, 1143 insertions(+), 8 deletions(-) create mode 100644 .coverage create mode 100644 coverage.xml diff --git a/.coverage b/.coverage new file mode 100644 index 0000000000000000000000000000000000000000..95fd2a2583abedd27687bb1c3b5c31f4c8b2265e GIT binary patch literal 53248 zcmeI4ZEO_B8OLXD?{04o`zDmqt7VKAq+ne9QiqC^hL<|jMW|5-4ayhN9D8?bd&}Le z+1&%ICSXG(nkfBLRZ*)dgnp<}RcckyCQYi=q@_|DT2hioRH4{Oo5n;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h|0|l@Q=2(y{U2W>Z>SZhS^vkHh%yno>n3ll;n@f)S{_|i9<-WjVLuKeOBm2X}>aJaJm zs(kQAe>uG;t|=sQud?&cul`YDZ&iwwA6w(BFE#kk=-I=SOS@KOcBSGZF+cwBXywYQ zm9wj*;W@FGr0!l6dt`XesbgzKPM`euh3;2ouf3LQUiH*Gbrq|>tN+s8)WP@O;ODGA z9DCsPqeJ_YXL(FwCuPRYjVyiaAMwgQ>A$BoAFCYQ-}u`87ZxL{}?^j&C{EeUfGqy~ooRU1c+#Y(d{o2LvjlFs0=TBsQJ9h2F$mL^a zE>=cPo%!_(g|SWNcRYPz^vLq@m3`kmv@HE}v>`&}J{LJMKDhJZqu*Zo+-Ky2<0~p_ z*mFZ#Op2;%N)pfiRsK81FY|N!1O6_(2k@`_D1U{L5I_I~KmY_l00ck)1V8`;KmY_l z00eFw0^;6=q^jN~k?=N(;%(w_Zxf4on`qSA$g;PIM51&9zH$yiSP-AOHd&00JNY0w4eaAOHd&00JNY0=FOmIZ3bd bmsJrZM5Kr)E~1!-q9T$-6cLel|Ns90 + + + + + src/libkernelbot + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/tests/conftest.py b/tests/conftest.py index e0fc0b0f..2ddee3af 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -7,11 +7,11 @@ DATABASE_URL = "postgresql://postgres:postgres@localhost:5433/clusterdev" @pytest.fixture -def with_required_env(monkeypatch): +def with_dummy_env(monkeypatch): REQUIRED = { - "DISCORD_TOKEN": "dummy", - "GITHUB_TOKEN": "dummy", - "GITHUB_REPO": "dummy", + "DISCORD_TOKEN": "dummy1", + "GITHUB_TOKEN": "dummy1", + "GITHUB_REPO": "dummy1", } for k, v in REQUIRED.items(): diff --git a/tests/test_background_submission_manager.py b/tests/test_background_submission_manager.py index ff023594..64e58f67 100644 --- a/tests/test_background_submission_manager.py +++ b/tests/test_background_submission_manager.py @@ -1,7 +1,7 @@ import asyncio import datetime from unittest import mock - +import os import pytest from kernelbot.api.background_submission_manager import BackgroundSubmissionManager @@ -9,7 +9,7 @@ from libkernelbot.submission import ProcessedSubmissionRequest # only replace env vars in this test file -pytestmark = pytest.mark.usefixtures("with_required_env") +pytestmark = pytest.mark.usefixtures("with_dummy_env") @pytest.fixture def mock_backend(): diff --git a/tests/test_leaderboard_db.py b/tests/test_leaderboard_db.py index 54023def..8340bbbd 100644 --- a/tests/test_leaderboard_db.py +++ b/tests/test_leaderboard_db.py @@ -10,7 +10,7 @@ from libkernelbot.utils import KernelBotError # only replace env vars in this test file -pytestmark = pytest.mark.usefixtures("with_required_env") +pytestmark = pytest.mark.usefixtures("with_dummy_env") def _submit_leaderboard(database, task_directory): """ diff --git a/tests/test_validate_user_header.py b/tests/test_validate_user_header.py index 70db9447..7d0a1a34 100644 --- a/tests/test_validate_user_header.py +++ b/tests/test_validate_user_header.py @@ -6,7 +6,7 @@ from kernelbot.api.main import validate_user_header from libkernelbot.db_types import IdentityType -pytestmark = pytest.mark.usefixtures("with_required_env") +pytestmark = pytest.mark.usefixtures("with_dummy_env") class DummyDBCtx: def __init__(self, to_return=None, to_raise: Optional[Exception] = None):