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
|
from __future__ import annotations
from unittest.mock import patch
import pytest
from zeroconf.asyncio import AsyncZeroconf
from aioesphomeapi.zeroconf import ZeroconfManager
from .common import get_mock_async_zeroconf
@pytest.mark.asyncio
async def test_does_not_closed_passed_in_async_instance(async_zeroconf: AsyncZeroconf):
"""Test that the passed in instance is not closed."""
manager = ZeroconfManager()
manager.set_instance(async_zeroconf)
await manager.async_close()
assert async_zeroconf.async_close.call_count == 0
@pytest.mark.asyncio
async def test_does_not_closed_passed_in_sync_instance(async_zeroconf: AsyncZeroconf):
"""Test that the passed in instance is not closed."""
manager = ZeroconfManager()
manager.set_instance(async_zeroconf.zeroconf)
await manager.async_close()
assert async_zeroconf.async_close.call_count == 0
@pytest.mark.asyncio
async def test_closes_created_instance(async_zeroconf: AsyncZeroconf):
"""Test that the created instance is closed."""
with patch("aioesphomeapi.zeroconf.AsyncZeroconf", return_value=async_zeroconf):
manager = ZeroconfManager()
assert manager.get_async_zeroconf() is async_zeroconf
await manager.async_close()
assert async_zeroconf.async_close.call_count == 1
@pytest.mark.asyncio
async def test_runtime_error_multiple_instances(async_zeroconf: AsyncZeroconf):
"""Test runtime error is raised on multiple instances."""
manager = ZeroconfManager(async_zeroconf)
new_instance = get_mock_async_zeroconf()
with pytest.raises(RuntimeError):
manager.set_instance(new_instance)
manager.set_instance(async_zeroconf)
manager.set_instance(async_zeroconf.zeroconf)
manager.set_instance(async_zeroconf)
await manager.async_close()
assert async_zeroconf.async_close.call_count == 0
|