
    	]jC                     f    d Z ddlmZmZ ddlmZ  G d de      Z G d de      Z G d d	e      Zy
)zTest data_manager.managers module functionality.

This file tests the core functionality of the excluded_fields_for_evaluation
feature that optimizes task API performance by excluding expensive fields.
    )Mockpatch)TestCasec                   "    e Zd ZdZd Zd Zd Zy)TestExcludedFieldsLogica  Test the core logic of excluded_fields_for_evaluation functionality.

    This test validates step by step:
    - The field inclusion/exclusion logic works correctly
    - Edge cases are handled properly
    - The boolean logic matches expected behavior

    Critical validation: The core logic that determines whether a field
    should be included based on fields_for_evaluation, all_fields, and
    excluded_fields_for_evaluation parameters works correctly.
    c                 f   dgdg ddfdgddgddfdgdg ddfdgddgddfdgdg ddfdgdg ddfdgddddfg ddddgddfg ddddgddfg	}|D ]W  \  }}}}}| j                  ||||      5  |g }||v xs |xr ||v}| j                  ||d	| d
| d| d|        ddd       Y y# 1 sw Y   dxY w)a  Test the core field inclusion logic used in annotate_queryset.

        This test validates step by step:
        - Testing various combinations of parameters
        - Verifying the boolean logic is correct
        - Ensuring all edge cases are covered

        Critical validation: The logic (field in fields_for_evaluation or all_fields)
        and field not in excluded_fields_for_evaluation works as expected.
        field1Tfield2FN)r	   r
   field3r   )fields_for_eval
all_fieldsexcluded_fields
test_fieldz(Field inclusion logic failed for field 'z' with fields_for_eval=z, all_fields=z, excluded_fields=)subTestassertEqual)selftest_scenariosr   r   r   r   expectedshould_includes           W/root/env/lib/python3.12/site-packages/label_studio/data_manager/tests/test_managers.pytest_field_inclusion_logicz2TestExcludedFieldsLogic.test_field_inclusion_logic   sb    Zr8T2Zz8U;ZHd3Z
He<ZHe4Zr8T2ZtXt4+THh3GSWX+THh3GSXY#
( Sa 	NOZ*h /% /%	    #*&(O","?"M:!tS]etSt  ">zl K''6&7}ZL Q''6&79 	 s   *2B''B0	c                 n    ddg}d}|g }|D ]&  }||v xs dxr ||v}| j                  |d| d       ( y)a  Test that None excluded_fields_for_evaluation is handled correctly.

        This test validates step by step:
        - Passing None as excluded_fields_for_evaluation
        - Verifying it's treated as an empty list
        - Ensuring no fields are excluded when None is passed

        Critical validation: None values for excluded_fields_for_evaluation
        should not cause errors and should behave as if no exclusions were specified.
        r	   r
   NFzField 'z1' should be included when excluded_fields is None)
assertTrue)r   fields_for_evaluationr   fieldr   s        r   "test_excluded_fields_none_handlingz:TestExcludedFieldsLogic.test_excluded_fields_none_handlingM   sg     "*8 4 " O + 	pE#'<<Eg5XgKgNOONgeW<m,no	p    c                     ddg}g d}||z   }|}|D ]\  }| j                  |      5  |dv xs dxr ||v}||v r| j                  |d| d       n| j                  |d	| d
       ddd       ^ y# 1 sw Y   ixY w)a  Test specific performance optimization field combinations.

        This test validates step by step:
        - Testing the exact field combinations used in the performance optimization
        - Verifying expensive fields are excluded when specified
        - Ensuring normal fields are still included

        Critical validation: The specific optimization used in the TaskAPI
        (excluding annotations_results and predictions_results) works correctly.
        annotations_resultspredictions_results)completed_atavg_lead_timedraft_exists
annotators)r    TzExpensive field 'z' should be excludedzNormal field 'z' should be includedN)r   assertFalser   )r   expensive_fieldsnormal_fieldsr   r   r   r   s          r   $test_performance_optimization_fieldsz<TestExcludedFieldsLogic.test_performance_optimization_fieldse   s     23HIW%5
 + 		bEE* bRK'4"3?2  ,,$$^7HOc5deOONnUGK_4`ab b		bb bs   A A22A;	N)__name__
__module____qualname____doc__r   r   r)   r%   r   r   r   r      s    
3jp0br   r   c                   "    e Zd ZdZd Zd Zd Zy)TestPreparedTaskManagerBehaviora  Test PreparedTaskManager behavior with mock annotation functions.

    This test validates step by step:
    - The annotate_queryset method calls the right functions
    - Excluded fields are properly skipped
    - The overall flow works correctly

    Critical validation: The excluded_fields_for_evaluation feature properly
    controls which annotation functions are executed.
    c                    ddl m} t               }d|j                  _        g fd} |d       |d       |d       |d      d	}t        d
|      5   |       }j                          |j                  |dddg       | j                  dd       | j                  dd       | j                  dd       | j                  dd       ddd       y# 1 sw Y   yxY w)a  Test annotate_queryset with simple trackable annotation functions.

        This test validates step by step:
        - Creating simple annotation functions that can be tracked
        - Calling annotate_queryset with exclusions
        - Verifying which functions were called vs skipped

        Critical validation: The exclusion logic properly controls function execution.
        r   PreparedTaskManagerNc                       fd}|S )Nc                 *    j                         | S Nappendquerysetcalled_functions
field_names    r   annotation_functionzTestPreparedTaskManagerBehavior.test_annotate_queryset_with_simple_functions.<locals>.make_annotation_function.<locals>.annotation_function        ''
3r   r%   r;   r<   r:   s   ` r   make_annotation_functionznTestPreparedTaskManagerBehavior.test_annotate_queryset_with_simple_functions.<locals>.make_annotation_function         '&r   r   r    r!   r"   r   r    r!   r"   )data_manager.managers.get_annotations_mapreturn_valueTr9   r   excluded_fields_for_evaluation5Non-excluded field 'completed_at' should be processed6Non-excluded field 'avg_lead_time' should be processed<Excluded field 'annotations_results' should not be processedz<Excluded field 'predictions_results' should not be processed)
data_manager.managersr2   r   firstrD   r   clearannotate_querysetassertInassertNotInr   r2   mock_querysetr?   test_annotationsmanagerr:   s         @r   ,test_annotate_queryset_with_simple_functionszLTestPreparedTaskManagerBehavior.test_annotate_queryset_with_simple_functions   s	    	> +/( 	' $<<Q#R#;<Q#R4^D5oF	
 >M]^ 	)+G ""$%%&0EG\/] &  MM.*:<stMM/+;=uv%'79w %'79w#	 	 	s   A:CC"c                    ddl m} t               }d|j                  _        g fd} |d       |d       |d      d}t        d	|
      5   |       }j                          |j                  |dd       | j                  dd       | j                  dd       | j                  dd       ddd       y# 1 sw Y   yxY w)az  Test annotate_queryset without any exclusions.

        This test validates step by step:
        - Creating annotation functions with no exclusions
        - Verifying all functions are called
        - Ensuring backward compatibility

        Critical validation: When no exclusions are specified, all fields
        should be processed maintaining existing behavior.
        r   r1   Nc                       fd}|S )Nc                 *    j                         | S r5   r6   r8   s    r   r<   zTestPreparedTaskManagerBehavior.test_annotate_queryset_without_exclusions.<locals>.make_annotation_function.<locals>.annotation_function   r=   r   r%   r>   s   ` r   r?   zkTestPreparedTaskManagerBehavior.test_annotate_queryset_without_exclusions.<locals>.make_annotation_function   r@   r   r   r    r!   )r   r    r!   rB   rC   TrE   zAField 'annotations_results' should be processed when not excludedzAField 'predictions_results' should be processed when not excludedz:Field 'completed_at' should be processed when not excluded)	rJ   r2   r   rK   rD   r   rL   rM   rN   rP   s         @r   )test_annotate_queryset_without_exclusionszITestPreparedTaskManagerBehavior.test_annotate_queryset_without_exclusions   s     	>+/(	' $<<Q#R#;<Q#R4^D
 >M]^ 	)+G ""$%%}nr%s MM% S
 MM% S
 MM 02n%	 	 	s   A%B==Cc                    ddl m} t               }d|j                  _        g fd} |d       |d       |d       |d      d	}t        d
|      5   |       }j                          |j                  |g ddg       | j                  dd       | j                  dd       | j                  dd       | j                  dd       ddd       y# 1 sw Y   yxY w)a  Test annotate_queryset with specific fields_for_evaluation and exclusions.

        This test validates step by step:
        - Specifying particular fields for evaluation
        - Adding exclusions to those fields
        - Verifying only the right subset is processed

        Critical validation: The combination of fields_for_evaluation and
        excluded_fields_for_evaluation works correctly together.
        r   r1   Nc                       fd}|S )Nc                 *    j                         | S r5   r6   r8   s    r   r<   zTestPreparedTaskManagerBehavior.test_annotate_queryset_with_specific_fields.<locals>.make_annotation_function.<locals>.annotation_function  r=   r   r%   r>   s   ` r   r?   zmTestPreparedTaskManagerBehavior.test_annotate_queryset_with_specific_fields.<locals>.make_annotation_function  r@   r   r   r    r!   r"   rA   rB   rC   )r   r!   r"   )r9   r   rF   rI   rG   rH   zRField 'predictions_results' should not be processed (not in fields_for_evaluation))
rJ   r2   r   rK   rD   r   rL   rM   rO   rN   rP   s         @r   +test_annotate_queryset_with_specific_fieldszKTestPreparedTaskManagerBehavior.test_annotate_queryset_with_specific_fields   s    	>+/(	' $<<Q#R#;<Q#R4^D5oF	
 >M]^ 	)+G ""$%%&&^0E/F &  %'79w MM.*:<stMM/+;=uv% d#	 	 	s   A;CC#N)r*   r+   r,   r-   rT   rX   r\   r%   r   r   r/   r/      s    	5n3j5r   r/   c                       e Zd ZdZd Zd Zy)TestGetQuerysetParameterPassinga  Test that get_queryset properly passes excluded_fields_for_evaluation parameter.

    This test validates step by step:
    - The get_queryset method accepts the parameter
    - The parameter is passed through to annotate_queryset
    - Default values work correctly

    Critical validation: The get_queryset method serves as the main interface
    and properly forwards the optimization parameters.
    c                    ddl m} ddlm}  |       }t	        |      }d|_        t	               |_        t        j                  |d      5 }t        j                  |d      5 }t	               }||_	        ||_	        |j                  |dd	d
g       |j                          |j                  d   }| j                  |j                  d      d	d
gd       ddd       ddd       y# 1 sw Y   xY w# 1 sw Y   yxY w)a  Test that get_queryset accepts excluded_fields_for_evaluation parameter.

        This test validates step by step:
        - Calling get_queryset with the excluded_fields_for_evaluation parameter
        - Ensuring the method accepts the parameter without errors
        - Verifying the interface is correctly defined

        Critical validation: The public API properly accepts the optimization parameter.
        r   r1   PrepareParamsspec   only_filteredrM   Tr   r    )prepare_paramsr   rF   rF   zDexcluded_fields_for_evaluation should be passed to annotate_querysetN)rJ   r2   data_manager.modelsra   r   projectrequestr   objectrD   get_querysetassert_called_once	call_argsr   get	r   r2   ra   rS   mock_prepare_paramsmock_only_filteredmock_annotaterQ   call_kwargss	            r   %test_get_queryset_parameter_interfacezETestGetQuerysetParameterPassing.test_get_queryset_parameter_interface@  s    	>5%'"6&'#&*f# \\'?3 	7I5<<(L
 	 FM.;+)6M&   20EG\/] !  ,,.'11!4K @A&(=>V%	 	 	 	 	 	s%   C2#A2C&C2&C/	+C22C;c                    ddl m} ddlm}  |       }t	        |      }d|_        t	               |_        t        j                  |d      5 }t        j                  |d      5 }t	               }||_	        ||_	        |j                  |d	       |j                          |j                  d   }| j                  |j                  d
      d       ddd       ddd       y# 1 sw Y   xY w# 1 sw Y   yxY w)a  Test that get_queryset handles default parameter values correctly.

        This test validates step by step:
        - Calling get_queryset without excluded_fields_for_evaluation
        - Verifying the parameter defaults appropriately
        - Ensuring backward compatibility

        Critical validation: When the parameter is not provided, the behavior
        should remain unchanged from the original implementation.
        r   r1   r`   rb   rd   re   rM   T)rf   r   rF   z5excluded_fields_for_evaluation should default to NoneN)rJ   r2   rg   ra   r   rh   ri   r   rj   rD   rk   rl   rm   assertIsNonern   ro   s	            r   ,test_get_queryset_default_parameter_handlingzLTestGetQuerysetParameterPassing.test_get_queryset_default_parameter_handlingk  s     	>5%'"6&'#&*f#\\'?3 	7I5<<(L
 	 FM.;+)6M&   0CPT U ,,.'11!4K @AG	 	 	 	 	 	s%   C,#A,C C, C)	%C,,C5N)r*   r+   r,   r-   rt   rw   r%   r   r   r^   r^   4  s    	)V$r   r^   N)	r-   unittest.mockr   r   django.testr   r   r/   r^   r%   r   r   <module>rz      s@   
 &  vbh vbrmh m`[h [r   