Skip to content

Add unit tests for validate_non_empty_iterable and validate_non_empty_string #179

Description

@mark-torres10

Issue Description

Problem:

  • lib/validation_utils.py contains core validation helpers used across the backend, but we currently don’t have direct unit tests for the two most foundational functions:
    • validate_non_empty_iterable
    • validate_non_empty_string (drives string validation across models/validators)
  • Bugs here would be high blast-radius (they’re reused widely), and the expected error messages are part of the developer contract (tests elsewhere sometimes match= specific strings).

Related files

  • lib/validation_utils.py (functions under test)
  • simulation/core/utils/validators.py (calls into validate_nonnegative_value / validate_non_empty_string)
  • Example downstream users (for context only; not required to test here):
    • simulation/core/models/profiles.py
    • simulation/core/models/posts.py
    • simulation/core/models/runs.py

Related tests (for how tests should look)

  • tests/ml_tooling/llm/test_llm_service.py
  • tests/simulation/core/test_action_generators_registry.py

What tests we have / don’t have today:

  • Have:
    • Validation behavior tests at higher layers (e.g. decorator + validators) in tests/test_validation_decorators.py.
    • Some tests/lib/* coverage, but it’s currently only test_load_env_vars.py.
  • Don’t have:
    • Direct unit coverage for lib/validation_utils.py (no tests/lib/test_validation_utils.py today).
    • Targeted assertions around exact error messages for these helpers.

Implementation notes / skeleton structure:

  • Add a new test module: tests/lib/test_validation_utils.py
  • Keep it simple (no classes needed); follow existing repo pattern: top-level pytest functions.
  • Suggested imports:
    • import pytest
    • from lib.validation_utils import validate_nonnegative_value, validate_non_empty_string
  • Suggested structure (function-based; optionally use @pytest.mark.parametrize):
    • validate_non_empty_iterable: few tests here, (1) if iterable is None, (2) if iterable is NOT None, (3) "if not iterable", (4) if iterable.
    • Optional robustness: repeat a couple cases with float inputs (e.g. 0.0, -0.1) since the helper supports both int/float.
    • def test_validate_non_empty_string_strips_whitespace(): ... (e.g. " hi " returns "hi")
    • def test_validate_non_empty_string_raises_on_none(): ... (message contains "cannot be None")
    • def test_validate_non_empty_string_raises_on_non_string(): ... (message contains "must be a string")
    • def test_validate_non_empty_string_raises_on_empty_or_whitespace(): ... (message contains "cannot be empty")

What success looks like:

  • New file tests/lib/test_validation_utils.py exists and passes locally:
    • uv run pytest tests/lib/test_validation_utils.py
  • Tests assert both behavior and the key error-message fragments emitted by lib/validation_utils.py:
    • validate_nonnegative_value:
      • negative value raises with "<field> must be >= 0" when ok_equals_zero=True
      • zero raises with "<field> must be greater than 0" when ok_equals_zero=False
    • validate_non_empty_string:
      • returns stripped value for valid strings
      • raises on None, non-str, and empty-after-strip inputs with the expected message fragments

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions