o
    ˷e2                     @   s   d Z dZ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
 dd
lmZ ddlmZ e	jG dd dejZedkrNe  dS dS )z*Tests for google.protobuf.message_factory.z"matthewtoia@google.com (Matt Toia)    N)descriptor_pb2)api_implementation)factory_test1_pb2)factory_test2_pb2)testing_refleaks)descriptor_database)descriptor_pool)message_factoryc                   @   sD   e Zd Zdd Zdd Zdd Zdd Zd	d
 Zdd Zdd Z	dS )MessageFactoryTestc                 C   s(   t jtjj| _t jtjj| _d S N)	r   FileDescriptorProto
FromStringr   
DESCRIPTORserialized_pbfactory_test1_fdr   factory_test2_fd)self r   d/var/www/ideatree/venv/lib/python3.10/site-packages/google/protobuf/internal/message_factory_test.pysetUp2   s   
zMessageFactoryTest.setUpc                 C   s8  | }d|_ d|_d|j_d|j_d|j_d|jj_d|j_|jj	
g d |jj	d d|_d|_d|j_d|j_ d|jj_ d	|j_d
|_|j	
g d |j	d |j  d|jd _d|jd _|jjddd d|jj_ d|jjjj_ | }tj|}| }| || ||}| || d S )N*   r   znested message value      )onetwothreefour   zone deepz	zero deep)r   r   r   r   helloworldtesting123)part_1part_2   )	mandatorynested_factory_2_enumnested_factory_2_messagevaluefactory_1_messagefactory_1_enumnested_factory_1_enumnested_factory_1_messagescalar_value
list_valueextendappendcircular_messagegroupedaddr"   r#   loopSerializeToStringr   Factory2Messager   assertEqual)r   clsmsg
serialized	convertedreserializedresultr   r   r   _ExerciseDynamicClass8   s@   



z(MessageFactoryTest._ExerciseDynamicClassc                 C   s~   t  }t|}|| j || j t }|	|
d}| |tju  | | |	|
d}| ||u  d S )N/google.protobuf.python.internal.Factory2Message)r   DescriptorDatabaser   DescriptorPoolAddr   r   r	   MessageFactoryGetPrototypeFindMessageTypeByNameassertFalser   r6   r>   
assertTrue)r   dbpoolfactoryr8   cls2r   r   r   testGetPrototypeZ   s   

z#MessageFactoryTest.testGetPrototypec                    sj   G  fdddt j t }t|}|| j || j   }|	|
d}| t|d d S )Nc                       s   e Zd Z fddZ  ZS )zHMessageFactoryTest.testCreatePrototypeOverride.<locals>.MyMessageFactoryc                    s   t  | |}d|_|S )Nz
Some value)superCreatePrototypeadditional_field)r   
descriptorr8   )MyMessageFactory	__class__r   r   rN   k   s   zXMessageFactoryTest.testCreatePrototypeOverride.<locals>.MyMessageFactory.CreatePrototype)__name__
__module____qualname__rN   __classcell__r   rQ   )rR   r   rQ   i   s    rQ   r?   rO   )r	   rC   r   r@   r   rA   rB   r   r   rD   rE   rG   hasattr)r   rH   rI   rJ   r8   r   rW   r   testCreatePrototypeOverrideh   s   
z.MessageFactoryTest.testCreatePrototypeOverridec              
   C   s  t dD ]}| | jj| jj t| j| jg}| t	ddg
t	|  | |d  |d }| t	ddg
t	dd |jjj|jD  |d  }|jd}|jd}| dt|j d	|j|< d
|j|< | d	|j|  | d
|j|  | d |jd | dt|j t dkr| t|jjd | t|jjd q| d |jd | d |jd qd S )Nr   r?   z/google.protobuf.python.internal.Factory1Messagez>google.protobuf.python.internal.Factory2Message.one_more_fieldz-google.protobuf.python.internal.another_fieldc                 s   s    | ]}|j V  qd S r   )	full_name).0extr   r   r   	<genexpr>   s
    
z5MessageFactoryTest.testGetMessages.<locals>.<genexpr>r   test1test2i!0  cpp )rangeassertInr   namer   
dependencyr	   GetMessagesrG   setissubsetkeysr>   r   filerI   FindAllExtensions
Extensions_FindExtensionByNamer7   len_FindExtensionByNumberr   TypeassertRaises	TypeError)r   _messagesfactory_msg1msg1ext1ext2r   r   r   testGetMessagesy   st   








z"MessageFactoryTest.testGetMessagesc                 C   sT  t  }tj|d}tjddd}|jjddjjddd	 |	| |
|jg}| d
| tjdddgd}|jjdd}|jjddtjjddd |	| |
|jg}| d| tjdddgd}|jjdd}|jjddtjjddd |	| | t}|
|jg W d    n1 sw   Y  | t|jddg d S )NrI   (google/protobuf/internal/container.protogoogle.protobuf.python.internalrd   package	Containerrd   r   
   startend)google.protobuf.python.internal.Container(google/protobuf/internal/extension.protord   r~   re   	Extensionextension_fieldr   rd   numberlabel	type_nameextendee)google.protobuf.python.internal.Extensionz(google/protobuf/internal/duplicate.proto	DuplicatezExtensions "google.protobuf.python.internal.Duplicate.extension_field" and "google.protobuf.python.internal.Extension.extension_field" both try to extend message type "google.protobuf.python.internal.Container" with field number 2.z!Double registration of Extensions)r   rA   r	   rC   r   r   message_typer3   extension_rangerB   rf   rd   rc   	extensionFieldDescriptorProtoLABEL_OPTIONALrq   	Exceptionstr	exception)r   rI   rJ   fmsgsr9   cmr   r   r   testDuplicateExtensionNumber   s`   


z/MessageFactoryTest.testDuplicateExtensionNumberc                 C   s  t jddd}|jjddjjddd t jd	dd}|jjd
djjddt jjt jjdd t jdd|j	|j	gd}|j
jddt jjd
dd |jjddj
jddt jjd
dd G dd d}||j	||j	||j	|i}t|}tj|d}||j	|j	g}|d }	|d j}
|
jjd }|
jd }|	 }d|j| _d|j| _| }t|}tj|d}||j	|j	g}|d }	|d j}
|
jjd }|
jd }|	|}| dt|  | d|j| j | d|j| j d S )Nr{   r|   r}   r   r   r   r   r   z)google/protobuf/internal/value_type.proto	ValueTypesettingr!   )rd   r   r   typedefault_valuer   r   top_level_extension_fieldr   r   r   nested_extension_field   c                   @   s   e Zd Zdd Zdd ZdS )zPMessageFactoryTest.testExtensionValueInDifferentFile.<locals>.SimpleDescriptorDBc                 S   s
   || _ d S r   _files)r   filesr   r   r   __init__	     
zYMessageFactoryTest.testExtensionValueInDifferentFile.<locals>.SimpleDescriptorDB.__init__c                 S   s
   | j | S r   r   )r   rd   r   r   r   FindFileByName  r   z_MessageFactoryTest.testExtensionValueInDifferentFile.<locals>.SimpleDescriptorDB.FindFileByNameN)rS   rT   rU   r   r   r   r   r   r   SimpleDescriptorDB  s    r   rz   r   r      iY  )r   r   r   r3   r   fieldr   r   
TYPE_INT32rd   r   r   rA   r	   rC   rf   r   rj   extensions_by_namerl   r   r5   r   r7   rn   
ListFields)r   f1f2f3r   rH   rI   rJ   r   r9   descrw   rx   mr:   r   r   r   !testExtensionValueInDifferentFile   sv   







z4MessageFactoryTest.testExtensionValueInDifferentFileN)
rS   rT   rU   r   r>   rL   rY   ry   r   r   r   r   r   r   r
   /   s    "37r
   __main__)__doc__
__author__unittestgoogle.protobufr   google.protobuf.internalr   r   r   r   r   r   r	   TestCaser
   rS   mainr   r   r   r   <module>   s"    {