Skip to content

Commit

Permalink
Make core tests that should run in the event loop coros
Browse files Browse the repository at this point in the history
  • Loading branch information
bdraco committed Feb 27, 2024
1 parent d812507 commit 5949489
Showing 1 changed file with 18 additions and 24 deletions.
42 changes: 18 additions & 24 deletions tests/test_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ def test_split_entity_id() -> None:
ha.split_entity_id(".empty_domain")


def test_async_add_hass_job_schedule_callback() -> None:
async def test_async_add_hass_job_schedule_callback() -> None:
"""Test that we schedule callbacks and add jobs to the job pool."""
hass = MagicMock()
job = MagicMock()
Expand All @@ -96,7 +96,7 @@ def test_async_add_hass_job_schedule_callback() -> None:
assert len(hass.add_job.mock_calls) == 0


def test_async_add_hass_job_coro_named(hass: HomeAssistant) -> None:
async def test_async_add_hass_job_coro_named(hass: HomeAssistant) -> None:
"""Test that we schedule coroutines and add jobs to the job pool with a name."""

async def mycoro():
Expand All @@ -109,7 +109,7 @@ async def mycoro():
assert "named coro" in str(task)


def test_async_add_hass_job_schedule_partial_callback() -> None:
async def test_async_add_hass_job_schedule_partial_callback() -> None:
"""Test that we schedule partial coros and add jobs to the job pool."""
hass = MagicMock()
job = MagicMock()
Expand All @@ -121,9 +121,9 @@ def test_async_add_hass_job_schedule_partial_callback() -> None:
assert len(hass.add_job.mock_calls) == 0


def test_async_add_hass_job_schedule_coroutinefunction(event_loop) -> None:
async def test_async_add_hass_job_schedule_coroutinefunction() -> None:
"""Test that we schedule coroutines and add jobs to the job pool."""
hass = MagicMock(loop=MagicMock(wraps=event_loop))
hass = MagicMock(loop=MagicMock(wraps=asyncio.get_running_loop()))

async def job():
pass
Expand All @@ -134,9 +134,9 @@ async def job():
assert len(hass.add_job.mock_calls) == 0


def test_async_add_hass_job_schedule_partial_coroutinefunction(event_loop) -> None:
async def test_async_add_hass_job_schedule_partial_coroutinefunction() -> None:
"""Test that we schedule partial coros and add jobs to the job pool."""
hass = MagicMock(loop=MagicMock(wraps=event_loop))
hass = MagicMock(loop=MagicMock(wraps=asyncio.get_running_loop()))

async def job():
pass
Expand All @@ -149,7 +149,7 @@ async def job():
assert len(hass.add_job.mock_calls) == 0


def test_async_add_job_add_hass_threaded_job_to_pool() -> None:
async def test_async_add_job_add_hass_threaded_job_to_pool() -> None:
"""Test that we schedule coroutines and add jobs to the job pool."""
hass = MagicMock()

Expand All @@ -162,11 +162,9 @@ def job():
assert len(hass.loop.run_in_executor.mock_calls) == 2


def test_async_create_task_schedule_coroutine(
event_loop: asyncio.AbstractEventLoop,
) -> None:
async def test_async_create_task_schedule_coroutine() -> None:
"""Test that we schedule coroutines and add jobs to the job pool."""
hass = MagicMock(loop=MagicMock(wraps=event_loop))
hass = MagicMock(loop=MagicMock(wraps=asyncio.get_running_loop()))

async def job():
pass
Expand All @@ -180,11 +178,9 @@ async def job():
@pytest.mark.skipif(
sys.version_info < (3, 12), reason="eager_start is only supported for Python 3.12"
)
def test_async_create_task_eager_start_schedule_coroutine(
event_loop: asyncio.AbstractEventLoop,
) -> None:
async def test_async_create_task_eager_start_schedule_coroutine() -> None:
"""Test that we schedule coroutines and add jobs to the job pool."""
hass = MagicMock(loop=MagicMock(wraps=event_loop))
hass = MagicMock(loop=MagicMock(wraps=asyncio.get_running_loop()))

async def job():
pass
Expand All @@ -198,11 +194,9 @@ async def job():
@pytest.mark.skipif(
sys.version_info >= (3, 12), reason="eager_start is not supported on < 3.12"
)
def test_async_create_task_eager_start_fallback_schedule_coroutine(
event_loop: asyncio.AbstractEventLoop,
) -> None:
async def test_async_create_task_eager_start_fallback_schedule_coroutine() -> None:
"""Test that we schedule coroutines and add jobs to the job pool."""
hass = MagicMock(loop=MagicMock(wraps=event_loop))
hass = MagicMock(loop=MagicMock(wraps=asyncio.get_running_loop()))

async def job():
pass
Expand All @@ -215,9 +209,9 @@ async def job():
assert len(hass.add_job.mock_calls) == 0


def test_async_create_task_schedule_coroutine_with_name(event_loop) -> None:
async def test_async_create_task_schedule_coroutine_with_name() -> None:
"""Test that we schedule coroutines and add jobs to the job pool with a name."""
hass = MagicMock(loop=MagicMock(wraps=event_loop))
hass = MagicMock(loop=MagicMock(wraps=asyncio.get_running_loop()))

async def job():
pass
Expand All @@ -229,7 +223,7 @@ async def job():
assert "named task" in str(task)


def test_async_run_hass_job_calls_callback() -> None:
async def test_async_run_hass_job_calls_callback() -> None:
"""Test that the callback annotation is respected."""
hass = MagicMock()
calls = []
Expand All @@ -242,7 +236,7 @@ def job():
assert len(hass.async_add_job.mock_calls) == 0


def test_async_run_hass_job_delegates_non_async() -> None:
async def test_async_run_hass_job_delegates_non_async() -> None:
"""Test that the callback annotation is respected."""
hass = MagicMock()
calls = []
Expand Down

0 comments on commit 5949489

Please sign in to comment.