10 KiB
NoDupeLabs Test Utilities
This directory contains comprehensive test utility functions for the NoDupeLabs project. These utilities are designed to support Task 1.3 of the test coverage implementation plan.
Overview
The test utilities provide helper functions for common testing patterns and scenarios, including:
- File System Operations - Temporary file/directory creation and management
- Database Operations - Database state verification and mocking
- Plugin System - Plugin loading, validation, and testing
- Performance Benchmarking - Performance measurement and analysis
- Error Condition Simulation - Error scenario creation and testing
- Data Validation - Schema validation and consistency checking
Modules
1. filesystem.py - File System Test Utilities
Purpose: Helper functions for file system operations testing
Key Functions:
create_test_file_structure()- Create complex file structures for testingcreate_duplicate_files()- Create multiple duplicate filescreate_files_with_varying_sizes()- Create files with specific sizescreate_symlinks_and_hardlinks()- Create symbolic and hard linkscalculate_file_hash()- Calculate file hashes for integrity checkingcompare_files()- Compare file contentscreate_nested_directory_structure()- Create nested directory structurescreate_files_with_timestamps()- Create files with specific timestampsverify_file_structure()- Verify file structure matches expectationsmock_file_operations()- Create mock file system operationscreate_large_file()- Create large files for performance testing
2. database.py - Database Test Utilities
Purpose: Helper functions for database operations testing
Key Functions:
create_test_database()- Create test databases with optional schema and datasetup_test_database_schema()- Set up database schema for testinginsert_test_data()- Insert test data into database tablesverify_database_state()- Verify database state matches expected statecreate_database_mock()- Create mock database connectionscreate_database_fixture()- Create pytest fixtures for database testingsimulate_database_errors()- Simulate various database error conditionsbenchmark_database_operations()- Benchmark database operation performancecreate_transaction_test_scenarios()- Create transaction testing scenariosverify_database_performance()- Verify database query performancecreate_database_snapshot()- Create database state snapshotsrestore_database_snapshot()- Restore database to previous state
3. plugins.py - Plugin Test Utilities
Purpose: Helper functions for plugin system testing
Key Functions:
create_mock_plugin()- Create mock plugins for testingcreate_plugin_directory_structure()- Create plugin directory structuresmock_plugin_loader()- Create mock plugin loaderscreate_plugin_test_scenarios()- Create plugin testing scenariossimulate_plugin_errors()- Simulate plugin error conditionsverify_plugin_functionality()- Verify plugin functionalitycreate_plugin_dependency_graph()- Create plugin dependency graphstest_plugin_dependency_resolution()- Test plugin dependency resolutioncreate_plugin_sandbox_environment()- Create sandbox environments for pluginsmock_plugin_registry()- Create mock plugin registriescreate_plugin_lifecycle_test_scenarios()- Create plugin lifecycle scenariosbenchmark_plugin_performance()- Benchmark plugin performancecreate_plugin_security_test_scenarios()- Create plugin security scenarios
4. performance.py - Performance Test Utilities
Purpose: Helper functions for performance benchmarking and testing
Key Functions:
benchmark_function_performance()- Benchmark function performancemeasure_memory_usage()- Measure memory usage of functionscreate_performance_test_scenarios()- Create performance test scenariossimulate_resource_constraints()- Simulate resource constraintscreate_load_test_scenarios()- Create load test scenariosbenchmark_file_operations()- Benchmark file operationscreate_performance_monitor()- Create performance monitoring contextssimulate_slow_operations()- Simulate slow operationscreate_stress_test_scenarios()- Create stress test scenariosbenchmark_database_operations()- Benchmark database operationscreate_network_performance_test_scenarios()- Create network performance scenariosmeasure_concurrency_performance()- Measure concurrency performancecreate_performance_regression_test_scenarios()- Create regression test scenariossimulate_performance_degradation()- Simulate performance degradationcreate_resource_monitoring_scenarios()- Create resource monitoring scenarios
5. errors.py - Error Test Utilities
Purpose: Helper functions for error condition simulation and testing
Key Functions:
simulate_file_system_errors()- Simulate file system errorscreate_error_test_scenarios()- Create error test scenariossimulate_network_errors()- Simulate network errorscreate_exception_test_cases()- Create exception test casessimulate_memory_errors()- Simulate memory errorscreate_error_recovery_test_scenarios()- Create error recovery scenariossimulate_database_errors()- Simulate database errorscreate_error_injection_test_scenarios()- Create error injection scenariossimulate_plugin_errors()- Simulate plugin errorscreate_error_handling_test_scenarios()- Create error handling scenariossimulate_concurrency_errors()- Simulate concurrency errorscreate_error_validation_test_scenarios()- Create error validation scenariossimulate_resource_exhaustion_errors()- Simulate resource exhaustioncreate_error_monitoring_test_scenarios()- Create error monitoring scenariossimulate_timeout_errors()- Simulate timeout errorscreate_error_recovery_validation_scenarios()- Create error recovery validation scenarios
6. validation.py - Validation Test Utilities
Purpose: Helper functions for data validation and testing
Key Functions:
validate_test_data_structure()- Validate data structure against schemacreate_data_validation_test_cases()- Create data validation test casesvalidate_file_integrity()- Validate file integrity using hashescreate_file_validation_test_scenarios()- Create file validation scenariosvalidate_json_schema()- Validate JSON data against schemacreate_json_validation_test_cases()- Create JSON validation test casesvalidate_database_schema()- Validate database schema structurecreate_database_validation_test_scenarios()- Create database validation scenariosvalidate_plugin_structure()- Validate plugin structure and metadatacreate_plugin_validation_test_cases()- Create plugin validation test casesvalidate_api_response()- Validate API response structurecreate_api_validation_test_scenarios()- Create API validation scenariosvalidate_configuration_files()- Validate configuration filescreate_configuration_validation_test_cases()- Create configuration validation scenariosvalidate_data_consistency()- Validate data consistencycreate_data_consistency_test_scenarios()- Create data consistency scenarios
Usage
Importing Utilities
# Import specific modules
from tests.utils import filesystem, database, plugins, performance, errors, validation
# Or import all utilities
from tests.utils import *
Example Usage
# Create a test file structure
structure = {
"config": {
"settings.json": '{"debug": true}',
"cache": {}
},
"data.txt": "Test data"
}
created_files = filesystem.create_test_file_structure(Path("/tmp/test"), structure)
# Create a test database
conn = database.create_test_database(use_memory=True)
schema = "CREATE TABLE users (id INTEGER PRIMARY KEY, name TEXT);"
database.setup_test_database_schema(conn, schema)
# Create mock plugins
mock_plugin = plugins.create_mock_plugin("test_plugin")
result = mock_plugin.execute("test_input")
# Benchmark performance
def process_data(data):
return [x * 2 for x in data]
results = performance.benchmark_function_performance(
process_data,
iterations=100,
args=([1, 2, 3, 4, 5],)
)
# Validate data structure
data = {"user": {"id": 123, "name": "test"}}
schema = {
"type": "dict",
"properties": {
"user": {
"type": "dict",
"required": ["id", "name"],
"properties": {
"id": {"type": "int"},
"name": {"type": "str"}
}
}
}
}
is_valid = validation.validate_test_data_structure(data, schema)
Testing
The utilities include comprehensive test coverage in tests/test_utils.py. Run the tests with:
# Run utility tests
python tests/test_utils.py
# Or with pytest
python -m pytest tests/test_utils.py -v
Integration with Test Coverage Plan
These utilities directly support Task 1.3: Test Utility Functions from the test coverage implementation plan:
- ✅ Temporary file/directory creation -
filesystem.py - ✅ Database state verification -
database.py - ✅ Plugin loading and validation -
plugins.py - ✅ Performance benchmarking -
performance.py - ✅ Error condition simulation -
errors.py - ✅ Data validation -
validation.py
Best Practices
- Isolation: Each utility function is designed to be independent and reusable
- Comprehensive Documentation: All functions include detailed docstrings
- Error Handling: Utilities include proper error handling and validation
- Performance: Performance-critical functions are optimized
- Maintainability: Code follows consistent style and patterns
Future Enhancements
- Add more specific validation patterns for NoDupeLabs data structures
- Enhance performance benchmarking with more detailed metrics
- Add support for additional database types beyond SQLite
- Expand error simulation capabilities for more edge cases
- Add utilities for testing parallel and distributed operations
Contributing
Contributions to the test utilities are welcome. Please follow the existing patterns and ensure all new functions include:
- Comprehensive docstrings
- Type hints
- Unit tests in
test_utils.py - Integration with the existing module structure