Test Fixtures & Mocking
Relevant source files
The following files were used as context for generating this wiki page:
The test infrastructure for flext-tap-ldap is designed to provide robust validation of LDAP data extraction through a combination of shared pytest fixtures, specialized test models, and extensive mocking of LDAP client interactions. The system supports various testing levels, including unit tests for client logic and LDIF processing, and end-to-end (E2E) integration tests that simulate Singer Tap execution.
By utilizing a unified namespace pattern (e.g., m.TapLdap.Tests.* and u.TapLdap.Tests.*), the project ensures that test-only objects and utilities remain isolated from production code while mirroring the structure of the main library. This architecture allows for realistic simulation of LDAP directory services and Singer Tap behaviors without requiring a live LDAP server for every test case.
Test Infrastructure Architecture
The testing architecture relies on pytest fixtures to manage state and configuration, while unittest.mock is used to intercept network calls to LDAP services.
The diagram above shows how fixtures from conftest.py and specific test files feed into the mocking strategy to support both Unit and E2E test execution.
Sources: tests/conftest.py, tests/e2e/test_integration.py:48-80
Pytest Fixtures
The project defines several key fixtures to standardize test environments across different suites.
Core Configuration Fixtures
These fixtures provide valid configuration structures required for initializing LDAP clients and Taps.
| Fixture Name | Source File | Description |
|---|---|---|
ldap_connection_config | tests/conftest.py | Returns a dictionary containing standard LDAP connection parameters (host, port, base_dn, etc.). |
ldap_source_config | tests/conftest.py | Provides a Meltano.DataSourceConfig object for testing Tap initialization. |
mock_ldap_config | tests/e2e/test_integration.py | Provides a mock LDAP configuration specifically for E2E CLI testing. |
Sources: tests/conftest.py:20-45, tests/e2e/test_integration.py:53-61
Data Fixtures
Fixtures that provide sample LDAP entries and catalog definitions for stream validation.
| Fixture Name | Source File | Description |
|---|---|---|
ldap_record_entries | tests/conftest.py | A list of dictionaries representing standard LDAP entries (e.g., uid=jdoe). |
sample_catalog | tests/e2e/test_integration.py | A JSON mapping representing a Singer catalog with a "users" stream. |
sample_state | tests/e2e/test_integration.py | An empty Singer state object for testing incremental syncs. |
Sources: tests/conftest.py:48-58, tests/e2e/test_integration.py:63-78
Mocking Strategies
Mocking is applied at multiple levels to isolate the code under test from external dependencies like LDAP servers and the filesystem.
LDAP Client Mocking
To avoid network dependencies, the FlextTapLdapClient.LDAPClient is frequently patched. In integration tests, the search method is mocked to return controlled lists of LDAP records.
This sequence illustrates the flow of a mocked integration test where the CLI runner interacts with a Tap that uses a mocked LDAP client.
Sources: tests/e2e/test_integration.py:82-120, tests/unit/test_tap.py:52-67
File System Fixtures
For tests requiring physical files (like --config or --catalog arguments in the CLI), the tmp_path fixture is used to create temporary JSON files.
config_file: Writesmock_ldap_configto a temporarysettings.json.catalog_file: Writessample_catalogto a temporarycatalog.json.state_file: Writessample_stateto a temporarystate.json.
Sources: tests/e2e/test_integration.py:80-100
Test Models & Utilities
The project uses a unified model and utility structure for testing, which inherits from core project models but adds test-specific properties.
Test Models (m.TapLdap.Tests)
Test models provide convenience properties for assertions.
- TestLdapConnection: Includes a
connection_stringproperty that computes the URI based on theuse_sslflag. - TestLdapEntry: Includes an
attribute_countproperty to easily verify entry complexity. - TestLdapSearch: Models LDAP search operations, including scope and size limits.
Sources: tests/models.py:29-106
Test Utilities (u.TapLdap.Tests)
Specialized utilities manage environment-specific logic, such as resolving credentials for Docker-based infrastructure.
- admin_credentials(): Attempts to resolve LDAP credentials by checking environment variables (
FLEXT_LDAP_BIND_DN), falling back to known defaults, and verifying them against a local container port. - FileLock: Provided for coordinating access to shared resources during parallel test execution.
Sources: tests/utilities.py:34-85
Summary
The "Test Fixtures & Mocking" system in flext-tap-ldap provides a comprehensive framework for validating LDAP integration. By combining pytest fixtures for configuration, temporary files for CLI simulation, and strategic mocking of the LDAPClient, the suite achieves high coverage and reliability. The use of specialized test models and automated credential resolution further supports advanced scenarios like Docker-based integration testing.