Skip to content

test: improve coverage for SSZVector and SSZList collections (85%) #524

@tcoratger

Description

@tcoratger

Summary

src/lean_spec/types/collections.py has 85% test coverage. While test_collections.py exists, several validation paths, serialization edge cases, and helper functions are not covered.

Coverage stats:

File Statements Covered Coverage
types/collections.py 221 27 uncovered 85%

Uncovered lines: 55→51, 62–70 (generic type extraction, JSON serialization helper), 82, 92 (offset validation), 135, 142, 145, 171 (vector validator edge cases), 303, 315–318, 332–333, 346, 359, 390, 411, 418, 422, 466 (list validator, deserialization, properties)

What needs testing

Helper functions

_extract_element_type_from_generic()

  • Generic parameter found: Returns the element type from Pydantic metadata
  • No generic parameter: Returns None

_serialize_ssz_elements_to_json()

  • BaseBytes elements: Serialized as "0x" + hex string
  • IntFieldElement: Serialized as .value integer
  • Other elements: Passed through unchanged

_validate_offsets()

  • Empty offsets: Returns without error
  • Valid monotonic offsets: No error
  • Non-monotonic offsets: Raises SSZSerializationError
  • Final offset exceeds scope: Raises SSZSerializationError

SSZVector

__init_subclass__()

  • Explicit ELEMENT_TYPE: When defined in class dict, not overwritten by generic inference
  • Generic parameter: ELEMENT_TYPE inferred from SSZVector[SomeType]

_validate_vector_data()

  • Missing ELEMENT_TYPE or LENGTH: Raises SSZTypeError
  • Iterator input: Non-list/tuple iterable converted to tuple
  • Wrong length: Raises SSZValueError
  • Element type conversion: Non-typed elements are cast to ELEMENT_TYPE

Serialization (variable-size elements)

  • Variable-size vector: Offset table + element data
  • Deserialization with invalid offset: Raises SSZSerializationError

Properties

  • elements: Returns list[T] copy of data
  • __getitem__ slice: Returns Sequence[T]

SSZList

_validate_list_data()

  • Missing ELEMENT_TYPE or LIMIT: Raises SSZTypeError
  • Non-iterable input: Raises SSZTypeError
  • Exceeds limit: Raises SSZValueError
  • Element conversion failure: Raises SSZTypeError with descriptive message

__add__()

  • SSZList + SSZList: Concatenates data
  • SSZList + list/tuple: Concatenates with raw sequence
  • Unsupported type: Returns NotImplemented

Deserialization (variable-size elements)

  • Empty list (scope=0): Returns empty list
  • Scope too small: Raises SSZSerializationError
  • Invalid first offset: Raises SSZSerializationError
  • Count exceeds limit: Raises SSZValueError

Properties

  • elements: Returns list[T] copy
  • is_fixed_size(): Always False
  • get_byte_length(): Raises SSZTypeError

Why this matters

  • SSZ correctness: These are the fundamental collection types for all spec containers. Serialization bugs corrupt test vectors and break cross-client compatibility
  • Validation safety: Proper input validation prevents garbage-in/garbage-out in the spec
  • JSON test vectors: The serialization helpers ensure test vectors are correctly formatted

How to test

Running tests with coverage

uv run pytest tests/lean_spec/types/test_collections.py -v \
  --cov=src/lean_spec/types/collections --cov-report=term-missing

Target: ≥95% line coverage.

Test file location

tests/lean_spec/types/test_collections.py  (extend existing)

Testing tips

  • Define small test types: class TestVector(SSZVector[Uint64]): LENGTH = 3
  • For variable-size element tests, use a container type as element
  • Test _serialize_ssz_elements_to_json with mixed element types
  • For deserialization, construct raw bytes with known offset tables
  • SSZList.__add__ with NotImplemented can be tested via list + sszlist (Python calls __radd__)

Using Claude Code subagents

1. code-tester agent — Generate the tests

Extend tests in tests/lean_spec/types/test_collections.py. Cover _serialize_ssz_elements_to_json with BaseBytes and IntFieldElement, _validate_offsets edge cases, SSZVector with missing ELEMENT_TYPE, SSZList._validate_list_data errors, SSZList.add with different types, and variable-size element deserialization edge cases.

Workflow

  1. Start with helper function tests (_serialize, _validate_offsets)
  2. Add validation edge cases for Vector and List
  3. Test variable-size serialization/deserialization
  4. Test add and properties
  5. Run uvx tox -e all-checks to pass all quality checks

Metadata

Metadata

Assignees

Labels

good first issueGood for newcomerstestsScope: Changes to the spec tests

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions