o
    iM                     @   s  d Z ddlZddlmZ ddlZddlZddlmZ ddlmZ	 ddlm
Z
 G dd	 d	ejZejZG d
d dejZejZG dd dejZejZG dd dejZG dd dejZG dd dejZejdd Zejdddgdd Zejdddgdd Zejdddgdd Zejdddgdd  Zejdddgd!d" Zd#d$ Zejdddgd%d& Zejdddgd'd( Z ejdddgd)d* Z!ejdddgd+d, Z"ejdddgd-d. Z#ejd/g d0d1d2 Z$ejd/g d0d3d4 Z%ejd/g d0d5d6 Z&ejd/g d0d7d8 Z'ejd/g d0d9d: Z(ejd/g d0d;d< Z)ejdddgd=d> Z*ejdddgd?d@ Z+ejdddgdAdB Z,dS )Cz<Tests schema processing methods in the _transformers module.    N)Union   )_transformers)client)typesc                   @   s&   e Zd ZU eed< eed< eed< dS )CurrencyInfonamecodesymbolN)__name__
__module____qualname__str__annotations__ r   r   _/home/ubuntu/.local/lib/python3.10/site-packages/google/genai/tests/transformers/test_schema.pyr      s   
 r   c                   @   sF   e Zd ZU eed< eed< eed< eed< eed< eed< eed< dS )	CountryInfor   
populationcapital	continentgdpofficial_languagetotal_area_sq_miN)r   r   r   r   r   intr   r   r   r   r   '   s   
 r   c                   @   sN   e Zd ZU eed< eed< eed< eed< eed< eed< eed< eed< d	S )
CountryInfoWithCurrencyr   r   r   r   r   r   r   currencyN)r   r   r   r   r   r   r   r   r   r   r   r   4   s   
 r   c                   @   s*   e Zd ZU eed< dZeedf ed< dS )CountryInfoWithNullFieldsr   Nr   )r   r   r   r   r   r   r   r   r   r   r   r   r   B   s   
 r   c                   @   s"   e Zd ZU eed< dZeed< dS )CountryInfoWithDefaultValuer   r   r   N)r   r   r   r   r   r   r   r   r   r   r   r   G   s   
 r   c                   @   s&   e Zd ZU eed< eeef ed< dS )CountryInfoWithAnyOfr   restaurants_per_capitaN)r   r   r   r   r   r   r   floatr   r   r   r   r   L   s   
 r   c                 c   s0    | rt j| dddV  d S t j| ddV  d S )Nztest-projectztest-location)vertexaiprojectlocationztest-api-key)r!   api_key)google_genai_client_moduleClient)
use_vertexr   r   r   r   Q   s   r   r'   TFc                 C   s   t | t }t|tsJ |d D ]E}d|d | v sJ d|d | v s)J |tv s/J t| jj}|d | d 	 
|	 sFJ d|v sLJ |d tt ksXJ qdS )zSTests _build_schema() when list[pydantic.BaseModel] is provided to response_schema.
propertiestitletyperequiredN)r   t_schemar   
model_dump
isinstancedictcountry_info_fields
annotationr   lower
startswithlistkeys)r   list_schema
field_namefield_type_strr   r   r   -test_build_schema_for_list_of_pydantic_schema]   s   
r9   c                 C   s   t | t }t|tsJ |d D ]}d|d | v sJ d|d | v s)J |tv s/J qd|d d v s:J |d d d D ]}|tv sJJ qBdS )zTests _build_schema() when list[pydantic.BaseModel] is provided to response_schema and the pydantic.BaseModel has nested pydantic fields.r(   r)   r*   r   N)r   r,   r   r-   r.   r/   nested_country_info_fieldscurrency_info_fields)r   r6   r7   r   r   r   4test_build_schema_for_list_of_nested_pydantic_schemas   s   r<   c                 C   sN   t | t}t|tjsJ |jD ]}|tv sJ t|j| tjs$J qdS )zDTests t_schema when pydantic.BaseModel is passed to response_schema.N)r   r,   r   r.   r   Schemar(   r0   r   transformed_schemaschema_propertyr   r   r   !test_t_schema_for_pydantic_schema   s   

rA   c                 C   sh   t | tt }t|tjsJ t|jtjsJ |jjD ]}|t	v s%J t|jj| tjs1J qdS )zJTests t_schema when list[pydantic.BaseModel] is passed to response_schema.N)
r   r,   r4   r   r.   r   r=   itemsr(   r0   r>   r   r   r   )test_t_schema_for_list_of_pydantic_schema   s   
rC   c                 C   s0   t | t}t|tjsJ |jd jsJ dS )z,Tests t_schema when null fields are present.r   N)r   r,   r   r.   r   r=   r(   nullable)r   r?   r   r   r   test_t_schema_for_null_fields   s   rE   c                  C   sZ   dddddiddigddd	d
} |   D ]\}}t|}t| ||ks*J qdS )zSTests handle_null_fields() doesn't change anything when no null fields are present.Namestring)r)   r*   r*   integerr    nullzTotal Area Sq Mi)anyOfdefaultr)   )r   r   N)rB   copydeepcopyr   handle_null_fields)test_properties_schemaschema_beforer   r   r   ,test_schema_with_no_null_fields_is_unchanged   s   	

rS   c                 C   sT   t | jt}tjtjdddtjdddddd	d
gdd
dgd}||ks(J d S )NSTRINGrF   r*   r)   INTEGERr   
Populationr*   rK   r)   r   r   OBJECTr   r   r   r(   r*   r+   r)   property_ordering)r   r,   _api_clientr   r   r=   r   r?   expected_schemar   r   r   test_schema_with_default_value   s(   r`   c                 C   sf   t | t}tjtjdddtjtjddtjddgddd	d
ddgdddgd}||ks1J d S )NrT   rF   rU   rV   )r*   NUMBERzRestaurants Per Capita)any_ofr)   )r   r   rZ   r   r   r   r[   )r   r,   r   r   r=   r^   r   r   r   test_schema_with_any_of   s&   

rc   c                 C   s   | j rPddddgdddddd	dd
ddd
dddddgddgddddd
ddd
dddddgddgdgddid}t|}t||  ||ksRJ dS dS )z]When a dict schema is passed to process_schema, the only change should be camel-casing anyOf.rZ   zFruit Basketz-A structured representation of a fruit basketfruitARRAYz*An ordered list of the fruit in the basketzA piece of fruitApplezDescribes an applerT   zAlways 'apple')r*   descriptionz$The color of the apple (e.g., 'red'))r*   colorr*   rh   )r)   rg   r*   r(   propertyOrderingr+   OrangezDescribes an orangezAlways 'orange'z'The size of the orange (e.g., 'medium'))r*   sizerk   )rg   rJ   )r*   rg   rB   )r*   r)   rg   r+   r(   N)r!   rL   rM   r   process_schema)r   dict_schemarR   r   r   r   0test_complex_dict_schema_with_anyof_is_unchanged   sZ   
9=rn   c                 C   s2   ddd}ddgd}t ||  ||ksJ dS )z<The 'const' field should be converted to a singleton 'enum'.rT   FOOr*   const)r*   enumNr   rl   )r   rQ   r_   r   r   r   *test_process_schema_converts_const_to_enum4  s   rt   c                 C   sJ   ddd}t jtdd t||  W d   dS 1 sw   Y  dS )z)The 'const' field only works for strings.rV   {   rp   z".*Literal values must be strings.*)matchN)pytestraises
ValueErrorr   rl   )r   rQ   r   r   r   ,test_process_schema_forbids_non_string_constE  s   "rz   zuse_vertex,order_properties))FF)FT)TF)TTc                 C   s   dddddiddiddid}dddiddidd}dddiddidd	d
gd}t j|| |d |r>||ks<J dS ||ksDJ dS )z7The `order_properties` setting should apply to '$defs'.z#/$defs/FooFoorZ   r*   rT   foobarr*   r(   )z$refz$defsr}   r~   r*   r(   r\   order_propertiesNrs   r   r   rQ    schema_without_property_orderingschema_with_property_orderingr   r   r   9test_process_schema_order_properties_propagates_into_defsQ  s4   		r   c                 C   s   ddddiddiddd}t |}ddddiddiddd	gd
d}tj|| |d |r8||ks6J dS ||ks>J dS )z7The `order_properties` setting should apply to 'items'.re   rZ   r*   rT   r|   r   )r*   rB   r}   r~   r   r   NrL   rM   r   rl   r   r   r   r   :test_process_schema_order_properties_propagates_into_items  s,   	

r   c                 C   s   ddddiddiddgd}t |}ddddiddiddd	gd
gd}tj|| |d |r:||ks8J dS ||ks@J dS )z=The `order_properties` setting should apply to 'prefixItems'.re   rZ   r*   rT   r|   r   )r*   prefixItemsr}   r~   r   r   Nr   r   r   r   r   Atest_process_schema_order_properties_propagates_into_prefix_items  s0   	
r   c                 C   s   ddddiddiddddidd}t |}ddddiddidddgd	ddidd
dgd	}tj|| |d |rE||ksCJ dS ||ksKJ dS )z<The `order_properties` setting should apply to 'properties'.rZ   r*   rT   r|   r   )xyzabcr}   r~   r   r   r   r   Nr   r   r   r   r   ?test_process_schema_order_properties_propagates_into_properties  s6   	
r   c                 C   s   ddddiddiddd}t |}ddddiddidddgd	d}| jrCtj|| |d
 |r;||ks9J dS ||ksAJ dS tt}tj|| |d
 W d   n1 s[w   Y  dt|v shJ dS )zFThe `order_properties` setting should apply to 'additionalProperties'.rZ   r*   rT   r|   r   )r*   additionalPropertiesr}   r~   r   r   Nz8additionalProperties is not supported in the Gemini API.)	rL   rM   r!   r   rl   rw   rx   ry   r   )r   r   rQ   r   r   er   r   r   Jtest_process_schema_order_properties_propagates_into_additional_properties  s:   	

r   c                 C   s   ddddiddiddddigi}t |}ddddiddidddgd	ddigi}tj|| |d
 |r>||ks<J dS ||ksDJ dS )z7The `order_properties` setting should apply to 'anyOf'.rJ   rZ   r*   rT   r|   r   r}   r~   r   r   Nr   r   r   r   r   ;test_process_schema_order_properties_propagates_into_any_of5  s4   	
r   c                 C   s:   t  }g d}| |d< t| |}|j|ksJ dS )zLTests t_schema doesn't overwrite the property_ordering field if already set.)r	   r
   r   r\   N)r   model_json_schemarL   r   r,   r\   )r   rQ   custom_property_orderingr?   r   r   r   6test_t_schema_does_not_change_property_ordering_if_setb  s
   r   c                 C   s*   t  }t| |}|jg dksJ dS )zATests t_schema sets the property_ordering field for json schemas.)r   r   r   r   r   r   r   N)r   r   r   r,   r\   r   rQ   r?   r   r   r   4test_t_schema_sets_property_ordering_for_json_scheman  s   r   c                 C   sR   t jt jdddt jdddddd	d
gdd}t| |}|jd
dgks'J dS )zATests t_schema sets the property_ordering field for Schema types.rT   rF   rU   rV   r   rW   rX   rY   rZ   r   r   )r(   r*   r+   r)   r   N)r   r=   r   r,   r\   r   r   r   r   4test_t_schema_sets_property_ordering_for_schema_type  s"   r   )-__doc__rL   typingr   pydanticrw    r   r   r%   r   	BaseModelr   model_fieldsr;   r   r0   r   r:   r   r   r   fixturemarkparametrizer9   r<   rA   rC   rE   rS   r`   rc   rn   rt   rz   r   r   r   r   r   r   r   r   r   r   r   r   r   <module>   s   









B


*
%
)
,
,
)

