Skip to content

Commit

Permalink
Clean up asyncio deprecations
Browse files Browse the repository at this point in the history
  • Loading branch information
linuxdaemon committed Mar 15, 2024
1 parent d70f1b9 commit d53b8dc
Show file tree
Hide file tree
Showing 15 changed files with 92 additions and 110 deletions.
2 changes: 1 addition & 1 deletion requirements-dev.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ mypy == 1.9.0
pre-commit == 3.3.3
pylint == 3.1.0
pytest == 8.1.1
pytest-asyncio == 0.20.3
pytest-asyncio == 0.23.5.post1
pytest-cov == 4.1.0
pytest-random-order == 1.1.1
responses == 0.25.0
Expand Down
28 changes: 14 additions & 14 deletions tests/core_tests/irc_client_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -440,8 +440,8 @@ def test_parse_pm_privmsg(self, caplog_bot, event_loop):


class TestConnect:
async def make_client(self, event_loop) -> irc.IrcClient:
bot = MagicMock(loop=event_loop, config={})
async def make_client(self) -> irc.IrcClient:
bot = MagicMock(loop=asyncio.get_running_loop(), config={})
conn_config = {
"connection": {
"server": "host.invalid",
Expand All @@ -457,8 +457,8 @@ async def make_client(self, event_loop) -> irc.IrcClient:
return client

@pytest.mark.asyncio()
async def test_exc(self, caplog_bot, event_loop):
client = await self.make_client(event_loop)
async def test_exc(self, caplog_bot):
client = await self.make_client()
runs = 0

# noinspection PyUnusedLocal
Expand Down Expand Up @@ -520,8 +520,8 @@ async def connect(timeout):
assert client.bot.mock_calls == []

@pytest.mark.asyncio()
async def test_timeout_exc(self, caplog_bot, event_loop):
client = await self.make_client(event_loop)
async def test_timeout_exc(self, caplog_bot):
client = await self.make_client()
runs = 0

# noinspection PyUnusedLocal
Expand Down Expand Up @@ -578,8 +578,8 @@ async def connect(timeout):
assert client.bot.mock_calls == []

@pytest.mark.asyncio()
async def test_other_exc(self, caplog_bot, event_loop):
client = await self.make_client(event_loop)
async def test_other_exc(self, caplog_bot):
client = await self.make_client()

client.connect = AsyncMock() # type: ignore
client.connect.side_effect = Exception("foo")
Expand All @@ -605,8 +605,8 @@ async def test_other_exc(self, caplog_bot, event_loop):
assert client.bot.mock_calls == []

@pytest.mark.asyncio()
async def test_one_connect(self, caplog_bot, event_loop):
client = await self.make_client(event_loop)
async def test_one_connect(self, caplog_bot):
client = await self.make_client()

async def _connect(timeout=5):
await asyncio.sleep(timeout)
Expand Down Expand Up @@ -636,8 +636,8 @@ async def _connect(timeout=5):
assert client.bot.mock_calls == []

@pytest.mark.asyncio()
async def test_create_socket(self, caplog_bot, event_loop):
client = await self.make_client(event_loop)
async def test_create_socket(self, caplog_bot):
client = await self.make_client()
client.loop.create_connection = mock = MagicMock()
fut: "Future[Tuple[None, None]]" = asyncio.Future(loop=client.loop)
fut.set_result((None, None))
Expand Down Expand Up @@ -668,8 +668,8 @@ async def test_create_socket(self, caplog_bot, event_loop):

class TestSend:
@pytest.mark.asyncio()
async def test_send_sieve_error(self, caplog_bot, event_loop):
conn = make_mock_conn(event_loop=event_loop)
async def test_send_sieve_error(self, caplog_bot):
conn = make_mock_conn(event_loop=asyncio.get_running_loop())
proto = irc._IrcProtocol(conn)
proto.connection_made(MagicMock())
sieve = object()
Expand Down
22 changes: 10 additions & 12 deletions tests/core_tests/reloader_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@

class TestConfigReload:
@pytest.mark.asyncio()
async def test_reload(self, mock_bot_factory, tmp_path, event_loop) -> None:
async def test_reload(self, mock_bot_factory, tmp_path) -> None:
config_file = tmp_path / "config.json"
config_file.touch()
bot = mock_bot_factory(loop=event_loop)
bot = mock_bot_factory()
reloader = ConfigReloader(bot)
bot.running = True
with patch.object(
Expand All @@ -25,12 +25,10 @@ async def test_reload(self, mock_bot_factory, tmp_path, event_loop) -> None:
assert mocked.mock_calls == [call()]

@pytest.mark.asyncio()
async def test_reload_not_running(
self, mock_bot_factory, tmp_path, event_loop
):
async def test_reload_not_running(self, mock_bot_factory, tmp_path):
config_file = tmp_path / "config.json"
config_file.touch()
bot = mock_bot_factory(loop=event_loop)
bot = mock_bot_factory()
reloader = ConfigReloader(bot)
bot.running = False
with patch.object(
Expand All @@ -45,12 +43,12 @@ async def test_reload_not_running(

class TestPluginReload:
@pytest.mark.asyncio()
async def test_reload(self, mock_bot_factory, tmp_path, event_loop):
async def test_reload(self, mock_bot_factory, tmp_path):
plugin_dir = tmp_path / "plugins"
plugin_dir.mkdir()
plugin_file = plugin_dir / "plugin.py"
plugin_file.touch()
bot = mock_bot_factory(loop=event_loop)
bot = mock_bot_factory()
reloader = PluginReloader(bot)
with patch.object(
reloader, "_reload", new_callable=AsyncMock
Expand All @@ -62,11 +60,11 @@ async def test_reload(self, mock_bot_factory, tmp_path, event_loop):
assert mocked.mock_calls == [call(Path(str(plugin_file)))]

@pytest.mark.asyncio()
async def test_reload_no_path(self, mock_bot_factory, tmp_path, event_loop):
async def test_reload_no_path(self, mock_bot_factory, tmp_path):
plugin_dir = tmp_path / "plugins"
plugin_dir.mkdir()
plugin_file = plugin_dir / "plugin.py"
bot = mock_bot_factory(loop=event_loop)
bot = mock_bot_factory()
reloader = PluginReloader(bot)
with patch.object(
reloader, "_reload", new_callable=AsyncMock
Expand All @@ -78,12 +76,12 @@ async def test_reload_no_path(self, mock_bot_factory, tmp_path, event_loop):
assert mocked.mock_calls == []

@pytest.mark.asyncio()
async def test_unload(self, mock_bot_factory, tmp_path, event_loop):
async def test_unload(self, mock_bot_factory, tmp_path):
plugin_dir = tmp_path / "plugins"
plugin_dir.mkdir()
plugin_file = plugin_dir / "plugin.py"
plugin_file.touch()
bot = mock_bot_factory(loop=event_loop)
bot = mock_bot_factory()
reloader = PluginReloader(bot)
with patch.object(
reloader, "_unload", new_callable=AsyncMock
Expand Down
47 changes: 21 additions & 26 deletions tests/core_tests/test_bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,7 @@


@pytest.mark.asyncio()
async def test_migrate_db(
mock_db, mock_bot_factory, event_loop, mock_requests, tmp_path
):
async def test_migrate_db(mock_db, mock_bot_factory, mock_requests, tmp_path):
old_db_url = "sqlite:///" + str(tmp_path / "database1.db")
old_db = MockDB(old_db_url, True)
table = Table(
Expand All @@ -38,7 +36,6 @@ async def test_migrate_db(
table.create(old_db.engine)
other_table.create(old_db.engine)
mock_bot = mock_bot_factory(
loop=event_loop,
db=mock_db,
config={"old_database": old_db_url, "migrate_db": True},
)
Expand Down Expand Up @@ -67,8 +64,8 @@ async def test_migrate_db(


@pytest.mark.asyncio()
async def test_connect_clients(mock_bot_factory, event_loop):
bot = mock_bot_factory(loop=event_loop)
async def test_connect_clients(mock_bot_factory):
bot = mock_bot_factory()
conn = MockConn()
bot.connections = {"foo": conn}
future = bot.loop.create_future()
Expand Down Expand Up @@ -114,8 +111,8 @@ def __init__(self, nick=None):

class TestProcessing:
@pytest.mark.asyncio()
async def test_irc_catch_all(self, mock_bot_factory, event_loop) -> None:
bot = mock_bot_factory(loop=event_loop)
async def test_irc_catch_all(self, mock_bot_factory) -> None:
bot = mock_bot_factory()
conn = MockConn(nick="bot")
event = Event(
irc_command="PRIVMSG",
Expand Down Expand Up @@ -146,10 +143,8 @@ async def coro(hook):
)

@pytest.mark.asyncio()
async def test_irc_catch_all_block(
self, mock_bot_factory, event_loop
) -> None:
bot = mock_bot_factory(loop=event_loop)
async def test_irc_catch_all_block(self, mock_bot_factory) -> None:
bot = mock_bot_factory()
conn = MockConn(nick="bot")
event = Event(
irc_command="PRIVMSG",
Expand Down Expand Up @@ -186,8 +181,8 @@ async def coro1(hook): # pragma: no cover
)

@pytest.mark.asyncio()
async def test_command(self, mock_bot_factory, event_loop) -> None:
bot = mock_bot_factory(loop=event_loop)
async def test_command(self, mock_bot_factory) -> None:
bot = mock_bot_factory()
conn = MockConn(nick="bot")
event = Event(
irc_command="PRIVMSG",
Expand Down Expand Up @@ -220,8 +215,8 @@ async def coro(hook):
)

@pytest.mark.asyncio()
async def test_command_partial(self, mock_bot_factory, event_loop) -> None:
bot = mock_bot_factory(loop=event_loop)
async def test_command_partial(self, mock_bot_factory) -> None:
bot = mock_bot_factory()
conn = MockConn(nick="bot")
event = Event(
irc_command="PRIVMSG",
Expand Down Expand Up @@ -256,8 +251,8 @@ async def coro(hook): # pragma: no cover
]

@pytest.mark.asyncio()
async def test_event(self, mock_bot_factory, event_loop) -> None:
bot = mock_bot_factory(loop=event_loop)
async def test_event(self, mock_bot_factory) -> None:
bot = mock_bot_factory()
conn = MockConn(nick="bot")
event = Event(
irc_command="PRIVMSG",
Expand Down Expand Up @@ -291,8 +286,8 @@ async def coro(hook):
)

@pytest.mark.asyncio()
async def test_event_block(self, mock_bot_factory, event_loop) -> None:
bot = mock_bot_factory(loop=event_loop)
async def test_event_block(self, mock_bot_factory) -> None:
bot = mock_bot_factory()
conn = MockConn(nick="bot")
event = Event(
irc_command="PRIVMSG",
Expand Down Expand Up @@ -338,8 +333,8 @@ async def coro1(hook): # pragma: no cover
)

@pytest.mark.asyncio()
async def test_irc_raw(self, mock_bot_factory, event_loop) -> None:
bot = mock_bot_factory(loop=event_loop)
async def test_irc_raw(self, mock_bot_factory) -> None:
bot = mock_bot_factory()
conn = MockConn(nick="bot")
event = Event(
irc_command="PRIVMSG",
Expand Down Expand Up @@ -369,8 +364,8 @@ async def coro(hook):
)

@pytest.mark.asyncio()
async def test_irc_raw_block(self, mock_bot_factory, event_loop):
bot = mock_bot_factory(loop=event_loop)
async def test_irc_raw_block(self, mock_bot_factory):
bot = mock_bot_factory()
conn = MockConn(nick="bot")
event = Event(
irc_command="PRIVMSG",
Expand Down Expand Up @@ -408,8 +403,8 @@ async def coro1(hook): # pragma: no cover


@pytest.mark.asyncio()
async def test_reload_config(mock_bot_factory, event_loop):
bot = mock_bot_factory(loop=event_loop)
async def test_reload_config(mock_bot_factory):
bot = mock_bot_factory()
conn = MockConn()
bot.connections = {"foo": conn}
bot.config.load_config = MagicMock()
Expand Down
30 changes: 12 additions & 18 deletions tests/core_tests/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,6 @@
from cloudbot.client import Client


class Bot(MagicMock):
def __init__(self, loop, *args, **kw):
super().__init__(*args, **kw)
self.loop = loop


class MockClient(Client): # pylint: disable=abstract-method
_connected = False

Expand All @@ -37,21 +31,21 @@ async def connect(self, timeout=None):
raise ValueError("This is a test")


def test_reload(event_loop):
client = MockClient(Bot(event_loop), "foo", "foobot", channels=["#foo"])
def test_reload(mock_bot):
client = MockClient(mock_bot, "foo", "foobot", channels=["#foo"])
client.permissions = MagicMock()
client.reload()
assert client.permissions.mock_calls == [call.reload()]


def test_client_no_config(event_loop):
client = MockClient(Bot(event_loop), "foo", "foobot", channels=["#foo"])
def test_client_no_config(mock_bot):
client = MockClient(mock_bot, "foo", "foobot", channels=["#foo"])
assert client.config.get("a") is None


def test_client(event_loop):
def test_client(mock_bot):
client = MockClient(
Bot(event_loop),
mock_bot,
"foo",
"foobot",
channels=["#foo"],
Expand All @@ -70,10 +64,10 @@ def test_client(event_loop):
client.loop.run_until_complete(client.try_connect())


def test_client_connect_exc(event_loop):
def test_client_connect_exc(mock_bot):
with patch("random.randrange", return_value=1):
client = FailingMockClient(
Bot(event_loop),
mock_bot,
"foo",
"foobot",
channels=["#foo"],
Expand All @@ -84,9 +78,9 @@ def test_client_connect_exc(event_loop):


@pytest.mark.asyncio()
async def test_try_connect(event_loop):
async def test_try_connect(mock_bot):
client = MockClient(
Bot(event_loop),
mock_bot,
"foo",
"foobot",
channels=["#foo"],
Expand All @@ -97,9 +91,9 @@ async def test_try_connect(event_loop):
await client.try_connect()


def test_auto_reconnect(event_loop):
def test_auto_reconnect(mock_bot):
client = MockClient(
Bot(event_loop),
mock_bot,
"foo",
"foobot",
channels=["#foo"],
Expand Down
11 changes: 5 additions & 6 deletions tests/core_tests/test_plugin_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,13 @@
from cloudbot.util import database
from tests.util.mock_module import MockModule


@pytest.fixture()
def mock_bot(mock_bot_factory, event_loop, tmp_path):
def mock_bot(mock_bot_factory, tmp_path):
tmp_base = tmp_path / "tmp"
tmp_base.mkdir(exist_ok=True)

yield mock_bot_factory(base_dir=tmp_base, loop=event_loop)
yield mock_bot_factory(base_dir=tmp_base)


@pytest.fixture()
Expand Down Expand Up @@ -765,11 +766,9 @@ async def post_hook(db):


@pytest.mark.asyncio
async def test_create_tables(
mock_bot_factory, caplog_bot, tmp_path, event_loop, mock_db
):
async def test_create_tables(mock_bot_factory, caplog_bot, tmp_path, mock_db):
db = mock_db
bot = mock_bot_factory(db=db, loop=event_loop)
bot = mock_bot_factory(db=db)
table = Table(
"test",
database.metadata,
Expand Down
Loading

0 comments on commit d53b8dc

Please sign in to comment.