o
    YiM                     @   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   e/home/kim/smarthome/.venv/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!   Zapi_key)google_genai_client_moduleZ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_nameZfield_type_strr   r   r   -test_build_schema_for_list_of_pydantic_schema]   s   
r6   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   r4   r5   r   r   r   4test_build_schema_for_list_of_nested_pydantic_schemas   s   r9   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&   r.   r   transformed_schemaZschema_propertyr   r   r   !test_t_schema_for_pydantic_schema   s   

r=   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*   r2   r   r,   r   r:   itemsr&   r.   r;   r   r   r   )test_t_schema_for_list_of_pydantic_schema   s   
r?   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&   Znullable)r   r<   r   r   r   test_t_schema_for_null_fields   s   r@   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)r>   copydeepcopyr   Zhandle_null_fields)Ztest_properties_schemaschema_beforer   r   r   ,test_schema_with_no_null_fields_is_unchanged   s   	

rL   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STRINGrA   r(   r'   INTEGERr   
Populationr(   rF   r'   r   r   OBJECTr   r   r   r&   r(   r)   r'   property_ordering)r   r*   Z_api_clientr   r   r:   r   r<   expected_schemar   r   r   test_schema_with_default_value   s(   rX   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 )NrM   rA   rN   rO   )r(   NUMBERzRestaurants Per Capita)Zany_ofr'   )r   r   rS   r   r   r   rT   )r   r*   r   r   r:   rV   r   r   r   test_schema_with_any_of   s&   

rZ   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.rS   zFruit Basketz-A structured representation of a fruit basketZfruitARRAYz*An ordered list of the fruit in the basketzA piece of fruitZApplezDescribes an applerM   zAlways 'apple')r(   descriptionz$The color of the apple (e.g., 'red'))r(   colorr(   r]   )r'   r\   r(   r&   ZpropertyOrderingr)   ZOrangezDescribes an orangezAlways 'orange'z'The size of the orange (e.g., 'medium'))r(   sizer^   )r\   rE   )r(   r\   r>   )r(   r'   r\   r)   r&   N)r!   rG   rH   r   process_schema)r   Zdict_schemarK   r   r   r   0test_complex_dict_schema_with_anyof_is_unchanged   sZ   
9=r`   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'.rM   ZFOOr(   const)r(   enumNr   r_   )r   rJ   rW   r   r   r   *test_process_schema_converts_const_to_enum4  s   re   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.rO   {   ra   z".*Literal values must be strings.*)matchN)pytestraises
ValueErrorr   r_   )r   rJ   r   r   r   ,test_process_schema_forbids_non_string_constE  s   "rk   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/FooZFoorS   r(   rM   foobarr(   r&   )z$refz$defsrm   rn   r(   r&   rU   order_propertiesNrd   r   rr   rJ    schema_without_property_orderingschema_with_property_orderingr   r   r   9test_process_schema_order_properties_propagates_into_defsQ  s4   		rv   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'.r[   rS   r(   rM   rl   ro   )r(   r>   rm   rn   rp   rq   NrG   rH   r   r_   rs   r   r   r   :test_process_schema_order_properties_propagates_into_items  s,   	

rx   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'.r[   rS   r(   rM   rl   ro   )r(   ZprefixItemsrm   rn   rp   rq   Nrw   rs   r   r   r   Atest_process_schema_order_properties_propagates_into_prefix_items  s0   	
ry   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'.rS   r(   rM   rl   ro   )xyzabcrm   rn   rp   rz   r{   rq   Nrw   rs   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'.rS   r(   rM   rl   ro   )r(   ZadditionalPropertiesrm   rn   rp   rq   Nz8additionalProperties is not supported in the Gemini API.)	rG   rH   r!   r   r_   rh   ri   rj   r   )r   rr   rJ   rt   ru   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'.rE   rS   r(   rM   rl   ro   rm   rn   rp   rq   Nrw   rs   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   rU   N)r   model_json_schemarG   r   r*   rU   )r   rJ   Z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*   rU   r   rJ   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.rM   rA   rN   rO   r   rP   rQ   rR   rS   r   r   )r&   r(   r)   r'   r   N)r   r:   r   r*   rU   r   r   r   r   4test_t_schema_sets_property_ordering_for_schema_type  s"   r   )-__doc__rG   typingr   Zpydanticrh    r   r   r$   r   Z	BaseModelr   Zmodel_fieldsr8   r   r.   r   r7   r   r   r   ZfixturemarkZparametrizer6   r9   r=   r?   r@   rL   rX   rZ   r`   re   rk   rv   rx   ry   r|   r~   r   r   r   r   r   r   r   r   <module>   s   









B


*
%
)
,
,
)

