File size: 12,219 Bytes
c636ebf
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
"""

Tests for Web Scraping Functionality

====================================



Comprehensive test suite for scraping service and API endpoints.

"""

import pytest
import asyncio
from unittest.mock import Mock, patch, AsyncMock
from datetime import datetime
from fastapi.testclient import TestClient
from bs4 import BeautifulSoup

from app.services.scraping_service import (
    ScrapingService,
    ScrapingRequest,
    ScrapedContent,
    scraping_service
)
from app.api.scraping import router
from app.main import app


class TestScrapingService:
    """Test cases for ScrapingService"""

    def setup_method(self):
        """Setup for each test method"""
        self.service = ScrapingService()
        self.sample_html = """

        <html>

        <head>

            <title>Test Legal Document</title>

            <meta name="description" content="Test legal content">

        </head>

        <body>

            <main>

                <article>

                    <h1>قرارداد نمونه</h1>

                    <p>این یک قرارداد نمونه است که برای تست استفاده می‌شود.</p>

                    <p>ماده 1: تعاریف</p>

                    <p>ماده 2: موضوع قرارداد</p>

                </article>

            </main>

            <nav>

                <a href="https://example.com/page1">صفحه اول</a>

                <a href="https://example.com/page2">صفحه دوم</a>

            </nav>

            <img src="https://example.com/image.jpg" alt="تصویر">

        </body>

        </html>

        """

    def test_clean_text(self):
        """Test text cleaning functionality"""
        dirty_text = "  متن   با   فاصله‌های   اضافی  \n\nو خط جدید  "
        cleaned = self.service._clean_text(dirty_text)

        assert cleaned == "متن با فاصله‌های اضافی و خط جدید"

    def test_extract_metadata(self):
        """Test metadata extraction"""
        soup = BeautifulSoup(self.sample_html, 'html.parser')
        metadata = self.service._extract_metadata(soup, "https://example.com")

        assert metadata['title'] == 'Test Legal Document'
        assert metadata['meta_description'] == 'Test legal content'
        assert metadata['domain'] == 'example.com'
        assert 'scraped_at' in metadata

    def test_extract_legal_content(self):
        """Test legal content extraction"""
        soup = BeautifulSoup(self.sample_html, 'html.parser')
        content = self.service._extract_legal_content(soup)

        assert 'قرارداد نمونه' in content
        assert 'ماده 1: تعاریف' in content
        assert 'ماده 2: موضوع قرارداد' in content
        assert 'صفحه اول' not in content  # Navigation should be excluded

    def test_validate_url(self):
        """Test URL validation"""
        # Valid URLs
        assert self.service.validate_url("https://gov.ir")
        assert self.service.validate_url("https://court.gov.ir")
        assert self.service.validate_url("https://example.com")

        # Invalid URLs
        assert not self.service.validate_url("not-a-url")
        assert not self.service.validate_url("ftp://example.com")
        assert not self.service.validate_url("")

    def test_get_scraping_stats(self):
        """Test scraping service statistics"""
        stats = self.service.get_scraping_stats()

        assert stats['service'] == 'Legal Dashboard Scraping Service'
        assert stats['version'] == '1.0.0'
        assert 'synchronous_scraping' in stats['supported_features']
        assert 'asynchronous_scraping' in stats['supported_features']
        assert 'legal_patterns' in stats

    @patch('requests.Session.get')
    def test_scrape_sync_success(self, mock_get):
        """Test successful synchronous scraping"""
        # Mock response
        mock_response = Mock()
        mock_response.status_code = 200
        mock_response.content = self.sample_html.encode('utf-8')
        mock_get.return_value = mock_response

        request = ScrapingRequest(url="https://example.com")
        result = self.service.scrape_sync(request)

        assert result.url == "https://example.com"
        assert result.status_code == 200
        assert result.title == "Test Legal Document"
        assert result.text_content is not None
        assert len(result.text_content) > 0

    @patch('requests.Session.get')
    def test_scrape_sync_error(self, mock_get):
        """Test synchronous scraping with error"""
        # Mock error response
        mock_get.side_effect = Exception("Connection error")

        request = ScrapingRequest(url="https://example.com")

        with pytest.raises(Exception):
            self.service.scrape_sync(request)

    @patch('aiohttp.ClientSession.get')
    @patch('aiohttp.ClientSession.__aenter__')
    @patch('aiohttp.ClientSession.__aexit__')
    def test_scrape_async_success(self, mock_exit, mock_enter, mock_get):
        """Test successful asynchronous scraping"""
        # Mock async response
        mock_response = AsyncMock()
        mock_response.status = 200
        mock_response.read = AsyncMock(
            return_value=self.sample_html.encode('utf-8'))
        mock_get.return_value.__aenter__.return_value = mock_response
        mock_enter.return_value = AsyncMock()
        mock_exit.return_value = AsyncMock()

        request = ScrapingRequest(url="https://example.com")

        # Run async test
        async def test_async():
            result = await self.service.scrape_async(request)
            assert result.url == "https://example.com"
            assert result.status_code == 200
            assert result.title == "Test Legal Document"
            assert result.text_content is not None

        asyncio.run(test_async())


class TestScrapingAPI:
    """Test cases for scraping API endpoints"""

    def setup_method(self):
        """Setup for each test method"""
        self.client = TestClient(app)

    def test_scrape_endpoint_success(self):
        """Test successful scraping via API"""
        with patch('app.services.scraping_service.scraping_service.scrape_async') as mock_scrape:
            # Mock successful scraping
            mock_scraped_content = ScrapedContent(
                url="https://example.com",
                title="Test Document",
                text_content="Test content",
                status_code=200,
                content_length=1000,
                scraped_at=datetime.now(),
                processing_time=1.5
            )
            mock_scrape.return_value = mock_scraped_content

            response = self.client.post("/api/scrape", json={
                "url": "https://example.com",
                "extract_text": True,
                "extract_metadata": True,
                "timeout": 30
            })

            assert response.status_code == 200
            data = response.json()
            assert data["success"] is True
            assert data["message"] == "Content scraped successfully"
            assert data["data"]["url"] == "https://example.com"

    def test_scrape_endpoint_invalid_url(self):
        """Test scraping with invalid URL"""
        response = self.client.post("/api/scrape", json={
            "url": "not-a-valid-url",
            "extract_text": True
        })

        assert response.status_code == 200  # Returns error in response body
        data = response.json()
        assert data["success"] is False
        assert "error" in data

    def test_scrape_stats_endpoint(self):
        """Test scraping statistics endpoint"""
        response = self.client.get("/api/scrape/stats")

        assert response.status_code == 200
        data = response.json()
        assert "service" in data
        assert "version" in data
        assert "supported_features" in data

    def test_scrape_history_endpoint(self):
        """Test scraping history endpoint"""
        response = self.client.get("/api/scrape/history")

        assert response.status_code == 200
        data = response.json()
        assert data["success"] is True
        assert "data" in data
        assert "scraped_documents" in data["data"]

    def test_validate_url_endpoint(self):
        """Test URL validation endpoint"""
        # Test valid URL
        response = self.client.get("/api/scrape/validate?url=https://gov.ir")
        assert response.status_code == 200
        data = response.json()
        assert data["is_valid"] is True

        # Test invalid URL
        response = self.client.get("/api/scrape/validate?url=invalid-url")
        assert response.status_code == 200
        data = response.json()
        assert data["is_valid"] is False

    def test_delete_scraped_document_endpoint(self):
        """Test delete scraped document endpoint"""
        response = self.client.delete("/api/scrape/1")

        assert response.status_code == 200
        data = response.json()
        assert "message" in data
        assert "Document 1 deleted successfully" in data["message"]

    def test_batch_scrape_endpoint(self):
        """Test batch scraping endpoint"""
        with patch('app.services.scraping_service.scraping_service.scrape_async') as mock_scrape:
            # Mock successful scraping for multiple URLs
            mock_scraped_content = ScrapedContent(
                url="https://example.com",
                title="Test Document",
                text_content="Test content",
                status_code=200,
                content_length=1000,
                scraped_at=datetime.now(),
                processing_time=1.5
            )
            mock_scrape.return_value = mock_scraped_content

            response = self.client.post("/api/scrape/batch", json=[
                "https://example1.com",
                "https://example2.com"
            ])

            assert response.status_code == 200
            data = response.json()
            assert data["success"] is True
            assert data["total_urls"] == 2
            assert data["successful"] == 2
            assert data["failed"] == 0


class TestScrapingIntegration:
    """Integration tests for scraping functionality"""

    def test_scraping_service_integration(self):
        """Test integration between scraping service and API"""
        service = ScrapingService()

        # Test that service can be instantiated
        assert service is not None

        # Test that service has required methods
        assert hasattr(service, 'scrape_sync')
        assert hasattr(service, 'scrape_async')
        assert hasattr(service, 'validate_url')
        assert hasattr(service, 'get_scraping_stats')

    def test_scraping_request_model(self):
        """Test ScrapingRequest model validation"""
        # Valid request
        request = ScrapingRequest(
            url="https://example.com",
            extract_text=True,
            extract_links=False,
            timeout=30
        )
        assert request.url == "https://example.com"
        assert request.extract_text is True
        assert request.extract_links is False
        assert request.timeout == 30

    def test_scraped_content_model(self):
        """Test ScrapedContent model"""
        content = ScrapedContent(
            url="https://example.com",
            title="Test Title",
            text_content="Test content",
            status_code=200,
            content_length=1000,
            scraped_at=datetime.now(),
            processing_time=1.5
        )

        assert content.url == "https://example.com"
        assert content.title == "Test Title"
        assert content.text_content == "Test content"
        assert content.status_code == 200
        assert content.content_length == 1000
        assert content.processing_time == 1.5


if __name__ == "__main__":
    pytest.main([__file__])