|
1 | 1 | import unittest
|
2 |
| -from src.oxylabs.sources.serp import SERP, SERPAsync |
3 |
| -from src.oxylabs.utils.types import user_agent_type |
| 2 | +from oxylabs.utils.types import user_agent_type |
| 3 | +from oxylabs.internal import AsyncClient, RealtimeClient |
4 | 4 |
|
5 | 5 | class TestBingSearchSync(unittest.TestCase):
|
| 6 | + """ |
| 7 | + Test case for synchronous Bing search. |
| 8 | +
|
| 9 | + This test case tests the functionality of the synchronous Bing search |
| 10 | + in the RealtimeClient class. It uses a mock response to simulate the |
| 11 | + behavior of the Bing search. |
| 12 | + """ |
| 13 | + |
6 | 14 | def test_bing_search_sync(self):
|
7 | 15 | """
|
8 |
| - Tests synchronous SERP's Bing search functionality to ensure it returns |
9 |
| - expected results. |
| 16 | + Test the synchronous Bing search. |
10 | 17 |
|
11 |
| - Mocks the response from the _get_resp method to simulate SERP |
12 |
| - processing and validates that the method handles the search query |
13 |
| - correctly and returns the correct mock response. |
| 18 | + This test creates a RealtimeClient and sets its _get_resp method to a |
| 19 | + lambda function that returns a mock response. It then calls the |
| 20 | + scrape_search method with a query and checks that the returned result |
| 21 | + contains the mock response. |
14 | 22 | """
|
15 |
| - serp = SERP('user', 'pass') |
| 23 | + client = RealtimeClient('user', 'pass') |
16 | 24 | query = "nike"
|
17 |
| - opts = {"domain": "com", "limit": 10} |
18 |
| - serp._get_resp = lambda payload, config: {"mocked_response": "search_results"} |
| 25 | + client._get_resp = lambda payload, config: {"mocked_response": "search_results"} |
19 | 26 |
|
20 |
| - result = serp.bing.scrape_search(query, opts) |
| 27 | + result = client.serp.bing.scrape_search(query, domain="com", limit=10) |
21 | 28 | self.assertIn("mocked_response", result)
|
22 | 29 | self.assertEqual(result["mocked_response"], "search_results")
|
23 | 30 |
|
24 | 31 | class TestBingUrlSync(unittest.TestCase):
|
| 32 | + """ |
| 33 | + Test case for synchronous Bing URL scraping. |
| 34 | +
|
| 35 | + This test case tests the functionality of the synchronous Bing URL scraping |
| 36 | + in the RealtimeClient class. It uses a mock response to simulate the |
| 37 | + behavior of the Bing URL scraping. |
| 38 | + """ |
| 39 | + |
25 | 40 | def test_bing_url_sync(self):
|
26 | 41 | """
|
27 |
| - Tests the SERP's Bing URL scraping functionality in a synchronous |
28 |
| - manner. |
| 42 | + Test the synchronous Bing URL scraping. |
29 | 43 |
|
30 |
| - Mocks the _get_resp method to return controlled responses, ensuring |
31 |
| - that the method correctly processes the URL and user agent type, |
32 |
| - returning the expected data. |
| 44 | + This test creates a RealtimeClient and sets its _get_resp method to a |
| 45 | + lambda function that returns a mock response. It then calls the |
| 46 | + scrape_url method with a URL and checks that the returned result |
| 47 | + contains the mock response. |
33 | 48 | """
|
34 |
| - serp = SERP('user', 'pass') |
| 49 | + |
| 50 | + client = RealtimeClient('user', 'pass') |
35 | 51 | url = "https://www.bing.com/search?q=nike"
|
36 | 52 | opts = {"user_agent_type": user_agent_type.DESKTOP}
|
37 |
| - serp._get_resp = lambda payload, config: {"mocked_response": "url_results"} |
| 53 | + client._get_resp = lambda payload, config: {"mocked_response": "url_results"} |
38 | 54 |
|
39 |
| - result = serp.bing.scrape_url(url, opts) |
| 55 | + result = client.serp.bing.scrape_url(url, opts) |
40 | 56 | self.assertIn("mocked_response", result)
|
41 | 57 | self.assertEqual(result["mocked_response"], "url_results")
|
42 | 58 |
|
| 59 | + |
43 | 60 | class TestBingSearchAsync(unittest.IsolatedAsyncioTestCase):
|
| 61 | + """ |
| 62 | + Test case for asynchronous Bing search. |
| 63 | +
|
| 64 | + This test case tests the functionality of the asynchronous Bing search |
| 65 | + in the AsyncClient class. It uses a mock response to simulate the |
| 66 | + behavior of the Bing search. |
| 67 | + """ |
| 68 | + |
44 | 69 | async def test_bing_search_async(self):
|
45 | 70 | """
|
46 |
| - Asynchronously tests SERP's Bing search to validate the async |
47 |
| - API handling. |
| 71 | + Test the asynchronous Bing search. |
48 | 72 |
|
49 |
| - Uses a mocked asynchronous response to verify that the search query |
50 |
| - processing is handled correctly and that the async functionality |
51 |
| - returns expected results. |
| 73 | + This test creates an AsyncClient and sets its _get_resp method to a |
| 74 | + mock function that returns a mock response. It then calls the |
| 75 | + scrape_search method with a query and checks that the returned result |
| 76 | + contains the mock response. |
52 | 77 | """
|
53 |
| - serp_async = SERPAsync('user', 'pass') |
| 78 | + client = AsyncClient('user', 'pass') |
54 | 79 | query = "nike"
|
55 | 80 | opts = {"domain": "com", "limit": 10}
|
56 | 81 | async def mock_get_resp(payload, config):
|
57 | 82 | return {"mocked_response": "async_search_results"}
|
58 |
| - serp_async._get_resp = mock_get_resp |
| 83 | + client._get_resp = mock_get_resp |
59 | 84 |
|
60 |
| - result = await serp_async.bing_async.scrape_search(query, opts) |
| 85 | + result = await client.serp.bing.scrape_search(query, opts) |
61 | 86 | self.assertIn("mocked_response", result)
|
62 | 87 | self.assertEqual(result["mocked_response"], "async_search_results")
|
63 | 88 |
|
64 | 89 | class TestBingUrlAsync(unittest.IsolatedAsyncioTestCase):
|
| 90 | + """ |
| 91 | + Test case for asynchronous Bing URL scraping. |
| 92 | +
|
| 93 | + This test case tests the functionality of the asynchronous Bing URL scraping |
| 94 | + in the AsyncClient class. It uses a mock response to simulate the |
| 95 | + behavior of the Bing URL scraping. |
| 96 | + """ |
| 97 | + |
65 | 98 | async def test_bing_url_async(self):
|
66 | 99 | """
|
67 |
| - Asynchronously tests SERP's Bing URL scraping functionality. |
| 100 | + Test the asynchronous Bing URL scraping. |
68 | 101 |
|
69 |
| - Mocks the _get_resp method to provide controlled async responses, |
70 |
| - verifying that the URL and user agent options are processed correctly |
71 |
| - and yield expected outcomes. |
| 102 | + This test creates an AsyncClient and sets its _get_resp method to a |
| 103 | + mock function that returns a mock response. It then calls the |
| 104 | + scrape_url method with a URL and checks that the returned result |
| 105 | + contains the mock response. |
72 | 106 | """
|
73 |
| - serp_async = SERPAsync('user', 'pass') |
| 107 | + client = AsyncClient('user', 'pass') |
74 | 108 | url = "https://www.bing.com/search?q=nike"
|
75 | 109 | opts = {"user_agent_type": user_agent_type.DESKTOP}
|
76 | 110 | async def mock_get_resp(payload, config):
|
77 | 111 | return {"mocked_response": "async_url_results"}
|
78 |
| - serp_async._get_resp = mock_get_resp |
| 112 | + client._get_resp = mock_get_resp |
79 | 113 |
|
80 |
| - result = await serp_async.bing_async.scrape_url(url, opts) |
| 114 | + result = await client.serp.bing.scrape_url(url, opts) |
81 | 115 | self.assertIn("mocked_response", result)
|
82 | 116 | self.assertEqual(result["mocked_response"], "async_url_results")
|
0 commit comments