U
    4J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re  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   
DESCRIPTORZserialized_pbfactory_test1_fdr   factory_test2_fd)self r   Q/tmp/pip-unpacked-wheel-0oacnh65/google/protobuf/internal/message_factory_test.pysetUp2   s    zMessageFactoryTest.setUpc                 C   s<  | }d|_ d|_d|j_d|j_d|j_d|jj_d|j_|jj	
dddg |jj	d	 d|_d|_d|j_d|j_ d
|jj_ d|j_d|_|j	
d	ddg |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      ZoneZtwoZthreeZfour   zone deepz	zero deepZhelloZworldZtesting123)part_1part_2   )	mandatoryZnested_factory_2_enumZnested_factory_2_messagevalueZfactory_1_messageZfactory_1_enumZnested_factory_1_enumZnested_factory_1_messageZscalar_valueZ
list_valueextendappendZcircular_messageZgroupedaddr   r   ZloopSerializeToStringr   Factory2Messager   assertEqual)r   clsmsg
serializedZ	convertedZreserializedresultr   r   r   _ExerciseDynamicClass8   s@    



z(MessageFactoryTest._ExerciseDynamicClassc                 C   s~   t  }t|}|| j || j t }|	|
d}| |tjk | | |	|
d}| ||k d S )N/google.protobuf.python.internal.Factory2Message)r   DescriptorDatabaser   DescriptorPoolAddr   r   r	   MessageFactoryGetPrototypeFindMessageTypeByNameZassertFalser   r#   r)   
assertTrue)r   dbpoolfactoryr%   Z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   Z
descriptorr%   )MyMessageFactory	__class__r   r   r7   k   s    zXMessageFactoryTest.testCreatePrototypeOverride.<locals>.MyMessageFactory.CreatePrototype)__name__
__module____qualname__r7   __classcell__r   r9   )r:   r   r9   i   s   r9   r*   r8   )r	   r.   r   r+   r   r,   r-   r   r   r/   r0   r1   hasattr)r   r2   r3   r4   r%   r   r?   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rd| 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   )Z	full_name).0extr   r   r   	<genexpr>   s   z5MessageFactoryTest.testGetMessages.<locals>.<genexpr>r   Ztest1Ztest2i!0  cpp )rangeassertInr   namer   
dependencyr	   GetMessagesr1   setissubsetkeysr)   r   filer3   ZFindAllExtensions
ExtensionsZ_FindExtensionByNamer$   lenZ_FindExtensionByNumberr   TypeassertRaises	TypeError)r   _messagesZfactory_msg1Zmsg1ext1ext2r   r   r   testGetMessagesy   sx    






  

z"MessageFactoryTest.testGetMessagesc              	   C   s@  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 5 Q R X | t|jddg d S )Nr3   (google/protobuf/internal/container.protogoogle.protobuf.python.internalrI   package	ContainerrI   r   
   startend)google.protobuf.python.internal.Container(google/protobuf/internal/extension.protorI   r^   rJ   	ExtensionZextension_fieldr   rI   numberlabel	type_nameZextendee)google.protobuf.python.internal.Extensionz(google/protobuf/internal/duplicate.protoZ	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   r,   r	   r.   r   r   message_typer!   extension_ranger-   rK   rI   rH   	extensionFieldDescriptorProtoLABEL_OPTIONALrS   	Exceptionstr	exception)r   r3   r4   fmsgsr&   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   ra   rb   z)google/protobuf/internal/value_type.protoZ	ValueTypesettingr   )rI   rj   rk   typedefault_valuerf   rg   Ztop_level_extension_fieldr   ri   rh   Z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__	  s    zYMessageFactoryTest.testExtensionValueInDifferentFile.<locals>.SimpleDescriptorDB.__init__c                 S   s
   | j | S r   r~   )r   rI   r   r   r   FindFileByName  s    z_MessageFactoryTest.testExtensionValueInDifferentFile.<locals>.SimpleDescriptorDB.FindFileByNameN)r;   r<   r=   r   r   r   r   r   r   SimpleDescriptorDB  s   r   rZ   re   rm      iY  )r   r   rn   r!   ro   fieldrq   rr   Z
TYPE_INT32rI   rp   r   r,   r	   r.   rK   r   rO   Zextensions_by_namerP   rz   r"   r   r$   rQ   Z
ListFields)r   f1f2Zf3r   r2   r3   r4   rw   r&   descrW   rX   mr'   r   r   r   !testExtensionValueInDifferentFile   sv    







z4MessageFactoryTest.testExtensionValueInDifferentFileN)
r;   r<   r=   r   r)   r5   rA   rY   ry   r   r   r   r   r   r
   /   s   "37r
   __main__)__doc__
__author__ZunittestZgoogle.protobufr   Zgoogle.protobuf.internalr   r   r   r   r   r   r	   ZTestCaser
   r;   mainr   r   r   r   <module>   s    {