
    	]jaL                     v    d dl Z d dlmZ d dlmZ d dlmZ d dlmZ d dl	m
Z
 d dlmZ  e       Z G d d	      Zy)
    N)get_user_model)LabelStudio)OrganizationFactory)ProjectFactory)TaskFactory)UserFactoryc                       e Zd ZdZ ej
                  d      d        Zd Zd Zd Z	d Z
d	 Zd
 Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zy)TestSDKPredictionValidationzDComprehensive tests for prediction validation using Label Studio SDKT)autousec                    |j                         5  t               | _        t        | j                        | _        | j                  | j                  _        | j                  j                          t        dd| j                  | j                        | _        t        | j                  ddi      | _
        ddd       y# 1 sw Y   yxY w)	zRSet up test environment with user, organization, project, and task using factories)
created_byzTest Projecta  
                <View>
                  <Text name="text" value="$text"/>
                  <Choices name="sentiment" toName="text">
                    <Choice value="positive"/>
                    <Choice value="negative"/>
                    <Choice value="neutral"/>
                  </Choices>
                  <Labels name="entities" toName="text">
                    <Label value="person" background="red"/>
                    <Label value="organization" background="blue"/>
                    <Label value="location" background="green"/>
                  </Labels>
                  <Rating name="quality" toName="text">
                    <Choice value="1"/>
                    <Choice value="2"/>
                    <Choice value="3"/>
                    <Choice value="4"/>
                    <Choice value="5"/>
                  </Rating>
                  <TextArea name="summary" toName="text"/>
                </View>
                )titlelabel_configr   organizationtextz)John Smith works at Microsoft in Seattle.)projectdataN)unblockr   userr   r   active_organizationsaver   r   r   task)selfdjango_db_setupdjango_db_blockers      [/root/env/lib/python3.12/site-packages/label_studio/tests/sdk/test_prediction_validation.pysetupz!TestSDKPredictionValidation.setup   s     &&( %	v#DI 3tyy ID,0,=,=DII)IINN *$.  99!..5DL< $DLLHs?tuDIK%	v %	v %	vs   B&C  C	c                 @   t        ||j                        }| j                  j                  dddddgidgddd	} |j                  j
                  di |}|j                  J |j                  | j                  j                  k(  sJ |j                  |d   k(  sJ y
)z7Test creating a valid prediction with choices using SDKbase_urlapi_key	sentimentr   choicespositive	from_nameto_nametypevalueffffff?v1.0r   resultscoremodel_versionNr-    r   r!   r   idpredictionscreater-   r   django_live_urlbusiness_clientlsprediction_data
predictions         r   test_valid_prediction_choicesz9TestSDKPredictionValidation.test_valid_prediction_choices9   s    /?;R;RS IILL)fi[dgqfrZst #
 +R^^**=_=
 }}((($)),,...  OH$====    c           	      F   t        ||j                        }| j                  j                  ddddgdddd	d
gddd} |j                  j
                  di |}|j                  J |j                  | j                  j                  k(  sJ |j                  |d   k(  sJ y)z6Test creating a valid prediction with labels using SDKr   entitiesr   labelspersonr   
   
John Smithr?   startendr   r%   333333?r+   r,   Nr-   r0   r1   r5   s         r   test_valid_prediction_labelsz8TestSDKPredictionValidation.test_valid_prediction_labelsN   s    /?;R;RS IILL ",%$)1
QrS_`	 #
 +R^^**=_=
}}((($)),,...  OH$====r<   c                 >   t        ||j                        }| j                  j                  dddddidgddd	} |j                  j
                  di |}|j                  J |j                  | j                  j                  k(  sJ |j                  |d   k(  sJ y
)z6Test creating a valid prediction with rating using SDKr   qualityr   rating   r%   ?r+   r,   Nr-   r0   r1   r5   s         r   test_valid_prediction_ratingz8TestSDKPredictionValidation.test_valid_prediction_ratingf   s    /?;R;RS IILL%.68_gij^klm#	
 +R^^**=_=
}}((($)),,...  OH$====r<   c                 @   t        ||j                        }| j                  j                  dddddgidgddd	} |j                  j
                  di |}|j                  J |j                  | j                  j                  k(  sJ |j                  |d   k(  sJ y
)z8Test creating a valid prediction with textarea using SDKr   summaryr   textareazThis is a summary of the text.r%   )\(?r+   r,   Nr-   r0   r1   r5   s         r   test_valid_prediction_textareaz:TestSDKPredictionValidation.test_valid_prediction_textareaw   s    /?;R;RS IILL "+%&$'G&HI	 #
 +R^^**=_=
}}((($)),,...  OH$====r<   c                    t        ||j                        }| j                  j                  ddigddd}t	        j
                  t              5   |j                  j                  di | ddd       y# 1 sw Y   yxY w)	z6Test prediction with missing required fields using SDKr   r&   r"   r*   r+   r,   Nr0   	r   r!   r   r2   pytestraises	Exceptionr3   r4   r   r6   r7   r8   r9   s        r   test_missing_required_fieldsz8TestSDKPredictionValidation.test_missing_required_fields   s{    /?;R;RS IILL   #

 ]]9% 	5!BNN!!4O4	5 	5 	5s   A55A>c                    t        ||j                        }| j                  j                  dddddgidgddd	}t	        j
                  t              5   |j                  j                  di | d
d
d
       y
# 1 sw Y   y
xY w)z5Test prediction with non-existent from_name using SDKr   nonexistent_tagr   r#   r$   r%   r*   r+   r,   Nr0   rT   rX   s        r   test_invalid_from_namez2TestSDKPredictionValidation.test_invalid_from_name   s    /?;R;RS IILL "3%%'*6	 #
 ]]9% 	5!BNN!!4O4	5 	5 	5   A;;Bc                    t        ||j                        }| j                  j                  dddddgidgddd	}t	        j
                  t              5   |j                  j                  di | d
d
d
       y
# 1 sw Y   y
xY w)z3Test prediction with non-existent to_name using SDKr   r"   nonexistent_targetr#   r$   r%   r*   r+   r,   Nr0   rT   rX   s        r   test_invalid_to_namez0TestSDKPredictionValidation.test_invalid_to_name   s    /?;R;RS IILL "-3%'*6	 #
 ]]9% 	5!BNN!!4O4	5 	5 	5r]   c                    t        ||j                        }| j                  j                  dddddgidgdd	d
}t	        j
                  t              5   |j                  j                  di | ddd       y# 1 sw Y   yxY w)z,Test prediction with type mismatch using SDKr   r"   r   r?   r#   r$   r%   r*   r+   r,   Nr0   rT   rX   s        r   test_type_mismatchz.TestSDKPredictionValidation.test_type_mismatch   s    /?;R;RS IILL "-%$'*6	 #
 ]]9% 	5!BNN!!4O4	5 	5 	5r]   c                    t        ||j                        }| j                  j                  dddddgidgddd	}t	        j
                  t              5   |j                  j                  di | d
d
d
       y
# 1 sw Y   y
xY w)z3Test prediction with invalid choice value using SDKr   r"   r   r#   invalid_choicer%   r*   r+   r,   Nr0   rT   rX   s        r   test_invalid_choice_valuez5TestSDKPredictionValidation.test_invalid_choice_value   s    /?;R;RS IILL "-%%'*:);<	 #
 ]]9% 	5!BNN!!4O4	5 	5 	5r]   c                    t        ||j                        }| j                  j                  dddddidgddd	}t	        j
                  t              5   |j                  j                  di | d
d
d
       y
# 1 sw Y   y
xY w)z3Test prediction with invalid rating value using SDKr   rI   r   rJ      r%   rL   r+   r,   Nr0   rT   rX   s        r   test_invalid_rating_valuez5TestSDKPredictionValidation.test_invalid_rating_value   s    /?;R;RS IILL "+%$&]	 #
 ]]9% 	5!BNN!!4O4	5 	5 	5   A::Bc           	         t        ||j                        }| j                  j                  ddddgdddd	d
gddd}t	        j
                  t              5   |j                  j                  di | ddd       y# 1 sw Y   yxY w)z3Test prediction with invalid labels value using SDKr   r>   r   r?   invalid_labelr   rA   rB   rC   r%   rF   r+   r,   Nr0   rT   rX   s        r   test_invalid_labels_valuez5TestSDKPredictionValidation.test_invalid_labels_value  s    /?;R;RS IILL ",%$#2"3!"! ,		
 #!
( ]]9% 	5!BNN!!4O4	5 	5 	5   A>>Bc                    t        ||j                        }| j                  j                  ddddgddd}t	        j
                  t              5   |j                  j                  d
i | d	d	d	       y	# 1 sw Y   y	xY w)z2Test prediction with missing value field using SDKr   r"   r   r#   )r&   r'   r(   r*   r+   r,   Nr0   rT   rX   s        r   test_missing_value_fieldz4TestSDKPredictionValidation.test_missing_value_field,  s    /?;R;RS IILL "-%% #
 ]]9% 	5!BNN!!4O4	5 	5 	5s   A77B c                 .   t        ||j                        }| j                  j                  g ddd} |j                  j
                  di |}|j                  J |j                  | j                  j                  k(  sJ |j                  |d   k(  sJ y)z1Test prediction with empty result array using SDKr   r*   r+   r,   Nr-   r0   r1   r5   s         r   test_empty_result_arrayz3TestSDKPredictionValidation.test_empty_result_arrayB  s    /?;R;RS#'99<<2_ef*R^^**=_=
}}((($)),,...  OH$====r<   c           
      :   t        ||j                        }| j                  j                  dddddgiddddd	ggd
dddddddddgidgddd}t	        j
                  t              5   |j                  j                  di | ddd       y# 1 sw Y   yxY w)zYTest prediction with multiple regions where some are valid and some are invalid using SDKr   r"   r   r#   r$   r%   r>   r?   r@   r   rA   rB   rC   r[   rF   r+   r,   Nr0   rT   rX   s        r   $test_multiple_regions_mixed_validityz@TestSDKPredictionValidation.test_multiple_regions_mixed_validityM  s    /?;R;RS IILL)fi[dgqfrZst!+%$*2qUab	 "3%%'*6	 #%
, ]]9% 	5!BNN!!4O4	5 	5 	5s   +BBc                    t        ||j                        }| j                  j                  dddddgiddddd	gd
ddddddddgddddddddddiddddddgidgddd} |j                  j
                  di |}|j                  J |j                  | j                  j                  k(  sJ |j                  |d   k(  sJ y)z?Test a complex valid prediction with multiple regions using SDKr   r"   r   r#   r$   r%   r>   r?   r@   r   rA   rB   rC   r         	MicrosoftrI   rJ   rK   rO   rP   z"A person works at an organization.gq=
ףp?zv2.0r,   Nr-   r0   r1   r5   s         r   test_complex_valid_predictionz9TestSDKPredictionValidation.test_complex_valid_predictionj  s   /?;R;RS IILL)fi[dgqfrZst!+%$)1
QrS_`	 ",%$)7(82bZef	 (FHX`bcWde!*%&$'K&LM	, #3
8 +R^^**=_=
}}((($)),,...  OH$====r<   c                    t        ||j                        }| j                  j                  dddddidgddd	}t	        j
                  t              5   |j                  j                  di | d
d
d
       y
# 1 sw Y   y
xY w)z?Test prediction with invalid textarea value structure using SDKr   rO   r   rP   zThis should be a listr%   rQ   r+   r,   Nr0   rT   rX   s        r   test_invalid_textarea_valuez7TestSDKPredictionValidation.test_invalid_textarea_value  s    /?;R;RS IILL "+%&$&=>	 #
 ]]9% 	5!BNN!!4O4	5 	5 	5ri   c           	         t        ||j                        }| j                  j                  dddddddd	d
gddd}t	        j
                  t              5   |j                  j                  di | ddd       y# 1 sw Y   yxY w)z7Test prediction with invalid labels structure using SDKr   r>   r   r?   r@   r   rA   rB   rC   r%   rF   r+   r,   Nr0   rT   rX   s        r   test_invalid_labels_structurez9TestSDKPredictionValidation.test_invalid_labels_structure  s    /?;R;RS IILL ",%$"*!"! ,		
 #!
( ]]9% 	5!BNN!!4O4	5 	5 	5s   A==Bc                    t        ||j                        }| j                  j                  ddddggdddd	gd
dd}t	        j
                  t              5   |j                  j                  di | ddd       y# 1 sw Y   yxY w)z;Test prediction with missing text field in labels using SDKr   r>   r   r?   r@   r   rA   )r?   rD   rE   r%   rF   r+   r,   Nr0   rT   rX   s        r   test_missing_text_in_labelsz7TestSDKPredictionValidation.test_missing_text_in_labels  s    /?;R;RS IILL ",%$$,:,!"!	
 #!
( ]]9% 	5!BNN!!4O4	5 	5 	5rm   c           	         t        ||j                        }| j                  j                  ddddggdddd	d
gddd}t	        j
                  t              5   |j                  j                  di | ddd       y# 1 sw Y   yxY w)z:Test prediction with invalid start/end positions using SDKr   r>   r   r?   r@   d   n   rB   rC   r%   rF   r+   r,   Nr0   rT   rX   s        r    test_invalid_start_end_positionsz<TestSDKPredictionValidation.test_invalid_start_end_positions  s    /?;R;RS IILL ",%$$,:,!$" ,		
 #!
( ]]9% 	5!BNN!!4O4	5 	5 	5   A??Bc           	         t        ||j                        }| j                  j                  ddddggdddd	d
gddd}t	        j
                  t              5   |j                  j                  di | ddd       y# 1 sw Y   yxY w)zATest prediction with end position before start position using SDKr   r>   r   r?   r@   rA      rB   rC   r%   rF   r+   r,   Nr0   rT   rX   s        r   test_end_before_startz1TestSDKPredictionValidation.test_end_before_start  s    /?;R;RS IILL ",%$*2r!Uab	 #
 ]]9% 	5!BNN!!4O4	5 	5 	5r   N)__name__
__module____qualname____doc__rU   fixturer   r;   rG   rM   rR   rY   r\   r`   rb   re   rh   rl   ro   rq   rs   rx   rz   r|   r~   r   r   r0   r<   r   r
   r
      s    NV^^D!'v "'vR>*>0>">05(5,5,5,5,5,565,	>5:$>L5,5656565r<   r
   )rU   django.contrib.authr   label_studio_sdkr   organizations.tests.factoriesr   projects.tests.factoriesr   tasks.tests.factoriesr   users.tests.factoriesr   Userr
   r0   r<   r   <module>r      s.     . ( = 3 - -5 5r<   