File: conftest.py

package info (click to toggle)
python-ovoenergy 3.0.2-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 352 kB
  • sloc: python: 1,519; makefile: 4
file content (92 lines) | stat: -rw-r--r-- 2,339 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
"""Fixtures for testing."""

from collections.abc import AsyncGenerator

from aiohttp import ClientSession
from aioresponses import aioresponses
import pytest

from ovoenergy import OVOEnergy
from ovoenergy.const import (
    AUTH_LOGIN_URL,
    AUTH_TOKEN_URL,
    BOOTSTRAP_GRAPHQL_URL,
    CARBON_FOOTPRINT_URL,
    CARBON_INTENSITY_URL,
    USAGE_DAILY_URL,
    USAGE_HALF_HOURLY_URL,
)

from . import (
    ACCOUNT,
    ACCOUNT_BAD,
    RESPONSE_JSON_AUTH,
    RESPONSE_JSON_BOOTSTRAP_ACCOUNTS,
    RESPONSE_JSON_DAILY_USAGE,
    RESPONSE_JSON_FOOTPRINT,
    RESPONSE_JSON_HALF_HOURLY_USAGE,
    RESPONSE_JSON_INTENSITY,
    RESPONSE_JSON_TOKEN,
)


@pytest.fixture(autouse=True)
def mock_aioresponse():
    """Return a client session."""
    with aioresponses() as mocker:
        mocker.post(
            AUTH_LOGIN_URL,
            payload=RESPONSE_JSON_AUTH,
            status=200,
            repeat=True,
        )
        mocker.get(
            AUTH_TOKEN_URL,
            payload=RESPONSE_JSON_TOKEN,
            status=200,
            repeat=True,
        )
        mocker.post(
            BOOTSTRAP_GRAPHQL_URL,
            payload=RESPONSE_JSON_BOOTSTRAP_ACCOUNTS,
            status=200,
            repeat=True,
        )
        mocker.get(
            f"{USAGE_DAILY_URL}/{ACCOUNT}?date=2024-01",
            payload=RESPONSE_JSON_DAILY_USAGE,
            status=200,
            repeat=True,
        )
        mocker.get(
            f"{USAGE_HALF_HOURLY_URL}/{ACCOUNT}?date=2024-01-01",
            payload=RESPONSE_JSON_HALF_HOURLY_USAGE,
            status=200,
            repeat=True,
        )
        mocker.get(
            f"{CARBON_FOOTPRINT_URL}/{ACCOUNT}/footprint",
            payload=RESPONSE_JSON_FOOTPRINT,
            status=200,
            repeat=True,
        )
        mocker.get(
            f"{USAGE_DAILY_URL}/{ACCOUNT_BAD}?date=2024-01",
            status=404,
            repeat=True,
        )
        mocker.get(
            CARBON_INTENSITY_URL,
            payload=RESPONSE_JSON_INTENSITY,
            status=200,
            repeat=True,
        )

        yield mocker


@pytest.fixture
async def ovoenergy_client() -> AsyncGenerator[OVOEnergy, None]:
    """Return a OVOEnergy client."""
    async with ClientSession() as session:
        yield OVOEnergy(client_session=session)