Skip to content

Commit

Permalink
Return empty data on missing attributes (#1389)
Browse files Browse the repository at this point in the history
* Return empty data not None on missing attributes

* Raise on missing data/attributes

* Revert "Raise on missing data/attributes"

This reverts commit e7db4d6.

* Fix

* Add tests
  • Loading branch information
epenet authored Dec 2, 2024
1 parent c8d8328 commit ef693c4
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 6 deletions.
11 changes: 5 additions & 6 deletions src/renault_api/kamereon/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -347,13 +347,12 @@ class KamereonVehicleDataResponse(KamereonResponse):

data: Optional[KamereonVehicleData]

def get_attributes(self, schema: Schema) -> Optional[KamereonVehicleDataAttributes]:
def get_attributes(self, schema: Schema) -> KamereonVehicleDataAttributes:
"""Return jwt token."""
return (
cast(KamereonVehicleDataAttributes, schema.load(self.data.attributes))
if self.data and self.data.attributes is not None
else None
)
attributes = {}
if self.data and self.data.attributes is not None:
attributes = self.data.attributes
return cast(KamereonVehicleDataAttributes, schema.load(attributes))


@dataclass
Expand Down
6 changes: 6 additions & 0 deletions tests/fixtures/kamereon/vehicle_data/no_data.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"data": {
"type": "ChargeMode",
"id": "VF1AAAA"
}
}
20 changes: 20 additions & 0 deletions tests/kamereon/test_kamereon_vehicle_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -500,3 +500,23 @@ def test_hvac_settings_schedule() -> None:
assert vehicle_data.schedules[i].id == i + 1
for day in DAYS_OF_WEEK:
assert vehicle_data.schedules[i].__dict__.get(day) is None


def test_no_data() -> None:
"""Test missing vehicle data."""
response: models.KamereonVehicleDataResponse = fixtures.get_file_content_as_schema(
f"{fixtures.KAMEREON_FIXTURE_PATH}/vehicle_data/no_data.json",
schemas.KamereonVehicleDataResponseSchema,
)
response.raise_for_error_code()
assert response.data is not None
assert response.data.raw_data == {"id": "VF1AAAA", "type": "ChargeMode"}

vehicle_data = cast(
models.KamereonVehicleCockpitData,
response.get_attributes(schemas.KamereonVehicleCockpitDataSchema),
)

assert vehicle_data.totalMileage is None
assert vehicle_data.fuelAutonomy is None
assert vehicle_data.fuelQuantity is None

0 comments on commit ef693c4

Please sign in to comment.