|
| 1 | +import os |
| 2 | +import pytest |
| 3 | +def test_gemini_real_api(): |
| 4 | + api_key = os.getenv("GEMINI_API_KEY") |
| 5 | + if not api_key: |
| 6 | + pytest.skip("GEMINI_API_KEY not set; skipping real Gemini API test.") |
| 7 | + from google import genai |
| 8 | + client = genai.Client(api_key=api_key) |
| 9 | + try: |
| 10 | + response = client.models.generate_content( |
| 11 | + model="models/gemini-flash-latest", |
| 12 | + contents="Say hello from Gemini!" |
| 13 | + ) |
| 14 | + completion = getattr(response, "text", None) |
| 15 | + assert completion is not None and "hello" in completion.lower() |
| 16 | + except Exception as e: |
| 17 | + pytest.fail(f"Gemini real API call failed: {e}") |
1 | 18 | import sys |
2 | 19 | import os |
3 | 20 | sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), "../"))) |
|
8 | 25 | client = TestClient(app) |
9 | 26 |
|
10 | 27 |
|
11 | | -def test_gemini_endpoint(): |
| 28 | + |
| 29 | +def test_gemini_get_endpoint(): |
12 | 30 | response = client.get("/gemini") |
13 | 31 | assert response.status_code == 200 |
14 | | - assert response.json() == {"message": "Welcome to the Gemini API!"} |
| 32 | + data = response.json() |
| 33 | + assert "meta" in data |
| 34 | + assert data["meta"]["severity"] == "success" |
| 35 | + assert "Gemini endpoint says hello" in data["meta"]["title"] |
| 36 | + |
| 37 | + |
| 38 | +def test_gemini_post_endpoint(monkeypatch): |
| 39 | + # Mock google-genai SDK to avoid real API call |
| 40 | + class MockGenAIResponse: |
| 41 | + text = "Test completion" |
| 42 | + |
| 43 | + class MockGenAIModel: |
| 44 | + def generate_content(self, model, contents): |
| 45 | + return MockGenAIResponse() |
| 46 | + |
| 47 | + class MockGenAIClient: |
| 48 | + models = MockGenAIModel() |
| 49 | + |
| 50 | + monkeypatch.setattr("google.genai.Client", lambda *args, **kwargs: MockGenAIClient()) |
| 51 | + |
| 52 | + payload = {"prompt": "Test prompt"} |
| 53 | + response = client.post("/gemini", json=payload) |
| 54 | + assert response.status_code == 200 |
| 55 | + data = response.json() |
| 56 | + assert "meta" in data |
| 57 | + assert data["meta"]["severity"] == "success" |
| 58 | + assert "Gemini completion received" in data["meta"]["title"] |
| 59 | + assert data["data"]["prompt"] == "Test prompt" |
| 60 | + assert data["data"]["completion"] == "Test completion" |
| 61 | + assert "data" in data |
| 62 | + assert data["data"]["prompt"] == "Test prompt" |
| 63 | + assert data["data"]["completion"] == "Test completion" |
0 commit comments