|
| 1 | +""" |
| 2 | +Tests for batch scraping operations. |
| 3 | +
|
| 4 | +Verifies that scraping multiple URLs returns List[ScrapeResult] correctly. |
| 5 | +""" |
| 6 | + |
| 7 | +import pytest |
| 8 | +from brightdata import BrightDataClient |
| 9 | +from brightdata.models import ScrapeResult |
| 10 | + |
| 11 | + |
| 12 | +class TestBatchOperations: |
| 13 | + """Test batch scraping returns correct types.""" |
| 14 | + |
| 15 | + def test_single_url_returns_single_result(self): |
| 16 | + """Test that a single URL returns ScrapeResult (not list).""" |
| 17 | + client = BrightDataClient(token="test_token_123456789") |
| 18 | + |
| 19 | + # Verify single URL behavior |
| 20 | + scraper = client.scrape.amazon |
| 21 | + |
| 22 | + # Single URL should return ScrapeResult |
| 23 | + import inspect |
| 24 | + |
| 25 | + sig = inspect.signature(scraper.products) |
| 26 | + return_annotation = sig.return_annotation |
| 27 | + |
| 28 | + # Should accept Union[str, List[str]] |
| 29 | + params = sig.parameters |
| 30 | + assert "url" in params |
| 31 | + |
| 32 | + def test_list_with_one_url_returns_single_result(self): |
| 33 | + """Test that list with 1 URL returns unwrapped ScrapeResult.""" |
| 34 | + # This is the expected behavior - list with 1 item gets unwrapped |
| 35 | + # This test documents the API contract |
| 36 | + pass |
| 37 | + |
| 38 | + def test_multiple_urls_should_return_list(self): |
| 39 | + """Test that multiple URLs should return List[ScrapeResult].""" |
| 40 | + # This documents that the API SHOULD return a list of results |
| 41 | + # when given multiple URLs, not a single result with data as list |
| 42 | + |
| 43 | + # Expected behavior: |
| 44 | + # Input: ["url1", "url2", "url3"] |
| 45 | + # Output: [ScrapeResult, ScrapeResult, ScrapeResult] |
| 46 | + # NOT: ScrapeResult with data=[item1, item2, item3] |
| 47 | + pass |
| 48 | + |
| 49 | + def test_batch_result_type_annotations(self): |
| 50 | + """Test that method signatures indicate Union[ScrapeResult, List[ScrapeResult]].""" |
| 51 | + from brightdata.scrapers.amazon import AmazonScraper |
| 52 | + |
| 53 | + scraper = AmazonScraper(bearer_token="test_token_123456789") |
| 54 | + |
| 55 | + import inspect |
| 56 | + |
| 57 | + sig = inspect.signature(scraper.products) |
| 58 | + |
| 59 | + # Check return type annotation |
| 60 | + return_type = sig.return_annotation |
| 61 | + assert return_type != inspect.Signature.empty, "Should have return type annotation" |
| 62 | + |
| 63 | + # Should be Union[ScrapeResult, List[ScrapeResult]] |
| 64 | + type_str = str(return_type) |
| 65 | + assert "ScrapeResult" in type_str |
| 66 | + assert "List" in type_str or "Union" in type_str |
| 67 | + |
| 68 | + |
| 69 | +class TestBatchScrapingBehavior: |
| 70 | + """Test actual batch scraping behavior.""" |
| 71 | + |
| 72 | + def test_batch_operations_contract(self): |
| 73 | + """Document the batch operations API contract.""" |
| 74 | + # API Contract: |
| 75 | + # 1. Single URL string → ScrapeResult |
| 76 | + # 2. List with 1 URL → ScrapeResult (unwrapped for convenience) |
| 77 | + # 3. List with 2+ URLs → List[ScrapeResult] (one per URL) |
| 78 | + |
| 79 | + # This ensures each URL gets its own result object with: |
| 80 | + # - Individual success/error status |
| 81 | + # - Individual timing information |
| 82 | + # - Individual cost tracking |
| 83 | + # - Individual data payload |
| 84 | + pass |
| 85 | + |
| 86 | + def test_batch_result_independence(self): |
| 87 | + """Test that batch results are independent.""" |
| 88 | + # Each result in a batch should be independent: |
| 89 | + # - If URL 1 fails, URL 2 should still have its own result |
| 90 | + # - Each result has its own cost calculation |
| 91 | + # - Each result has its own timing data |
| 92 | + # - Each result has its own url field set |
| 93 | + pass |
| 94 | + |
| 95 | + |
| 96 | +class TestBatchErrorHandling: |
| 97 | + """Test batch operations error handling.""" |
| 98 | + |
| 99 | + def test_batch_with_mixed_success_failure(self): |
| 100 | + """Test batch operations with some URLs succeeding and some failing.""" |
| 101 | + # Expected: Each URL gets its own ScrapeResult |
| 102 | + # Some have success=True, some have success=False |
| 103 | + # All are in the returned list |
| 104 | + pass |
| 105 | + |
| 106 | + def test_batch_cost_calculation(self): |
| 107 | + """Test that costs are divided among batch results.""" |
| 108 | + # If total cost is $0.003 for 3 URLs |
| 109 | + # Each result should have cost=$0.001 |
| 110 | + pass |
0 commit comments