File: conftest.py

package info (click to toggle)
pyenphase 2.4.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 9,068 kB
  • sloc: python: 9,672; makefile: 15; sh: 4
file content (48 lines) | stat: -rw-r--r-- 1,470 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
import logging
from unittest.mock import patch

import aiohttp
import pytest
import pytest_asyncio
from aioresponses import aioresponses
from syrupy import SnapshotAssertion

from pyenphase.ssl import NO_VERIFY_SSL_CONTEXT
from tests.syrupy import EnphaseSnapshotExtension


@pytest.fixture
def mock_aioresponse():
    """Return aioresponses fixture."""
    # Note: aioresponses will mock all ClientSession instances by default
    with aioresponses(passthrough=["http://127.0.0.1:8123"]) as m:
        yield m


@pytest_asyncio.fixture
async def test_client_session():
    """Create an aiohttp ClientSession with low timeout for tests."""
    timeout = aiohttp.ClientTimeout(total=5.0, connect=1.0, sock_read=1.0)
    connector = aiohttp.TCPConnector(ssl=NO_VERIFY_SSL_CONTEXT)
    session = aiohttp.ClientSession(timeout=timeout, connector=connector)
    yield session
    await session.close()


@pytest.fixture
def snapshot(snapshot: SnapshotAssertion) -> SnapshotAssertion:
    """Return snapshot assertion fixture with the Enphase extension."""
    return snapshot.use_extension(EnphaseSnapshotExtension)


@pytest.fixture(autouse=True)
def fast_tenacity():
    """Make tenacity retries fast by mocking time.sleep."""
    with patch("tenacity.nap.time"), patch("asyncio.sleep", return_value=None):
        yield


@pytest.fixture(autouse=True)
def setup_logging():
    """Set up logging for all tests."""
    logging.getLogger("pyenphase").setLevel(logging.DEBUG)