1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112
|
"""Tests for utils."""
import datetime
import json
try:
import zoneinfo
except ImportError:
from backports import zoneinfo # type: ignore[import, no-redef]
import pytest
import respx
from bimmer_connected.api.utils import get_capture_position
from bimmer_connected.models import ChargingSettings, ValueWithUnit
from bimmer_connected.utils import MyBMWJSONEncoder, get_class_property_names, parse_datetime
from . import RESPONSE_DIR, VIN_G26, load_response
from .conftest import prepare_account_with_vehicles
@pytest.mark.asyncio
async def test_drive_train(bmw_fixture: respx.Router):
"""Tests available attribute."""
vehicle = (await prepare_account_with_vehicles()).get_vehicle(VIN_G26)
assert get_class_property_names(vehicle) == [
"available_attributes",
"brand",
"drive_train",
"drive_train_attributes",
"has_combustion_drivetrain",
"has_electric_drivetrain",
"is_charging_plan_supported",
"is_charging_settings_supported",
"is_lsc_enabled",
"is_remote_charge_start_enabled",
"is_remote_charge_stop_enabled",
"is_remote_climate_start_enabled",
"is_remote_climate_stop_enabled",
"is_remote_horn_enabled",
"is_remote_lights_enabled",
"is_remote_lock_enabled",
"is_remote_sendpoi_enabled",
"is_remote_set_ac_limit_enabled",
"is_remote_set_target_soc_enabled",
"is_remote_unlock_enabled",
"is_vehicle_active",
"is_vehicle_tracking_enabled",
"lsc_type",
"mileage",
"name",
"timestamp",
"vin",
]
def test_parse_datetime(caplog):
"""Test datetime parser."""
dt_without_milliseconds = datetime.datetime(2021, 11, 12, 13, 14, 15, tzinfo=datetime.timezone.utc)
assert dt_without_milliseconds == parse_datetime("2021-11-12T13:14:15.567Z")
assert dt_without_milliseconds == parse_datetime("2021-11-12T13:14:15Z")
assert dt_without_milliseconds == parse_datetime("2021-11-12T16:14:15+03:00")
unparseable_datetime = "2021-14-12T13:14:15Z"
assert parse_datetime(unparseable_datetime) is None
errors = [r for r in caplog.records if r.levelname == "ERROR" and unparseable_datetime in r.message]
assert len(errors) == 1
def test_json_encoder():
"""Test the MyBMWJSONEncoder."""
encoded = json.dumps(
{
"datetime": datetime.datetime(2022, 6, 2, 22, 19, 34, 123456),
"date": datetime.date(2022, 6, 2),
"value": ValueWithUnit(17, "mi"),
"list": [
{
"value_int": 1,
"value_str": "string",
},
zoneinfo.ZoneInfo("America/Los_Angeles"),
],
},
cls=MyBMWJSONEncoder,
)
assert encoded == (
'{"datetime": "2022-06-02T22:19:34.123456", "date": "2022-06-02", "value": [17, "mi"],'
' "list": [{"value_int": 1, "value_str": "string"}, "America/Los_Angeles"]}'
)
def test_charging_settings():
"""Test parsing and validation of charging settings."""
cs = ChargingSettings(chargingTarget=90, acLimitValue=32)
assert cs.acLimitValue == 32
assert cs.chargingTarget == 90
assert cs.dcLoudness is None
assert cs.isUnlockCableActive is None
def test_get_capture_position():
"""Test the auto get slider captcha position."""
base64_background_img = load_response(RESPONSE_DIR / "auth" / "auth_slider_captcha.json")["data"]["backGroundImg"]
position = get_capture_position(base64_background_img)
assert position == "0.81"
|