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 3 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.
| _ = self.node.controlword | ||
|
|
||
|
|
||
| class TestBaseNode402NextState(unittest.TestCase): |
There was a problem hiding this comment.
This is an internal method, not an API. While it's nice to see some consistency checks on it, the real test subject should be the .state property. I know that's a bit more involved, but injecting TPDO value changes should provide a nice way to simulate a real CAN node answering.
| status = None | ||
| for key, value in Homing.STATES.items(): | ||
| bitmask, bits = value | ||
| if sw & bitmask == bits: | ||
| status = key | ||
| self.assertEqual(status, expected) |
There was a problem hiding this comment.
This is pretty useless, as the node itself is not even involved. Again, trying to simulate TPDO reception would be a more valuable approach. As is, this test class could be nuked.
| def test_supported_bitmask_unique(self): | ||
| values = list(OperationMode.SUPPORTED.values()) | ||
| # All bitmasks should be unique (each is a single bit) | ||
| self.assertEqual(len(values), len(set(values))) |
There was a problem hiding this comment.
This is a funky AI idea. The standard defines the bit values, I see no need for testing them against some artificial logic condition.
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).