Mocking and Test Discovery
Relevant source files
The following files were used as context for generating this wiki page:
Mocking and test discovery in the FLEXT Plugin system provide the infrastructure for identifying, validating, and simulating plugin behavior within a controlled environment. The system utilizes a structured discovery mechanism to locate plugins across various storage mediums and a comprehensive mocking/testing framework to ensure high reliability through automated verification of lifecycle events and data processing.
The testing architecture is built upon enterprise standards, emphasizing Plugin Lifecycle validation and business rule enforcement. It allows developers to simulate complex integrations—such as Docker services or third-party APIs—without requiring live infrastructure during the initial development phases.
Sources: examples/README.md:20-30, tests/unit/test_discovery.py:10-20, tests/unit/test_domain_entities.py:1-15
Plugin Discovery Mechanism
The discovery system is responsible for identifying potential plugins within the file system or other defined paths. It employs specific strategies to scan directories and validate that the discovered metadata matches the platform's requirements.
Discovery Strategies and Data Structure
The FlextPluginDiscovery class manages multiple discovery strategies (typically two by default) to locate plugin artifacts. When a plugin is found, the system generates DiscoveryData, which contains the essential identifiers for the platform to begin the loading process.
The diagram shows the logic flow from initiating a path scan to producing validated plugin discovery metadata.
Sources: tests/unit/test_discovery.py:35-65, tests/unit/test_models.py:106-115
Discovery Metadata Fields
| Field | Type | Description |
|---|---|---|
name | String | Unique identifier of the plugin |
version | String | Semantic version of the plugin |
path | Path | Filesystem path to the plugin root |
discovery_type | Literal | Type of discovery (e.g., "file") |
discovery_method | Literal | Method used (e.g., "file_system") |
Sources: tests/unit/test_models.py:107-115, tests/unit/test_discovery.py:75-80
Mocking and Simulation Patterns
Mocking is used extensively to simulate external dependencies and internal processing errors. This ensures that the plugin platform remains resilient even when plugins encounter unexpected states.
Simulating Execution Errors
Testing suites utilize patching to inject failures into core methods like _process_data to verify that the plugin correctly increments error counters and returns appropriate failure results using the r pattern.
def test_execution_error_handling(self, plugin):
plugin.initialize()
plugin.activate()
# Force an error by providing non-dict data
with patch.t.JsonValue(plugin, "_process_data", side_effect=Exception("Processing error")):
result = plugin.execute({"payload": {"test": "data"}})
assert result.failure()
assert "Processing error" in result.error
stats = plugin.get_statistics()
assert stats["total_errors"] == 1
Sources: examples/basic-plugin.md:465-478
Mocking Platform Fixtures
For integration testing, a test platform fixture is created with specific settings (e.g., test_mode: True). This allows for the registration and activation of plugins in a sandbox environment that does not affect global platform state.
This sequence demonstrates how the test platform coordinates the lifecycle of a mock plugin during automated verification.
Sources: examples/README.md:65-80, examples/basic-plugin.md:428-445
Test Discovery and Execution
Automated test discovery for the FLEXT Plugin system relies on standard Python testing conventions (via pytest) and custom utility wrappers for running example scripts as integration smoke tests.
Verification of Examples
The project ensures that all provided example scripts (e.g., 01_basic_plugin.py, 03_docker_integration.py) function correctly by executing them in isolated subprocesses and verifying their exit codes.
| Test Case | target File | Validation Goal |
|---|---|---|
test_basic_plugin_example_execution | 01_basic_plugin.py | Verify minimal API fetch and list operations |
test_plugin_configuration_example_execution | 02_plugin_configuration.py | Ensure complex JSON configurations load without error |
test_docker_integration_example_execution | 03_docker_integration.py | Validate DB, Redis, and LDAP plugin rule enforcement |
Sources: tests/unit/test_examples.py:20-55
Domain Entity Validation
Domain entities are tested for business rule enforcement. The Plugin.Entity includes built-in methods to record execution metrics, which are then verified in unit tests to ensure accurate telemetry.
- Metric Tracking: Tests verify
execution_count,success_count, andfailure_countincrements. - State Enforcement: Tests ensure plugins cannot be enabled if they are already in an enabled state.
- Schema Validation: Ensures names and versions follow mandatory constraints (e.g., non-empty strings).
Sources: tests/unit/test_domain_entities.py:35-85, tests/unit/test_models.py:85-105
Summary
Mocking and test discovery form a critical safety net for the FLEXT Plugin system. By combining metadata-driven discovery with a robust simulation framework, the system allows for the validation of complex lifecycle transitions, performance profiling, and error resilience without requiring a full production environment.
Sources: examples/basic-plugin.md:520-540, tests/unit/test_discovery.py:25-30