Ϫf%dZddlZddlmZddlmZmZmZ ddlm Z ddl m Z ddl mZmZmZmZdd lmZdd lmZGd d e j,Zy#e $rdZ Y=wxYw) z+ Test cases for L{twisted.logger._format}. N)count)AnyCallableOptional)tzset)unittest) KeyFlattener aFormatter extractField flattenEvent) formatEvent)LogEventceZdZdZddZddZddZddZ ddee de fd Z dd Z dd Z dd Z d fdee ge fddfdZddZddZddZddZy)FlatFormattingTestsz9 Tests for flattened event formatting functions. returnNctGdd}tdfd|ddd}t|t|}|d =|d =tjtj |}|j t|d y ) a) L{flattenEvent} will "flatten" an event so that, if scrubbed of all but serializable objects, it will preserve all necessary data to be formatted once serialized. When presented with an event thusly flattened, L{formatEvent} will produce the same output. ceZdZdZy);FlatFormattingTests.test_formatFlatEvent..EphemeralvalueN)__name__ __module__ __qualname__ attributeB/usr/lib/python3/dist-packages/twisted/logger/test/test_flatten.py Ephemeralr%sIrrzcallable: {callme()} attribute: {object.attribute} numrepr: {number!r} numstr: {number!s} strrepr: {string!r} unistr: {unistr!s}ctSN)nextcountersrz:FlatFormattingTests.test_formatFlatEvent..1s 4=rhelloö) log_formatcallmeobjectnumberstringunistrr)r*uMcallable: 0 attribute: value numrepr: 7 numstr: 7 strrepr: 'hello' unistr: öN)rdictr jsonloadsdumps assertEqualr)selfrevent1event2event3r#s @rtest_formatFlatEventz(FlatFormattingTests.test_formatFlatEvents'  %);  Vf 8  8 DJJv./    rctdd}t|tjtj|}|j t |jdy)zH If the format string is invalid, an error is produced. zstrrepr: {string!X}r&)r(r,zUnable to format eventN)r.r r/r0r1 assertTruer startswith)r3r4r5s rtest_formatFlatEventBadFormatz1FlatFormattingTests.test_formatFlatEventBadFormatJsR-  VDJJv./  F+667OPQrcGdd}|}td|}t||j|jt |dy)z L{formatEvent} will prefer the stored C{str()} or C{repr()} value for an object, in case the other version. c(eZdZdZdZddZdefdZy)PFlatFormattingTests.test_formatFlatEventWithMutatedFields..Unpersistablez. Unpersitable object. FrNcd|_y)z0 Self destruct. TN destructedr3s r selfDestructz]FlatFormattingTests.test_formatFlatEventWithMutatedFields..Unpersistable.selfDestructes #'rc|jryy)Nzpost-serialization garbagezun-persistabler@rBs r__repr__zYFlatFormattingTests.test_formatFlatEventWithMutatedFields..Unpersistable.__repr__ks??7+rrN)rrr__doc__rArCstrrErrr Unpersistabler>^s J '  ,# ,rrIzunpersistable: {unpersistable})r( unpersistablezunpersistable: un-persistableN)r.r rCr2r)r3rIupr4s r%test_formatFlatEventWithMutatedFieldsz9FlatFormattingTests.test_formatFlatEventWithMutatedFieldsXsK  , ,&_!AQSTV  V,.MNrcdtdtfd} |j|dd|j|dd|j|dd |j|d d |j|d d |j|dd|j|dd|j|dd|j|dd|j|ddt}t j d\\}}}}|J|j|j |||d|j|j |||dy#t$rwxYw)zh Test that L{KeyFlattener.flatKey} returns the expected keys for format fields. formatrctj|D]'\}}}}|Jtj|||cSJd)Nz*Unable to derive key from format: {format})r parser flatKey)rN literalText fieldName formatSpec conversions r keyFromFormatz=FlatFormattingTests.test_keyFlattening..keyFromFormats` !!&)  Q ,,,#~--iZPP Q GF F5rz{}z!:z{foo}zfoo!:z{foo!s}zfoo!s:z{foo!r}zfoo!r:z{foo:%s}zfoo!:%sz{foo:!}zfoo!:!z{foo::}zfoo!::z {foo!s:%s}zfoo!s:%sz {foo!s:!}zfoo!s:!z {foo!s::}zfoo!s::z{x}Nzx!:zx!:/2)rHr2 ValueErrorr r rPrQ)r3rV sameFlattenerrRrSrTrUs rtest_keyFlatteningz&FlatFormattingTests.test_keyFlatteningys G# G# G    ]40$ 7 w/9 y18< y18< z2I> y18< y18< |4jA {3Y? {3Y?$    U #  $$$   ! !)Z De    ! !)Z Dg K    s E33 E>eventc|(tGfdd}td|}t||jt |d|S)z The same format field used twice in one event is rendered twice. @param event: An event to flatten. If L{None}, create a new event. @return: C{event} or the event created. c"eZdZdZdeffd Zy)JFlatFormattingTests._test_formatFlatEvent_fieldNamesSame..CountStrz& Hack rc,ttSr )rHr!)r3r#s r__str__zRFlatFormattingTests._test_formatFlatEvent_fieldNamesSame..CountStr.__str__stG}--rN)rrrrGrHr_r"srCountStrr]s.S.rr`z{x} {x}r(xz0 1)rr.r r2r)r3rZr`r#s @r$_test_formatFlatEvent_fieldNamesSamez8FlatFormattingTests._test_formatFlatEvent_fieldNamesSamesS =gG . .$*E U U+U3 rc$|jy)zR The same format field used twice in one event is rendered twice. NrcrBs r"test_formatFlatEventFieldNamesSamez6FlatFormattingTests.test_formatFlatEventFieldNamesSames 113rcF|j}|j|y)zb The same event flattened twice gives the same (already rendered) result. Nre)r3rZs r'test_formatFlatEventFieldNamesSameAgainz;FlatFormattingTests.test_formatFlatEventFieldNamesSameAgains 99; 11%8rcntdd}t|t|}|j|dy)zs L{formatEvent} will handle a flattened event with tailing text after a replacement field. ztest {x} trailingrraztest value trailingN)r.r rr2)r3rZresults r test_formatEventFlatTrailingTextz4FlatFormattingTests.test_formatEventFlatTrailingTexts9 *  UU# !67rc|Sr r)rbs rr$zFlatFormattingTests.sqr flattenFirstcGddGfdd}td|}||dtdtffd }|j|d d |j|d d |j|ddy)z L{extractField} will extract a field used in the format string. @param flattenFirst: callable to flatten an event ceZdZdefdZy)=FlatFormattingTests.test_extractField..ObjectWithReprrcy)NreprrrBs rrEzFFlatFormattingTests.test_extractField..ObjectWithRepr.__repr__srN)rrrrHrErrrObjectWithReprrps # rrsc"eZdZdfd ZddZy)8FlatFormattingTests.test_extractField..SomethingNc,d|_|_y)Nr%)r+r*)r3rss r__init__zAFlatFormattingTests.test_extractField..Something.__init__s ,. rctd)Nz Just in case.)NotImplementedErrorrBs r __getstate__zEFlatFormattingTests.test_extractField..Something.__getstate__s )/::rrF)rrrrwrz)rssr Somethingrus  / ;rr{z%{something.number} {something.object})r( somethingfieldrct|Sr )r )r} flatteneds rextractz6FlatFormattingTests.test_extractField..extract sy1 1rzsomething.numberr%zsomething.number!s7zsomething.object!srrN)r.rHrr2)r3rmr{rZrrsrs @@rtest_extractFieldz%FlatFormattingTests.test_extractFields   ; ;>k  !'  23 23 2 !34a8 !56< !56?rcFdtdtfd}|j|y)zi L{extractField} behaves identically if the event is explicitly flattened first. rZrct||Sr )r )rZs rrzDFlatFormattingTests.test_extractFieldFlattenFirst..flatteneds  LrN)rr)r3rs rtest_extractFieldFlattenFirstz1FlatFormattingTests.test_extractFieldFlattenFirsts%  X (  y)rcNddd}t||j|dddy)zT L{flattenEvent} will do nothing to an event with no format string. b)acNr r2r3 inputEvents rtest_flattenEventWithoutFormatz2FlatFormattingTests.test_flattenEventWithoutFormat s+Q' Z  3Q%78rcRdddd}t||j|ddddy)zz L{flattenEvent} will do nothing to an event with a format string that contains no format fields. rrzsimple messagerrr(Nrrs r test_flattenEventWithInertFormatz4FlatFormattingTests.test_flattenEventWithInertFormat(s8 Q6FG Z   .  rcRdddd}t||j|ddddy)zb L{flattenEvent} will do nothing to an event with log_format set to None. rrNrrrs rtest_flattenEventWithNoneFormatz3FlatFormattingTests.test_flattenEventWithNoneFormat8s7 Qd; Z   "  rrFr )rrrrGr7r;rLrYrrrcrfrhrkrrrrrrrrrrrs, \ ROB; |+/h' <4 9 8 >I!@$hZ%9:!@ !@F *9  rr)rGr/ itertoolsrtypingrrrtimer ImportError twisted.trialr_flattenr r r r _formatr _interfacesrTestCaserrrrrs[ **#KK!"o (++o  EsAAA