Skip to content

Add unit tests for CiA 402 profile (coverage 36% → 45%)#644

Open
bizfsc wants to merge 8 commits intocanopen-python:masterfrom
bizfsc:test/p402-coverage
Open

Add unit tests for CiA 402 profile (coverage 36% → 45%)#644
bizfsc wants to merge 8 commits intocanopen-python:masterfrom
bizfsc:test/p402-coverage

Conversation

@bizfsc
Copy link
Copy Markdown
Collaborator

@bizfsc bizfsc commented Apr 28, 2026

Summary

Add 24 unit tests for canopen/profiles/p402.py to improve test coverage from 36% to 45%.

Tests Added

  • State402 enum: all states present, statusword decoding via bitmask
  • State decoding: State402.decode_statusword() for all defined states
  • Command words: verify controlword values for every state transition
  • Next-state calculation: walk through the CiA 402 state machine graph
  • Homing status: IS_HOMING / HOMING_COMPLETED / HOMING_ERROR evaluation
  • Operation mode: switching and reading via OD entries 0x6060/0x6061
  • TPDO callback: statusword update propagation through PDO mapping

Approach

Uses a minimal ObjectDictionary with the required DS402 objects (0x6040 controlword, 0x6041 statusword, 0x6060/0x6061 operation mode, 0x6502 supported drives). PDO maps are mocked with unittest.mock.MagicMock to test the TPDO callback path without requiring a real CAN bus.

Coverage

canopen/profiles/p402.py    36% → 45%

The remaining uncovered code is primarily in BaseNode402 methods that require a live CAN bus interaction (SDO reads/writes, NMT state changes, PDO communication).

bizfsc and others added 3 commits April 28, 2026 14:12
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
Copy link
Copy Markdown

codecov Bot commented Apr 29, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

Copy link
Copy Markdown
Member

@acolomb acolomb left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Overall this looks nice, but much more valuable would be a simulation of actual TPDO reception.

Comment thread test/test_p402.py Outdated
Comment thread test/test_p402.py Outdated
Comment thread test/test_p402.py Outdated
bizfsc and others added 4 commits April 29, 2026 13:47
- 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.
Copy link
Copy Markdown
Member

@acolomb acolomb left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment thread test/test_p402.py
Comment thread test/test_p402.py
Comment on lines +204 to +208
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)
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

True

Comment thread test/test_p402.py
Comment on lines +333 to +347
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)
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@acolomb
Copy link
Copy Markdown
Member

acolomb commented May 5, 2026

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants