uf.testing#
Testing utilities for uf applications.
Provides tools for testing uf apps, including test clients, fixtures, and assertion helpers.
Functions
|
Assert that a field is required. |
|
Assert that a field has the expected type. |
|
Assert that a spec has a specific field. |
|
Assert that a specification is valid RJSF format. |
|
Context manager to mock a function's response. |
|
Test a function with form-like input. |
Classes
Builder for constructing form data for tests. |
|
|
Context manager for testing uf apps. |
|
Test client for uf applications. |
- class uf.testing.FormDataBuilder[source]#
Bases:
objectBuilder for constructing form data for tests.
Provides a fluent interface for building test form data.
Example
>>> form_data = ( ... FormDataBuilder() ... .field('name', 'John Doe') ... .field('email', 'john@example.com') ... .field('age', 30) ... .build() ... )
- field(name, value)[source]#
Add a field to the form data.
- Parameters:
- Return type:
- Returns:
Self for method chaining
- class uf.testing.UfAppTester(app)[source]#
Bases:
objectContext manager for testing uf apps.
Provides a testing context with common utilities and assertions.
Example
>>> with UfAppTester(app) as tester: ... result = tester.submit_form('add', {'x': 10, 'y': 20}) ... tester.assert_success(result) ... tester.assert_result_equals(result, 30)
- assert_error_type(result, error_type)[source]#
Assert that error type matches.
- Parameters:
- Raises:
AssertionError – If error types don’t match
- assert_failure(result, message='')[source]#
Assert that a result indicates failure.
- Parameters:
- Raises:
AssertionError – If result is successful
- assert_result_equals(result, expected)[source]#
Assert that result value equals expected.
- Parameters:
- Raises:
AssertionError – If values don’t match
- assert_success(result, message='')[source]#
Assert that a result indicates success.
- Parameters:
- Raises:
AssertionError – If result is not successful
- class uf.testing.UfTestClient(app)[source]#
Bases:
objectTest client for uf applications.
Provides a convenient interface for testing uf apps without running a web server.
Example
>>> from uf import mk_rjsf_app >>> app = mk_rjsf_app([my_func]) >>> client = UfTestClient(app) >>> response = client.call_function('my_func', {'x': 10, 'y': 20}) >>> assert response['success']
- call_function(func_name, params, *, expect_success=True)[source]#
Call a function with the given parameters.
- Parameters:
- Return type:
- Returns:
Result dictionary with ‘success’ and ‘result’ or ‘error’
- Raises:
AssertionError – If expect_success=True and call fails
- uf.testing.assert_field_required(spec, field_name)[source]#
Assert that a field is required.
- Parameters:
- Raises:
AssertionError – If field is not required
- uf.testing.assert_field_type(spec, field_name, expected_type)[source]#
Assert that a field has the expected type.
- Parameters:
- Raises:
AssertionError – If type doesn’t match
- uf.testing.assert_has_field(spec, field_name)[source]#
Assert that a spec has a specific field.
- Parameters:
- Raises:
AssertionError – If field not found
- uf.testing.assert_valid_rjsf_spec(spec)[source]#
Assert that a specification is valid RJSF format.
- Parameters:
spec (
dict) – The specification to validate- Raises:
AssertionError – If spec is invalid
- uf.testing.mock_function_response(app, func_name, mock_result)[source]#
Context manager to mock a function’s response.
- Parameters:
- Yields:
None
Example
>>> with mock_function_response(app, 'get_user', {'name': 'Test'}): ... # Calls to get_user will return {'name': 'Test'} ... result = client.call_function('get_user', {'id': 1})
- uf.testing.test_ui_function(func, test_inputs, *, expected_output=None, expected_exception=None)[source]#
Test a function with form-like input.
- Parameters:
- Return type:
- Returns:
True if test passes
- Raises:
AssertionError – If test fails
Example
>>> def add(x: int, y: int) -> int: ... return x + y >>> test_ui_function(add, {'x': 10, 'y': 20}, expected_output=30) True