Spaces:
Running

File size: 2,702 Bytes
a8c7066
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d946221
 
 
 
 
 
 
 
 
 
 
 
 
a8c7066
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
import unittest

from fastapi.testclient import TestClient

from src.adapters.inbound.api.app import create_app


class FakeAskQuestionResult:
    def __init__(self):
        self.answer = "answer"
        self.trace_id = "trace-123"
        self.chunks = []


class FakeAskQuestionService:
    def execute(self, command):
        self.last_command = command
        return FakeAskQuestionResult()


class FakeSubmitFeedbackResult:
    def __init__(self):
        self.status = "ok"


class FakeSubmitFeedbackService:
    def execute(self, command):
        self.last_command = command
        return FakeSubmitFeedbackResult()


class FakeContainer:
    def __init__(self):
        self.ask_question = FakeAskQuestionService()
        self.submit_feedback = FakeSubmitFeedbackService()


class AppContractTests(unittest.TestCase):
    def setUp(self):
        self.container = FakeContainer()
        self.client = TestClient(create_app(container=self.container))

    def test_root_endpoint_returns_welcome_message(self):
        response = self.client.get("/")

        self.assertEqual(response.status_code, 200)
        self.assertEqual(
            response.json(),
            {
                "message": "Bienvenue sur l'API RAG.",
                "docs": "/docs",
                "health": "/health",
            },
        )

    def test_health_endpoint(self):
        response = self.client.get("/health")

        self.assertEqual(response.status_code, 200)
        self.assertEqual(response.json(), {"status": "ok"})

    def test_ask_endpoint_returns_answer(self):
        response = self.client.post(
            "/api/v1/ask",
            json={
                "question": "Hello?",
                "top_k": 3,
                "use_mmr": False,
                "lambda_mult": None,
                "similarity_threshold": 0.4,
            },
        )

        self.assertEqual(response.status_code, 200)
        self.assertEqual(response.json()["answer"], "answer")
        self.assertIn("x-request-id", response.headers)
        self.assertEqual(self.container.ask_question.last_command.question, "Hello?")

    def test_feedback_endpoint_returns_ok(self):
        response = self.client.post(
            "/api/v1/feedback",
            json={
                "trace_id": "trace-123",
                "score_value": "OK",
                "feedback_type": "helpful",
                "comment": "nice",
            },
        )

        self.assertEqual(response.status_code, 200)
        self.assertEqual(response.json(), {"status": "ok"})
        self.assertEqual(self.container.submit_feedback.last_command.trace_id, "trace-123")


if __name__ == "__main__":
    unittest.main()