Skip to content

Commit

Permalink
Migrate away from asynctest
Browse files Browse the repository at this point in the history
  • Loading branch information
rajlaud committed Oct 26, 2020
1 parent d6665da commit a8e3cf9
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 19 deletions.
4 changes: 2 additions & 2 deletions tests/test_discovery.py
Expand Up @@ -2,10 +2,10 @@
attempt to cover code that is covered by the live discovery test in test_integration.py."""

import logging
from unittest.mock import AsyncMock, Mock, patch

import pysqueezebox
import pytest
from asynctest import CoroutineMock, Mock, patch

# pylint: disable=C0103
# All test coroutines will be treated as marked.
Expand All @@ -32,7 +32,7 @@ async def test_bad_response():
async def test_callbacks():
"""Test detection and handling of both sync and async callbacks."""
callback = Mock()
async_callback = CoroutineMock()
async_callback = AsyncMock()

with patch(
"pysqueezebox.discovery._unpack_discovery_response", return_value=RESPONSE
Expand Down
15 changes: 5 additions & 10 deletions tests/test_player.py
@@ -1,10 +1,9 @@
"""
The following tests check the pysqueezebox.Player module while mocking I/O.
"""
from unittest.mock import call, patch
from unittest.mock import AsyncMock, call, patch

import pytest
from asynctest import CoroutineMock
from pysqueezebox import Player, Server

# pylint: disable=C0103
Expand Down Expand Up @@ -34,7 +33,7 @@ async def test_image_url():

async def test_wait():
"""Test player._wait_for_property()."""
with patch.object(Player, "async_update", CoroutineMock()):
with patch.object(Player, "async_update", AsyncMock()):
mock_player = Player(None, "00:11:22:33:44:55", "Test Player")
await mock_player._wait_for_property(None, None, 0)
mock_player.async_update.assert_not_called()
Expand All @@ -47,13 +46,9 @@ async def test_wait():

async def test_verified_pause():
"""Test player._verified_pause_stop."""
with patch.object(
Player, "async_query", CoroutineMock(return_val=True)
), patch.object(
Player, "async_update", CoroutineMock(return_val=True)
), patch.object(
Player, "mode", "play"
):
with patch.object(Player, "async_query", AsyncMock(return_val=True)), patch.object(
Player, "async_update", AsyncMock(return_val=True)
), patch.object(Player, "mode", "play"):
mock_player = Player(None, "11:22:33:44:55", "Test Player")
assert not await mock_player.async_pause(timeout=0.1)
pause_args = ["pause", "1"]
Expand Down
13 changes: 6 additions & 7 deletions tests/test_server.py
Expand Up @@ -2,11 +2,10 @@
The following tests check the pysqueezebox.Server module while mocking I/O.
"""
import asyncio
from unittest.mock import Mock, patch
from unittest.mock import AsyncMock, Mock, patch

import pytest
from aiohttp import ClientSession
from asynctest import CoroutineMock
from pysqueezebox import Server

# pylint: disable=C0103
Expand All @@ -16,7 +15,7 @@

async def test_get_players():
"""Test async_get_players() method."""
with patch.object(Server, "async_query", CoroutineMock(return_value=False)):
with patch.object(Server, "async_query", AsyncMock(return_value=False)):
mock_lms = Server(None, None)
await mock_lms.async_get_players()
assert await mock_lms.async_get_player() is None
Expand All @@ -29,18 +28,18 @@ async def test_async_query():
assert await lms.async_query("serverstatus")

response = Mock(status="404", text="could not find page")
with patch.object(ClientSession, "post", CoroutineMock(return_value=response)):
with patch.object(ClientSession, "post", AsyncMock(return_value=response)):
mock_lms = Server(ClientSession(), None)
assert not await mock_lms.async_query("serverstatus")

with patch.object(
ClientSession, "post", CoroutineMock(side_effect=asyncio.TimeoutError)
ClientSession, "post", AsyncMock(side_effect=asyncio.TimeoutError)
):
mock_lms = Server(ClientSession(), None)
assert not await mock_lms.async_query("serverstatus")

data = {"bogus_key": "bogus_value"}
response = Mock(status=200, json=CoroutineMock(return_value=data))
with patch.object(ClientSession, "post", CoroutineMock(return_value=response)):
response = Mock(status=200, json=AsyncMock(return_value=data))
with patch.object(ClientSession, "post", AsyncMock(return_value=response)):
mock_lms = Server(ClientSession(), None)
assert not await mock_lms.async_query("serverstatus")

0 comments on commit a8e3cf9

Please sign in to comment.