Back to Repositories

Testing Route and Segment Name Parsing in OpenPilot

This test suite validates the parsing and handling of route and segment names in the OpenPilot system. It ensures correct formatting and extraction of route information, segment numbers, and data directory paths from various input formats.

Test Coverage Overview

The test suite provides comprehensive coverage for route and segment name parsing functionality in OpenPilot.

Key areas tested include:
  • Route name extraction from different format patterns
  • Segment number identification
  • Data directory path handling
  • Support for both pipe and forward slash delimiters
Edge cases covered include handling of missing segment numbers and various directory path combinations.

Implementation Analysis

The testing approach utilizes Python’s namedtuple for structured test case definition and validation. The implementation follows a data-driven testing pattern, where multiple test cases are defined and executed through a common validation function. The test leverages Python’s assert statements for verification and employs the SegmentName class from the OpenPilot route library.

Technical Details

Testing components include:
  • Python’s collections.namedtuple for test case structure
  • OpenPilot’s tools.lib.route.SegmentName class
  • Custom validation function for consistent case checking
  • Array-based test case definition for maintainability

Best Practices Demonstrated

The test implementation showcases several testing best practices including structured test case organization, clear separation of test data from validation logic, and comprehensive edge case coverage. The code demonstrates clean organization through the use of data structures and modular validation functions, making it maintainable and extensible.

commaai/openpilot

tools/lib/tests/test_route_library.py

            
from collections import namedtuple

from openpilot.tools.lib.route import SegmentName

class TestRouteLibrary:
  def test_segment_name_formats(self):
    Case = namedtuple('Case', ['input', 'expected_route', 'expected_segment_num', 'expected_data_dir'])

    cases = [ Case("a2a0ccea32023010|2023-07-27--13-01-19", "a2a0ccea32023010|2023-07-27--13-01-19", -1, None),
              Case("a2a0ccea32023010/2023-07-27--13-01-19--1", "a2a0ccea32023010|2023-07-27--13-01-19", 1, None),
              Case("a2a0ccea32023010|2023-07-27--13-01-19/2", "a2a0ccea32023010|2023-07-27--13-01-19", 2, None),
              Case("a2a0ccea32023010/2023-07-27--13-01-19/3", "a2a0ccea32023010|2023-07-27--13-01-19", 3, None),
              Case("/data/media/0/realdata/a2a0ccea32023010|2023-07-27--13-01-19", "a2a0ccea32023010|2023-07-27--13-01-19", -1, "/data/media/0/realdata"),
              Case("/data/media/0/realdata/a2a0ccea32023010|2023-07-27--13-01-19--1", "a2a0ccea32023010|2023-07-27--13-01-19", 1, "/data/media/0/realdata"),
              Case("/data/media/0/realdata/a2a0ccea32023010|2023-07-27--13-01-19/2", "a2a0ccea32023010|2023-07-27--13-01-19", 2, "/data/media/0/realdata") ]

    def _validate(case):
      route_or_segment_name = case.input

      s = SegmentName(route_or_segment_name, allow_route_name=True)

      assert str(s.route_name) == case.expected_route
      assert s.segment_num == case.expected_segment_num
      assert s.data_dir == case.expected_data_dir

    for case in cases:
      _validate(case)