conftest.py 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. # conftest.py/Open GoPro, Version 2.0 (C) Copyright 2021 GoPro, Inc. (http://gopro.com/OpenGoPro).
  2. # This copyright was auto-generated on Wed, Sep 1, 2021 5:05:52 PM
  3. # pylint: disable=redefined-outer-name
  4. import logging
  5. import re
  6. from pathlib import Path
  7. from typing import Any, Generator
  8. import pytest
  9. import pytest_asyncio
  10. from open_gopro.gopro_base import GoProBase
  11. from open_gopro.network.ble import (
  12. BleClient,
  13. Characteristic,
  14. Descriptor,
  15. GattDB,
  16. Service,
  17. UUIDs,
  18. )
  19. from open_gopro.network.ble.adapters.bleak_wrapper import BleakWrapperController
  20. from open_gopro.network.ble.services import CharProps
  21. from open_gopro.network.wifi import WifiClient
  22. from open_gopro.util.logger import set_logging_level, setup_logging
  23. from tests.mocks import (
  24. MockBleCommunicator,
  25. MockBleController,
  26. MockFeature,
  27. MockGoProMaintainBle,
  28. MockWifiCommunicator,
  29. MockWifiController,
  30. MockWiredGoPro,
  31. MockWirelessGoPro,
  32. )
  33. ##############################################################################################################
  34. # Log Management
  35. ##############################################################################################################
  36. logger = logging.getLogger(__name__)
  37. @pytest.fixture(scope="session", autouse=True)
  38. def configure_logging():
  39. global logger
  40. logger = setup_logging(logger)
  41. set_logging_level(logging.ERROR)
  42. @pytest.fixture(scope="module", autouse=True)
  43. def manage_logs(request):
  44. top_dir_stripped = Path(*Path(request.node.name).parts[1:])
  45. extension_changed = Path(str(top_dir_stripped).strip(".py") + ".log")
  46. request.config.pluginmanager.get_plugin("logging-plugin").set_log_path(
  47. Path(".reports") / "logs" / extension_changed
  48. )
  49. @pytest.fixture(scope="function", autouse=True)
  50. def test_log(request):
  51. logging.debug("################################################################################")
  52. logging.debug("Test '{}' STARTED".format(request.node.nodeid))
  53. logging.debug("################################################################################")
  54. # TODO. I think this is a bug in pytest-cov.
  55. @pytest.fixture(scope="function", autouse=True)
  56. def ignore_coroutine_never_awaited_warning():
  57. import warnings
  58. warnings.filterwarnings("ignore", message="coroutine 'enforce_message_rules' was never awaited")
  59. yield
  60. ##############################################################################################################
  61. # Bleak Unit Testing
  62. ##############################################################################################################
  63. @pytest_asyncio.fixture(scope="function")
  64. async def mock_bleak_wrapper():
  65. ble = BleakWrapperController()
  66. yield ble
  67. @pytest_asyncio.fixture(scope="function")
  68. async def mock_bleak_client():
  69. def disconnected_cb(_) -> None:
  70. print("Entered test disconnect callback")
  71. def notification_cb(handle: int, data: bytearray) -> None:
  72. print("Entered test notification callback")
  73. ble = BleClient(
  74. BleakWrapperController(),
  75. disconnected_cb,
  76. notification_cb,
  77. target=re.compile("###invalid_device###"), # type: ignore
  78. )
  79. await ble.open(timeout=30)
  80. print("GoPro Bleak opened!")
  81. yield ble
  82. await ble.close()
  83. ##############################################################################################################
  84. # GATT Database Unit Testing
  85. ##############################################################################################################
  86. @pytest.fixture()
  87. def mock_descriptor():
  88. yield Descriptor(0xABCD, UUIDs.CLIENT_CHAR_CONFIG)
  89. @pytest.fixture()
  90. def mock_characteristic(mock_descriptor: Descriptor) -> Generator[Characteristic, Any, None]:
  91. yield Characteristic(2, UUIDs.ACC_APPEARANCE, CharProps.READ, init_descriptors=[mock_descriptor])
  92. @pytest.fixture()
  93. def mock_service(mock_characteristic: Characteristic):
  94. yield Service(UUIDs.S_GENERIC_ACCESS, 3, init_chars=[mock_characteristic])
  95. @pytest.fixture()
  96. def mock_gatt_db(mock_service: Service):
  97. yield GattDB([mock_service])
  98. ##############################################################################################################
  99. # BLE Unit Testing
  100. ##############################################################################################################
  101. def disconnection_handler(_) -> None:
  102. print("Entered test disconnect callback")
  103. def notification_handler(handle: int, data: bytearray) -> None:
  104. print("Entered test notification callback")
  105. @pytest_asyncio.fixture(scope="function")
  106. async def mock_ble_client():
  107. test_client = BleClient(
  108. controller=MockBleController(),
  109. disconnected_cb=disconnection_handler,
  110. notification_cb=notification_handler, # type: ignore
  111. target=("device", []),
  112. )
  113. yield test_client
  114. @pytest_asyncio.fixture(scope="function")
  115. async def mock_ble_communicator(request):
  116. test_client = MockBleCommunicator("2.0")
  117. yield test_client
  118. ##############################################################################################################
  119. # WiFi Unit Testing
  120. ##############################################################################################################
  121. @pytest_asyncio.fixture(scope="function")
  122. async def mock_wifi_client():
  123. test_client = WifiClient(controller=MockWifiController())
  124. yield test_client
  125. @pytest_asyncio.fixture(scope="function")
  126. async def mock_wifi_communicator(request):
  127. test_client = MockWifiCommunicator("2.0")
  128. yield test_client
  129. ##############################################################################################################
  130. # GoPro Unit Testing
  131. ##############################################################################################################
  132. @pytest_asyncio.fixture(scope="function")
  133. async def mock_wired_gopro():
  134. test_client = MockWiredGoPro("2.0")
  135. yield test_client
  136. def mock_features(monkeypatch):
  137. monkeypatch.setattr("open_gopro.features.AccessPointFeature", MockFeature)
  138. monkeypatch.setattr("open_gopro.features.CohnFeature", MockFeature)
  139. monkeypatch.setattr("open_gopro.features.StreamFeature", MockFeature)
  140. @pytest_asyncio.fixture(scope="function")
  141. async def mock_wireless_gopro_basic(monkeypatch):
  142. mock_features(monkeypatch)
  143. test_client = MockWirelessGoPro("2.0")
  144. GoProBase.HTTP_GET_RETRIES = 1 # type: ignore
  145. try:
  146. yield test_client
  147. await test_client.close()
  148. except Exception as e:
  149. logger.error(f"PYTEST Error closing GoPro: {e}")
  150. @pytest_asyncio.fixture(scope="function")
  151. async def mock_wireless_gopro(monkeypatch):
  152. mock_features(monkeypatch)
  153. test_client = MockGoProMaintainBle()
  154. try:
  155. yield test_client
  156. await test_client.close()
  157. except Exception as e:
  158. logger.error(f"PYTEST Error closing GoPro: {e}")