File: test_helpers.py

package info (click to toggle)
python-homewizard-energy 10.0.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,104 kB
  • sloc: python: 3,523; makefile: 3
file content (68 lines) | stat: -rw-r--r-- 1,594 bytes parent folder | download | duplicates (2)
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
"""Test the helper functions."""

import asyncio
from unittest.mock import AsyncMock

import pytest
from aiohttp import ClientSession

from homewizard_energy import has_v2_api

pytestmark = [pytest.mark.asyncio]


async def test_has_v2_api_true(aresponses):
    """Test if has_v2_api returns True for a v2 device."""

    aresponses.add(
        "example.com",
        "/api",
        "GET",
        aresponses.Response(
            status=401,
        ),
    )

    async with ClientSession() as session:
        result = await has_v2_api("example.com", session)
        assert result is True


async def test_has_v2_api_false(aresponses):
    """Test if has_v2_api returns False for a non-v2 device."""
    aresponses.add(
        "example.com",
        "/api",
        "GET",
        aresponses.Response(
            status=404,
        ),
    )

    async with ClientSession() as session:
        result = await has_v2_api("example.com", session)
        assert result is False


async def test_has_v2_api_exception():
    """Test if has_v2_api returns False when an exception occurs."""
    session = AsyncMock()
    session.get = AsyncMock(side_effect=asyncio.TimeoutError)

    result = await has_v2_api("example.com", session)
    assert result is False


async def test_has_v2_api_own_session(aresponses):
    """Test if has_v2_api opens and closes its own session."""
    aresponses.add(
        "example.com",
        "/api",
        "GET",
        aresponses.Response(
            status=401,
        ),
    )

    result = await has_v2_api("example.com")
    assert result is True