initial commit
This commit is contained in:
0
backend/tests/__init__.py
Normal file
0
backend/tests/__init__.py
Normal file
22
backend/tests/test_main.py
Normal file
22
backend/tests/test_main.py
Normal file
@@ -0,0 +1,22 @@
|
||||
import pytest
|
||||
from fastapi.testclient import TestClient
|
||||
|
||||
from app import app
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def client():
|
||||
with TestClient(app) as client:
|
||||
yield client
|
||||
|
||||
|
||||
def test_home_route(client):
|
||||
response = client.get("/")
|
||||
assert response.status_code == 200
|
||||
assert response.json() == {"message": "Hello"}
|
||||
|
||||
|
||||
def test_about_route(client):
|
||||
response = client.get("/about")
|
||||
assert response.status_code == 200
|
||||
assert response.json() == {"message": "This is the about page."}
|
||||
45
backend/tests/test_settings.py
Normal file
45
backend/tests/test_settings.py
Normal file
@@ -0,0 +1,45 @@
|
||||
from pathlib import Path
|
||||
|
||||
import pytest
|
||||
|
||||
from settings import load_settings
|
||||
|
||||
|
||||
def test_default_settings(monkeypatch: pytest.MonkeyPatch) -> None:
|
||||
monkeypatch.delenv("CONFIG_FILE", raising=False)
|
||||
for k in ("HOST", "PORT", "WORKERS", "LOG_LEVEL"):
|
||||
monkeypatch.delenv(k, raising=False)
|
||||
|
||||
s = load_settings()
|
||||
assert s.host == "0.0.0.0" # noqa: S104
|
||||
assert s.port == 8000 # noqa: PLR2004
|
||||
assert s.workers == 1
|
||||
assert s.log_level == "info"
|
||||
|
||||
|
||||
def test_env_overrides(monkeypatch: pytest.MonkeyPatch) -> None:
|
||||
monkeypatch.setenv("HOST", "127.0.0.1")
|
||||
monkeypatch.setenv("PORT", "9000")
|
||||
monkeypatch.setenv("WORKERS", "3")
|
||||
monkeypatch.setenv("LOG_LEVEL", "debug")
|
||||
monkeypatch.delenv("CONFIG_FILE", raising=False)
|
||||
|
||||
s = load_settings()
|
||||
assert s.host == "127.0.0.1"
|
||||
assert s.port == 9000 # noqa: PLR2004
|
||||
assert s.workers == 3 # noqa: PLR2004
|
||||
assert s.log_level == "debug"
|
||||
|
||||
|
||||
def test_yaml_config_file(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> None:
|
||||
cfg = tmp_path / "config.yaml"
|
||||
cfg.write_text("host: 10.0.0.5\nport: 8088\nworkers: 5\nlog_level: debug\n")
|
||||
monkeypatch.setenv("CONFIG_FILE", str(cfg))
|
||||
for k in ("HOST", "PORT", "WORKERS", "LOG_LEVEL"):
|
||||
monkeypatch.delenv(k, raising=False)
|
||||
|
||||
s = load_settings()
|
||||
assert s.host == "10.0.0.5"
|
||||
assert s.port == 8088 # noqa: PLR2004
|
||||
assert s.workers == 5 # noqa: PLR2004
|
||||
assert s.log_level == "debug"
|
||||
Reference in New Issue
Block a user