Unit Testing Suite
Relevant source files
The following files were used as context for generating this wiki page:
The Unit Testing Suite for flext-target-ldap provides a comprehensive framework for validating the behavior of the LDAP Singer target. It ensures the integrity of the data flow from Singer messages (SCHEMA, RECORD, STATE) to LDAP directory operations. The suite covers critical components including the LDAP client connection logic, stream-specific sink implementations (Users, Groups, Organizational Units), and the primary target facade.
The testing architecture relies on pytest and extensive mocking of LDAP interactions to ensure tests are fast, deterministic, and isolated from actual LDAP server dependencies. This suite validates configuration parsing, DN (Distinguished Name) template construction, attribute mapping, and the upsert/delete logic required for directory synchronization.
Sources: pyproject.toml:343-376, tests/unit/test_target.py
Test Infrastructure and Configuration
The testing suite utilizes a centralized configuration and utility layer to provide consistent mock data and target instances across different test modules.
Shared Fixtures and Utilities
Common test data is managed in tests/conftest.py and tests/utilities.py. These files define the standard LDAP configuration used for testing, including host, port, and credentials.
| Fixture Name | Description |
|---|---|
mock_ldap_config | Generates a standard SettingsPayload with host test.ldap.com and REDACTED credentials. |
sample_user_record | A complete LDAP user record with dn, uid, cn, and mail fields. |
singer_message_schema | A JSON-serialized Singer SCHEMA message for the users stream. |
singer_message_record | A JSON-serialized Singer RECORD message containing user data. |
mock_target | A MagicMock instance of the target configured with test settings. |
Sources: tests/conftest.py:27-104, tests/utilities.py:34-49
Specialized Test Utilities
The suite includes a ProcessTarget stub used to verify that sinks correctly delegate record processing to the target runtime. This utility records all calls made to the process_record method for later assertion.
Sources: tests/utilities.py:51-75
LDAP Client Testing
The client tests validate the FlextTargetLdapClient, focusing on its ability to construct URIs, manage connections, and map internal data structures to flext-ldap API calls.
The diagram shows the lifecycle of a client operation where the internal API is connected, executed, and disconnected within a single operation.
Sources: tests/unit/test_client.py:46-63
Key Client Validations
- URI Construction: Validates that
ldap://is used for port 389 andldaps://is used whenuse_sslis enabled. - Entry Persistence: Ensures that
add_entrycorrectly packages attributes into LDIF entries and thatmodify_entrytranslates changes into the format expected by the underlying LDAP library. - Search Mapping: Confirms that LDAP search results are correctly mapped back into the project's internal model.
Sources: tests/unit/test_client.py:25-132
Stream Sink Logic
Sinks are responsible for translating stream-specific Singer records into LDAP entries. The testing suite verifies the implementation of different sink types.
Sink Class Hierarchy
The target uses a factory pattern to return the appropriate sink based on the stream name.
| Stream Name | Sink Class |
|---|---|
users | FlextTargetLdapUsersSink |
groups | FlextTargetLdapGroupsSink |
organizational_units | FlextTargetLdapOrganizationalUnitsSink |
| (Custom/Other) | FlextTargetLdapBaseSink |
Sources: tests/unit/test_target.py:33-47, src/flext_target_ldap/api.py:91-105
Validation of Data Transformation
Tests in test_sinks.py ensure that:
- DN Construction: Users sinks correctly use
user_rdn_attribute(defaultuid) and groups usegroup_rdn_attribute(defaultcn) to build the full Distinguished Name. - Attribute Mapping: Complex Singer fields like
emailsare mapped to standard LDAP attributes likemail. - Object Classes: The suite verifies that the correct
objectClassvalues (e.g.,inetOrgPerson,groupOfNames) are applied to entries based on configuration. - Entry Validation: Checks that mandatory fields (DN, attributes, object classes) are present before attempting an LDAP write.
Sources: tests/unit/test_sinks.py:95-215
Integration and CLI Execution
Integration tests validate the end-to-end flow from the Command Line Interface (CLI) to the LDAP mock.
This flowchart illustrates how the CLI processes the input stream and routes operations through the client to the directory.
Sources: tests/unit/test_integration.py:84-140, src/flext_target_ldap/api.py:165-215
Critical Integration Scenarios
- Basic Load: Confirms that SCHEMA and RECORD messages trigger the creation of entries.
- Upsert Behavior: Validates that if an entry exists, the target attempts a
modify_entrycall. - Deletion Handling: Ensures that records with the
_sdc_deleted_atmetadata field result in an LDAPdelete_entryoperation. - Multi-Stream Support: Tests the target's ability to handle interleaved messages from different streams (e.g., users followed by groups) in a single session.
Sources: tests/unit/test_integration.py:64-167
Conclusion
The Unit Testing Suite provides the necessary safety net for the LDAP target by simulating diverse directory synchronization scenarios. By covering everything from low-level client connection logic to high-level CLI execution, it ensures that data transformations are accurate and that the target adheres to both the Singer specification and LDAP protocol requirements.
Sources: tests/unit/test_target.py, tests/unit/test_integration.py