Skip to content

Commit

Permalink
fix test
Browse files Browse the repository at this point in the history
  • Loading branch information
bdraco committed Mar 13, 2024
1 parent fa85088 commit 052dbf9
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 4 deletions.
8 changes: 5 additions & 3 deletions src/bluetooth_auto_recovery/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,14 @@ async def recover_adapter(hci: int, mac: str) -> bool:
the `bluetooth_auto_recovery.recover` module and calls
its `recover_adapter` function.
"""
if not (recover_module := _MODULE_CACHE.get("bluetooth_auto_recovery.recover")):
recover_module_name = f"{__package__}.recover"

if not (recover_module := _MODULE_CACHE.get(recover_module_name)):
loop = asyncio.get_running_loop()
recover_module = await loop.run_in_executor(
None, importlib.import_module, "bluetooth_auto_recovery.recover"
None, importlib.import_module, recover_module_name
)
_MODULE_CACHE["bluetooth_auto_recovery.recover"] = recover_module # type: ignore
_MODULE_CACHE[recover_module_name] = recover_module # type: ignore
this_module = sys.modules[__package__]
this_module.recover_adapter = recover_module.recover_adapter # type: ignore

Expand Down
7 changes: 6 additions & 1 deletion tests/test_init.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import pytest

import bluetooth_auto_recovery
from bluetooth_auto_recovery import recover


def test_init():
Expand All @@ -13,7 +14,11 @@ def test_init():
@pytest.mark.asyncio
async def test_recover_adapter():
"""Test the recover_adapter function."""
with patch("bluetooth_auto_recovery.recover_adapter") as recover_adapter:
assert bluetooth_auto_recovery.recover_adapter is not recover.recover_adapter

with patch("bluetooth_auto_recovery.recover.recover_adapter") as recover_adapter:
await bluetooth_auto_recovery.recover_adapter(0, "00:00:00:00:00:00")

assert recover_adapter.called
assert bluetooth_auto_recovery.recover_adapter is recover_adapter
bluetooth_auto_recovery.recover_adapter = recover.recover_adapter

0 comments on commit 052dbf9

Please sign in to comment.