
    q[iM                        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	ej                        Zej                  Z G d
 dej                        Zej                  Z G d dej                        Zej                  Z G d dej                        Z G d dej                        Z G d dej                        Zej,                  d        Zej.                  j1                  dddg      d        Zej.                  j1                  dddg      d        Zej.                  j1                  dddg      d        Zej.                  j1                  dddg      d        Zej.                  j1                  dddg      d        Zd Zej.                  j1                  dddg      d        Zej.                  j1                  dddg      d        Z ej.                  j1                  dddg      d         Z!ej.                  j1                  dddg      d!        Z"ej.                  j1                  dddg      d"        Z#ej.                  j1                  d#g d$      d%        Z$ej.                  j1                  d#g d$      d&        Z%ej.                  j1                  d#g d$      d'        Z&ej.                  j1                  d#g d$      d(        Z'ej.                  j1                  d#g d$      d)        Z(ej.                  j1                  d#g d$      d*        Z)ej.                  j1                  dddg      d+        Z*ej.                  j1                  dddg      d,        Z+ej.                  j1                  dddg      d-        Z,y).z<Tests schema processing methods in the _transformers module.    N)Union   )_transformers)client)typesc                   ,    e Zd ZU eed<   eed<   eed<   y)CurrencyInfonamecodesymbolN)__name__
__module____qualname__str__annotations__     d/mnt/data/GridWatch/venv/lib/python3.12/site-packages/google/genai/tests/transformers/test_schema.pyr	   r	      s    ))
+r   r	   c                   T    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<   y)	CountryInfor
   
populationcapital	continentgdpofficial_languagetotal_area_sq_miN)r   r   r   r   r   intr   r   r   r   r   '   s'    )/,.
(r   r   c                   ^    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<   y	)
CountryInfoWithCurrencyr
   r   r   r   r   r   r   currencyN)r   r   r   r   r   r   r	   r   r   r   r   r   4   s-    )/,.
(r   r   c                   0    e Zd ZU eed<   dZeedf   ed<   y)CountryInfoWithNullFieldsr
   Nr   )r   r   r   r   r   r   r   r   r   r   r   r"   r"   B   s    )!%*eCI%r   r"   c                   &    e Zd ZU eed<   dZeed<   y)CountryInfoWithDefaultValuer
   r   r   N)r   r   r   r   r   r   r   r   r   r   r$   r$   G   s    )*cr   r$   c                   ,    e Zd ZU eed<   eeef   ed<   y)CountryInfoWithAnyOfr
   restaurants_per_capitaN)r   r   r   r   r   r   r   floatr   r   r   r&   r&   L   s    )U
++r   r&   c              #   x   K   | rt        j                  | dd       y t        j                  | d       y w)Nztest-projectztest-location)vertexaiprojectlocationztest-api-key)r*   api_key)google_genai_client_moduleClient)
use_vertexs    r   r   r   Q   s=     
$
+
+^o  %
+
+^ s   8:r0   TFc                    t        j                  | t              j                         }t	        |t
              sJ |d   D ]  }d|d   |   v sJ d|d   |   v sJ |t        v sJ t        |   j                  j                  }|d   |   d   j                         j                  |j                               sJ d|v sJ |d   t        t        j                               k(  rJ  y)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_strs       r   -test_build_schema_for_list_of_pydantic_schemarC   ]   s    &&v{;FFH+	K	&&	&- Gjk,/
;;;;[.z::::,,,,(4??HHNL!*-f5		N((*	+	,	, $$$z"d+>+C+C+E&FFFFGr   c                    t        j                  | t              j                         }t	        |t
              sJ |d   D ]$  }d|d   |   v sJ d|d   |   v sJ |t        v r$J  d|d   d   v sJ |d   d   d   D ]  }|t        v rJ  y)zTests _build_schema() when list[pydantic.BaseModel] is provided to response_schema and the pydantic.BaseModel has nested pydantic fields.r2   r3   r4   r    N)r   r6   r   r7   r8   r9   nested_country_info_fieldscurrency_info_fields)r   r@   rA   s      r   4test_build_schema_for_list_of_nested_pydantic_schemarG   s   s     &&%JL  
K	&&	&- 4jk,/
;;;;[.z::::33334 
\2:>	>>	>-j9,G .j----.r   c                     t        j                  | t              }t        |t        j
                        sJ |j                  D ]5  }|t        v sJ t        |j                  |   t        j
                        r5J  y)zDTests t_schema when pydantic.BaseModel is passed to response_schema.N)r   r6   r   r8   r   Schemar2   r:   r   transformed_schemaschema_propertys      r   !test_t_schema_for_pydantic_schemarM      sv     %--fkB	&	55	5+66 o1111%%o6  r   c                 z   t        j                  | 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                        r?J  y)zJTests t_schema when list[pydantic.BaseModel] is passed to response_schema.N)
r   r6   r>   r   r8   r   rI   itemsr2   r:   rJ   s      r   )test_t_schema_for_list_of_pydantic_schemarP      s     %--fd;6GH	&	55	5	&,,ell	;;	;+11<< o1111  ++O<ell  r   c                     t        j                  | t              }t        |t        j
                        sJ |j                  d   j                  sJ y)z,Tests t_schema when null fields are present.r   N)r   r6   r"   r8   r   rI   r2   nullable)r   rK   s     r   test_t_schema_for_null_fieldsrS      sG     %--f6OP	&	55	5		&	&|	4	=	==	=r   c                      dddddiddigddd	d
} | j                         D ]6  \  }}t        j                  |      }t        j                  |       ||k(  r6J  y)zSTests handle_null_fields() doesn't change anything when no null fields are present.Namestring)r3   r4   r4   integerr(   nullzTotal Area Sq Mi)anyOfdefaultr3   )r
   r   N)rO   copydeepcopyr   handle_null_fields)test_properties_schemaschema_befores       r   ,test_schema_with_no_null_fields_is_unchangedrb      sx     1Y'&'):;%/ #((* #iaMM&)M$$V,F"""#r   c                     t        j                  | j                  t              }t	        j
                  t	        j
                  dd      t	        j
                  ddd      dd	d
gdd
dg      }||k(  sJ y )NSTRINGrU   r4   r3   INTEGERr   
Populationr4   rZ   r3   r
   r   OBJECTr
   r$   r   r2   r4   r5   r3   property_ordering)r   r6   _api_clientr$   r   rI   r   rK   expected_schemas      r   test_schema_with_default_valuerp      s     %--5 LL,,  
 x)./$ 
	..	.r   c           	      0   t        j                  | t              }t        j                  t        j                  dd      t        j                  t        j                  d      t        j                  d      gd      d	d
ddgdddg      }||k(  sJ y )Nrd   rU   re   rf   )r4   NUMBERzRestaurants Per Capita)any_ofr3   )r
   r'   rj   r
   r'   r&   rk   )r   r6   r&   r   rI   rn   s      r   test_schema_with_any_ofrt      s    $--f6JKLL,, %*LL,,I.,,H- -% 01"!9:#/( 
	..	.r   c                     | j                   rod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        j                  |      }t        j                  ||        ||k(  sJ yy)z]When a dict schema is passed to process_schema, the only change should be camel-casing anyOf.rj   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 applerd   zAlways 'apple')r4   descriptionz$The color of the apple (e.g., 'red'))r4   colorr4   rz   )r3   ry   r4   r2   propertyOrderingr5   OrangezDescribes an orangezAlways 'orange'z'The size of the orange (e.g., 'medium'))r4   sizer}   )ry   rY   )r4   ry   rO   )r4   r3   ry   r5   r2   N)r*   r[   r\   r   process_schema)r   dict_schemara   s      r   0test_complex_dict_schema_with_anyof_is_unchangedr      s     __FIK#5 &-+?$, -53C)"
 -5(N*"+ 280A)/(9#( &.+@$, -53D)"
 -5)5)"+ 280@)/(8%)(+/1
7Kr MM+.M  f5K'''{ r   c                 T    ddd}ddgd}t        j                  ||        ||k(  sJ y)z<The 'const' field should be converted to a singleton 'enum'.rd   FOOr4   const)r4   enumNr   r~   )r   r`   ro   s      r   *test_process_schema_converts_const_to_enumr   4  sB     &
 g/
 vv.	?	""	"r   c                     ddd}t        j                  t        d      5  t        j                  ||        ddd       y# 1 sw Y   yxY w)z)The 'const' field only works for strings.rf   {   r   z".*Literal values must be strings.*)matchN)pytestraises
ValueErrorr   r~   )r   r`   s     r   ,test_process_schema_forbids_non_string_constr   E  sG     &
 }}Z'KL 1  01 1 1s   AA
zuse_vertex,order_properties))FF)FT)TF)TTc                     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                  || |       |r||k(  sJ y||k(  sJ y)z7The `order_properties` setting should apply to '$defs'.z#/$defs/FooFoorj   r4   rd   foobarr4   r2   )z$refz$defsr   r   r4   r2   rl   order_propertiesNr   r   r   r`    schema_without_property_orderingschema_with_property_orderings        r   9test_process_schema_order_properties_propagates_into_defsr   Q  s     
 (+ (+& (#(#&" (#(# "5># f'7 22225555r   c                     ddddiddiddd}t        j                  |      }ddddiddiddd	gd
d}t        j                  || |       |r||k(  sJ y||k(  sJ y)z7The `order_properties` setting should apply to 'items'.rw   rj   r4   rd   r   r   )r4   rO   r   r   r   r   Nr[   r\   r   r~   r   s        r   :test_process_schema_order_properties_propagates_into_itemsr     s     h'h'	& &*]]6%:"h'h' !&u~
# f'7 22225555r   c                     ddddiddiddgd}t        j                  |      }ddddiddiddd	gd
gd}t        j                  || |       |r||k(  sJ y||k(  sJ y)z=The `order_properties` setting should apply to 'prefixItems'.rw   rj   r4   rd   r   r   )r4   prefixItemsr   r   r   r   Nr   r   s        r   Atest_process_schema_order_properties_propagates_into_prefix_itemsr     s       (+ (+& &*]]6%:"  (+ (+ %*5>	# f'7 22225555r   c                     ddddiddiddddidd}t        j                  |      }ddddiddidddgd	ddidd
dgd	}t        j                  || |       |r||k(  sJ y||k(  sJ y)z<The `order_properties` setting should apply to 'properties'.rj   r4   rd   r   r   )xyzabcr   r   r   r   r   r   Nr   r   s        r   ?test_process_schema_order_properties_propagates_into_propertiesr     s       (+ (+ (#	& &*]]6%:"  (+ (+ %*5> (#
 "5>#  f'7 22225555r   c                    ddddiddiddd}t        j                  |      }ddddiddidddgd	d}| j                  r*t        j                  || |
       |r||k(  s
J ||k(  sJ yyt        j                  t              5 }t        j                  || |
       ddd       dt              v sJ y# 1 sw Y   xY w)zFThe `order_properties` setting should apply to 'additionalProperties'.rj   r4   rd   r   r   )r4   additionalPropertiesr   r   r   r   Nz8additionalProperties is not supported in the Gemini API.)	r[   r\   r*   r   r~   r   r   r   r   )r   r   r`   r   r   es         r   Jtest_process_schema_order_properties_propagates_into_additional_propertiesr     s    h'h'	& &*]]6%:"h'h' !&u~
# __  )9 44447777 5 
z	" a""
&+; FQOOO	 s   B77C c                     ddddiddiddddigi}t        j                  |      }ddddiddidddgd	ddigi}t        j                  || |
       |r||k(  sJ y||k(  sJ y)z7The `order_properties` setting should apply to 'anyOf'.rY   rj   r4   rd   r   r   r   r   r   r   Nr   r   s        r   ;test_process_schema_order_properties_propagates_into_any_ofr   5  s      (+ (+ 8
	& &*]]6%:" (+ (+ %*5> 8

# f'7 22225555r   c                     t         j                         }g d}|j                         |d<   t        j                  | |      }|j
                  |k(  sJ y)zLTests t_schema doesn't overwrite the property_ordering field if already set.)r   r   r
   rl   N)r   model_json_schemar[   r   r6   rl   )r   r`   custom_property_orderingrK   s       r   6test_t_schema_does_not_change_property_ordering_if_setr   b  sT     ((*&7 8 = = ?&	$--ff=		-	-1I	II	Ir   c                 ~    t         j                         }t        j                  | |      }|j                  g dk(  sJ y)zATests t_schema sets the property_ordering field for json schemas.)r
   r   r   r   r   r   r   N)r   r   r   r6   rl   r   r`   rK   s      r   4test_t_schema_sets_property_ordering_for_json_schemar   n  sB     ((*&$--ff=		-	- 2 
  
r   c                     t        j                  t        j                  dd      t        j                  ddd      dd	d
gd      }t        j                  | |      }|j                  d
dgk(  sJ y)zATests t_schema sets the property_ordering field for Schema types.rd   rU   re   rf   r   rg   rh   ri   rj   r
   r$   )r2   r4   r5   r3   r   N)r   rI   r   r6   rl   r   s      r   4test_t_schema_sets_property_ordering_for_schema_typer     s{     <<,,  
 x)&" %--ff=		-	-&,1G	GG	Gr   )-__doc__r[   typingr   pydanticr    r   r   r.   r   	BaseModelr	   model_fieldsrF   r   r:   r   rE   r"   r$   r&   fixturemarkparametrizerC   rG   rM   rP   rS   rb   rp   rt   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   <module>r      s  " C      4 8%%  $00 ($$  ".. h00  5AA & 2 2 &
("4"4 
,8-- ,
   e}5G 6G* e}5. 6.( e}5 6 e}5
 6
 e}5> 6>#" e}5/ 6/4 e}5/ 6/2 e}5?( 6?(D e}5# 6#  e}51 61 !@'6	'6T !@"6	"6J !@&6	&6R !@)6	)6X !@)P	)PX !@&6	&6R e}5J 6J e}5 6" e}5H 6Hr   