|
| 1 | +from hyperbrowser.client.managers.job_pagination_utils import ( |
| 2 | + initialize_job_paginated_response, |
| 3 | + merge_job_paginated_page_response, |
| 4 | +) |
| 5 | +from hyperbrowser.models.crawl import CrawlJobResponse |
| 6 | +from hyperbrowser.models.scrape import BatchScrapeJobResponse |
| 7 | + |
| 8 | + |
| 9 | +def test_initialize_job_paginated_response_for_batch_scrape(): |
| 10 | + response = initialize_job_paginated_response( |
| 11 | + model=BatchScrapeJobResponse, |
| 12 | + job_id="job-1", |
| 13 | + status="completed", |
| 14 | + total_counter_alias="totalScrapedPages", |
| 15 | + ) |
| 16 | + |
| 17 | + assert response.job_id == "job-1" |
| 18 | + assert response.status == "completed" |
| 19 | + assert response.data == [] |
| 20 | + assert response.current_page_batch == 0 |
| 21 | + assert response.total_page_batches == 0 |
| 22 | + assert response.total_scraped_pages == 0 |
| 23 | + assert response.batch_size == 100 |
| 24 | + |
| 25 | + |
| 26 | +def test_initialize_job_paginated_response_for_crawl_with_custom_batch_size(): |
| 27 | + response = initialize_job_paginated_response( |
| 28 | + model=CrawlJobResponse, |
| 29 | + job_id="job-2", |
| 30 | + status="running", |
| 31 | + total_counter_alias="totalCrawledPages", |
| 32 | + batch_size=25, |
| 33 | + ) |
| 34 | + |
| 35 | + assert response.job_id == "job-2" |
| 36 | + assert response.status == "running" |
| 37 | + assert response.data == [] |
| 38 | + assert response.current_page_batch == 0 |
| 39 | + assert response.total_page_batches == 0 |
| 40 | + assert response.total_crawled_pages == 0 |
| 41 | + assert response.batch_size == 25 |
| 42 | + |
| 43 | + |
| 44 | +def test_merge_job_paginated_page_response_updates_totals_and_error(): |
| 45 | + job_response = initialize_job_paginated_response( |
| 46 | + model=CrawlJobResponse, |
| 47 | + job_id="job-2", |
| 48 | + status="running", |
| 49 | + total_counter_alias="totalCrawledPages", |
| 50 | + ) |
| 51 | + page_response = CrawlJobResponse( |
| 52 | + jobId="job-2", |
| 53 | + status="running", |
| 54 | + data=[], |
| 55 | + currentPageBatch=3, |
| 56 | + totalPageBatches=9, |
| 57 | + totalCrawledPages=21, |
| 58 | + batchSize=50, |
| 59 | + error="partial failure", |
| 60 | + ) |
| 61 | + |
| 62 | + merge_job_paginated_page_response( |
| 63 | + job_response, |
| 64 | + page_response, |
| 65 | + total_counter_attr="total_crawled_pages", |
| 66 | + ) |
| 67 | + |
| 68 | + assert job_response.current_page_batch == 3 |
| 69 | + assert job_response.total_page_batches == 9 |
| 70 | + assert job_response.total_crawled_pages == 21 |
| 71 | + assert job_response.batch_size == 50 |
| 72 | + assert job_response.error == "partial failure" |
0 commit comments