mirror of
https://github.com/imartinez/privateGPT.git
synced 2025-09-18 08:09:49 +00:00
fix: Remove global state (#1216)
* Remove all global settings state * chore: remove autogenerated class * chore: cleanup * chore: merge conflicts
This commit is contained in:
15
tests/fixtures/fast_api_test_client.py
vendored
15
tests/fixtures/fast_api_test_client.py
vendored
@@ -1,15 +1,14 @@
|
||||
import pytest
|
||||
from fastapi import FastAPI
|
||||
from fastapi.testclient import TestClient
|
||||
|
||||
from private_gpt.main import app
|
||||
from private_gpt.launcher import create_app
|
||||
from tests.fixtures.mock_injector import MockInjector
|
||||
|
||||
|
||||
@pytest.fixture()
|
||||
def current_test_app() -> FastAPI:
|
||||
return app
|
||||
def test_client(request: pytest.FixtureRequest, injector: MockInjector) -> TestClient:
|
||||
if request is not None and hasattr(request, "param"):
|
||||
injector.bind_settings(request.param or {})
|
||||
|
||||
|
||||
@pytest.fixture()
|
||||
def test_client() -> TestClient:
|
||||
return TestClient(app)
|
||||
app_under_test = create_app(injector.test_injector)
|
||||
return TestClient(app_under_test)
|
||||
|
9
tests/fixtures/mock_injector.py
vendored
9
tests/fixtures/mock_injector.py
vendored
@@ -1,10 +1,13 @@
|
||||
from collections.abc import Callable
|
||||
from typing import Any
|
||||
from unittest.mock import MagicMock
|
||||
|
||||
import pytest
|
||||
from injector import Provider, ScopeDecorator, singleton
|
||||
|
||||
from private_gpt.di import create_application_injector
|
||||
from private_gpt.settings.settings import Settings, unsafe_settings
|
||||
from private_gpt.settings.settings_loader import merge_settings
|
||||
from private_gpt.utils.typing import T
|
||||
|
||||
|
||||
@@ -24,6 +27,12 @@ class MockInjector:
|
||||
self.test_injector.binder.bind(interface, to=mock, scope=scope)
|
||||
return mock # type: ignore
|
||||
|
||||
def bind_settings(self, settings: dict[str, Any]) -> Settings:
|
||||
merged = merge_settings([unsafe_settings, settings])
|
||||
new_settings = Settings(**merged)
|
||||
self.test_injector.binder.bind(Settings, new_settings)
|
||||
return new_settings
|
||||
|
||||
def get(self, interface: type[T]) -> T:
|
||||
return self.test_injector.get(interface)
|
||||
|
||||
|
@@ -8,7 +8,7 @@ NOTE: We are not testing the switch based on the config in
|
||||
from typing import Annotated
|
||||
|
||||
import pytest
|
||||
from fastapi import Depends, FastAPI
|
||||
from fastapi import Depends
|
||||
from fastapi.testclient import TestClient
|
||||
|
||||
from private_gpt.server.utils.auth import (
|
||||
@@ -29,15 +29,16 @@ def _copy_simple_authenticated(
|
||||
|
||||
|
||||
@pytest.fixture(autouse=True)
|
||||
def _patch_authenticated_dependency(current_test_app: FastAPI):
|
||||
def _patch_authenticated_dependency(test_client: TestClient):
|
||||
# Patch the server to use simple authentication
|
||||
current_test_app.dependency_overrides[authenticated] = _copy_simple_authenticated
|
||||
|
||||
test_client.app.dependency_overrides[authenticated] = _copy_simple_authenticated
|
||||
|
||||
# Call the actual test
|
||||
yield
|
||||
|
||||
# Remove the patch for other tests
|
||||
current_test_app.dependency_overrides = {}
|
||||
test_client.app.dependency_overrides = {}
|
||||
|
||||
|
||||
def test_default_auth_working_when_enabled_401(test_client: TestClient) -> None:
|
||||
@@ -50,6 +51,6 @@ def test_default_auth_working_when_enabled_200(test_client: TestClient) -> None:
|
||||
assert response_fail.status_code == 401
|
||||
|
||||
response_success = test_client.get(
|
||||
"/v1/ingest/list", headers={"Authorization": settings.server.auth.secret}
|
||||
"/v1/ingest/list", headers={"Authorization": settings().server.auth.secret}
|
||||
)
|
||||
assert response_success.status_code == 200
|
||||
|
@@ -1,5 +1,12 @@
|
||||
from private_gpt.settings.settings import settings
|
||||
from private_gpt.settings.settings import Settings, settings
|
||||
from tests.fixtures.mock_injector import MockInjector
|
||||
|
||||
|
||||
def test_settings_are_loaded_and_merged() -> None:
|
||||
assert settings.server.env_name == "test"
|
||||
assert settings().server.env_name == "test"
|
||||
|
||||
|
||||
def test_settings_can_be_overriden(injector: MockInjector) -> None:
|
||||
injector.bind_settings({"server": {"env_name": "overriden"}})
|
||||
mocked_settings = injector.get(Settings)
|
||||
assert mocked_settings.server.env_name == "overriden"
|
||||
|
10
tests/ui/test_ui.py
Normal file
10
tests/ui/test_ui.py
Normal file
@@ -0,0 +1,10 @@
|
||||
import pytest
|
||||
from fastapi.testclient import TestClient
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"test_client", [{"ui": {"enabled": True, "path": "/ui"}}], indirect=True
|
||||
)
|
||||
def test_ui_starts_in_the_given_endpoint(test_client: TestClient) -> None:
|
||||
response = test_client.get("/ui")
|
||||
assert response.status_code == 200
|
Reference in New Issue
Block a user