add mobilede tests
This commit is contained in:
@@ -1,12 +1,13 @@
|
||||
from __future__ import annotations
|
||||
|
||||
from dataclasses import dataclass
|
||||
from types import SimpleNamespace
|
||||
|
||||
from iaai_scraper.browser.fast_client import FastListingVehicle
|
||||
from iaai_scraper.core.config import Settings
|
||||
from iaai_scraper.core.runtime_config import RuntimeConfig, RuntimeFiltersConfig
|
||||
from iaai_scraper.fast_sync import FastSyncEngine
|
||||
from iaai_scraper.parsing.fast_mapper import FastCarMapper
|
||||
from mobilede_scraper.browser.fast_client import FastListingVehicle
|
||||
from mobilede_scraper.core.config import Settings
|
||||
from mobilede_scraper.core.runtime_config import RuntimeConfig, RuntimeFiltersConfig
|
||||
from mobilede_scraper.fast_sync import FastSyncEngine
|
||||
from mobilede_scraper.parsing.fast_mapper import FastCarMapper
|
||||
|
||||
|
||||
def _listing_vehicle(inventory_id: str, *, status: str = "RS") -> FastListingVehicle:
|
||||
@@ -54,12 +55,46 @@ def _detail_payload(inventory_id: str, *, make: str = "ACURA", model: str = "RSX
|
||||
}
|
||||
|
||||
|
||||
def _detail_payload_with_fallbacks(inventory_id: str) -> dict:
|
||||
return {
|
||||
"inventoryView": {
|
||||
"attributes": {
|
||||
"Id": inventory_id,
|
||||
"FirstRegistration": "2011-03",
|
||||
"Make": "BMW",
|
||||
"Model": "120",
|
||||
"Variant": "Cabrio",
|
||||
"Currency": "EUR",
|
||||
"Kilometerstand": "95 000",
|
||||
"Color": "weiss",
|
||||
"DriveType": "Front wheel drive",
|
||||
"Gearbox": "Automatik",
|
||||
"VehicleClass": "Cabrio",
|
||||
"EngineInformation": "2,0 l",
|
||||
"VehicleGrade": "A",
|
||||
},
|
||||
"imageDimensions": {
|
||||
"keys": {"$values": [{"k": f"{inventory_id}~I1", "w": 1600, "h": 1200, "i": 0}]}
|
||||
},
|
||||
},
|
||||
"auctionInformation": {
|
||||
"biddingInformation": {"buyNowPrice": "8.899 €"},
|
||||
},
|
||||
}
|
||||
|
||||
|
||||
class FakeFastClient:
|
||||
def __init__(self, listing: list[FastListingVehicle], details: dict[str, dict]) -> None:
|
||||
self.listing = listing
|
||||
self.details = details
|
||||
self.detail_calls = 0
|
||||
self.persist_calls = 0
|
||||
self._settings = SimpleNamespace(
|
||||
scraping_profile=SimpleNamespace(
|
||||
verbose_progress_logs=False,
|
||||
request_jitter_max_s=0.0,
|
||||
)
|
||||
)
|
||||
|
||||
def iter_listing_vehicles(self, *, listing_start_url=None, make=None, max_pages=None): # noqa: ANN001, ANN202
|
||||
del listing_start_url, make, max_pages
|
||||
@@ -87,7 +122,7 @@ class FakePersistence:
|
||||
self.images += sum(len(record.images) for record in records)
|
||||
return {"inserted": len(records), "updated": 0, "images_upserted": sum(len(record.images) for record in records)}
|
||||
|
||||
def mark_sold_not_in_listing_by_urls(self, active_origin_urls, lane="iaai"): # noqa: ANN001, ANN202
|
||||
def mark_sold_not_in_listing_by_urls(self, active_origin_urls, lane="MOBILEDE"): # noqa: ANN001, ANN202
|
||||
del active_origin_urls, lane
|
||||
return 0
|
||||
|
||||
@@ -139,3 +174,24 @@ def test_fast_sync_engine_applies_runtime_filters_before_db() -> None:
|
||||
assert result["cars_upserted"] == 1
|
||||
assert result["cars_filtered"] == 1
|
||||
assert persistence.inserted == 1
|
||||
|
||||
|
||||
def test_fast_sync_engine_maps_fallback_mobilede_fields() -> None:
|
||||
listing = [_listing_vehicle("449252166")]
|
||||
details = {
|
||||
"449252166": _detail_payload_with_fallbacks("449252166"),
|
||||
}
|
||||
persistence = FakePersistence()
|
||||
engine = FastSyncEngine(
|
||||
client=FakeFastClient(listing, details), # type: ignore[arg-type]
|
||||
mapper=FastCarMapper(),
|
||||
persistence=persistence, # type: ignore[arg-type]
|
||||
batch_size=10,
|
||||
fetch_concurrency=1,
|
||||
)
|
||||
|
||||
result = engine.sync_listing(runtime_config=RuntimeConfig(), only_new=False)
|
||||
|
||||
assert result["cars_upserted"] == 1
|
||||
assert persistence.inserted == 1
|
||||
assert persistence.images == 1
|
||||
|
||||
Reference in New Issue
Block a user