3
d                 @   s@  d Z ddlZddlZddlZddlmZmZmZmZ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eZd	Zd
ZG dd deZG dd dejZG dd dejZG dd dejZG dd dejZG dd dejZ G dd dejZ!G dd deeeeZ"G dd dee"Z#edkr<ej$  dS )z4
Created on Jan 25, 2012

@author: Trung Dong Huynh
    N)ProvDocument
ProvBundleProvExceptionfirstLiteral)examples)TestAttributesBase)TestQualifiedNamesBase)TestStatementsBase)RoundTripTestCasezhttp://www.example.org/zhttp://www.example2.org/c               @   s   e Zd ZdZdd ZdS )TestExamplesBasezThis is the base class for testing support for all the examples provided
    in prov.tests.examples.
    It is not runnable and needs to be included in a subclass of
    RoundTripTestCase.
    c             C   sB   d}x8t jD ].\}}|d7 }tjd|| | }| j| qW d S )Nr      z%d. Testing the %s example)r   testsloggerinfoZdo_tests)selfcounternamegraphg r   5/tmp/pip-build-7vycvbft/prov/prov/tests/test_model.pytest_all_examples   s    z"TestExamplesBase.test_all_examplesN)__name__
__module____qualname____doc__r   r   r   r   r   r      s   r   c               @   s   e Zd Zdd Zdd ZdS )TestLoadingProvToolboxJSONc             C   s   t jjt jjtd | _t j| j}g | _x|D ]z}|jdr2t	| j| X}y8t
j|}|jdd}t
j|d}| j||d|  W n   | jj| Y nX W d Q R X q2W d S )Nz/json/z.json   )indent)contentz.Round-trip JSON encoding/decoding failed:  %s.)ospathdirnameabspath__file__	json_pathlistdirfailsendswithopenr   deserialize	serializeassertEqualappend)r   	filenamesfilename	json_fileg1json_strg2r   r   r   setUp)   s     


z TestLoadingProvToolboxJSON.setUpc             C   sr   xl| j D ]b}| j| }t|F}tjd| tj|}|jdd}tj|d}| j||d|  W d Q R X qW d S )NzLoading %s...r   )r   )r    z.Round-trip JSON encoding/decoding failed:  %s.)	r(   r&   r*   r   r   r   r+   r,   r-   )r   r0   filepathr1   r2   r3   r4   r   r   r   test_loading_all_json<   s    


z0TestLoadingProvToolboxJSON.test_loading_all_jsonN)r   r   r   r5   r7   r   r   r   r   r   (   s   r   c               @   s   e Zd Zdd ZdS )TestFlatteningc       	      C   s   xt jD ]\}}tjd| | }|j }t|j }d}x$|j D ]}|d7 }| j|| qDW x4|jD ]*}x$|j D ]}|d7 }| j|| qvW qhW | j	|t
|j  qW d S )Nz$Testing flattening of the %s exampler   r   )r   r   r   r   	flattenedsetget_recordsassertInbundlesr-   len)	r   r   r   documentr9   Zflattened_recordsZ	n_recordsrecordbundler   r   r   test_flatteningO   s    zTestFlattening.test_flatteningN)r   r   r   rB   r   r   r   r   r8   N   s   r8   c               @   s   e Zd Zdd ZdS )TestUnificationc       	      C   s   t jjt jjtd }t j|}x|D ]z}|jds8q(|| }t|T}tj	d| tj
d| tj|}|j }|j }| jt|j t|j  W d Q R X q(W d S )Nz/unification/z.jsonzTesting unifying: %szLoading %s...)r!   r"   r#   r$   r%   r'   r)   r*   r   r   debugr   r+   r9   unifiedZ
assertLessr>   r;   )	r   r&   r/   r0   r6   r1   r?   r9   rE   r   r   r   test_unifyingb   s    




zTestUnification.test_unifyingN)r   r   r   rF   r   r   r   r   rC   a   s   rC   c               @   s   e Zd Zdd Zdd ZdS )TestBundleUpdatec                s   t  jt jd  jd jd}|jd | jt fdd | jt fdd  j| | jt	 j
 d d S )Nb1eb2c                  s
    j dS )Nr   )updater   )rH   r   r   <lambda>   s    z<TestBundleUpdate.test_bundle_update_simple.<locals>.<lambda>c                  s
    j S )N)rK   r   )rH   docr   r   rL      s       )r   set_default_namespaceEX_URIrA   entityassertRaisesr   rK   r-   r>   r;   )r   rJ   r   )rH   rM   r   test_bundle_update_simplew   s    





z*TestBundleUpdate.test_bundle_update_simplec                s   t    jt  jd  jd}|jd t  }|jt |jd |jd}|jd |jd}|jd | jt fdd  j| | jt	 j
 d | jt	 jd d S )NrI   rH   rJ   c                  s
    j dS )Nr   )rK   r   )d1r   r   rL      s    z>TestBundleUpdate.test_document_update_simple.<locals>.<lambda>rN   )r   rO   rP   rQ   rA   rR   r   rK   r-   r>   r;   r=   )r   rH   d2rJ   r   )rT   r   test_document_update_simple   s     










z,TestBundleUpdate.test_document_update_simpleN)r   r   r   rS   rV   r   r   r   r   rG   v   s   rG   c               @   s4   e Zd Zdd Zdd Zdd Zdd Zd	d
 ZdS )TestAddBundlec             C   s$   t  }|jdt}|j|d  |S )NexZe1)r   add_namespacerP   rQ   )r   rT   ns_exr   r   r   
document_1   s    zTestAddBundle.document_1c             C   s$   t  }|jdt}|j|d  |S )NrX   Za1)r   rY   EX2_URIZactivity)r   rU   rZ   r   r   r   
document_2   s    zTestAddBundle.document_2c             C   s   t dtid}|S )NrX   )
namespaces)r   r\   )r   br   r   r   bundle_0   s    zTestAddBundle.bundle_0c                s   | j  | j   fdd}| jt| | jj  j d | jj  | j j	  fdd}| jt| jt
 d | jtj	d d S )Nc                  s   j   d S )N)
add_bundler   )b0rT   r   r   
sub_test_1   s    z8TestAddBundle.test_add_bundle_simple.<locals>.sub_test_1zex:b0c                 s    j } jt| d d S )N)
identifier)rd   ra   r   )Zex2_b0)rb   rT   r   r   
sub_test_2   s    z8TestAddBundle.test_add_bundle_simple.<locals>.sub_test_2rN   )r[   r`   rR   r   ZassertFalseZhas_bundlesra   
assertTruer<   r=   r   r-   r>   )r   rc   re   r   )rb   rT   r   test_add_bundle_simple   s    z$TestAddBundle.test_add_bundle_simplec                s   | j   | j  fdd}| jt| jd} jd | j|t jj	 | j
 j t }|j | j| j d S )Nc                  s    j  d S )N)ra   r   )rT   rU   r   r   rc      s    z:TestAddBundle.test_add_bundle_document.<locals>.sub_test_1zex:b2)r[   r]   rR   r   Zvalid_qualified_namera   r-   r   r=   rd   ZassertNotInr   rK   r<   )r   rc   Zex2_b2rJ   r   )rT   rU   r   test_add_bundle_document   s    

z&TestAddBundle.test_add_bundle_documentN)r   r   r   r[   r]   r`   rg   rh   r   r   r   r   rW      s
   rW   c               @   s   e Zd Zdd Zdd ZdS )TestLiteralRepresentationc             C   s"   t d}|j }| jd|k d S )Nz{"foo": "bar"}z{\"f)r   provn_representationrf   )r   l
string_repr   r   r   %test_literal_provn_with_single_quotes   s    z?TestLiteralRepresentation.test_literal_provn_with_single_quotesc             C   s"   t d}|j }| jd|k d S )Nz"""foo\nbar"""z\"\"\"f)r   rj   rf   )r   rk   rl   r   r   r   %test_literal_provn_with_triple_quotes   s    z?TestLiteralRepresentation.test_literal_provn_with_triple_quotesN)r   r   r   rm   rn   r   r   r   r   ri      s   ri   c               @   s   e Zd ZdZdS )AllTestsBasez.This is a test to include all available tests.N)r   r   r   r   r   r   r   r   ro      s   ro   c               @   s   e Zd ZdddZdS )RoundTripModelTestNc             C   s   |j  }| j||d|  dS )z]Exercises prov.model without the actual serialization and PROV-N
        generation.
        z The document is not self-equal:
N)Z	get_provnr-   )r   Zprov_docmsgZprovn_contentr   r   r   assertRoundTripEquivalence   s    z-RoundTripModelTest.assertRoundTripEquivalence)N)r   r   r   rr   r   r   r   r   rp      s   rp   __main__)%r   Zunittestloggingr!   Z
prov.modelr   r   r   r   r   Z
prov.testsr   Zprov.tests.attributesr   Zprov.tests.qnamesr	   Zprov.tests.statementsr
   Zprov.tests.utilityr   	getLoggerr   r   rP   r\   objectr   ZTestCaser   r8   rC   rG   rW   ri   ro   rp   mainr   r   r   r   <module>   s0   
&):

