|
1 | | -from uuid import uuid4 |
2 | | - |
3 | 1 | import pytest |
4 | | -import responses |
5 | | - |
6 | | -from scrapegraph_py.client import SyncClient |
7 | | -from tests.utils import generate_mock_api_key |
8 | | - |
9 | | - |
10 | | -@pytest.fixture |
11 | | -def mock_api_key(): |
12 | | - return generate_mock_api_key() |
13 | | - |
14 | | - |
15 | | -@pytest.fixture |
16 | | -def mock_uuid(): |
17 | | - return str(uuid4()) |
18 | | - |
19 | | - |
20 | | -@responses.activate |
21 | | -def test_smartscraper(mock_api_key): |
22 | | - # Mock the API response |
23 | | - responses.add( |
24 | | - responses.POST, |
25 | | - "https://api.scrapegraphai.com/v1/smartscraper", |
26 | | - json={ |
27 | | - "request_id": str(uuid4()), |
28 | | - "status": "completed", |
29 | | - "result": {"description": "Example domain."}, |
30 | | - }, |
31 | | - ) |
32 | | - |
33 | | - with SyncClient(api_key=mock_api_key) as client: |
| 2 | +from unittest.mock import patch |
| 3 | +from scrapegraph_py import SyncClient |
| 4 | + |
| 5 | +def test_smartscraper(): |
| 6 | + # Mock response data |
| 7 | + mock_response = { |
| 8 | + "request_id": "test-123", |
| 9 | + "result": { |
| 10 | + "heading": "Example Domain", |
| 11 | + "description": "This is a sample description", |
| 12 | + "summary": "A test webpage summary" |
| 13 | + } |
| 14 | + } |
| 15 | + |
| 16 | + # Create client instance with dummy API key |
| 17 | + client = SyncClient(api_key="test-api-key") |
| 18 | + |
| 19 | + # Mock the API call |
| 20 | + with patch.object(client, '_make_request') as mock_request: |
| 21 | + # Configure mock to return our test data |
| 22 | + mock_request.return_value = mock_response |
| 23 | + |
| 24 | + # Make the smartscraper request |
34 | 25 | response = client.smartscraper( |
35 | | - website_url="https://example.com", user_prompt="Describe this page." |
| 26 | + website_url="https://example.com", |
| 27 | + user_prompt="Extract the main heading, description, and summary of the webpage" |
36 | 28 | ) |
37 | | - assert response["status"] == "completed" |
38 | | - |
39 | | - |
40 | | -@responses.activate |
41 | | -def test_get_smartscraper(mock_api_key, mock_uuid): |
42 | | - responses.add( |
43 | | - responses.GET, |
44 | | - f"https://api.scrapegraphai.com/v1/smartscraper/{mock_uuid}", |
45 | | - json={ |
46 | | - "request_id": mock_uuid, |
47 | | - "status": "completed", |
48 | | - "result": {"data": "test"}, |
49 | | - }, |
50 | | - ) |
51 | | - |
52 | | - with SyncClient(api_key=mock_api_key) as client: |
53 | | - response = client.get_smartscraper(mock_uuid) |
54 | | - assert response["status"] == "completed" |
55 | | - assert response["request_id"] == mock_uuid |
56 | | - |
57 | | - |
58 | | -@responses.activate |
59 | | -def test_get_credits(mock_api_key): |
60 | | - responses.add( |
61 | | - responses.GET, |
62 | | - "https://api.scrapegraphai.com/v1/credits", |
63 | | - json={"remaining_credits": 100, "total_credits_used": 50}, |
64 | | - ) |
65 | | - |
66 | | - with SyncClient(api_key=mock_api_key) as client: |
67 | | - response = client.get_credits() |
68 | | - assert response["remaining_credits"] == 100 |
69 | | - assert response["total_credits_used"] == 50 |
70 | | - |
71 | | - |
72 | | -@responses.activate |
73 | | -def test_submit_feedback(mock_api_key): |
74 | | - responses.add( |
75 | | - responses.POST, |
76 | | - "https://api.scrapegraphai.com/v1/feedback", |
77 | | - json={"status": "success"}, |
78 | | - ) |
79 | | - |
80 | | - with SyncClient(api_key=mock_api_key) as client: |
81 | | - response = client.submit_feedback( |
82 | | - request_id=str(uuid4()), rating=5, feedback_text="Great service!" |
83 | | - ) |
84 | | - assert response["status"] == "success" |
85 | | - |
86 | | - |
87 | | -@responses.activate |
88 | | -def test_network_error(mock_api_key): |
89 | | - responses.add( |
90 | | - responses.POST, |
91 | | - "https://api.scrapegraphai.com/v1/smartscraper", |
92 | | - body=ConnectionError("Network error"), |
93 | | - ) |
94 | 29 |
|
95 | | - with SyncClient(api_key=mock_api_key) as client: |
96 | | - with pytest.raises(ConnectionError): |
97 | | - client.smartscraper( |
98 | | - website_url="https://example.com", user_prompt="Describe this page." |
99 | | - ) |
| 30 | + # Verify the request was made with correct parameters |
| 31 | + mock_request.assert_called_once() |
| 32 | + call_args = mock_request.call_args[0][0] |
| 33 | + assert call_args['method'] == 'POST' |
| 34 | + assert 'smartscraper' in call_args['url'] |
| 35 | + assert call_args['json']['website_url'] == "https://example.com" |
| 36 | + assert call_args['json']['user_prompt'] == "Extract the main heading, description, and summary of the webpage" |
| 37 | + |
| 38 | + # Verify response structure and content |
| 39 | + assert isinstance(response, dict) |
| 40 | + assert response['request_id'] == "test-123" |
| 41 | + assert isinstance(response['result'], dict) |
| 42 | + |
| 43 | + # Clean up |
| 44 | + client.close() |
0 commit comments