Skip to content

Commit 4740c58

Browse files
committed
add tests
1 parent c7506ba commit 4740c58

File tree

5 files changed

+40
-301
lines changed

5 files changed

+40
-301
lines changed
Lines changed: 0 additions & 103 deletions
Original file line numberDiff line numberDiff line change
@@ -1,103 +0,0 @@
1-
from uuid import uuid4
2-
3-
import pytest
4-
from aioresponses import aioresponses
5-
6-
from scrapegraph_py.async_client import AsyncClient
7-
from scrapegraph_py.exceptions import APIError
8-
from tests.utils import generate_mock_api_key
9-
10-
11-
@pytest.fixture
12-
def mock_api_key():
13-
return generate_mock_api_key()
14-
15-
16-
@pytest.fixture
17-
def mock_uuid():
18-
return str(uuid4())
19-
20-
21-
@pytest.mark.asyncio
22-
async def test_smartscraper(mock_api_key):
23-
with aioresponses() as mocked:
24-
mocked.post(
25-
"https://api.scrapegraphai.com/v1/smartscraper",
26-
payload={
27-
"request_id": str(uuid4()),
28-
"status": "completed",
29-
"result": {"description": "Example domain."},
30-
},
31-
)
32-
33-
async with AsyncClient(api_key=mock_api_key) as client:
34-
response = await client.smartscraper(
35-
website_url="https://example.com", user_prompt="Describe this page."
36-
)
37-
assert response["status"] == "completed"
38-
assert "description" in response["result"]
39-
40-
41-
@pytest.mark.asyncio
42-
async def test_get_credits(mock_api_key):
43-
with aioresponses() as mocked:
44-
mocked.get(
45-
"https://api.scrapegraphai.com/v1/credits",
46-
payload={"remaining_credits": 100, "total_credits_used": 50},
47-
)
48-
49-
async with AsyncClient(api_key=mock_api_key) as client:
50-
response = await client.get_credits()
51-
assert response["remaining_credits"] == 100
52-
assert response["total_credits_used"] == 50
53-
54-
55-
@pytest.mark.asyncio
56-
async def test_submit_feedback(mock_api_key):
57-
with aioresponses() as mocked:
58-
mocked.post(
59-
"https://api.scrapegraphai.com/v1/feedback", payload={"status": "success"}
60-
)
61-
62-
async with AsyncClient(api_key=mock_api_key) as client:
63-
response = await client.submit_feedback(
64-
request_id=str(uuid4()), rating=5, feedback_text="Great service!"
65-
)
66-
assert response["status"] == "success"
67-
68-
69-
@pytest.mark.asyncio
70-
async def test_get_smartscraper(mock_api_key, mock_uuid):
71-
with aioresponses() as mocked:
72-
mocked.get(
73-
f"https://api.scrapegraphai.com/v1/smartscraper/{mock_uuid}",
74-
payload={
75-
"request_id": mock_uuid,
76-
"status": "completed",
77-
"result": {"data": "test"},
78-
},
79-
)
80-
81-
async with AsyncClient(api_key=mock_api_key) as client:
82-
response = await client.get_smartscraper(mock_uuid)
83-
assert response["status"] == "completed"
84-
assert response["request_id"] == mock_uuid
85-
86-
87-
@pytest.mark.asyncio
88-
async def test_api_error(mock_api_key):
89-
with aioresponses() as mocked:
90-
mocked.post(
91-
"https://api.scrapegraphai.com/v1/smartscraper",
92-
status=400,
93-
payload={"error": "Bad request"},
94-
exception=APIError("Bad request", status_code=400),
95-
)
96-
97-
async with AsyncClient(api_key=mock_api_key) as client:
98-
with pytest.raises(APIError) as exc_info:
99-
await client.smartscraper(
100-
website_url="https://example.com", user_prompt="Describe this page."
101-
)
102-
assert exc_info.value.status_code == 400
103-
assert "Bad request" in str(exc_info.value)

scrapegraph-py/tests/test_client.py

Lines changed: 40 additions & 95 deletions
Original file line numberDiff line numberDiff line change
@@ -1,99 +1,44 @@
1-
from uuid import uuid4
2-
31
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
3425
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"
3628
)
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-
)
9429

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()

scrapegraph-py/tests/test_exceptions.py

Lines changed: 0 additions & 15 deletions
This file was deleted.

scrapegraph-py/tests/test_models.py

Lines changed: 0 additions & 82 deletions
This file was deleted.

scrapegraph-py/tests/utils.py

Lines changed: 0 additions & 6 deletions
This file was deleted.

0 commit comments

Comments
 (0)