From a489244e764a47076395bcd6ef60c05c989a0f74 Mon Sep 17 00:00:00 2001 From: v Date: Tue, 1 Sep 2026 10:59:06 +0300 Subject: [PATCH] fix(hardware_utils): get_akida_device only checks the first device --- brainchip_utils/hardware_utils.py | 8 ++++---- test/test_hardware_utils.py | 27 +++++++++++++++++++++++++++ 2 files changed, 31 insertions(+), 4 deletions(-) create mode 100644 test/test_hardware_utils.py diff --git a/brainchip_utils/hardware_utils.py b/brainchip_utils/hardware_utils.py index d05ecf5..be8a267 100644 --- a/brainchip_utils/hardware_utils.py +++ b/brainchip_utils/hardware_utils.py @@ -60,13 +60,13 @@ def get_akida_device(target_version=None): print(str(len(devices)) + ' Akida devices found. Using the first device detected.') return devices[0] else: - for dd in akida.devices(): + for dd in devices: if dd.ip_version == target_version: print('Target Akida device found') return dd - print('Connected Akida Device does not match the requested IPVersion.') - print('Calls to akida will run on the software backend.') - return None + print('Connected Akida Device does not match the requested IPVersion.') + print('Calls to akida will run on the software backend.') + return None #---------------------------------------------------------------------------------- # diff --git a/test/test_hardware_utils.py b/test/test_hardware_utils.py new file mode 100644 index 0000000..b8df946 --- /dev/null +++ b/test/test_hardware_utils.py @@ -0,0 +1,27 @@ +"""Unit tests for brainchip_utils.hardware_utils (no hardware required).""" +import akida + +from brainchip_utils.hardware_utils import get_akida_device + + +class _FakeDevice: + def __init__(self, ip_version): + self.ip_version = ip_version + + +def test_matching_device_after_the_first_is_found(monkeypatch): + """A device matching target_version must be found wherever it sits in the list.""" + first, second = _FakeDevice(akida.IpVersion.v2), _FakeDevice(akida.IpVersion.v1) + monkeypatch.setattr(akida, "devices", lambda: [first, second]) + assert get_akida_device(target_version=akida.IpVersion.v1) is second + + +def test_returns_none_when_no_device_matches(monkeypatch): + monkeypatch.setattr(akida, "devices", lambda: [_FakeDevice(akida.IpVersion.v2)]) + assert get_akida_device(target_version=akida.IpVersion.v1) is None + + +def test_returns_first_device_when_no_target_requested(monkeypatch): + first = _FakeDevice(akida.IpVersion.v1) + monkeypatch.setattr(akida, "devices", lambda: [first, _FakeDevice(akida.IpVersion.v2)]) + assert get_akida_device() is first