Skip to content

Commit

Permalink
Update calculation of fingerprint count in tests
Browse files Browse the repository at this point in the history
  • Loading branch information
rikroe committed Jan 23, 2024
1 parent 1948711 commit e026fd6
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 31 deletions.
20 changes: 12 additions & 8 deletions bimmer_connected/tests/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,18 @@
REMOTE_SERVICE_RESPONSE_EVENTPOSITION = RESPONSE_DIR / "remote_services" / "eadrax_service_eventposition.json"


def get_fingerprint_state_count() -> int:
"""Return number of loaded vehicles."""
return len(ALL_STATES)


def get_fingerprint_charging_settings_count() -> int:
"""Return number of loaded vehicles."""
return len(ALL_CHARGING_SETTINGS)
def get_fingerprint_count(type: str) -> int:
"""Return number of requests/fingerprints for a given type."""

if type == "vehicles":
return len(CarBrands)
if type == "states":
return len(ALL_STATES)
if type == "profiles":
return len(ALL_PROFILES)
if type == "charging_settings":
return len(ALL_CHARGING_SETTINGS)
return 0


def load_response(path: Union[Path, str]) -> Any:
Expand Down
22 changes: 13 additions & 9 deletions bimmer_connected/tests/test_account.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,7 @@
TEST_USERNAME,
VIN_G26,
VIN_I20,
get_fingerprint_charging_settings_count,
get_fingerprint_state_count,
get_fingerprint_count,
load_response,
)
from .conftest import prepare_account_with_vehicles
Expand Down Expand Up @@ -177,7 +176,7 @@ async def test_vehicles(bmw_fixture: respx.Router):
await account.get_vehicles()

assert account.config.authentication.access_token is not None
assert get_fingerprint_state_count() == len(account.vehicles)
assert get_fingerprint_count("profiles") == len(account.vehicles)

vehicle = account.get_vehicle(VIN_G26)
assert vehicle is not None
Expand All @@ -198,15 +197,15 @@ async def test_vehicle_init(bmw_fixture: respx.Router):

# First call on init
await account.get_vehicles()
assert len(account.vehicles) == get_fingerprint_state_count()
assert len(account.vehicles) == get_fingerprint_count("profiles")

# No call to _init_vehicles()
await account.get_vehicles()
assert len(account.vehicles) == get_fingerprint_state_count()
assert len(account.vehicles) == get_fingerprint_count("profiles")

# Second, forced call _init_vehicles()
await account.get_vehicles(force_init=True)
assert len(account.vehicles) == get_fingerprint_state_count()
assert len(account.vehicles) == get_fingerprint_count("profiles")

assert mock_listener.call_count == 2

Expand Down Expand Up @@ -261,7 +260,12 @@ async def test_get_fingerprints(monkeypatch: pytest.MonkeyPatch, bmw_fixture: re

# Prepare Number of good responses (vehicle profiles + vehicle states, charging settings per vehicle)
# and 2x vehicle list
json_count = get_fingerprint_state_count() * 2 + get_fingerprint_charging_settings_count() + 2
json_count = (
get_fingerprint_count("vehicles")
+ get_fingerprint_count("profiles")
+ get_fingerprint_count("states")
+ get_fingerprint_count("charging_settings")
)

account = MyBMWAccount(TEST_USERNAME, TEST_PASSWORD, TEST_REGION, log_responses=True)
await account.get_vehicles()
Expand Down Expand Up @@ -565,7 +569,7 @@ async def test_401_after_429_ok(bmw_fixture: respx.Router):
)
with mock.patch("asyncio.sleep", new_callable=mock.AsyncMock):
await account.get_vehicles()
assert len(account.vehicles) == get_fingerprint_state_count()
assert len(account.vehicles) == get_fingerprint_count("profiles")


@pytest.mark.asyncio
Expand Down Expand Up @@ -656,7 +660,7 @@ async def test_no_vehicle_details(caplog, bmw_fixture: respx.Router):
await account.get_vehicles()

log_error = [r for r in caplog.records if "Unable to get details" in r.message]
assert len(log_error) == get_fingerprint_state_count()
assert len(log_error) == get_fingerprint_count("profiles")


@pytest.mark.asyncio
Expand Down
25 changes: 11 additions & 14 deletions bimmer_connected/tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,7 @@
TEST_REGION,
TEST_USERNAME,
VIN_G26,
get_fingerprint_charging_settings_count,
get_fingerprint_state_count,
# get_fingerprint_count,
get_fingerprint_count,
load_response,
)

Expand Down Expand Up @@ -85,22 +83,21 @@ async def test_storing_fingerprints(tmp_path, bmw_fixture: respx.Router, bmw_log
json_files = [f for f in files if f.suffix == ".json"]
txt_files = [f for f in files if f.suffix == ".txt"]

assert len(json_files) == (get_fingerprint_state_count() * 2 + get_fingerprint_charging_settings_count() + 2 - 2)
assert len(txt_files) == 1
assert len(json_files) == (
get_fingerprint_count("vehicles")
+ get_fingerprint_count("profiles")
+ get_fingerprint_count("states")
- 1 # state with error 500
+ get_fingerprint_count("charging_settings")
- 1 # not loaded due to state with error 500
)
assert len(txt_files) == 1 # state with error 500


@pytest.mark.asyncio
async def test_fingerprint_deque(monkeypatch: pytest.MonkeyPatch, bmw_fixture: respx.Router):
"""Test storing fingerprints to file."""
# Increase length of response store for local testing

# temp_store = deque(maxlen=100)
# monkeypatch.setattr("bimmer_connected.api.client.RESPONSE_STORE", temp_store)
# monkeypatch.setattr("bimmer_connected.account.RESPONSE_STORE", temp_store)

# Prepare Number of good responses (vehicle states, charging settings per vehicle)
# # and 2x vehicle list
# json_count = get_fingerprint_state_count() + get_fingerprint_charging_settings_count() + 2
# Prepare Number of good responses

account = MyBMWAccount(TEST_USERNAME, TEST_PASSWORD, TEST_REGION, log_responses=True)
await account.get_vehicles()
Expand Down

0 comments on commit e026fd6

Please sign in to comment.