Test Fixtures and Factories
Relevant source files
The following files were used as context for generating this wiki page:
The Test Fixtures and Factories system in flext-ldap provides a structured, data-driven approach to validating enterprise LDAP operations. It utilizes a centralized hierarchy of constants, mock objects, and specialized test data generators to ensure consistency across unit, integration, and performance testing tiers. This system is built to adhere to FLEXT standards, emphasizing zero-debt baselines and executable documentation.
Sources: tests/constants.py:1-15, examples/README.md:15-25
Core Architecture and Data Flow
Testing in flext-ldap revolves around a "flat namespace" of typed constants and utility facades. This architecture prevents hardcoding values within individual tests and ensures that configuration changes propagate correctly through the test suite.
The diagram above illustrates how centralized constants and utility facades drive the validation of both unit and integration test tiers.
Sources: tests/constants.py:18-30, tests/unit/test_utilities.py:25-45
Centralized Test Constants
The primary source of truth for test data is the TestsFlextLdapConstants class, accessed via the alias c. This class organizes constants into logical sub-namespaces such as FieldName, PhaseName, and LdapValueCase.
Connection and Configuration Constants
Used to validate FlextLdapSettings and connection lifecycle management.
| Constant Category | Description | Key Examples |
|---|---|---|
| Field Names | Parameter keys for configuration models | host, port, bind_dn, scope |
| Host Scenarios | Valid and invalid host strings for validation | localhost, example.com, invalid..host.. |
| Ports | Range of ports for boundary testing | 1, 389, 636, 65535 |
| Security Combos | SSL/TLS boolean combinations | (True, False), (False, True) |
Sources: tests/constants.py:44-53, tests/constants.py:96-115, tests/unit/test_config.py:20-50
LDAP Operation Scenarios
These constants define inputs for search filters, DN structures, and entry attributes.
- Search Filters: Defined in
SEARCH_FILTER_SCENARIOS_ADVANCED, covering equality, substrings, and complex nested compounds (e.g.,(&(objectClass=person)(cn=John))). - DN Scenarios: Includes valid, empty, and invalid DN formats, as well as DNs containing special characters like escaped commas (
cn=test\,user). - Object Categories: Maps LDAP attributes to categories like
personorunknown.
Sources: tests/constants.py:265-285, tests/constants.py:328-340, tests/unit/test_models_search.py:130-150
Structural Mocks and Factories
The project utilizes "structural mocks"—lightweight classes that mimic the interface of external libraries like ldap3—to test internal logic without requiring a live server.
LDAP3 Adapter Helpers
Mock implementations for ldap3.Attribute, ldap3.Entry, and ldap3.Connection are used to verify the FlextLdapLdap3Wrappers and ResultConverter.
class RecordingConnection:
"""Structural ldap3 connection that records wrapper calls."""
def __init__(self, result=None, entries=()):
self.last_search_base = ""
self.last_search_filter = ""
# ... additional state tracking
Sources: tests/unit/test_ldap3_adapter_helpers.py:112-130, tests/unit/test_ldap3_adapter_helpers.py:155-180
Entry and Search Result Factories
Helper methods within test classes facilitate the creation of m.Ldif.Entry and m.Ldap.SearchOptions objects.
_entry(dn, attributes): Standardizes the creation of LDIF entries for synchronization and search tests.SearchOptions.normalized(): A factory method that creates search configurations with default scopes and filters.
Sources: tests/unit/test_sync.py:25-33, tests/unit/test_models_search.py:15-25, tests/unit/test_models_search.py:80-90
Synchronization Fixtures
The synchronization system uses LDIF (LDAP Data Interchange Format) templates and phase configurations to validate batch operations.
The sequence shows how LDIF fixtures are passed through the synchronization mixin to validate entry processing.
Sources: tests/unit/test_sync.py:110-130, tests/constants.py:305-320
Sync Validation Metrics
Test fixtures include expected success rates and entry counts for multi-phase synchronization.
| Metric | Constant Value | Source |
|---|---|---|
| Phase Total Entries | 100 | tests/constants.py:244 |
| Expected Success Rate | 95.0 | tests/constants.py:248 |
| Batch Synced Count | 80 | tests/constants.py:258 |
Test Data Generation
For comprehensive validation against live servers (OpenLDAP/OUD), the project includes a test_data_generator.py utility.
- Large-scale generation: Creates ~1000 test entries.
- Diverse Schemas: Generates entries for
person,group,service, andcomputerobject classes. - Organizational Structure: Builds realistic hierarchies with departments, groups, and containers.
- LDIF Export: Formats data specifically for OpenLDAP or Oracle Unified Directory (OUD).
Sources: examples/README.md:280-305, examples/README.md:325-340
Conclusion
The Test Fixtures and Factories in flext-ldap provide a robust foundation for maintaining enterprise-grade LDAP code. By decoupling test data from test logic through centralized constants and utilizing structural mocks, the suite achieves high resilience to configuration changes and ensures consistent behavior across diverse LDAP server implementations.