88import responses
99from pydantic import ValidationError
1010
11+ from aioresponses import aioresponses
12+
13+ from scrapegraph_py .exceptions import APIError
1114from scrapegraph_py .models .schema import (
1215 GenerateSchemaRequest ,
1316 GetSchemaStatusRequest ,
@@ -184,8 +187,8 @@ def test_generate_schema_api_error(self, mock_api_key):
184187 )
185188
186189 with Client (api_key = mock_api_key ) as client :
187- response = client . generate_schema ( "Find laptops" )
188- assert "error" in response
190+ with pytest . raises ( APIError ):
191+ client . generate_schema ( "Find laptops" )
189192
190193 @responses .activate
191194 def test_get_schema_status_success (self , mock_api_key , mock_uuid ):
@@ -228,61 +231,58 @@ def test_get_schema_status_not_found(self, mock_api_key, mock_uuid):
228231 )
229232
230233 with Client (api_key = mock_api_key ) as client :
231- response = client . get_schema_status ( mock_uuid )
232- assert "error" in response
234+ with pytest . raises ( APIError ):
235+ client . get_schema_status ( mock_uuid )
233236
234237
235238class TestSchemaGenerationAsyncClient :
236239 """Test cases for schema generation using async client"""
237240
238241 @pytest .mark .asyncio
239- @responses .activate
240242 async def test_generate_schema_async_success (self , mock_api_key ):
241243 """Test successful async schema generation"""
242244 mock_response = {
243245 "request_id" : str (uuid4 ()),
244246 "status" : "pending" ,
245247 "user_prompt" : "Find laptops with brand and price" ,
246248 }
247-
248- responses .add (
249- responses .POST ,
250- "https://api.scrapegraphai.com/v1/generate_schema" ,
251- json = mock_response ,
252- status = 200 ,
253- )
254249
255- async with AsyncClient (api_key = mock_api_key ) as client :
256- response = await client .generate_schema ("Find laptops with brand and price" )
257- assert response ["status" ] == "pending"
258- assert response ["request_id" ] is not None
250+ with aioresponses () as m :
251+ m .post (
252+ "https://api.scrapegraphai.com/v1/generate_schema" ,
253+ payload = mock_response ,
254+ status = 200 ,
255+ )
256+
257+ async with AsyncClient (api_key = mock_api_key ) as client :
258+ response = await client .generate_schema ("Find laptops with brand and price" )
259+ assert response ["status" ] == "pending"
260+ assert response ["request_id" ] is not None
259261
260262 @pytest .mark .asyncio
261- @responses .activate
262263 async def test_generate_schema_async_with_existing_schema (self , mock_api_key , sample_schema ):
263264 """Test async schema generation with existing schema"""
264265 mock_response = {
265266 "request_id" : str (uuid4 ()),
266267 "status" : "pending" ,
267268 "user_prompt" : "Add rating field" ,
268269 }
269-
270- responses .add (
271- responses .POST ,
272- "https://api.scrapegraphai.com/v1/generate_schema" ,
273- json = mock_response ,
274- status = 200 ,
275- )
276270
277- async with AsyncClient (api_key = mock_api_key ) as client :
278- response = await client .generate_schema (
279- "Add rating field" ,
280- existing_schema = sample_schema
271+ with aioresponses () as m :
272+ m .post (
273+ "https://api.scrapegraphai.com/v1/generate_schema" ,
274+ payload = mock_response ,
275+ status = 200 ,
281276 )
282- assert response ["status" ] == "pending"
277+
278+ async with AsyncClient (api_key = mock_api_key ) as client :
279+ response = await client .generate_schema (
280+ "Add rating field" ,
281+ existing_schema = sample_schema
282+ )
283+ assert response ["status" ] == "pending"
283284
284285 @pytest .mark .asyncio
285- @responses .activate
286286 async def test_get_schema_status_async_success (self , mock_api_key , mock_uuid ):
287287 """Test successful async schema status retrieval"""
288288 mock_response = {
@@ -299,18 +299,18 @@ async def test_get_schema_status_async_success(self, mock_api_key, mock_uuid):
299299 },
300300 },
301301 }
302-
303- responses .add (
304- responses .GET ,
305- f"https://api.scrapegraphai.com/v1/generate_schema/{ mock_uuid } " ,
306- json = mock_response ,
307- status = 200 ,
308- )
309302
310- async with AsyncClient (api_key = mock_api_key ) as client :
311- response = await client .get_schema_status (mock_uuid )
312- assert response ["status" ] == "completed"
313- assert response ["generated_schema" ] is not None
303+ with aioresponses () as m :
304+ m .get (
305+ f"https://api.scrapegraphai.com/v1/generate_schema/{ mock_uuid } " ,
306+ payload = mock_response ,
307+ status = 200 ,
308+ )
309+
310+ async with AsyncClient (api_key = mock_api_key ) as client :
311+ response = await client .get_schema_status (mock_uuid )
312+ assert response ["status" ] == "completed"
313+ assert response ["generated_schema" ] is not None
314314
315315
316316class TestSchemaGenerationIntegration :
@@ -430,13 +430,12 @@ def test_generate_schema_network_error(self, mock_api_key):
430430 responses .add (
431431 responses .POST ,
432432 "https://api.scrapegraphai.com/v1/generate_schema" ,
433- body = Exception ("Network error" ),
434- status = 500 ,
433+ body = ConnectionError ("Network error" ),
435434 )
436435
437436 with Client (api_key = mock_api_key ) as client :
438- response = client . generate_schema ( "Find laptops" )
439- assert "error" in response
437+ with pytest . raises ( ConnectionError ):
438+ client . generate_schema ( "Find laptops" )
440439
441440 @responses .activate
442441 def test_generate_schema_malformed_response (self , mock_api_key ):
0 commit comments