File: conftest.py

package info (click to toggle)
python-bimmer-connected 0.16.3-1.2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 8,304 kB
  • sloc: python: 4,469; makefile: 15
file content (60 lines) | stat: -rw-r--r-- 1,829 bytes parent folder | download
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
"""Fixtures for BMW tests."""

from collections import deque
from typing import Deque, Generator, Optional

import pytest
import respx

from bimmer_connected.account import MyBMWAccount
from bimmer_connected.const import Regions
from bimmer_connected.models import AnonymizedResponse

from . import (
    ALL_CHARGING_SETTINGS,
    ALL_PROFILES,
    ALL_STATES,
    TEST_PASSWORD,
    TEST_REGION,
    TEST_USERNAME,
)
from .common import MyBMWMockRouter


@pytest.fixture
def bmw_fixture(request: pytest.FixtureRequest) -> Generator[respx.MockRouter, None, None]:
    """Patch MyBMW login API calls."""
    # Now we can start patching the API calls
    router = MyBMWMockRouter(
        vehicles_to_load=getattr(request, "param", []),
        profiles=ALL_PROFILES,
        states=ALL_STATES,
        charging_settings=ALL_CHARGING_SETTINGS,
    )

    with router:
        yield router


@pytest.fixture
def bmw_log_all_responses(monkeypatch: pytest.MonkeyPatch):
    """Increase the length of the response store to log all responses."""
    temp_store: Deque[AnonymizedResponse] = deque(maxlen=100)
    monkeypatch.setattr("bimmer_connected.api.client.RESPONSE_STORE", temp_store)
    monkeypatch.setattr("bimmer_connected.account.RESPONSE_STORE", temp_store)


@pytest.fixture
def cli_home_dir(tmp_path_factory: pytest.TempPathFactory, monkeypatch: pytest.MonkeyPatch):
    """Create a temporary home directory for the CLI tests."""
    tmp_path = tmp_path_factory.mktemp("cli-home-")
    monkeypatch.setattr("pathlib.Path.home", lambda: tmp_path)

    return tmp_path


async def prepare_account_with_vehicles(region: Optional[Regions] = None):
    """Initialize account and get vehicles."""
    account = MyBMWAccount(TEST_USERNAME, TEST_PASSWORD, region or TEST_REGION)
    await account.get_vehicles()
    return account