Skip to content

Commit

Permalink
Merge pull request #12 from justmobilize/test-updates
Browse files Browse the repository at this point in the history
Update how we test modules
  • Loading branch information
dhalbert authored Apr 25, 2024
2 parents 0c77819 + e76b13c commit 9322a01
Show file tree
Hide file tree
Showing 4 changed files with 103 additions and 34 deletions.
60 changes: 45 additions & 15 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,25 +7,55 @@
import sys

import mocket
import pytest


# pylint: disable=unused-argument
def set_interface(iface):
"""Helper to set the global internet interface"""


socketpool_module = type(sys)("socketpool")
socketpool_module.SocketPool = mocket.MocketPool
sys.modules["socketpool"] = socketpool_module

esp32spi_module = type(sys)("adafruit_esp32spi")
esp32spi_socket_module = type(sys)("adafruit_esp32spi_socket")
esp32spi_socket_module.set_interface = set_interface
sys.modules["adafruit_esp32spi"] = esp32spi_module
sys.modules["adafruit_esp32spi.adafruit_esp32spi_socket"] = esp32spi_socket_module

wiznet5k_module = type(sys)("adafruit_wiznet5k")
wiznet5k_socket_module = type(sys)("adafruit_wiznet5k_socket")
wiznet5k_socket_module.set_interface = set_interface
sys.modules["adafruit_wiznet5k"] = wiznet5k_module
sys.modules["adafruit_wiznet5k.adafruit_wiznet5k_socket"] = wiznet5k_socket_module
@pytest.fixture
def circuitpython_socketpool_module():
socketpool_module = type(sys)("socketpool")
socketpool_module.SocketPool = mocket.MocketPool
sys.modules["socketpool"] = socketpool_module
yield
del sys.modules["socketpool"]


@pytest.fixture
def adafruit_esp32spi_socket_module():
esp32spi_module = type(sys)("adafruit_esp32spi")
esp32spi_socket_module = type(sys)("adafruit_esp32spi_socket")
esp32spi_socket_module.set_interface = set_interface
sys.modules["adafruit_esp32spi"] = esp32spi_module
sys.modules["adafruit_esp32spi.adafruit_esp32spi_socket"] = esp32spi_socket_module
yield
del sys.modules["adafruit_esp32spi"]
del sys.modules["adafruit_esp32spi.adafruit_esp32spi_socket"]


@pytest.fixture
def adafruit_wiznet5k_socket_module():
wiznet5k_module = type(sys)("adafruit_wiznet5k")
wiznet5k_socket_module = type(sys)("adafruit_wiznet5k_socket")
wiznet5k_socket_module.set_interface = set_interface
wiznet5k_socket_module.SOCK_STREAM = 0x21
sys.modules["adafruit_wiznet5k"] = wiznet5k_module
sys.modules["adafruit_wiznet5k.adafruit_wiznet5k_socket"] = wiznet5k_socket_module
yield
del sys.modules["adafruit_wiznet5k"]
del sys.modules["adafruit_wiznet5k.adafruit_wiznet5k_socket"]


@pytest.fixture(autouse=True)
def reset_connection_manager(monkeypatch):
monkeypatch.setattr(
"adafruit_connection_manager._global_socketpool",
{},
)
monkeypatch.setattr(
"adafruit_connection_manager._global_ssl_contexts",
{},
)
39 changes: 29 additions & 10 deletions tests/get_radio_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,28 +5,36 @@
""" Get socketpool and ssl_context Tests """

import ssl
from unittest import mock

import mocket
import pytest

import adafruit_connection_manager


def test_get_radio_socketpool_wifi():
def test_get_radio_socketpool_wifi( # pylint: disable=unused-argument
circuitpython_socketpool_module,
):
radio = mocket.MockRadio.Radio()
socket_pool = adafruit_connection_manager.get_radio_socketpool(radio)
assert isinstance(socket_pool, mocket.MocketPool)


def test_get_radio_socketpool_esp32spi():
def test_get_radio_socketpool_esp32spi( # pylint: disable=unused-argument
adafruit_esp32spi_socket_module,
):
radio = mocket.MockRadio.ESP_SPIcontrol()
socket_pool = adafruit_connection_manager.get_radio_socketpool(radio)
assert socket_pool.__name__ == "adafruit_esp32spi_socket"


def test_get_radio_socketpool_wiznet5k():
def test_get_radio_socketpool_wiznet5k( # pylint: disable=unused-argument
adafruit_wiznet5k_socket_module,
):
radio = mocket.MockRadio.WIZNET5K()
socket_pool = adafruit_connection_manager.get_radio_socketpool(radio)
with mock.patch("sys.implementation", return_value=[9, 0, 0]):
socket_pool = adafruit_connection_manager.get_radio_socketpool(radio)
assert socket_pool.__name__ == "adafruit_wiznet5k_socket"


Expand All @@ -37,28 +45,37 @@ def test_get_radio_socketpool_unsupported():
assert "Unsupported radio class" in str(context)


def test_get_radio_socketpool_returns_same_one():
def test_get_radio_socketpool_returns_same_one( # pylint: disable=unused-argument
circuitpython_socketpool_module,
):
radio = mocket.MockRadio.Radio()
socket_pool_1 = adafruit_connection_manager.get_radio_socketpool(radio)
socket_pool_2 = adafruit_connection_manager.get_radio_socketpool(radio)
assert socket_pool_1 == socket_pool_2


def test_get_radio_ssl_context_wifi():
def test_get_radio_ssl_context_wifi( # pylint: disable=unused-argument
circuitpython_socketpool_module,
):
radio = mocket.MockRadio.Radio()
ssl_contexts = adafruit_connection_manager.get_radio_ssl_context(radio)
assert isinstance(ssl_contexts, ssl.SSLContext)


def test_get_radio_ssl_context_esp32spi():
def test_get_radio_ssl_context_esp32spi( # pylint: disable=unused-argument
adafruit_esp32spi_socket_module,
):
radio = mocket.MockRadio.ESP_SPIcontrol()
ssl_contexts = adafruit_connection_manager.get_radio_ssl_context(radio)
assert isinstance(ssl_contexts, adafruit_connection_manager._FakeSSLContext)


def test_get_radio_ssl_context_wiznet5k():
def test_get_radio_ssl_context_wiznet5k( # pylint: disable=unused-argument
adafruit_wiznet5k_socket_module,
):
radio = mocket.MockRadio.WIZNET5K()
ssl_contexts = adafruit_connection_manager.get_radio_ssl_context(radio)
with mock.patch("sys.implementation", return_value=[9, 0, 0]):
ssl_contexts = adafruit_connection_manager.get_radio_ssl_context(radio)
assert isinstance(ssl_contexts, adafruit_connection_manager._FakeSSLContext)


Expand All @@ -69,7 +86,9 @@ def test_get_radio_ssl_context_unsupported():
assert "Unsupported radio class" in str(context)


def test_get_radio_ssl_context_returns_same_one():
def test_get_radio_ssl_context_returns_same_one( # pylint: disable=unused-argument
circuitpython_socketpool_module,
):
radio = mocket.MockRadio.Radio()
ssl_contexts_1 = adafruit_connection_manager.get_radio_ssl_context(radio)
ssl_contexts_2 = adafruit_connection_manager.get_radio_ssl_context(radio)
Expand Down
8 changes: 6 additions & 2 deletions tests/get_socket_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,9 @@ def test_get_socket_runtime_error_ties_again_only_once():
free_sockets_mock.assert_called_once()


def test_fake_ssl_context_connect():
def test_fake_ssl_context_connect( # pylint: disable=unused-argument
adafruit_esp32spi_socket_module,
):
mock_pool = mocket.MocketPool()
mock_socket_1 = mocket.Mocket()
mock_pool.socket.return_value = mock_socket_1
Expand All @@ -234,7 +236,9 @@ def test_fake_ssl_context_connect():
socket._socket.connect.assert_called_once()


def test_fake_ssl_context_connect_error():
def test_fake_ssl_context_connect_error( # pylint: disable=unused-argument
adafruit_esp32spi_socket_module,
):
mock_pool = mocket.MocketPool()
mock_socket_1 = mocket.Mocket()
mock_pool.socket.return_value = mock_socket_1
Expand Down
30 changes: 23 additions & 7 deletions tests/fake_ssl_context_test.py → tests/ssl_context_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,26 @@
#
# SPDX-License-Identifier: Unlicense

""" FakeSLLSocket Tests """
""" SLL Context Tests """

import ssl

import mocket
import pytest

import adafruit_connection_manager


def test_connect_https():
def test_connect_esp32spi_https( # pylint: disable=unused-argument
adafruit_esp32spi_socket_module,
):
mock_pool = mocket.MocketPool()
mock_socket_1 = mocket.Mocket()
mock_pool.socket.return_value = mock_socket_1
mock_socket_2 = mocket.Mocket()
mock_pool.socket.side_effect = [
mock_socket_1,
mock_socket_2,
]

radio = mocket.MockRadio.ESP_SPIcontrol()
ssl_context = adafruit_connection_manager.get_radio_ssl_context(radio)
Expand All @@ -24,15 +32,23 @@ def test_connect_https():
mocket.MOCK_HOST_1, 443, "https:", ssl_context=ssl_context
)
assert socket != mock_socket_1
assert socket != mock_socket_2
assert socket._socket == mock_socket_1
assert isinstance(socket, adafruit_connection_manager._FakeSSLSocket)


def test_connect_https_not_supported():
mock_pool = mocket.MocketPool()
mock_socket_1 = mocket.Mocket()
mock_pool.socket.return_value = mock_socket_1
def test_connect_wifi_https( # pylint: disable=unused-argument
circuitpython_socketpool_module,
):
radio = mocket.MockRadio.Radio()
ssl_context = adafruit_connection_manager.get_radio_ssl_context(radio)
assert isinstance(ssl_context, ssl.SSLContext)


def test_connect_wiznet5k_https_not_supported( # pylint: disable=unused-argument
adafruit_wiznet5k_socket_module,
):
mock_pool = mocket.MocketPool()
radio = mocket.MockRadio.WIZNET5K()
ssl_context = adafruit_connection_manager.get_radio_ssl_context(radio)
connection_manager = adafruit_connection_manager.ConnectionManager(mock_pool)
Expand Down

0 comments on commit 9322a01

Please sign in to comment.