valid8r.testing
Testing utilities for Valid8r.
This module provides tools for testing applications that use Valid8r, making it easier to test validation logic, user prompts, and Maybe monads.
Submodules
Functions
|
Assert that a Maybe is a Failure with the expected error message. |
|
Assert that a Maybe is a Success with the expected value. |
|
Generate random inputs that include both valid and invalid cases. |
|
Generate test cases for a validator. |
|
Test a composed validator with various inputs to verify it works correctly. |
|
Context manager for mocking user input. |
|
Configure input to be mocked globally. |
Package Contents
- valid8r.testing.assert_maybe_failure(result, expected_error)[source]
Assert that a Maybe is a Failure with the expected error message.
- Parameters:
result (valid8r.core.maybe.Maybe[T]) – The Maybe instance to check
expected_error (str) – The expected error message inside the Maybe
- Returns:
True if result is a Failure with the expected error, False otherwise
- Return type:
Examples
>>> result = Maybe.failure("Invalid input") >>> assert_maybe_failure(result, "Invalid input") True >>> assert_maybe_failure(result, "Other error") False
- valid8r.testing.assert_maybe_success(result, expected_value)[source]
Assert that a Maybe is a Success with the expected value.
- Parameters:
result (valid8r.core.maybe.Maybe[T]) – The Maybe instance to check
expected_value (Any) – The expected value inside the Maybe
- Returns:
True if result is a Success with the expected value, False otherwise
- Return type:
Examples
>>> result = Maybe.success(42) >>> assert_maybe_success(result, 42) True >>> assert_maybe_success(result, 43) False
- valid8r.testing.generate_random_inputs(validator, count=20, range_min=-100, range_max=100)[source]
Generate random inputs that include both valid and invalid cases.
- Parameters:
validator (valid8r.core.validators.Validator[T]) – The validator to test against
count (int) – Number of inputs to generate
range_min (int) – Minimum value for generated integers
range_max (int) – Maximum value for generated integers
- Returns:
A list of random integers
- Return type:
list[T]
Examples
>>> from valid8r.core.validators import minimum >>> inputs = generate_random_inputs(minimum(0), count=10) >>> len(inputs) 10
- valid8r.testing.generate_test_cases(validator)[source]
Generate test cases for a validator.
This function analyzes the validator and generates appropriate test cases that should pass and fail the validation.
- Parameters:
validator (valid8r.core.validators.Validator[T]) – The validator to generate test cases for
- Returns:
A dictionary with ‘valid’ and ‘invalid’ lists of test cases
- Return type:
Examples
>>> from valid8r.core.validators import minimum >>> test_cases = generate_test_cases(minimum(10)) >>> test_cases {'valid': [10, 11, 15, 20, 100], 'invalid': [9, 5, 0, -10]}
- valid8r.testing.test_validator_composition(validator)[source]
Test a composed validator with various inputs to verify it works correctly.
- Parameters:
validator (valid8r.core.validators.Validator[T]) – The composed validator to test
- Returns:
True if the validator behaves as expected, False otherwise
- Return type:
Examples
>>> from valid8r.core.validators import minimum, maximum >>> is_valid_age = minimum(0) & maximum(120) >>> test_validator_composition(is_valid_age) True
- valid8r.testing.MockInputContext(inputs=None)[source]
Context manager for mocking user input.
- Parameters:
inputs (list[str] | None) – A list of strings to be returned sequentially by input().
- Yields:
None
- Return type:
collections.abc.Iterator[None]
Examples
>>> with MockInputContext(["yes", "42"]): ... answer = input("Continue? ") # returns "yes" ... number = input("Enter number: ") # returns "42"
- valid8r.testing.configure_mock_input(inputs)[source]
Configure input to be mocked globally.
Unlike MockInputContext, this function replaces the input function globally without restoring it automatically. Use for simple tests where cleanup isn’t critical.
- Parameters:
inputs (list[str]) – A list of strings to be returned sequentially by input().
- Return type:
None
Examples
>>> configure_mock_input(["yes", "42"]) >>> answer = input("Continue? ") # returns "yes" >>> number = input("Enter number: ") # returns "42"