oem_crawler/tests/test_schema.py
2026-11-07 16:53:30 +01:00

82 lines
3 KiB
Python

"""
Unit tests for data schemas and content hashing.
"""
from decimal import Decimal
import unittest
from oem_crawler.db.schemas import (
OptionSchema,
VehicleCondition,
VehicleIngestSchema,
compute_content_hash,
)
class TestSchemas(unittest.TestCase):
def setUp(self):
self.sample_payload = {
"brand": "BMW",
"oem_vehicle_id": "BMW-DE-STOCK-839210",
"vin": "WBA33AY08NFP12345",
"series": "3 Series",
"model_name": "330e xDrive Touring M Sport",
"condition": "new",
"fuel_type": "phev",
"transmission": "automatic",
"power_kw": 215,
"mileage_km": 10,
"price_gross": Decimal("69250.00"),
"options": [
{
"option_code": "05AU",
"name": "Driving Assistant Professional",
"category": "Driver Assistance",
"price_gross": Decimal("2200.00"),
"is_standard": False,
},
{
"option_code": "0402",
"name": "Panorama Glass Roof",
"price_gross": Decimal("1500.00"),
"is_standard": False,
},
],
}
def test_schema_parsing(self):
vehicle = VehicleIngestSchema(**self.sample_payload)
self.assertEqual(vehicle.brand, "BMW")
self.assertEqual(len(vehicle.options), 2)
self.assertEqual(vehicle.options[0].option_code, "05AU")
def test_content_hash_stability(self):
v1 = VehicleIngestSchema(**self.sample_payload)
v2 = VehicleIngestSchema(**self.sample_payload)
self.assertEqual(compute_content_hash(v1), compute_content_hash(v2))
def test_hash_sensitivity_on_price_change(self):
v1 = VehicleIngestSchema(**self.sample_payload)
data2 = dict(self.sample_payload, price_gross=Decimal("68000.00"))
v2 = VehicleIngestSchema(**data2)
self.assertNotEqual(compute_content_hash(v1), compute_content_hash(v2))
def test_ev_battery_and_charging_schema(self):
ev_payload = dict(
self.sample_payload,
fuel_type="bev",
battery_capacity=Decimal("141.00"),
battery_capacity_unit="ah",
battery_capacity_net_kwh=Decimal("108.40"),
charging_power_ac_kw=Decimal("11.00"),
charging_power_dc_kw=Decimal("170.00"),
charging_time_dc_minutes=22,
)
ev_vehicle = VehicleIngestSchema(**ev_payload)
self.assertEqual(ev_vehicle.battery_capacity, Decimal("141.00"))
self.assertEqual(ev_vehicle.battery_capacity_unit.value, "ah")
self.assertEqual(ev_vehicle.battery_capacity_net_kwh, Decimal("108.40"))
self.assertEqual(ev_vehicle.charging_power_ac_kw, Decimal("11.00"))
self.assertEqual(ev_vehicle.charging_power_dc_kw, Decimal("170.00"))
self.assertEqual(ev_vehicle.charging_time_dc_minutes, 22)