86 lines
3.4 KiB
Python
86 lines
3.4 KiB
Python
from __future__ import annotations
|
|
|
|
import unittest
|
|
|
|
from iaai_scraper.parsing.mapper import CarMapper
|
|
|
|
|
|
class TestCarMapper(unittest.TestCase):
|
|
def setUp(self) -> None:
|
|
self.mapper = CarMapper()
|
|
|
|
def test_content_hash_is_sha256(self) -> None:
|
|
record = self.mapper.map_to_car_record(
|
|
vehicle_url="https://www.iaai.com/VehicleDetail/45089484~US",
|
|
vehicle_summary={"make": "Toyota", "model": "Camry", "year": "2014"},
|
|
payload_insights={
|
|
"vehicle_core": {"odometer": "120,000", "body_type": "sedan"},
|
|
"pricing": {"buy_now": "$4,500"},
|
|
"damage": {"primary": "normal wear"},
|
|
"auction": {},
|
|
"images": {"urls": []},
|
|
},
|
|
)
|
|
|
|
# Длина hex-представления SHA-256
|
|
self.assertEqual(len(record.content_hash), 64)
|
|
|
|
def test_deduplicates_images_by_image_key(self) -> None:
|
|
urls = [
|
|
"https://vis.iaai.com/resizer?imageKeys=1&width=200&height=150",
|
|
"https://vis.iaai.com/resizer?imageKeys=1&width=845&height=633",
|
|
"https://vis.iaai.com/resizer?imageKeys=2&width=400&height=300",
|
|
]
|
|
|
|
record = self.mapper.map_to_car_record(
|
|
vehicle_url="https://www.iaai.com/VehicleDetail/123~US",
|
|
vehicle_summary={"make": "Honda", "model": "Civic", "image_urls": urls},
|
|
payload_insights={"vehicle_core": {}, "pricing": {}, "damage": {}, "auction": {}, "images": {}},
|
|
)
|
|
|
|
self.assertEqual(len(record.images), 2)
|
|
self.assertIn("width=845", record.images[0].fullres_image)
|
|
self.assertIn("height=633", record.images[0].fullres_image)
|
|
|
|
def test_no_damage_marker_is_not_damaged(self) -> None:
|
|
record = self.mapper.map_to_car_record(
|
|
vehicle_url="https://www.iaai.com/VehicleDetail/123~US",
|
|
vehicle_summary={"make": "Ford", "model": "Focus"},
|
|
payload_insights={
|
|
"vehicle_core": {},
|
|
"pricing": {},
|
|
"damage": {"primary": "normal wear"},
|
|
"auction": {},
|
|
"images": {},
|
|
},
|
|
)
|
|
|
|
self.assertFalse(record.is_damaged)
|
|
|
|
def test_unknown_and_empty_values_fallbacks(self) -> None:
|
|
record = self.mapper.map_to_car_record(
|
|
vehicle_url="https://www.iaai.com/VehicleDetail/999~US",
|
|
vehicle_summary={"make": " ", "model": None, "drive": "???", "gearbox": "unknown"},
|
|
payload_insights={"vehicle_core": {}, "pricing": {}, "damage": {}, "auction": {}, "images": {}},
|
|
)
|
|
|
|
self.assertEqual(record.brand, "UNKNOWN")
|
|
self.assertEqual(record.model, "UNKNOWN")
|
|
self.assertIsNone(record.steering_wheel if record.steering_wheel not in {"LEFT", None} else None)
|
|
self.assertEqual(record.drive, "NA")
|
|
self.assertEqual(record.gearbox, "NA")
|
|
|
|
def test_mapper_handles_case_and_spaces(self) -> None:
|
|
record = self.mapper.map_to_car_record(
|
|
vehicle_url="https://www.iaai.com/VehicleDetail/888~US",
|
|
vehicle_summary={"make": "Honda", "model": "Civic", "drive": " Front Wheel Drive ", "gearbox": " AUTOMATIC "},
|
|
payload_insights={"vehicle_core": {}, "pricing": {}, "damage": {}, "auction": {}, "images": {}},
|
|
)
|
|
|
|
self.assertEqual(record.drive, "FWD")
|
|
self.assertEqual(record.gearbox, "AT")
|
|
|
|
|
|
if __name__ == "__main__":
|
|
unittest.main()
|