uf.testing#

Testing utilities for uf applications.

Provides tools for testing uf apps, including test clients, fixtures, and assertion helpers.

Functions

assert_field_required(spec, field_name)

Assert that a field is required.

assert_field_type(spec, field_name, ...)

Assert that a field has the expected type.

assert_has_field(spec, field_name)

Assert that a spec has a specific field.

assert_valid_rjsf_spec(spec)

Assert that a specification is valid RJSF format.

mock_function_response(app, func_name, ...)

Context manager to mock a function's response.

test_ui_function(func, test_inputs, *[, ...])

Test a function with form-like input.

Classes

FormDataBuilder()

Builder for constructing form data for tests.

UfAppTester(app)

Context manager for testing uf apps.

UfTestClient(app)

Test client for uf applications.

class uf.testing.FormDataBuilder[source]#

Bases: object

Builder 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()
... )
build()[source]#

Build and return the form data.

Return type:

dict

Returns:

Dictionary of form data

field(name, value)[source]#

Add a field to the form data.

Parameters:
  • name (str) – Field name

  • value (Any) – Field value

Return type:

FormDataBuilder

Returns:

Self for method chaining

fields(**kwargs)[source]#

Add multiple fields at once.

Parameters:

**kwargs – Field name-value pairs

Return type:

FormDataBuilder

Returns:

Self for method chaining

class uf.testing.UfAppTester(app)[source]#

Bases: object

Context 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:
  • result (dict) – Result dictionary

  • error_type (str) – Expected error type name

Raises:

AssertionError – If error types don’t match

assert_failure(result, message='')[source]#

Assert that a result indicates failure.

Parameters:
  • result (dict) – Result dictionary

  • message (str) – Optional custom error message

Raises:

AssertionError – If result is successful

assert_result_equals(result, expected)[source]#

Assert that result value equals expected.

Parameters:
  • result (dict) – Result dictionary

  • expected (Any) – Expected value

Raises:

AssertionError – If values don’t match

assert_success(result, message='')[source]#

Assert that a result indicates success.

Parameters:
  • result (dict) – Result dictionary

  • message (str) – Optional custom error message

Raises:

AssertionError – If result is not successful

get_history()[source]#

Get history of all results.

Return type:

list[dict]

Returns:

List of result dictionaries

submit_form(func_name, form_data)[source]#

Simulate form submission.

Parameters:
  • func_name (str) – Name of the function

  • form_data (dict) – Form data as dictionary

Return type:

dict

Returns:

Result dictionary

class uf.testing.UfTestClient(app)[source]#

Bases: object

Test 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:
  • func_name (str) – Name of the function to call

  • params (dict) – Dictionary of parameters

  • expect_success (bool) – Whether to expect success (raises on failure)

Return type:

dict

Returns:

Result dictionary with ‘success’ and ‘result’ or ‘error’

Raises:

AssertionError – If expect_success=True and call fails

get_spec(func_name)[source]#

Get RJSF specification for a function.

Parameters:

func_name (str) – Name of the function

Return type:

dict

Returns:

Function specification dict

Raises:

KeyError – If function not found

list_functions()[source]#

Get list of available function names.

Return type:

list[str]

Returns:

List of function name strings

validate_params(func_name, params)[source]#

Validate parameters against function schema.

Parameters:
  • func_name (str) – Name of the function

  • params (dict) – Parameters to validate

Return type:

tuple[bool, Optional[str]]

Returns:

Tuple of (is_valid, error_message)

uf.testing.assert_field_required(spec, field_name)[source]#

Assert that a field is required.

Parameters:
  • spec (dict) – The specification

  • field_name (str) – Name of the field

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:
  • spec (dict) – The specification

  • field_name (str) – Name of the field

  • expected_type (str) – Expected JSON Schema type

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:
  • spec (dict) – The specification

  • field_name (str) – Name of the field to check

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:
  • app – The uf application

  • func_name (str) – Name of function to mock

  • mock_result (Any) – Value to return

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:
  • func (Callable) – Function to test

  • test_inputs (dict) – Dictionary of test parameters

  • expected_output (Any) – Expected return value (if any)

  • expected_exception (Optional[type]) – Expected exception type (if any)

Return type:

bool

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