U
    ?hޓ                     @   s   d dl mZ d dlZd dlmZmZmZmZmZ d dl	m
Z
 eddd dd d	d d
d dZdd ZG dd dZG dd dZG dd dZG dd dZG dd dZejdeeeeejgdd ZdS )    )HashTrieMapN)AnchorRegistryResourceSpecification
exceptions)DRAFT202012zid-and-childrenc                 C   s
   |  dS )NIDgetcontents r   M/var/www/html/venv/lib/python3.8/site-packages/referencing/tests/test_core.py<lambda>	       r   c                 C   s   |  dg S )Nchildrenr
   r   r   r   r   r   
   r   c                    s    fdd| di  D S )Nc                    s$   g | ]\}}t | j|d dqS )r   nameresource)r   create_resource).0r   eachspecificationr   r   
<listcomp>   s
   
z<lambda>.<locals>.<listcomp>anchors)r   itemsr   r   r   r   r   r      s   
c                 C   s6   t | d s2tdd | d d d D r2||S |S )N   c                 s   s   | ]}|d kV  qdS )r   Nr   r   r   r   r   r   	<genexpr>   s     z<lambda>.<locals>.<genexpr>)lenallin_subresourcesegmentsresolverZsubresourcer   r   r   r      s
    
r   id_ofsubresources_of
anchors_inZmaybe_in_subresourcec                 C   s   t ddS )zQ
    A retriever suitable for use in tests which expect it never to be used.
    z2This retrieve function expects to never be called!N)RuntimeErrorurir   r   r   blow_up   s    r/   c                   @   s  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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'd( Zd)d* Zd+d, Zd-d. Zd/d0 Zd1d2 Zd3d4 Zd5d6 Zd7d8 Zd9d: Zd;d< Z d=d> Z!d?d@ Z"dAdB Z#dCdD Z$dEdF Z%dGdH Z&dIdJ Z'dKdL Z(dMdN Z)dOdP Z*dQdR Z+dSdT Z,dUdV Z-dWdX Z.dYdZ Z/d[d\ Z0d]d^ Z1d_S )`TestRegistryc                 C   s8   t jddid}d}t j||d}|| |ks4tdS )zQ
        Adding a resource to the registry then allows re-retrieving it.
        foobarr   urn:exampler.   r   N)r   opaquer   with_resourceAssertionErrorselfr   r.   registryr   r   r   test_with_resource#   s    zTestRegistry.test_with_resourcec                 C   sZ   t ji d}t dditd}t d|fd|fg}|t jd|djd|dksVtdS )	zT
        Adding multiple resources to the registry is like adding each one.
        r   r1   r2   r   r   http://example.com/1http://example.com/foo/barr4   N)r   r5   ID_AND_CHILDRENr   with_resourcesr6   r7   r9   onetwor:   r   r   r   test_with_resources-   s    z TestRegistry.test_with_resourcesc                 C   s8   d}t |dd}|t  }|t ||ks4td S )Nzurn:example:resource   r	   r1   )r?   r   r   r6   r7   r9   r.   r   r:   r   r   r   test_matmul_resourceB   s    
z!TestRegistry.test_matmul_resourcec                 C   sZ   d}t |dd}d}t |dd}||gt  }|t ||f||fgksVtd S )Nzurn:example:onerE   rF   zurn:example:two)r?   r   r   r@   r7   )r9   Zone_urirB   Ztwo_urirC   r:   r   r   r   test_matmul_many_resourcesH   s    z'TestRegistry.test_matmul_many_resourcesc              	   C   sL   t jddid}ttj}|t   W 5 Q R X |jtj|dksHtd S )Nr1   r2   r   r   )	r   r5   pytestraisesr   ZNoInternalIDr   valuer7   )r9   r   er   r   r   test_matmul_resource_without_idT   s    z,TestRegistry.test_matmul_resource_without_idc                 C   s>   d}ddi}t  ||fg}t|td}|| |ks:td S )Nr3   $schema,https://json-schema.org/draft/2020-12/schemar<   )r   with_contentsr   r   r7   )r9   r.   schemar:   expectedr   r   r   #test_with_contents_from_json_schemaZ   s
    z0TestRegistry.test_with_contents_from_json_schemac                 C   s>   d}t  j|ddifgtjd}|| tddiks:td S )Nr3   r1   r2   Zdefault_specification)r   rR   r   OPAQUEr   r5   r7   )r9   r.   r:   r   r   r   ,test_with_contents_and_default_specificationb   s    z9TestRegistry.test_with_contents_and_default_specificationc                 C   s8   d}t  jdd t|D tjd}t||ks4td S )N   c                 S   s   g | ]}t |d difqS r1   r2   strr   ir   r   r   r   m   s     z)TestRegistry.test_len.<locals>.<listcomp>rV   )r   rR   ranger   rW   r"   r7   )r9   totalr:   r   r   r   test_lenj   s    zTestRegistry.test_lenc                 C   s   t  r
td S N)r   r7   r9   r   r   r   test_bool_emptyr   s    zTestRegistry.test_bool_emptyc                 C   s,   t  jdd tdD tjd}|s(td S )Nc                 S   s   g | ]}t |d difqS rZ   r[   r]   r   r   r   r   w   s     z4TestRegistry.test_bool_not_empty.<locals>.<listcomp>   rV   )r   rR   r_   r   rW   r7   r9   r:   r   r   r   test_bool_not_emptyu   s
    z TestRegistry.test_bool_not_emptyc                 C   sB   t  jdd tdD tjd}t|dd tdD ks>td S )Nc                 S   s   g | ]}t |d difqS rZ   r[   r]   r   r   r   r   ~   s     z*TestRegistry.test_iter.<locals>.<listcomp>   rV   c                 S   s   h | ]}t |qS r   r[   r]   r   r   r   	<setcomp>   s     z)TestRegistry.test_iter.<locals>.<setcomp>)r   rR   r_   r   rW   setr7   rf   r   r   r   	test_iter|   s
    zTestRegistry.test_iterc                 C   s6   t ddi}d}t||i }|| |ks2td S Nr1   r2   r3   )r   r5   r   crawlr7   r8   r   r   r   'test_crawl_still_has_top_level_resource   s    z4TestRegistry.test_crawl_still_has_top_level_resourcec              	   C   sl   d}t d|ddgd}|t  }tt ||  W 5 Q R X t |dd}| | |kshtd S )N	urn:childurn:rootrE   rF   r	   r   )r?   r   r   rK   rL   LookupErrorrm   r7   )r9   Zchild_idrootr:   rT   r   r   r   test_crawl_finds_a_subresource   s    
z+TestRegistry.test_crawl_finds_a_subresourcec                 C   sN   t dddid}|t  }| | djtdt ddksJtd S )Nurn:barr1   rE   r	   r   r   )	r?   r   r   rm   anchoridrM   r   r7   r9   r   r:   r   r   r    test_crawl_finds_anchors_with_id   s    
z-TestRegistry.test_crawl_finds_anchors_with_idc                 C   sL   t dddii}t d|}| ddjtdt ddksHtd S )Nr   r1   rE   rp   r   )	r?   r   r   r6   rm   rw   rM   r   r7   ry   r   r   r   test_crawl_finds_anchors_no_id   s    z+TestRegistry.test_crawl_finds_anchors_no_idc                 C   s:   t ddi}d}t ||}||ddiks6td S rl   )r   r5   r   r6   r   r7   r8   r   r   r   test_contents   s    zTestRegistry.test_contentsc                 C   sJ   d}t d|d i}|t  }|| ||d    kr@|ksFn td S Nhttp://example.com/r	   #)r?   r   r   r7   rG   r   r   r   #test_getitem_strips_empty_fragments   s    
z0TestRegistry.test_getitem_strips_empty_fragmentsc                 C   sV   d}t d|d i}|t  }||||d   krLd|d iksRn td S r}   )r?   r   r   r   r7   rG   r   r   r   $test_contents_strips_empty_fragments   s    

z1TestRegistry.test_contents_strips_empty_fragmentsc              	   C   sB   t  }ttj}|d W 5 Q R X |jtjddks>td S )Nr3   ref)r   rK   rL   r   NoSuchResourcer   rM   r7   r9   r:   rN   r   r   r   "test_contents_nonexistent_resource   s    z/TestRegistry.test_contents_nonexistent_resourcec                 C   s^   t dddii}t d|}|dd}|jtdt ddksHt|j|	 ksZtd S )Nr   r1   r2   r3   r   )
r?   r   r   r6   rw   rM   r   r7   r:   rm   )r9   r   r:   Z	retrievedr   r   r   test_crawled_anchor   s    z TestRegistry.test_crawled_anchorc              	   C   sD   t  }ttj}|dd W 5 Q R X |jtjddks@td S )Nr3   r1   r   )r   rK   rL   r   r   rw   rM   r7   r   r   r   r   #test_anchor_in_nonexistent_resource   s    z0TestRegistry.test_anchor_in_nonexistent_resourcec                 C   sP   t ji d}tddi}t||d}|t d|fd|fg ksLtd S )Nr   r1   r2   )r=   r>   r=   r>   )r   r5   r?   r   r   r@   rm   r7   rA   r   r   r   	test_init   s    zTestRegistry.test_initc                 C   sZ   t ji d}tddi}td|id|}| t d|fd|fg ksVtdS )z
        Passing a `dict` to `Registry` gets converted to a `HashTrieMap`.

        So continuing to use the registry works.
        r   r1   r2   r=   r>   N)	r   r5   r?   r   r   r6   rm   r@   r7   rA   r   r   r   test_dict_conversion   s      z!TestRegistry.test_dict_conversionc              	   C   s@   t  }ttj}|d  W 5 Q R X |jtjddks<td S )Nzurn:bigboomr   )r   rK   rL   r   r   rM   r7   r   r   r   r   test_no_such_resource  s    z"TestRegistry.test_no_such_resourcec           	   
   C   s   t ji d}tddi}tddi}tdddii}td|i}t d	|}t||d
}t d| }||||td|fd|fd|fgtdt	dtddidd	|kst
d S )Nr   r1   r2   bazquuxr   rE   r=   r>   r=   http://example.com/bazhttp://example.com/foo/quuxr   )r   r1   r   )r   )r   r5   r?   r   r   r6   rm   combiner   r   r7   )	r9   rB   rC   threefourfirstsecondthirdfourthr   r   r   test_combine  sB      zTestRegistry.test_combinec                 C   s"   t ddi}|||kstdS )z
        Combining a registry with itself short-circuits.

        This is a performance optimization -- otherwise we do lots more work
        (in jsonschema this seems to correspond to making the test suite take
         *3x* longer).
        urn:foor2   N)r   r   r7   rf   r   r   r   test_combine_self.  s    	zTestRegistry.test_combine_selfc           	      C   s   t ji d}tddi}tddi}t d|}t d|}t||d}td|fd|fd	|fg}|||}||kst| |kstd S )
Nr   r1   r2   r   r   r=   r>   r   r   )	r   r5   r?   r   r   r6   r   r7   rm   )	r9   rB   rC   r   r   r   r   rT   combinedr   r   r   %test_combine_with_uncrawled_resources:  s&    z2TestRegistry.test_combine_with_uncrawled_resourcesc                 C   s   t ji d}tddi}tddi}dd }t d|}t|d	d
|}t d|}|||t|d	d|fd
|fd|fgkst|||t|d	d|fd
|fd|fgkstd S )Nr   r1   r2   r   r   c                 S   s   d S rb   r   r-   r   r   r   retrieveW  s    z@TestRegistry.test_combine_with_single_retrieve.<locals>.retriever=   r   http://example.com/2http://example.com/3	r   r5   r?   r   r   r6   r   r@   r7   r9   rB   rC   r   r   r   r   r   r   r   r   !test_combine_with_single_retrieveR  s8     
	z.TestRegistry.test_combine_with_single_retrievec                 C   s   t ji d}tddi}tddi}dd }t|dd	|}t|dd
|}t|dd|}|||t|dd	|fd
|fd|fgkst|||t|dd	|fd
|fd|fgkstd S )Nr   r1   r2   r   r   c                 S   s   d S rb   r   r-   r   r   r   r   x  s    z@TestRegistry.test_combine_with_common_retrieve.<locals>.retriever   r=   r   r   r   r   r   r   r   !test_combine_with_common_retrieves  sD    
 

	z.TestRegistry.test_combine_with_common_retrievec           	   	   C   s   t ji d}tddi}tddi}dd }dd	 }t|d
d|}t d|}t|d
d|}tjtdd |	|| W 5 Q R X d S )Nr   r1   r2   r   r   c                 S   s   d S rb   r   r-   r   r   r   foo_retrieve  s    zDTestRegistry.test_combine_conflicting_retrieve.<locals>.foo_retrievec                 S   s   d S rb   r   r-   r   r   r   bar_retrieve  s    zDTestRegistry.test_combine_conflicting_retrieve.<locals>.bar_retriever   r=   r   r   zconflict.*retrievmatch)
r   r5   r?   r   r   r6   rK   rL   	Exceptionr   )	r9   rB   rC   r   r   r   r   r   r   r   r   r   !test_combine_conflicting_retrieve  s     

z.TestRegistry.test_combine_conflicting_retrievec                 C   sF   t ji d}tddi}t||d}|dtd|iksBtd S )Nr   r1   r2   )r   ru   r   ru   )r   r5   r?   r   r   remover7   rA   r   r   r   test_remove  s    zTestRegistry.test_removec                 C   sR   t ji d}tddi}t d|fd|fg}|dt d|ksNtd S )Nr   r1   r2   r   ru   )	r   r5   r?   r   r   r@   r   r6   r7   rA   r   r   r   test_remove_uncrawled  s    z"TestRegistry.test_remove_uncrawledc                 C   s^   t ji d}tdddii}t d|fd|fg }|dt d| ksZt	d S )Nr   r   r1   r2   r   ru   )
r   r5   r?   r   r   r@   rm   r   r6   r7   rA   r   r   r   test_remove_with_anchors  s    z%TestRegistry.test_remove_with_anchorsc              	   C   s>   t tj}t d W 5 Q R X |jtjddks:td S )Nzurn:doesNotExistr   )rK   rL   r   r   r   r   rM   r7   )r9   rN   r   r   r   test_remove_nonexistent_uri  s    z(TestRegistry.test_remove_nonexistent_uric                    s8   t ddi t fddd}|dj ks4td S )Nr1   r2   c                    s    S rb   r   r-   r1   r   r   r     r   z,TestRegistry.test_retrieve.<locals>.<lambda>r   r3   )r   r5   r   get_or_retrieverM   r7   rf   r   r   r   test_retrieve  s    zTestRegistry.test_retrievec              	      s^   t ddi  fdd}t|d}|dj ks8tttj	 |d W 5 Q R X d S )Nr1   r2   c                    s   | dkr S t dd S )Nurn:succeedzOh no!)r   r-   r   r   r   r     s    z@TestRegistry.test_retrieve_arbitrary_exception.<locals>.retriever   r   urn:uhoh)
r   r5   r   r   rM   r7   rK   rL   r   Unretrievabler9   r   r:   r   r   r   !test_retrieve_arbitrary_exception  s    
z.TestRegistry.test_retrieve_arbitrary_exceptionc              	      s^   t ddi  fdd}t|d}|dj ks8tttj	 |d W 5 Q R X d S )Nr1   r2   c                    s   | dkr S t j| dd S )Nr   r   )r   r   r-   r   r   r   r     s    z=TestRegistry.test_retrieve_no_such_resource.<locals>.retriever   r   r   )
r   r5   r   r   rM   r7   rK   rL   r   r   r   r   r   r   test_retrieve_no_such_resource  s    
z+TestRegistry.test_retrieve_no_such_resourcec              	   C   s8   dd }t |d}ttj |d W 5 Q R X d S )Nc                 S   s
   t i S rb   )r   from_contentsr-   r   r   r   r     s    zKTestRegistry.test_retrieve_cannot_determine_specification.<locals>.retriever   r   )r   rK   rL   r   CannotDetermineSpecificationr   r   r   r   r   ,test_retrieve_cannot_determine_specification  s    
z9TestRegistry.test_retrieve_cannot_determine_specificationc                 C   sF   t ddi}td|itd}|d |ks.t|dj|ksBtd S )Nr1   r2   r3   r   )r   r5   r   r/   r7   r   rM   )r9   r1   r:   r   r   r   (test_retrieve_already_available_resource  s    z5TestRegistry.test_retrieve_already_available_resourcec                 C   sL   t ddd}t d|jgi}ttdd|}| d |ksHtd S )Nro   rE   rF   r   r   rp   )r?   r   r   r   r/   r6   rm   r7   )r9   childrs   r:   r   r   r   -test_retrieve_first_checks_crawlable_resource	  s    z:TestRegistry.test_retrieve_first_checks_crawlable_resourcec                 C   s<   t ji d}td|i}|jdd}|dji ks8td S )Nr   http://example.com)Zbase_urir   r   r5   r   r'   lookupr   r7   )r9   rB   r:   r'   r   r   r   test_resolver  s    zTestRegistry.test_resolverc                 C   sJ   t ddi}t |}|dj|jks0t|dj|jksFtd S )Nr	   r   r   )r?   r   r   resolver_with_rootr   r   r7   r9   rs   r'   r   r   r   "test_resolver_with_root_identified  s    z/TestRegistry.test_resolver_with_root_identifiedc                 C   s2   t ji d}t |}|dj|jks.td S )Nr   r   )r   r5   r   r   r   r   r7   r   r   r   r   $test_resolver_with_root_unidentified  s    z1TestRegistry.test_resolver_with_root_unidentifiedc                 C   sZ   t ji d}tddi}t d|fd|fg}t|dksBtt| dksVtd S )Nr   r1   r2   r=   r>   z"<Registry (2 uncrawled resources)>z<Registry (2 resources)>)	r   r5   r?   r   r   r@   reprr7   rm   rA   r   r   r   	test_repr   s    zTestRegistry.test_reprc                 C   sH   t ji d}tddi}td|i jd|d}t|dksDtd S )Nr   r1   r2   r=   r>   r4   z%<Registry (2 resources, 1 uncrawled)>)	r   r5   r?   r   r   rm   r6   r   r7   rA   r   r   r   test_repr_mixed_crawled,  s     z$TestRegistry.test_repr_mixed_crawledc                 C   s,   t  jdtji dd}t|dks(td S )Nr=   r   r4   z!<Registry (1 uncrawled resource)>)r   r6   r   r5   r   r7   rf   r   r   r   test_repr_one_resource8  s
    
z#TestRegistry.test_repr_one_resourcec                 C   s   t t dkstd S )Nz<Registry (0 resources)>)r   r   r7   rc   r   r   r   test_repr_empty?  s    zTestRegistry.test_repr_emptyN)2__name__
__module____qualname__r;   rD   rH   rI   rO   rU   rX   ra   rd   rg   rk   rn   rt   rz   r{   r|   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r0   "   s^   
	

&!'r0   c                   @   s   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d Zdd Zdd Zdd Zdd Zdd ZdS ) TestResourcec                 C   s*   ddi}t |}|t |tdks&td S )NrP   rQ   r<   )r   r   r   r7   r9   rS   r   r   r   r   #test_from_contents_from_json_schemaD  s    
z0TestResource.test_from_contents_from_json_schemac              	   C   s*   t tj tddi W 5 Q R X dS )z
        Creating a resource with no discernible way to see what
        specification it belongs to (e.g. no ``$schema`` keyword for JSON
        Schema) raises an error.
        r1   r2   N)rK   rL   r   r   r   r   rc   r   r   r   2test_from_contents_with_no_discernible_informationI  s    z?TestResource.test_from_contents_with_no_discernible_informationc                 C   s0   t jdditjd}|t jddidks,td S Nr1   r2   rV   r   r   r   r   rW   r5   r7   r9   r   r   r   r   >test_from_contents_with_no_discernible_information_and_defaultS  s
    zKTestResource.test_from_contents_with_no_discernible_information_and_defaultc                 C   s0   ddi}t j|tjd}|t |tdks,td S )NrP   rQ   rV   r<   )r   r   r   rW   r   r7   r   r   r   r   #test_from_contents_unneeded_defaultZ  s    z0TestResource.test_from_contents_unneeded_defaultc                 C   s&   t jdtd}|t dtdks"td S )NTrV   r<   )r   r   r?   r7   r   r   r   r   test_non_mapping_from_contentse  s    z+TestResource.test_non_mapping_from_contentsc                 C   s0   t jdditjd}|t jddidks,td S r   r   r   r   r   r    test_from_contents_with_fallbacko  s
    z-TestResource.test_from_contents_with_fallbackc                 C   sF   t ddd dd dd dd d}tdd	i|d
}| dksBtd S )N c                 S   s   dS Nurn:fixedIDr   r   r   r   r   r   y  r   zATestResource.test_id_delegates_to_specification.<locals>.<lambda>c                 S   s   g S rb   r   r   r   r   r   r   z  r   c                 S   s   g S rb   r   r   r   r   r   r   {  r   c                 S   s   |S rb   r   r%   r   r   r   r   }  r   r(   r1   r   r<   r   )r   r   rx   r7   r9   r   r   r   r   r   "test_id_delegates_to_specificationv  s    	z/TestResource.test_id_delegates_to_specificationc                 C   s*   d}t d|d i}| |ks&td S r}   )r?   r   rx   r7   )r9   r.   rs   r   r   r   test_id_strips_empty_fragment  s    z*TestResource.test_id_strips_empty_fragmentc                 C   s8   t di dgi}t| dd i dfD ks4td S )Nr   rE   c                 S   s   g | ]}t |qS r   )r?   r   r    r   r   r   r     s    zMTestResource.test_subresources_delegates_to_specification.<locals>.<listcomp>)r?   r   listsubresourcesr7   r   r   r   r   ,test_subresources_delegates_to_specification  s    z9TestResource.test_subresources_delegates_to_specificationc                 C   s8   ddi}t d|gi}t| t|gks4td S )NrP   rQ   r   )r?   r   r   r   r   r7   r   r   r   r   -test_subresource_with_different_specification  s
    
z:TestResource.test_subresource_with_different_specificationc                 C   s^   t di dddi}t| tdt i dtdt ddtdt ddgksZtd S )	Nr      r   )r1   r2   r   r1   r   r2   r   )r?   r   r   r   r   r7   r   r   r   r   'test_anchors_delegates_to_specification  s    
z4TestResource.test_anchors_delegates_to_specificationc                 C   s6   t jddid}t  }|jd|djdks2td S )Nr1   r   r   z/foor'   r   r5   r   r'   Zpointerr   r7   r9   r   r'   r   r   r   test_pointer_to_mapping  s    
z$TestResource.test_pointer_to_mappingc                 C   s<   t jdddgiid}t  }|jd|djdks8td S )Nr1   r2   re   r   z
/foo/bar/0r   r   r   r   r   r   test_pointer_to_array  s    
z"TestResource.test_pointer_to_arrayc                 C   s:   ddi}t j|d}t  }|jd|dj|ks6td S )Nr1   r   r   r   r   r   )r9   r   r   r'   r   r   r   test_root_pointer  s    
zTestResource.test_root_pointerc                 C   s(   ddi}t |t |tjdks$td S )Nr1   r2   r<   )r   r5   r   rW   r7   )r9   r   r   r   r   test_opaque  s
    
zTestResource.test_opaqueN)r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   C  s   


r   c                   @   s   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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 )&TestResolverc                 C   s>   t jddid}td|i }|d}|j|jks:td S )Nr1   r   r   r=   r   )r9   r   r'   resolvedr   r   r   test_lookup_exact_uri  s    
z"TestResolver.test_lookup_exact_uric                 C   sH   t ddddgd}|t  }| d}|jdddksDtd S )Nr~   zhttp://example.com/arE   rF   rq   r?   r   r   r'   r   r   r7   r9   rs   r:   r   r   r   r   test_lookup_subresource  s    
z$TestResolver.test_lookup_subresourcec                 C   s>   t dddid}|t  }| d}|jdks:td S )Nr~   r1   rE   rv   zhttp://example.com/#foor   r   r   r   r   test_lookup_anchor_with_id  s    
z'TestResolver.test_lookup_anchor_with_idc                 C   s@   t dddii}t d| }|d}|jdks<td S )Nr   r1   rE   r3   zurn:example#foo)r?   r   r   r6   r'   r   r   r7   )r9   rs   r'   r   r   r   r   test_lookup_anchor_without_id  s    
z*TestResolver.test_lookup_anchor_without_idc              	   C   sJ   t   }d}ttj}|| W 5 Q R X |jtj|dksFtd S )Nz!http://example.com/does/not/existr   )	r   r'   rK   rL   r   Unresolvabler   rM   r7   )r9   r'   r   rN   r   r   r   test_lookup_unknown_reference  s
    
z*TestResolver.test_lookup_unknown_referencec              	   C   sr   t di i}td|i }d}ttj}|| W 5 Q R X |j	tj
d|dks\tt|j	dksntd S )Nr1   r=   zhttp://example.com/1#/foo/barz/foo/barr   r   z,'/foo/bar' does not exist within {'foo': {}})r   r5   r   r'   rK   rL   r   r   r   rM   PointerToNowherer7   r\   r9   r   r'   r   rN   r   r   r    test_lookup_non_existent_pointer  s    z-TestResolver.test_lookup_non_existent_pointerc              	   C   sd   t ddddg}td|i }d}ttj}|| W 5 Q R X |j	tj
d|dks`td S )	Nr   r      rh   r=   zhttp://example.com/1#/10z/10r   )r   r5   r   r'   rK   rL   r   r   r   rM   r   r7   r   r   r   r   /test_lookup_non_existent_pointer_to_array_index  s    z<TestResolver.test_lookup_non_existent_pointer_to_array_indexc                 C   s.   t  tdi i}|dji ks*td S )Nr   #/)r   r   r   r5   r   r   r7   )r9   r'   r   r   r   #test_lookup_pointer_to_empty_string  s    z0TestResolver.test_lookup_pointer_to_empty_stringc              	   C   s\   t di i}t |}tjtjdd}|d W 5 Q R X |j	tj
d|dksXtd S )Nr1   z+^'/' does not exist within {'foo': {}}.*'#'r   r   /r   )r   r5   r   r   rK   rL   r   r   r   rM   r   r7   )r9   r   r'   rN   r   r   r   0test_lookup_non_existent_pointer_to_empty_string  s    z=TestResolver.test_lookup_non_existent_pointer_to_empty_stringc              	   C   s   t di i}t d| }|d}|j|jks:td}t	t
j}|| W 5 Q R X dt|jksrt|jt
jd|ddkstd S )Nr   r3   zurn:example#noSuchAnchorz'noSuchAnchor' does not existZnoSuchAnchor)r   r   rw   )r?   r   r   r6   r'   r   r   r7   rK   rL   r   r   r\   rM   ZNoSuchAnchor)r9   rs   r'   r   r   rN   r   r   r   test_lookup_non_existent_anchor  s    
z,TestResolver.test_lookup_non_existent_anchorc              	   C   sl   t  tdddid}|d}|jdks4ttt	j
}|d W 5 Q R X dt|jkshtd S )Nr~   r2   rE   rF   z	#/foo/barz#foo/barz '#/foo/bar')r   r   r?   r   r   r   r7   rK   rL   r   ZInvalidAnchorr\   rM   )r9   r'   ZvalidrN   r   r   r   *test_lookup_invalid_JSON_pointerish_anchor  s    	
z7TestResolver.test_lookup_invalid_JSON_pointerish_anchorc                    sD   t jddid t fddd }|d}|j jks@td S )Nr1   r   r   c                    s    S rb   r   r-   rJ   r   r   r   1  r   z=TestResolver.test_lookup_retrieved_resource.<locals>.<lambda>r   r~   r   )r9   r'   r   r   rJ   r   test_lookup_retrieved_resource/  s    
z+TestResolver.test_lookup_retrieved_resourcec              	   C   s^   d}t td}ttj || W 5 Q R X | }ttj |	| W 5 Q R X dS )zL
        Unretrievable exceptions are also wrapped in Unresolvable.
        r~   r   N)
r   r/   rK   rL   r   r   r   r'   r   r   )r9   r.   r:   r'   r   r   r   %test_lookup_failed_retrieved_resource5  s    
z2TestResolver.test_lookup_failed_retrieved_resourcec                    sj   t jddid}|g  fdd}t|d }|d}|j|jksJt|jd}|j|jksftdS )	zk
        A (custom-)retrieved resource is added to the registry returned by
        looking it up.
        r1   r   r   c                    s      S rb   popr-   oncer   r   r   L  s    zKTestResolver.test_repeated_lookup_from_retrieved_resource.<locals>.retriever   r~   Nr   r9   r   r   r'   r   r   r  r   ,test_repeated_lookup_from_retrieved_resourceD  s    
z9TestResolver.test_repeated_lookup_from_retrieved_resourcec                    sj   t jddid}|g  fdd}t|d }|d}|j|jksJt|jd}|j|jksftd S )	Nr1   r   r   c                    s      S rb   r  r-   r  r   r   r   Z  s    zRTestResolver.test_repeated_anchor_lookup_from_retrieved_resource.<locals>.retriever   r~   r   r   r	  r   r  r   3test_repeated_anchor_lookup_from_retrieved_resourceV  s    
z@TestResolver.test_repeated_anchor_lookup_from_retrieved_resourcec              	   C   s   t ddddigdgd}|t  }| }|d}|j|jksJttt	j
 |jd W 5 Q R X |jt |jd d }|d}|jddikstd S )Nr~   child/r	   
grandchildrq   r   r   )r?   r   r   r'   r   r   r7   rK   rL   r   r   r$   )r9   rs   r:   r'   r   subr   r   r   r   test_in_subresourceg  s&    


z TestResolver.test_in_subresourcec              	   C   s   t ddddigdgd}|t  }| }|d}|j|jksJttt	j
 |jd W 5 Q R X |jd}|jd}|jddikstd S )Nr~   r  r	   r  rq   #/children/0)r?   r   r   r'   r   r   r7   rK   rL   r   r   )r9   rs   r:   r'   r   r   r   r   r   r   test_in_pointer_subresource  s"    

z(TestResolver.test_in_pointer_subresourcec           	      C   s  t ddddigdgd}t dddigd}||gt  }| }|d}|jd}|jd}|jd}t|j d	|jjfd
|jjfd|jjfgkstt|j d
|jjfd|jjfgkstt|j d|jjfgkstt|j g kstd S )Nr~   r  r	   r  rq   zhttp://example.com/twoz
two-child/r  z#http://example.com/child/grandchildzhttp://example.com/child/)	r?   r   r   r'   r   r   Zdynamic_scope	_registryr7   )	r9   rB   rC   r:   r'   r   r   r   r   r   r   r   test_dynamic_scope  s@    









zTestResolver.test_dynamic_scopeN)r   r   r   r   r   r   r   r   r   r   r   r   r  r  r  r  r
  r  r  r  r  r   r   r   r   r     s$   r   c                   @   s\   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d ZdS )TestSpecificationc                 C   s^   t ddd dd dd dd d}|jdd	id
}|tdd	i|dksJt| dksZtd S )Nr   c                 S   s   dS r   r   r   r   r   r   r     r   z8TestSpecification.test_create_resource.<locals>.<lambda>c                 S   s   g S rb   r   r   r   r   r   r     r   c                 S   s   g S rb   r   r   r   r   r   r     r   c                 S   s   |S rb   r   r%   r   r   r   r     r   r(   r1   r   r   r<   r   )r   r   r   r7   rx   r   r   r   r   test_create_resource  s    	z&TestSpecification.test_create_resourcec                 C   s"   ddi}t |}|tkstd S NrP   rQ   )r   detectr   r7   r9   rS   r   r   r   r   test_detect_from_json_schema  s    
z.TestSpecification.test_detect_from_json_schemac              	   C   s*   t tj tddi W 5 Q R X d S Nr1   r2   rK   rL   r   r   r   r  rc   r   r   r   +test_detect_with_no_discernible_information  s    z=TestSpecification.test_detect_with_no_discernible_informationc              	   C   s*   t tj tddi W 5 Q R X d S )NrP   %   r  rc   r   r   r   test_detect_with_non_URI_schema  s    z1TestSpecification.test_detect_with_non_URI_schemac                 C   s"   t jddi}|t jkstd S r  r   rW   r  r7   r9   r   r   r   r   7test_detect_with_no_discernible_information_and_default  s    zITestSpecification.test_detect_with_no_discernible_information_and_defaultc                 C   s$   ddi}t j|}|tks td S r  )r   rW   r  r   r7   r  r   r   r   test_detect_unneeded_default  s    z.TestSpecification.test_detect_unneeded_defaultc              	   C   s&   t tj td W 5 Q R X d S NTr  rc   r   r   r   test_non_mapping_detect  s    z)TestSpecification.test_non_mapping_detectc                 C   s   t d}|t kstd S r#  )r?   r  r7   r   r   r   r   $test_non_mapping_detect_with_default  s    
z6TestSpecification.test_non_mapping_detect_with_defaultc                 C   s"   t jddi}|t jkstd S r  r  r   r   r   r   test_detect_with_fallback  s    z+TestSpecification.test_detect_with_fallbackc                 C   s   t tdkstd S )Nz&<Specification name='id-and-children'>)r   r?   r7   rc   r   r   r   r     s    
zTestSpecification.test_reprN)r   r   r   r  r  r  r  r!  r"  r$  r%  r&  r   r   r   r   r   r    s   r  c                   @   sb   e Zd Zddiddde gZejdedd Zejdedd	 Z	ejded
d Z
dS )TestOpaqueSpecificationr1   r2   Tr  thingc                 C   s   t j|dkstdS )z/
        An arbitrary thing has no ID.
        N)r   rW   r)   r7   r9   r(  r   r   r   
test_no_id   s    z"TestOpaqueSpecification.test_no_idc                 C   s   t tj|g kstdS )z9
        An arbitrary thing has no subresources.
        N)r   r   rW   r*   r7   r)  r   r   r   test_no_subresources  s    z,TestOpaqueSpecification.test_no_subresourcesc                 C   s   t tj|g kstdS )z4
        An arbitrary thing has no anchors.
        N)r   r   rW   r+   r7   r)  r   r   r   test_no_anchors  s    z'TestOpaqueSpecification.test_no_anchorsN)r   r   r   objectZTHINGSrK   markparametrizer*  r+  r,  r   r   r   r   r'    s   

r'  clsc              	   C   s.   t jtdd G dd d| }W 5 Q R X d S )Nz(?i)subclassingr   c                   @   s   e Zd ZdS )z"test_nonsubclassable.<locals>.BoomN)r   r   r   r   r   r   r   Boom   s   r1  )rK   rL   r   )r0  r1  r   r   r   test_nonsubclassable  s    r2  )Zrpdsr   rK   Zreferencingr   r   r   r   r   Zreferencing.jsonschemar   r?   r/   r0   r   r   r  r'  r.  r/  r   r2  r   r   r   r   <module>   s6       %w  :