File size: 5,232 Bytes
e86dfae
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
"""
Integration Tests: Documents API
Tests for document CRUD endpoints.
"""

import pytest
from fastapi.testclient import TestClient


class TestDocumentList:
    """GET /api/v1/documents"""

    @pytest.mark.integration
    def test_list_documents_returns_200(self, client: TestClient):
        """List endpoint returns HTTP 200."""
        response = client.get("/api/v1/documents")
        assert response.status_code == 200

    @pytest.mark.integration
    def test_list_documents_has_pagination(self, client: TestClient):
        """Response has pagination fields."""
        response = client.get("/api/v1/documents")
        data = response.json()
        assert "documents" in data
        assert "total" in data
        assert "page" in data
        assert "page_size" in data

    @pytest.mark.integration
    def test_list_documents_empty_for_new_user(self, client: TestClient):
        """New user has 0 documents."""
        response = client.get("/api/v1/documents")
        data = response.json()
        assert data["total"] == 0
        assert data["documents"] == []

    @pytest.mark.integration
    def test_list_documents_with_completed_doc(
        self, client: TestClient, sample_document
    ):
        """Lists documents after one is created."""
        response = client.get("/api/v1/documents")
        data = response.json()
        assert data["total"] == 1
        assert data["documents"][0]["title"] == "Test Mining Safety Protocol"

    @pytest.mark.integration
    def test_list_documents_pagination(self, client: TestClient):
        """Pagination parameters are respected."""
        response = client.get("/api/v1/documents?page=1&page_size=5")
        assert response.status_code == 200
        data = response.json()
        assert data["page"] == 1
        assert data["page_size"] == 5

    @pytest.mark.integration
    def test_list_documents_search_filter(self, client: TestClient, sample_document):
        """Search by title works."""
        response = client.get("/api/v1/documents?search=Mining")
        data = response.json()
        assert data["total"] >= 1

    @pytest.mark.integration
    def test_list_documents_no_match_search(self, client: TestClient, sample_document):
        """Search with no match returns 0."""
        response = client.get("/api/v1/documents?search=xyznonexistent123")
        data = response.json()
        assert data["total"] == 0


class TestGetDocument:
    """GET /api/v1/documents/{id}"""

    @pytest.mark.integration
    def test_get_document_returns_200(self, client: TestClient, sample_document):
        """Get by ID returns HTTP 200."""
        response = client.get(f"/api/v1/documents/{sample_document.id}")
        assert response.status_code == 200

    @pytest.mark.integration
    def test_get_document_has_correct_data(self, client: TestClient, sample_document):
        """Returned document has correct fields."""
        response = client.get(f"/api/v1/documents/{sample_document.id}")
        data = response.json()
        assert data["id"] == str(sample_document.id)
        assert data["title"] == "Test Mining Safety Protocol"
        assert data["file_name"] == "mining_safety.pdf"

    @pytest.mark.integration
    def test_get_nonexistent_document_returns_404(self, client: TestClient):
        """Non-existent document ID returns 404."""
        fake_id = "00000000-0000-0000-0000-000000000000"
        response = client.get(f"/api/v1/documents/{fake_id}")
        assert response.status_code == 404


class TestDeleteDocument:
    """DELETE /api/v1/documents/{id}"""

    @pytest.mark.integration
    def test_delete_document_returns_200(self, client: TestClient, sample_document):
        """Delete returns HTTP 200 with success message."""
        response = client.delete(f"/api/v1/documents/{sample_document.id}")
        assert response.status_code == 200
        data = response.json()
        assert data["success"] is True

    @pytest.mark.integration
    def test_delete_makes_document_not_found(self, client: TestClient, sample_document):
        """After deletion, GET returns 404."""
        doc_id = str(sample_document.id)
        client.delete(f"/api/v1/documents/{doc_id}")
        response = client.get(f"/api/v1/documents/{doc_id}")
        assert response.status_code == 404

    @pytest.mark.integration
    def test_delete_nonexistent_returns_404(self, client: TestClient):
        """Deleting non-existent document returns 404."""
        fake_id = "00000000-0000-0000-0000-000000000000"
        response = client.delete(f"/api/v1/documents/{fake_id}")
        assert response.status_code == 404


class TestDocumentAnalysis:
    """GET /api/v1/documents/{id}/analysis"""

    @pytest.mark.integration
    def test_get_analysis_for_completed_document(
        self, client: TestClient, sample_document
    ):
        """Analysis endpoint returns analysis data for completed document."""
        response = client.get(f"/api/v1/documents/{sample_document.id}/analysis")
        assert response.status_code == 200
        data = response.json()
        assert data["document_id"] == str(sample_document.id)
        assert data["status"] == "completed"
        assert data["analysis"] is not None