Skip to content

Add Comprehensive Unit Test Coverage for Decorators #1103

Open
@devin-ai-integration

Description

@devin-ai-integration

Add Comprehensive Unit Test Coverage for Decorators

Overview

The AgentOps decorator system in the main agentops repository needs comprehensive unit test coverage to ensure reliability and maintainability. While some tests exist for span nesting scenarios, significant gaps remain in testing the core decorator functionality.

Current State

Repository: AgentOps-AI/agentops
Location: agentops/sdk/decorators/
Existing Tests: tests/unit/sdk/test_decorators.py

Current Test Coverage

  • ✅ Span nesting scenarios (sync, async, generators)
  • ✅ Basic tool decorator with cost attributes
  • ✅ Async context manager functionality
  • ✅ Workflow and task nesting

Missing Test Coverage

Required Test Coverage

1. Factory Function Tests (factory.py)

Priority: High

The create_entity_decorator factory function needs comprehensive testing:

# Test areas needed:
- Factory function with different SpanKind values
- Decorator creation with various parameter combinations
- Partial application scenarios (@decorator vs @decorator())
- Invalid parameter handling
- Memory management and decorator reuse

Specific test cases:

  • Test factory with all SpanKind values (SESSION, AGENT, TASK, WORKFLOW, TOOL, GUARDRAIL)
  • Test decorator parameter validation (name, version, tags, cost, spec)
  • Test partial application patterns
  • Test decorator reuse across multiple functions/classes

2. Guardrail Decorator Tests

Priority: High

The @guardrail decorator lacks dedicated test coverage:

# Test areas needed:
- Input guardrail functionality (spec="input")
- Output guardrail functionality (spec="output") 
- Spec attribute recording
- Integration with other decorators
- Error handling for invalid spec values

3. Class Decoration Edge Cases

Priority: Medium

Expand class decoration testing beyond basic async context manager:

# Test areas needed:
- Class inheritance with decorated parent classes
- Multiple decorator application on classes
- Class method decoration vs class decoration
- __init__ parameter recording
- Class instance lifecycle management
- Exception handling in __aenter__/__aexit__

4. Input/Output Recording Tests

Priority: High

Test the serialization and recording functionality in utility.py:

# Test areas needed:
- _record_entity_input with various data types
- _record_entity_output with complex objects
- _check_content_size boundary testing (1MB limit)
- Serialization failure handling
- Large object truncation behavior
- Entity kind parameter variations

5. Generator and Async Generator Edge Cases

Priority: Medium

Expand generator testing beyond basic functionality:

# Test areas needed:
- Generator exception handling
- Early generator termination
- Nested generator scenarios
- Async generator cancellation
- Generator context management
- Memory cleanup for long-running generators

6. Error Handling and Edge Cases

Priority: High

Comprehensive error scenario testing:

# Test areas needed:
- Tracer not initialized scenarios
- Span creation failures
- Context management errors
- Serialization exceptions
- Network/API failures during span recording
- Concurrent decorator usage

7. Utility Function Tests (utility.py)

Priority: Medium

Direct testing of utility functions:

# Functions needing tests:
- set_workflow_name()
- set_entity_path()
- _process_sync_generator()
- _process_async_generator()
- _create_as_current_span()
- _get_current_span_info()

8. Integration and Performance Tests

Priority: Low

# Test areas needed:
- Decorator performance overhead measurement
- Memory usage with many decorated functions
- Concurrent execution scenarios
- Integration with OpenTelemetry tracing
- Span attribute validation

Implementation Guidelines

Test Structure

  • Create separate test classes for each decorator type
  • Use parameterized tests for testing multiple SpanKind values
  • Mock external dependencies (tracer, OpenTelemetry components)
  • Include both positive and negative test cases

Test Data

  • Use realistic test data that reflects actual usage patterns
  • Include edge cases (empty inputs, large objects, special characters)
  • Test with various Python data types (primitives, collections, custom objects)

Coverage Goals

  • Aim for >90% code coverage on decorator modules
  • Ensure all code paths in factory function are tested
  • Cover all exception handling branches
  • Test all decorator parameter combinations

Acceptance Criteria

  • All decorator types have comprehensive unit tests
  • Factory function has >95% code coverage
  • Error scenarios are properly tested
  • Utility functions have dedicated test coverage
  • Tests pass consistently in CI/CD pipeline
  • Performance regression tests are included
  • Documentation includes testing examples

Files to Modify/Create

New Test Files

  • tests/unit/sdk/test_decorator_factory.py - Factory function tests
  • tests/unit/sdk/test_decorator_utilities.py - Utility function tests
  • tests/unit/sdk/test_guardrail_decorator.py - Guardrail-specific tests

Existing Files to Expand

  • tests/unit/sdk/test_decorators.py - Add missing edge cases

Related Code References

Main Implementation:

  • agentops/sdk/decorators/factory.py - Core factory implementation
  • agentops/sdk/decorators/utility.py - Helper functions
  • agentops/sdk/decorators/__init__.py - Public decorator exports

Existing Tests:

  • tests/unit/sdk/test_decorators.py - Current test coverage
  • tests/unit/sdk/instrumentation_tester.py - Test utilities

Timeline Estimate

  • Phase 1 (Week 1): Factory function and guardrail decorator tests
  • Phase 2 (Week 2): Input/output recording and utility function tests
  • Phase 3 (Week 3): Edge cases, error handling, and integration tests
  • Phase 4 (Week 4): Performance tests and documentation updates

Success Metrics

  • Code coverage increase from current ~60% to >90% for decorator modules
  • All CI/CD tests pass consistently
  • Zero regression in existing functionality
  • Improved debugging capabilities through better test coverage

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions