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
|
"""Tests for base"""
import asyncio
from socket import gaierror
from unittest.mock import patch
from aiohttp import ClientError
import pytest
from pyhaversion import (
HaVersion,
HaVersionFetchException,
HaVersionNotModifiedException,
HaVersionParseException,
)
@pytest.mark.asyncio
async def test_timeout_exception():
"""Test timeout exception."""
async def mocked_fetch_TimeoutError(*_, **__):
"""mocked"""
raise asyncio.TimeoutError
with patch("pyhaversion.local.HaVersionLocal.fetch", mocked_fetch_TimeoutError):
haversion = HaVersion()
with pytest.raises(HaVersionFetchException):
await haversion.get_version()
@pytest.mark.asyncio
async def test_fetch_exception():
"""Test fetch exception."""
haversion = HaVersion()
async def mocked_fetch_gaierror(*_, **__):
"""mocked"""
raise gaierror
async def mocked_fetch_ClientError(*_, **__):
"""mocked"""
raise ClientError
with patch("pyhaversion.local.HaVersionLocal.fetch", mocked_fetch_gaierror):
with pytest.raises(HaVersionFetchException):
await haversion.get_version()
with patch("pyhaversion.local.HaVersionLocal.fetch", mocked_fetch_ClientError):
with pytest.raises(HaVersionFetchException):
await haversion.get_version()
@pytest.mark.asyncio
async def test_parse_exception():
"""Test parse exception."""
haversion = HaVersion()
async def mocked_fetch(*_, **__):
"""mocked"""
pass
def mocked_parse_KeyError(*_):
"""mocked"""
raise KeyError
def mocked_parse_TypeError(*_):
"""mocked"""
raise TypeError
with patch("pyhaversion.local.HaVersionLocal.fetch", mocked_fetch):
with patch("pyhaversion.local.HaVersionLocal.parse", mocked_parse_KeyError):
with pytest.raises(HaVersionParseException):
await haversion.get_version()
with patch("pyhaversion.local.HaVersionLocal.parse", mocked_parse_TypeError):
with pytest.raises(HaVersionParseException):
await haversion.get_version()
@pytest.mark.asyncio
async def test_not_modified_exception():
"""Test not_modified exception."""
async def mocked_fetch_not_modified(*_, **__):
"""mocked"""
raise HaVersionNotModifiedException
with patch("pyhaversion.local.HaVersionLocal.fetch", mocked_fetch_not_modified):
haversion = HaVersion()
with pytest.raises(HaVersionNotModifiedException):
await haversion.get_version()
|