From e026fd6abe38537d94f6e8e48bbad8e76be3793f Mon Sep 17 00:00:00 2001 From: Richard Date: Tue, 23 Jan 2024 22:24:41 +0100 Subject: [PATCH] Update calculation of fingerprint count in tests --- bimmer_connected/tests/__init__.py | 20 ++++++++++++-------- bimmer_connected/tests/test_account.py | 22 +++++++++++++--------- bimmer_connected/tests/test_api.py | 25 +++++++++++-------------- 3 files changed, 36 insertions(+), 31 deletions(-) diff --git a/bimmer_connected/tests/__init__.py b/bimmer_connected/tests/__init__.py index aecac15e..d7fe4dd1 100644 --- a/bimmer_connected/tests/__init__.py +++ b/bimmer_connected/tests/__init__.py @@ -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: diff --git a/bimmer_connected/tests/test_account.py b/bimmer_connected/tests/test_account.py index 2e8b4b17..c5067ebc 100644 --- a/bimmer_connected/tests/test_account.py +++ b/bimmer_connected/tests/test_account.py @@ -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 @@ -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 @@ -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 @@ -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() @@ -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 @@ -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 diff --git a/bimmer_connected/tests/test_api.py b/bimmer_connected/tests/test_api.py index 4eef41be..17b4ff2e 100644 --- a/bimmer_connected/tests/test_api.py +++ b/bimmer_connected/tests/test_api.py @@ -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, ) @@ -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()