File: conftest.py

package info (click to toggle)
pytraccar 3.0.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 456 kB
  • sloc: python: 786; makefile: 3
file content (98 lines) | stat: -rw-r--r-- 2,782 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
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
"""Test fixtures and configuration."""

from __future__ import annotations

import logging
from typing import Any, AsyncGenerator

import aiohttp
import pytest
import pytest_asyncio

from pytraccar import ApiClient
from tests.common import MockedRequests, MockResponse, WSMessageHandler

logging.basicConfig(level=logging.ERROR)
logging.getLogger("pytraccar").setLevel(logging.DEBUG)

pytest_plugins = ("pytest_asyncio",)


@pytest.fixture
def mock_requests() -> MockedRequests:
    """Return a new mock request instance."""
    return MockedRequests()


@pytest.fixture
def mock_response() -> MockResponse:
    """Return a new mock response instance."""
    return MockResponse()


@pytest.fixture
def mock_ws_messages() -> WSMessageHandler:
    """Return a new mock ws instance."""
    return WSMessageHandler()


@pytest_asyncio.fixture
async def client_session(
    mock_response: MockResponse,
    mock_requests: MockedRequests,
    mock_ws_messages: WSMessageHandler,
) -> AsyncGenerator[aiohttp.ClientSession, None]:
    """Mock our the request part of the client session."""

    class MockedWSContext:
        @property
        def closed(self) -> bool:
            return len(mock_ws_messages.messages) == 0

        async def receive(self) -> Any:
            return mock_ws_messages.get()

        async def __aenter__(self) -> MockedWSContext:
            return self

        async def __aexit__(self, *args: Any) -> None:
            pass

        def __aiter__(self) -> MockedWSContext:
            return self

        async def __anext__(self) -> Any:
            if len(mock_ws_messages.messages) == 0:
                raise StopAsyncIteration
            return mock_ws_messages.get()

    async def _mocked_ws_connect(*_: Any, **__: Any) -> Any:
        return MockedWSContext()

    async def _mocked_request(*args: Any, **kwargs: Any) -> Any:
        if len(args) > 2:
            mock_response.mock_endpoint = args[2].split("/api/")[-1]
            mock_requests.add({"method": args[1], "url": args[2], **kwargs})
        else:
            mock_response.mock_endpoint = args[1].split("/api/")[-1]
            mock_requests.add({"method": args[0], "url": args[1], **kwargs})
        return mock_response

    async with aiohttp.ClientSession() as session:
        mock_requests.clear()
        session._request = _mocked_request  # noqa: SLF001
        session._ws_connect = _mocked_ws_connect  # noqa: SLF001
        yield session


@pytest_asyncio.fixture
async def api_client(
    client_session: AsyncGenerator[aiohttp.ClientSession, None],
) -> AsyncGenerator[ApiClient, None]:
    """Fixture to provide a API Client."""
    yield ApiClient(
        host="127.0.0.1",
        port=1337,
        token="test",
        client_session=client_session,
    )