3 SLj @s$dZdZddlZy ddlZWnek r8ddlZYnXddlmZddlmZddlm Z ddlm Z ddl m Z dd l m Z dd lmZdd lmZdd lmZdd lmZdZGdddejZGdddeZGdddejZGdddejZGdddejZedkr ejdS)z1Unittest for google.protobuf.internal.descriptor.z#robinson@google.com (Will Robinson)N)unittest_custom_options_pb2)unittest_import_pb2) unittest_pb2)descriptor_pb2)api_implementation) test_util) descriptor)descriptor_pool)symbol_database) text_formatz name: 'TestEmptyMessage' c@seZdZddZddZddZddZd d Zd d Zd dZ ddZ ddZ ddZ ddZ ddZddZddZejejdkpejdkdd d!Zd"S)#DescriptorTestcCstjddd}|jjdd}|jjddtjjtjjd|jjd d}|j jd d d |j jd dd |j jddd |jjdd|j jdd}|j jdddd}|j |_ |j j||j j|j|_|jj|j|_|jj|j|_|jj|j|_|jj|j|_dS)Nzsome/filename/some.protoprotobuf_unittest)namepackage NestedMessage)rZbb)rnumbertypelabel ForeignEnum FOREIGN_FOO)rrZ FOREIGN_BARZ FOREIGN_BAZZResponseMessageZServiceZ CallMethodz .protobuf_unittest.NestedMessagez".protobuf_unittest.ResponseMessage)rZ input_typeZ output_type)rFileDescriptorProto message_typeaddfieldZFieldDescriptorProtoZ TYPE_INT32ZLABEL_OPTIONAL enum_typevalueZservicemethodGetDescriptorPoolpoolZAddZFindFileByNamermy_filemessage_types_by_name my_messageenum_types_by_namemy_enumservices_by_name my_servicemethods_by_name my_method)self file_protoZ message_protoZ enum_protoZ service_protoZ method_protor.%/usr/lib/python3.6/descriptor_test.pysetUp>s:   zDescriptorTest.setUpcCs tjjS)N)r Defaultr")r,r.r.r/r!bsz DescriptorTest.GetDescriptorPoolcCs|j|jjddd|j|jjdjdj|jjdd|jt|jjddWdQRX|jt|jjddWdQRX|jt|jjWdQRXdS)NrrriZNoneEnum) assertEqualr%Z EnumValueNamer&Zvalues_by_numberr assertRaisesKeyError TypeError)r,r.r.r/testEnumValueNamees    z DescriptorTest.testEnumValueNamecCs|j|j|jjdjdS)Nr)r2r'valuesr)r,r.r.r/testEnumFixupstszDescriptorTest.testEnumFixupscCs.|j|j|jjdj|j|j|jjdS)Nr)r2r%fieldscontaining_typer')r,r.r.r/testContainingTypeFixupswsz'DescriptorTest.testContainingTypeFixupscCs|j|j|jjdS)N)r2r)r+Zcontaining_service)r,r.r.r/testContainingServiceFixups{sz*DescriptorTest.testContainingServiceFixupscCs|j|jjtj|j|jjdjtj|j|jjtj|j|jj djtj |j|j jtj |j|j jtjdS)Nr)r2r' GetOptionsrZ EnumOptionsr7ZEnumValueOptionsr%ZMessageOptionsr9Z FieldOptionsr+Z MethodOptionsr)ZServiceOptions)r,r.r.r/testGetOptions~s         zDescriptorTest.testGetOptionscCstj}tjj}|jd}|jd}|jd}|jd}|jd}tjj}|jd} |j } tj } |j d| j | |j } tj } |j d| j | |j }tj}|j d |j |tj}|j d |j ||j }tj}|j d|j ||j }tj}|j d|j ||j }tj}|j d |j ||j }tj}|j d|j || j }tj}|j tj|j |tjj}|j|j|j|j|j|j|j|j|j|j|j|j|j|jdS)NZfield1ZAnOneofZAnEnumZ ANENUM_VAL2Z ANENUM_VAL1Fool` 8l-a*ci{iiil` )r DESCRIPTORZTestMessageWithCustomOptionsfields_by_nameoneofs_by_namer&enum_values_by_nameZTestServiceWithCustomOptionsFindMethodByNamer= file_opt1r2 Extensions message_opt1 field_opt1 field_opt2 oneof_opt1 enum_opt1enum_value_opt1 service_opt1 method_opt1ZMETHODOPT1_VAL2DummyMessageContainingEnumZ assertTrue has_options assertFalse)r,file_descriptormessage_descriptorfield_descriptoroneof_descriptorenum_descriptorenum_value_descriptorZother_enum_value_descriptorservice_descriptormethod_descriptor file_optionsrImessage_optionsrKZ field_optionsrLrMZ oneof_optionsrNZ enum_optionsrOZenum_value_optionsrPZservice_optionsrQZmethod_optionsrRr.r.r/testSimpleCustomOptionss\           z&DescriptorTest.testSimpleCustomOptionsc Csd}d}d}d}d}d}tjj}|j}|jd|jtj|j||jtj|j||jtj|jd|jtj |jd|jtj |j||jtj |j||jtj |jd|jtj |jd|jtj|j||jtj|j||jtjtjj}|j}|jd |jtj|j||jtj|j||jtj|j||jtj |j||jtj |j||jtj |j||jtj |j||jtj |j||jtj|j||jtj|j||jtjtjj}|j}|jd|jtj|jd |jtjd |jd |jtj|jd|jtj|jd|jtjtj} |j| j|jtjtjj}|j}|jd|jtjd |jd|jtjtjj}|j}|jd|jtjd |jd |jtjdS)!N?r @FrTdg$(@rgYB?zHello, "World"s HelloWorld lilllilllllliiif)rZCustomOptionMinIntegerValuesrDr=r2rJZbool_optZ int32_optZ int64_optZ uint32_optZ uint64_optZ sint32_optZ sint64_optZ fixed32_optZ fixed64_optZ sfixed32_optZ sfixed64_optZCustomOptionMaxIntegerValuesZCustomOptionOtherValuesZassertAlmostEqualZ float_optZ double_optZ string_optZ bytes_optrSZTEST_OPTION_ENUM_TYPE2Zenum_optZSettingRealsFromPositiveIntsZSettingRealsFromNegativeInts) r,Z kint32minZ kint64minZ kint32maxZ kint64maxZ kuint32maxZ kuint64maxrWr_Z dummy_enumr.r.r/testDifferentCustomOptionTypess                                                             z-DescriptorTest.testDifferentCustomOptionTypescCstjj}|j}|jd|jtjj|jd|jtjjtj|jd|jtjjtj j |jd|jtj j |jd|jtj jtj |jd|jtj jj|jd|jtj jjtj|jd|jtj jjtj j |jd |jtj jtjj|jd |jtj jtjjtj|jd |jtj jtjjtj j |jd |jtjjjj|jd |jtj jj|jd|jtjj |jd|jtjjj|jd|jtjjdS)NrAiDiliiiiiiiiIiiA )rZVariousComplexOptionsrDr=r2rJZ complex_opt1ZfooZquuxZcorgeZquxZ complex_opt2ZbazZgraultZbarZgarplyZComplexOptionType2ZComplexOptionType4Z complex_opt4ZwaldoZfredZ complex_opt3Zcomplexoptiontype5ZplughZ complexopt6Zxyzzy)r,roptionsr.r.r/testComplexExtensionOptions sX                             z*DescriptorTest.testComplexExtensionOptionsc CsJtj}tjj}|jd}tjj}|jd}tjj}|jd}|jj tj }|j d|j |j d|j |j d|jj |j d|jj tj j |j d|jj tjjj |j d |jj tjj |j d |jj tjj |j d |jj tjj |j d |jj tjj |j d |jj tjj |j d|jj tjj dS)NZ fieldnameZVALUEZMethodrfZFileAnnotationZNestedFileAnnotationZFileExtensionAnnotationZEmbeddedMessageSetElementZMessageAnnotationZFieldAnnotationZEnumAnnotationZEnumValueAnnotationZServiceAnnotationZMethodAnnotation)rrDAggregateMessagerEZ AggregateEnumvalues_by_nameZAggregateServicerHr=rJZfileoptr2issubfileZmsetZAggregateMessageSetElementZmessage_set_extensionmsgoptZfieldoptZenumoptZ enumvaloptZ serviceoptZ methodopt) r,rVrWrXrZr[r\r]r^r.r.r/testAggregateOptionsRsP           z#DescriptorTest.testAggregateOptionscCstjjj}|jd|jjtj|jd}|jd|jjtj tjj}|j d}|jd|jjtj |j d}|jd|jjtj |jd}|jd |jjtjdS) Ni nested_fieldi NestedEnumiZNESTED_ENUM_VALUEinested_extensioni)rZNestedOptionTyperrDr2r=rJrKrErLr&rOrGrPextensions_by_namerM)r,Znested_messagerwZ outer_messageZ nested_enumZnested_enum_valueryr.r.r/testNestedOptionss          z DescriptorTest.testNestedOptionscCs(|j|jj|j|j|jj|jdS)N)r2r'rtr#r%)r,r.r.r/testFileDescriptorReferencessz+DescriptorTest.testFileDescriptorReferencescCs|j|jjd|j|jjd|j|jj|j|j|jj|jd|jjtj }|jj ||j|jj |j |jt jjtjdS)Nzsome/filename/some.protor Zproto2)r2r#rrr"rUrTZsyntaxrr CopyToProtoZ serialized_pbZSerializeToStringrrDr r1)r,r-r.r.r/testFileDescriptors   z!DescriptorTest.testFileDescriptorcpprazAImmutability of descriptors is only enforced in v2 implementationcCCsHtj}tjj}|jd}|jd}|jd}|jt d|_WdQRX|jtd|jd<WdQRX|jt|j j dWdQRX|jt ||_ WdQRX|jt d|_ WdQRX|jt d|_ WdQRX|jt d|_ WdQRX|jt d|_ WdQRX|jt }d|_ WdQRX|j dt|jdS)Noptional_int32rx oneof_fieldZAnotherFTz&attribute is not writable: has_options)rrD TestAllTypesrEr&rFr3AttributeErrorr5r9appendr:rTr2strZ exception)r,rVrWrXrZrYer.r.r/testImmutableCppDescriptors2            z)DescriptorTest.testImmutableCppDescriptorN)__name__ __module__ __qualname__r0r!r6r8r;r<r>r`rirnrvr{r|r~unittestskipIfrTypeZVersionrr.r.r.r/r <s"$6^21 r c@seZdZdZddZdS)NewDescriptorTestzARedo the same tests as above, but with a separate DescriptorPool.cCstjS)N)r ZDescriptorPool)r,r.r.r/r!sz#NewDescriptorTest.GetDescriptorPoolN)rrr__doc__r!r.r.r.r/rsrc@sXeZdZdZddZddZddZdd Zd d Zd d Z ddZ ddZ ddZ dS)GeneratedDescriptorTestz:Tests for the properties of descriptors in generated code.cCs|j|jd|j|jd|j|||j|jdj||j||g|j||di|j|j|j|j|j|j |j|j |j|j |j|j |j|j |j|jdj|j|jgdS)Nrzprotobuf_unittest.TestAllTypesr)r2r full_namer9r:assertInCheckDescriptorSequenceCheckDescriptorMappingrEZfields_by_numberfields_by_camelcase_namer&rGrF enum_typesrpextension_ranges)r,rWr.r.r/CheckMessageDescriptors"        z.GeneratedDescriptorTest.CheckMessageDescriptorcCs|j|jd|j|jd|j|jd|j|jjd|j|jtj|j|||j|jjd||j|jj d||j ||g|j ||di|jd|j |jd|j t jdkr|j|j|jdS)Nr optionalInt32z-protobuf_unittest.TestAllTypes.optional_int32rr)r2rcamelcase_namerr:rtrrDrErrZextension_scoperrrid)r,rXr.r.r/CheckFieldDescriptors(   z,GeneratedDescriptorTest.CheckFieldDescriptorcCs|j|tjjj|j|g|j|d|j|dk|j||t|}|j|||jt |d|jt |t ||j|t |d|d|d}|j||d|j |||j|j |d|j|j |dtj jjd}|j|||j|j |d|jt|j ||jt|j gt|}|jt|t|ddd|jtt|d|d<|j||tjdkr|jtt|t|n|jt|dddS)Nrrz change valuepython.CheckItemsz change valuenewZnew_keyZ key_errorr)r)r)rrrrDrErUdictitemsr2rrsys version_inforiterrgetr3r5rrkeysr7ZiterkeysZ itervaluesZ iteritemsr4 __getitem__r)r,mappingZ excepted_dictkeyrrr.)r,r/r$sJ             &z.GeneratedDescriptorTest.CheckDescriptorMappingcCstjj}|j||jd}|j||jd}|j|tjjd}|jd|j |jtj jj d g|jtj jj d d dgdS)Nrrrr rA+/)rr)rAr)rr)rr) rrrDrrErrr&r2r:rrTestMultipleExtensionRanges)r,rWrXrZr.r.r/testDescriptorUs     z&GeneratedDescriptorTest.testDescriptorcCsVtj}|j|j|j|j|j|j|j|j|j|j|jtj jjdS)N) rrDrZ dependenciesrr$r&r(rzZTestNestedExtension)r,Zcontaining_filer.r.r/testCppDescriptorContainerhs     z2GeneratedDescriptorTest.testCppDescriptorContainercCs0tjjjd}t|j}~|jdt|jdS)NrxFOO) rrrDr&rr7r2rr)r,enumZ values_iterr.r.r/#testCppDescriptorContainer_Iteratorrs z;GeneratedDescriptorTest.testCppDescriptorContainer_IteratorcCs\tjjd}|j|jd|j|jdjd|j|jtj|j|jd|j |j dS)N TestServicerr?) rrDr(r2rmethodsZassertIsrtrrr*)r,r\r.r.r/testServiceDescriptorys  z-GeneratedDescriptorTest.testServiceDescriptorcCsZtjj}|jd}|j|j|j||j|jd|j|jd|j |jd|j dS)Nrz*protobuf_unittest.TestAllTypes.oneof_fieldr) rrrDrFrUrTr2r:rrr)r,rWrYr.r.r/testOneofDescriptors  z+GeneratedDescriptorTest.testOneofDescriptorN) rrrrrrrrrrrrrr.r.r.r/rs#1 rc@seZdZdZddZddZddZdd Zd d Zd d Z ddZ ddZ ddZ ddZ ddZejejdkdddZejejdkdddZdS) DescriptorCopyToProtoTestz)Tests for CopyTo functions of Descriptor.cCs4|}tj|||j||dt|t|fdS)Nz#Not equal, Actual: %s Expected: %s )r ZMerger2r)r,Z actual_protoZexpected_classexpected_asciiZexpected_protor.r.r/_AssertProtoEquals  z+DescriptorCopyToProtoTest._AssertProtoEqualcCs"|}|j||j|||dS)N)r}r)r,ZdescZexpected_proto_classZexpected_proto_asciiactualr.r.r/_InternalTestCopyToProtos z2DescriptorCopyToProtoTest._InternalTestCopyToProtocCs|jtjjtjtdS)N)rrTestEmptyMessagerDrDescriptorProto#TEST_EMPTY_MESSAGE_DESCRIPTOR_ASCII)r,r.r.r/testCopyToProto_EmptyMessagesz6DescriptorCopyToProtoTest.testCopyToProto_EmptyMessagecCsd}|jtjjjtj|dS)Nz name: 'NestedMessage' field: < name: 'bb' number: 1 label: 1 # Optional type: 5 # TYPE_INT32 > )rrrrrDrr)r,ZTEST_NESTED_MESSAGE_ASCIIr.r.r/testCopyToProto_NestedMessages z7DescriptorCopyToProtoTest.testCopyToProto_NestedMessagecCsd}|jtjjtj|dS)Nz name: 'TestForeignNested' field: < name: 'foreign_nested' number: 1 label: 1 # Optional type: 11 # TYPE_MESSAGE type_name: '.protobuf_unittest.TestAllTypes.NestedMessage' > )rrZTestForeignNestedrDrr)r,ZTEST_FOREIGN_NESTED_ASCIIr.r.r/$testCopyToProto_ForeignNestedMessages z>DescriptorCopyToProtoTest.testCopyToProto_ForeignNestedMessagecCsd}|jtjjtj|dS)Nz name: 'ForeignEnum' value: < name: 'FOREIGN_FOO' number: 4 > value: < name: 'FOREIGN_BAR' number: 5 > value: < name: 'FOREIGN_BAZ' number: 6 > )rrrrDrZEnumDescriptorProto)r,ZTEST_FOREIGN_ENUM_ASCIIr.r.r/testCopyToProto_ForeignEnums z5DescriptorCopyToProtoTest.testCopyToProto_ForeignEnumcCsd}|jtjjtj|dS)Nz name: 'TestDeprecatedFields' field: < name: 'deprecated_int32' number: 1 label: 1 # Optional type: 5 # TYPE_INT32 options: < deprecated: true > > )rrZTestDeprecatedFieldsrDrr)r,ZTEST_DEPRECATED_FIELDS_ASCIIr.r.r/testCopyToProto_Optionss z1DescriptorCopyToProtoTest.testCopyToProto_OptionscCsd}|jtjjtj|dS)Nz} name: 'TestEmptyMessageWithExtensions' extension_range: < start: 1 end: 536870912 > )rrZTestEmptyMessageWithExtensionsrDrr)r,Z(TEST_EMPTY_MESSAGE_WITH_EXTENSIONS_ASCIIr.r.r/testCopyToProto_AllExtensionss z7DescriptorCopyToProtoTest.testCopyToProto_AllExtensionscCsd}|jtjjtj|dS)Na name: 'TestMultipleExtensionRanges' extension_range: < start: 42 end: 43 > extension_range: < start: 4143 end: 4244 > extension_range: < start: 65536 end: 536870912 > )rrrrDrr)r,Z*TEST_MESSAGE_WITH_SEVERAL_EXTENSIONS_ASCIIr.r.r/!testCopyToProto_SeveralExtensionss z;DescriptorCopyToProtoTest.testCopyToProto_SeveralExtensionscCsd}|jtjtj|dS)Nad name: 'google/protobuf/unittest_import.proto' package: 'protobuf_unittest_import' dependency: 'google/protobuf/unittest_import_public.proto' message_type: < name: 'ImportMessage' field: < name: 'd' number: 1 label: 1 # Optional type: 5 # TYPE_INT32 > > aenum_type: < name: 'ImportEnum' value: < name: 'IMPORT_FOO' number: 7 > value: < name: 'IMPORT_BAR' number: 8 > value: < name: 'IMPORT_BAZ' number: 9 > > enum_type: < name: 'ImportEnumForMap' value: < name: 'UNKNOWN' number: 0 > value: < name: 'FOO' number: 1 > value: < name: 'BAR' number: 2 > > options: < java_package: 'com.google.protobuf.test' optimize_for: 1 # SPEED zG cc_enable_arenas: true > public_dependency: 0 a name: 'google/protobuf/unittest_import.proto' package: 'protobuf_unittest_import' dependency: 'google/protobuf/unittest_import_public.proto' message_type: < name: 'ImportMessage' field: < name: 'd' number: 1 label: 1 # Optional type: 5 # TYPE_INT32 > > enum_type: < name: 'ImportEnum' value: < name: 'IMPORT_FOO' number: 7 > value: < name: 'IMPORT_BAR' number: 8 > value: < name: 'IMPORT_BAZ' number: 9 > > enum_type: < name: 'ImportEnumForMap' value: < name: 'UNKNOWN' number: 0 > value: < name: 'FOO' number: 1 > value: < name: 'BAR' number: 2 > > options: < java_package: 'com.google.protobuf.test' optimize_for: 1 # SPEED a7 name: 'google/protobuf/unittest_import.proto' package: 'protobuf_unittest_import' dependency: 'google/protobuf/unittest_import_public.proto' message_type: < name: 'ImportMessage' field: < name: 'd' number: 1 label: 1 # Optional type: 5 # TYPE_INT32 > > enum_type: < name: 'ImportEnum' value: < name: 'IMPORT_FOO' number: 7 > value: < name: 'IMPORT_BAR' number: 8 > value: < name: 'IMPORT_BAZ' number: 9 > > enum_type: < name: 'ImportEnumForMap' value: < name: 'UNKNOWN' number: 0 > value: < name: 'FOO' number: 1 > value: < name: 'BAR' number: 2 > > options: < java_package: 'com.google.protobuf.test' optimize_for: 1 # SPEED cc_enable_arenas: true > public_dependency: 0 )rrrDrr)r,Z%UNITTEST_IMPORT_FILE_DESCRIPTOR_ASCIIr.r.r/testCopyToProto_FileDescriptors 0z8DescriptorCopyToProtoTest.testCopyToProto_FileDescriptorcCsd}|jtjjtj|dS)NaM name: 'TestService' method: < name: 'Foo' input_type: '.protobuf_unittest.FooRequest' output_type: '.protobuf_unittest.FooResponse' > method: < name: 'Bar' input_type: '.protobuf_unittest.BarRequest' output_type: '.protobuf_unittest.BarResponse' > )rrrrDrZServiceDescriptorProto)r,ZTEST_SERVICE_ASCIIr.r.r/!testCopyToProto_ServiceDescriptorNs z;DescriptorCopyToProtoTest.testCopyToProto_ServiceDescriptorrz It is not implemented in python.cCs&d}tjjjd}|j|tj|dS)Nz} name: 'Foo' input_type: '.protobuf_unittest.FooRequest' output_type: '.protobuf_unittest.FooResponse' r?)rrrDrHrrZMethodDescriptorProto)r,rr]r.r.r/ testCopyToProto_MethodDescriptoras z:DescriptorCopyToProtoTest.testCopyToProto_MethodDescriptorz!Pure python does not raise error.cCsbtj}|jttjjj||jttjjj||jttj jj|tj }|jtt jj|dS)N) rrr3r5rrrDr}rrrr)r,r-protor.r.r/testCopyToProto_TypeErrorrsz3DescriptorCopyToProtoTest.testCopyToProto_TypeErrorN)rrrrrrrrrrrrrrrrrrrrrr.r.r.r/rs$ ;  rc@s4eZdZddZddZddZddZd d Zd S) MakeDescriptorTestc Cstj}d|_|jj}|j|_|jj}d|_|jj}d|_|jj}d|_d|_|j j}d|_d|_t j j |_ t j j|_|j j}d|_d |_t j j |_ t j j|_d|_|j j}d|_d |_t j j |_ t j j|_d |_t j|}|j|jd jt j j|j|jdjt j j|j|jdjj||j|jd jd jd |j|jd jd j|jd jd |j|j|j|jd jtj dkr|j!t"d|jd _WdQRXdS)NZFoo2ZSubrBARrr uint64_fieldraZnested_message_field bar_fieldz Foo2.Sub.FOOrzFoo2.Sub.bar_fieldrF)#rrrrr nested_typerrrrrFieldDescriptorLABEL_REQUIREDr TYPE_UINT64rZ TYPE_MESSAGE type_name TYPE_ENUMMakeDescriptorr2r9cpp_typeCPPTYPE_UINT64ZCPPTYPE_MESSAGEr:Z nested_typesrrrUrTrrr3r) r,file_descriptor_protorrr enum_type_valr enum_fieldresultr.r.r/"testMakeDescriptorWithNestedFieldssX                  z5MakeDescriptorTest.testMakeDescriptorWithNestedFieldscCstj}d|_|jj}|j|_|jj}d|_|jj}d|_d|_|jj}d|_d|_t j j |_ t j j |_|jj}d|_d|_t j j |_ t j j|_d |_t j|}|j|jd jt j jdS) Nr?rrrrrrarzFoo.FOOr)rrrrrrrrrrrrrrrrrrr2r9rr)r,rrrrrrrr.r.r/&testMakeDescriptorWithUnsignedIntFields.          z9MakeDescriptorTest.testMakeDescriptorWithUnsignedIntFieldcCsFtj}tj}|jj|tj|}|j}|j d|j tj j dS)Ne) rrrrorDr}rrr=r2rJrurq)r,descriptor_protoZaggregate_messageZreformed_descriptorrmr.r.r/testMakeDescriptorWithOptionss  z0MakeDescriptorTest.testMakeDescriptorWithOptionscCstj}d|_dddddg}dddddg}x2tt|D]"}|jj}|d|_|||_q8Wtj |}x,tt|D]}|j |j |j ||qvWdS) NZBarZfoo_fooZFooBarZfooBazZfooFooZfoobarZfooBarr) rrrrangerrrrrrr2r9r)r,rnamesZcamelcase_namesrrrr.r.r/testCamelcaseNames   z$MakeDescriptorTest.testCamelcaseNamecCstj}d|_ddddddg}dddddd g}x2tt|D]"}|jj}|d |_|||_q s:           ?|v