Skip to content

Linting tests directory #15

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 23, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ repos:
- id: pylint
name: pylint (library code)
types: [python]
exclude: "^(docs/|examples/|setup.py$)"
exclude: "^(docs/|tests/|examples/|setup.py$)"
- repo: local
hooks:
- id: pylint_examples
Expand All @@ -32,3 +32,11 @@ repos:
entry: /usr/bin/env bash -c
args: ['([[ ! -d "examples" ]] || for example in $(find . -path "./examples/*.py"); do pylint --disable=missing-docstring,invalid-name $example; done)']
language: system
- repo: local
hooks:
- id: pylint_tests
name: pylint (tests code)
description: Run pylint rules on "tests/*.py" files
entry: /usr/bin/env bash -c
args: ['([[ ! -d "tests" ]] || for test in $(find . -path "./tests/*.py"); do pylint --disable=missing-docstring $test; done)']
language: system
12 changes: 6 additions & 6 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,17 +31,17 @@ def mock_imported_modules():
can be imported with modules which may not be available.
"""
module_paths = set()
for m in MOCK_MODULES:
namespaces = m.split(".")
ns = []
for mock in MOCK_MODULES:
namespaces = mock.split(".")
namespace = []
for n in namespaces:
ns.append(n)
module_paths.add(".".join(ns))
namespace.append(n)
module_paths.add(".".join(namespace))
for m_path in module_paths:
sys.modules[m_path] = MagicMock()


def pytest_runtest_setup(item):
def pytest_runtest_setup():
"""
Called immediately before any test function is called.

Expand Down
27 changes: 16 additions & 11 deletions tests/test_adafruit_radio.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@
found in the testconf.py file. See comments therein for explanation of how it
works.
"""
import adafruit_ble_radio
import pytest
import struct
import time
from unittest import mock
import pytest
import adafruit_ble_radio


@pytest.fixture
Expand All @@ -35,25 +35,25 @@ def test_radio_init_default():
assert r.ble == adafruit_ble_radio.BLERadio()
assert r.uid == 0
assert r.msg_pool == set()
assert r._channel == 42
assert r._channel == 42 # pylint: disable=protected-access


def test_radio_init_channel():
"""
If a channel argument is passed to initialisation, this is correctly set.
"""
r = adafruit_ble_radio.Radio(channel=7)
assert r._channel == 7
assert r._channel == 7 # pylint: disable=protected-access


def test_radio_configure_channel(radio):
"""
If a valid channel argument is passed to the configure method, the Radio
instance's channel is updated to reflect this.
"""
assert radio._channel == 42
assert radio._channel == 42 # pylint: disable=protected-access
radio.configure(channel=7)
assert radio._channel == 7
assert radio._channel == 7 # pylint: disable=protected-access


def test_radio_configure_channel_out_of_bounds(radio):
Expand All @@ -67,9 +67,9 @@ def test_radio_configure_channel_out_of_bounds(radio):
radio.configure(channel=256)
# Add just-in-bounds checks too.
radio.configure(channel=0)
assert radio._channel == 0
assert radio._channel == 0 # pylint: disable=protected-access
radio.configure(channel=255)
assert radio._channel == 255
assert radio._channel == 255 # pylint: disable=protected-access


def test_radio_send(radio):
Expand Down Expand Up @@ -102,8 +102,10 @@ def test_radio_send_bytes(radio):
with mock.patch("adafruit_ble_radio.time.sleep") as mock_sleep:
radio.send_bytes(msg)
mock_sleep.assert_called_once_with(adafruit_ble_radio.AD_DURATION)
spy_advertisement = adafruit_ble_radio.AdafruitRadio()
chan = struct.pack("<B", radio._channel)
spy_advertisement = (
adafruit_ble_radio._RadioAdvertisement()
) # pylint: disable=protected-access
chan = struct.pack("<B", radio._channel) # pylint: disable=protected-access
uid = struct.pack("<B", 255)
assert spy_advertisement.msg == chan + uid + msg
radio.ble.start_advertising.assert_called_once_with(spy_advertisement)
Expand Down Expand Up @@ -140,7 +142,10 @@ def test_radio_receive_full_no_messages(radio):
radio.ble.start_scan.return_value = []
assert radio.receive_full() is None
radio.ble.start_scan.assert_called_once_with(
adafruit_ble_radio.AdafruitRadio, minimum_rssi=-255, timeout=1, extended=True
adafruit_ble_radio._RadioAdvertisement,
minimum_rssi=-255,
timeout=1,
extended=True,
)
radio.ble.stop_scan.assert_called_once_with()

Expand Down