Add unit tests for CiA 402 profile (coverage 36% → 45%)#644
Add unit tests for CiA 402 profile (coverage 36% → 45%)#644bizfsc wants to merge 8 commits intocanopen-python:masterfrom
Conversation
Add 24 unit tests covering: - State402 enum and state decoding from statusword - Command word generation for state transitions - Next-state calculation through the state machine - Homing status evaluation - Operation mode switching and reading - TPDO callback handling for statusword updates - Lookup table consistency checks
Codecov Report✅ All modified and coverable lines are covered by tests. 📢 Thoughts on this report? Let us know! |
acolomb
left a comment
There was a problem hiding this comment.
Overall this looks nice, but much more valuable would be a simulation of actual TPDO reception.
- Remove TestBaseNode402NextState: replaced by state setter tests that exercise the public .state property via TPDO/RPDO simulation - Remove TestBaseNode402HomingStatus: replaced by TestBaseNode402Homing that calls _homing_status() with TPDO-injected statusword - Remove test_supported_bitmask_unique: standard-defined values - Remove TestBaseNode402TPDOCallback: implicitly covered by all tests using _inject_tpdo() New test classes using TPDO reception simulation: - TestBaseNode402StateTransition: full state machine transitions with _FakeRpdoVar simulating drive controlword/statusword exchange - TestBaseNode402Homing: homing status via node._homing_status() - TestBaseNode402OpMode: operation mode reading via TPDO/SDO fallback - Extended TestBaseNode402State: check_statusword and statusword SDO fallback paths
Make sure that ruff agrees with the previously used import ordering style from isort.
acolomb
left a comment
There was a problem hiding this comment.
I've read through the code, but didn't verify each test case against the spec. While it does increase coverage significantly, some test cases are of questionable value - typical for AI fantasies I guess.
But still better to have a basis to improve on later, we can always go back and remove / improve tests when the library code evolves.
Please have a look at the few outstanding comments.
| def test_check_statusword_no_tpdo_pointers(self): | ||
| """check_statusword returns cached statusword when no TPDO pointers.""" | ||
| self._inject_statusword(0x0027) | ||
| result = self.node.check_statusword() | ||
| self.assertEqual(result, 0x0027) |
There was a problem hiding this comment.
This is a bit weird, as there should be no TPDO value when it is not mapped. The implementation does behave this way, but it is not an intended access pattern. Thus I'm fine with removing this test at the cost of slightly lower coverage.
| def test_homing_status(self): | ||
| """Verify _homing_status from TPDO-injected statusword.""" | ||
| test_cases = [ | ||
| (0x0000, "IN PROGRESS"), | ||
| (0x0400, "INTERRUPTED"), | ||
| (0x1000, "ATTAINED"), | ||
| (0x1400, "TARGET REACHED"), | ||
| (0x2000, "ERROR VELOCITY IS NOT ZERO"), | ||
| (0x2400, "ERROR VELOCITY IS ZERO"), | ||
| ] | ||
| for sw, expected in test_cases: | ||
| with self.subTest(statusword=hex(sw)): | ||
| _inject_tpdo(self.node, 0x6041, sw) | ||
| result = self.node._homing_status() | ||
| self.assertEqual(result, expected) |
There was a problem hiding this comment.
While this is a nice overall exercise of the statusword mapping, it is an internal method and thus not a good candidate for future-proof testing. I'll let you decide whether you want to keep it.
|
One thing about the import ordering: I added a bit of ruff configuration in pyproject.toml to make it behave in line with isort. The latter is what we previously applied for sorting, but the ruff default settings disagree because of type-grouping in a case-insensitive manner. Let's quiet down that noise so you can keep using ruff. I recently started using it in parallel to flake8, but unfortunately they sometimes clash, while flake8 does give way more specific hints. So the current approach is to tame ruff to accept the established style. |
Summary
Add 24 unit tests for
canopen/profiles/p402.pyto improve test coverage from 36% to 45%.Tests Added
State402.decode_statusword()for all defined statesIS_HOMING/HOMING_COMPLETED/HOMING_ERRORevaluationApproach
Uses a minimal
ObjectDictionarywith the required DS402 objects (0x6040 controlword, 0x6041 statusword, 0x6060/0x6061 operation mode, 0x6502 supported drives). PDO maps are mocked withunittest.mock.MagicMockto test the TPDO callback path without requiring a real CAN bus.Coverage
The remaining uncovered code is primarily in
BaseNode402methods that require a live CAN bus interaction (SDO reads/writes, NMT state changes, PDO communication).