Skip to content

Commit

Permalink
add some tests
Browse files Browse the repository at this point in the history
  • Loading branch information
dlech committed May 24, 2024
1 parent 45cab2d commit 69b19e4
Show file tree
Hide file tree
Showing 3 changed files with 101 additions and 57 deletions.
89 changes: 34 additions & 55 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,8 @@ flake8 = "^5.0.0"
isort = "^5.13.2"

[tool.poetry.group.test.dependencies]
pytest = "^7.0.0"
pytest-asyncio = "^0.19.0"
pytest = "^8.2.1"
pytest-asyncio = "^0.23.7"
pytest-cov = "^3.0.0 "

[build-system]
Expand Down
65 changes: 65 additions & 0 deletions tests/bleak/backends/winrt/test_utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
#!/usr/bin/env python

"""Tests for `bleak.backends.winrt.util` package."""

import sys

import pytest

if not sys.platform.startswith("win"):
pytest.skip("skipping windows-only tests", allow_module_level=True)

from ctypes import windll, wintypes

from bleak.backends.winrt.util import _check_hresult, assert_mta
from bleak.exc import BleakError

# https://learn.microsoft.com/en-us/windows/win32/api/objbase/ne-objbase-coinit
COINIT_MULTITHREADED = 0x0
COINIT_APARTMENTTHREADED = 0x2

_CoInitializeEx = windll.ole32.CoInitializeEx
_CoInitializeEx.restype = wintypes.LONG
_CoInitializeEx.argtypes = [wintypes.LPVOID, wintypes.DWORD]
_CoInitializeEx.errcheck = _check_hresult

# https://learn.microsoft.com/en-us/windows/win32/api/combaseapi/nf-combaseapi-couninitialize
_CoUninitialize = windll.ole32.CoUninitialize
_CoUninitialize.restype = None
_CoUninitialize.argtypes = []


@pytest.mark.asyncio
async def test_assert_mta_no_init():
"""Test device_path_from_characteristic_path."""

await assert_mta()


@pytest.mark.asyncio
async def test_assert_mta_init_mta():
"""Test device_path_from_characteristic_path."""

_CoInitializeEx(None, COINIT_MULTITHREADED)

try:
await assert_mta()
assert hasattr(assert_mta, "_allowed")
finally:
_CoUninitialize()


@pytest.mark.asyncio
async def test_assert_mta_init_sta():
"""Test device_path_from_characteristic_path."""

_CoInitializeEx(None, COINIT_APARTMENTTHREADED)

try:
with pytest.raises(
BleakError,
match="Thread is configured for Windows GUI but callbacks are not working.",
):
await assert_mta()
finally:
_CoUninitialize()

0 comments on commit 69b19e4

Please sign in to comment.