54 lines
1.1 KiB
Python
54 lines
1.1 KiB
Python
"""Fixtures de test : client FastAPI + curseur SQL simulé (aucune vraie BDD)."""
|
|
import os
|
|
from contextlib import contextmanager
|
|
|
|
os.environ.setdefault("JWT_SECRET", "test-secret")
|
|
|
|
import pytest
|
|
from fastapi.testclient import TestClient
|
|
|
|
import main
|
|
from auth import create_access_token
|
|
|
|
|
|
class FakeCursor:
|
|
"""Curseur pyodbc simulé : on règle description / rows / one par test."""
|
|
def __init__(self):
|
|
self.description = []
|
|
self._rows = []
|
|
self._one = None
|
|
self.executed = []
|
|
|
|
def execute(self, query, *args):
|
|
self.executed.append((query, args))
|
|
return self
|
|
|
|
def fetchall(self):
|
|
return self._rows
|
|
|
|
def fetchone(self):
|
|
return self._one
|
|
|
|
|
|
@pytest.fixture
|
|
def cur(monkeypatch):
|
|
c = FakeCursor()
|
|
|
|
@contextmanager
|
|
def fake_get_cursor():
|
|
yield c
|
|
|
|
monkeypatch.setattr(main, "get_cursor", fake_get_cursor)
|
|
return c
|
|
|
|
|
|
@pytest.fixture
|
|
def client():
|
|
return TestClient(main.app)
|
|
|
|
|
|
@pytest.fixture
|
|
def auth_headers():
|
|
token = create_access_token({"sub": "admin", "role": "Admin", "uid": 1})
|
|
return {"Authorization": f"Bearer {token}"}
|