3
d5                 @   s  d dl Z d dlZd dlZd dlZd dlmZ d dlZd dlZd dlZd dl	m
Z
mZ d dlmZ d dljZd dlmZ d dlmZ dZd Zejjejjejjejej dZdd Zdd ZG dd dej Z!G dd dej Z"xrej#ejjedejj$ d D ]NZ%ejj&ejj'e%d  Z(de( Z)e(d!kr:qdd Z*e*e%Z+e,e)e+_-qW G dd deeZ.e-dkr~ej/  dS )"    N)etree)	NamespaceQualifiedName)PROV)AllTestsBase)RoundTripTestCaseexhttp://example.com/ns/ex#trhttp://example.com/ns/tr#xmlc             C   sB   | j d k	r| j j dkrd | _ x| D ]}tj|r$t| q$W d S )N )textstripr   	iselementremove_empty_tags)treeelem r   3/tmp/pip-build-7vycvbft/prov/prov/tests/test_xml.pyr      s
    

r   c             C   st  y| j dd W n tk
r$   Y nX y|j dd W n tk
rJ   Y nX tj| }tj|}x(|j jdD ]}|j }|j| qpW x(|j jdD ]}|j }|j| qW t|j  t|j  t	j
 }|j| |j dd |j j }dd |j D }t	j
 }|j| |j dd |j j }dd |j D }tj||}	dj|	}
|
rpd}t||
 dS )	z|
    Helper function to compare two XML files. It will parse both once again
    and write them in a canonical fashion.
    r   z//comment()c             S   s   g | ]}|j  r|j  qS r   )r   ).0_ir   r   r   
<listcomp>B   s    zcompare_xml.<locals>.<listcomp>c             S   s   g | ]}|j  r|j  qS r   )r   )r   r   r   r   r   r   H   s    
zStrings are not equal.
N)seekAttributeErrorr   parsegetrootZxpathZ	getparentremover   ioBytesIO
write_c14nreaddecode
splitlinesdifflibunified_diffjoinAssertionError)Zdoc1Zdoc2Zobj1obj2cpbufZstr1Zstr2r&   err_msgmsgr   r   r   compare_xml"   sB    




r/   c               @   sT   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d Z
dd ZdS )ProvXMLTestCasec             C   sp   t j }|jt }|jt  |jdt j|d fd	f tj (}|j	d|d t
tjjtd| W dQ R X dS )
zc
        Test the serialization of example 6 which is a simple entity
        description.
        ztr:WD-prov-dm-20111215Document
ex:version2r   )formatdestinationzexample_06.xmlN)r2   r3   )provProvDocumentadd_namespaceEX_NSEX_TRentity	PROV_TYPEr   r    	serializer/   ospathr'   	DATA_PATH)selfdocumentex_nsactualr   r   r   test_serialization_example_6S   s    


z,ProvXMLTestCase.test_serialization_example_6c             C   sp   t j }|jt  |jdddt jt jdt jfdg tj	 (}|j
d|d ttjjtd	| W d
Q R X d
S )zP
        Test the serialization of example 7 which is a basic activity.
        zex:a1z2011-11-16T16:05:00z2011-11-16T16:06:00zex:editex:hostserver.example.orgr   )r4   r5   zexample_07.xmlN)rF   rG   )r6   r7   r8   r9   activityr<   LiteralZ	XSD_QNAMEr   r    r=   r/   r>   r?   r'   r@   )rA   rB   rD   r   r   r   test_serialization_example_7e   s    

z,ProvXMLTestCase.test_serialization_example_7c             C   s   t j }|jt  |jd}|jd}|j||dddid |jd}|j||ddd	id tj (}|j	d
|d t
tjjtd| W dQ R X dS )zR
        Test the serialization of example 8 which deals with generation.
        zex:e1zex:a1z2001-10-26T21:32:52zex:portp1)r;   rH   timeZother_attributeszex:e2z2001-10-26T10:00:00p2r   )r4   r5   zexample_08.xmlN)r6   r7   r8   r9   r;   rH   ZwasGeneratedByr   r    r=   r/   r>   r?   r'   r@   )rA   rB   Ze1Za1e2rD   r   r   r   test_serialization_example_8z   s$    




z,ProvXMLTestCase.test_serialization_example_8c             C   s`   t jjtjjtddd}t j }|jt }|jt	  |j
dt j|d fd	f | j|| dS )
ze
        Test the deserialization of example 6 which is a simple entity
        description.
        zexample_06.xmlr   )sourcer4   ztr:WD-prov-dm-20111215r1   
ex:versionr3   N)rQ   r3   )r6   r7   deserializer>   r?   r'   r@   r8   r9   r:   r;   r<   assertEqual)rA   
actual_docexpected_documentrC   r   r   r   test_deserialization_example_6   s    

z.ProvXMLTestCase.test_deserialization_example_6c             C   sd   t jjtjjtddd}t j }tt }|j	| |j
dddt jt|dfdg | j|| d
S )zg
        Test the deserialization of example 7 which is a simple activity
        description.
        zexample_07.xmlr   )rP   r4   zex:a1z2011-11-16T16:05:00z2011-11-16T16:06:00Zeditex:hostserver.example.orgN)rW   rX   )r6   r7   rR   r>   r?   r'   r@   r   r9   r8   rH   r<   r   rS   )rA   rT   rU   rC   r   r   r   test_deserialization_example_7   s    
z.ProvXMLTestCase.test_deserialization_example_7c             C   s4  d}t j ,}|j| |jdd tjj|dd}W dQ R X tj }tt }|j	| |j	t
  |jdtjt|dftjtd ff | j||d	 d
}t j ,}|j| |jdd tjj|dd}W dQ R X tj }|j	t  |j	t
  |jdtjt|dftjtd ftjtd ff | j||d dS )a#  
        Example 4 and 5 have a different type specification. They use an
        xsi:type as an attribute on an entity. This can be read but if
        written again it will become an XML child element. This is
        semantically identical but cannot be tested with a round trip.
        a  
        <prov:document
            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
            xmlns:xsd="http://www.w3.org/2001/XMLSchema"
            xmlns:prov="http://www.w3.org/ns/prov#"
            xmlns:ex="http://example.com/ns/ex#"
            xmlns:tr="http://example.com/ns/tr#">

          <prov:entity prov:id="tr:WD-prov-dm-20111215" xsi:type="prov:Plan">
            <prov:type xsi:type="xsd:QName">ex:Workflow</prov:type>
          </prov:entity>

        </prov:document>
        r   r   )rP   r4   Nztr:WD-prov-dm-20111215ZWorkflowZPlanZ
example_04a  
        <prov:document
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xmlns:xsd="http://www.w3.org/2001/XMLSchema"
          xmlns:prov="http://www.w3.org/ns/prov#"
          xmlns:ex="http://example.com/ns/ex#"
          xmlns:tr="http://example.com/ns/tr#">

        <prov:entity prov:id="tr:WD-prov-dm-20111215" xsi:type="prov:Plan">
          <prov:type xsi:type="xsd:QName">ex:Workflow</prov:type>
          <prov:type xsi:type="xsd:QName">prov:Plan</prov:type> <!-- inferred -->
          <prov:type xsi:type="xsd:QName">prov:Entity</prov:type> <!-- inferred -->
        </prov:entity>

        </prov:document>
        EntityZ
example_05)r   StringIOwriter   r6   r7   rR   r   r9   r8   r:   r;   r<   r   r   rS   )rA   
xml_stringr   Zactual_documentrU   rC   r   r   r   &test_deserialization_example_04_and_05   s8    







z6ProvXMLTestCase.test_deserialization_example_04_and_05c             C   s   d}t jddJ}t jd tj ,}|j| |jdd tjj	|dd}W dQ R X W dQ R X | j
t|d	 | jd
t|d jk | j
t|jd dS )z
        PROV XML uses the <prov:other> element to enable the storage of non
        PROV information in a PROV XML document. It will be ignored by this
        library a warning will be raised informing the user.
        a  
        <prov:document
            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
            xmlns:xsd="http://www.w3.org/2001/XMLSchema"
            xmlns:prov="http://www.w3.org/ns/prov#"
            xmlns:ex="http://example.com/ns/ex#">

          <!-- prov statements go here -->

          <prov:other>
            <ex:foo>
              <ex:content>bar</ex:content>
            </ex:foo>
          </prov:other>

          <!-- more prov statements can go here -->

        </prov:document>
        T)recordalwaysr   r   )rP   r4   N   z[Document contains non-PROV information in <prov:other>. It will be ignored in this package.)warningscatch_warningssimplefilterr   r[   r\   r   r6   r7   rR   rS   lenZ
assertTruestrmessage_records)rA   r]   wr   docr   r   r   test_other_elements  s    


$z#ProvXMLTestCase.test_other_elementsc             C   sz   t jjtd}tjj|dd}tdd}| jt	|j
d | j|j | | j|j
d jj| | j|j
d jjd d	S )
zu
        Tests that a default namespace that is defined in a lower level tag is
        written to a bundle.
        znested_default_namespace.xmlr   )rP   r4   r   zhttp://example.org/0/ra   r   Ze001N)r>   r?   r'   r@   r6   r7   rR   r   rS   re   rh   Zget_default_namespace
identifier	namespaceZ	localpart)rA   filenamerj   nsr   r   r   test_nested_default_namespaceA  s    
z-ProvXMLTestCase.test_nested_default_namespacec             C   s   t jjtd}tjj|dd}| jt|j	d t
dd}| j|j	d jd d j| t jjtd}tjj|dd}|j	d jd d j}| j|| | j|jd	 d
S )zX
        Test the behaviour when namespaces are redefined at the element level.
        z+namespace_redefined_but_does_not_change.xmlr   )rP   r4   ra   r   zhttp://example.com/ns/ex#r   znamespace_redefined.xmlzhttp://example.com/ns/new_ex#N)r>   r?   r'   r@   r6   r7   rR   rS   re   rh   r   
attributesrm   ZassertNotEqualuri)rA   rn   rj   ro   Znew_nsr   r   r   test_redefining_namespacesP  s    
z*ProvXMLTestCase.test_redefining_namespacesN)__name__
__module____qualname__rE   rJ   rO   rV   rY   r^   rk   rp   rs   r   r   r   r   r0   R   s   Q+r0   c               @   s   e Zd ZdddZdS ) ProvXMLRoundTripFromFileTestCaseFc             C   sB   t jj|dd}tj  }|jd||d t|| W d Q R X d S )Nr   )rP   r4   )r4   r5   force_types)r6   r7   rR   r   r    r=   r/   )rA   rn   rx   rB   Znew_xmlr   r   r   _perform_round_tripf  s
    
z4ProvXMLRoundTripFromFileTestCase._perform_round_tripN)F)rt   ru   rv   ry   r   r   r   r   rw   e  s   rw   *ztest_roundtrip_from_xml_%snested_default_namespace!nested_changing_default_namespace'namespace_redefined_but_does_not_changenamespace_redefinedc                s$   t dkrdnd fdd}|S )Npc1TFc                s   | j  d d S )N)rx   )ry   )rA   )frx   r   r   fct  s    zget_fct.<locals>.fct)r   )name)r   r   r   )r   rx   r   get_fct  s
    r   c               @   s   e Zd ZdZdS )RoundTripXMLTestsr   N)rt   ru   rv   ZFORMATr   r   r   r   r     s   r   __main__)r   r	   )r
   r   )r{   r|   r}   r~   )0r%   globinspectr   lxmlr   r>   Zunittestrb   Zprov.identifierr   r   Zprov.constantsr   Z
prov.modelmodelr6   Zprov.tests.test_modelr   Zprov.tests.utilityr   r9   r:   r?   r'   dirnameabspathgetfilecurrentframer@   r   r/   ZTestCaser0   rw   iglobextseprn   splitextbasenamer   Z	test_namer   r   rf   rt   r   mainr   r   r   r   <module>   sJ   
"0  &   

