o
    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   O/var/www/vscode/kcb/lib/python3.10/site-packages/referencing/tests/test_core.py<lambda>	   s   
 r   c                 C   s   |  dg S )Nchildrenr
   r   r   r   r   r   
   s    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 stdd | d d d D r||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resolversubresourcer   r   r   r      s
   
r   id_ofsubresources_of
anchors_inmaybe_in_subresourcec                 C   s   t d)zQ
    A retriever suitable for use in tests which expect it never to be used.
    z2This retrieve function expects to never be called!)RuntimeErrorurir   r   r   blow_up   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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}|| |u sJ 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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s+J dS )	zT
        Adding multiple resources to the registry is like adding each one.
        r   r2   r3   r   r   http://example.com/1http://example.com/foo/barr5   N)r   r6   ID_AND_CHILDRENr   with_resourcesr7   r9   onetwor:   r   r   r   test_with_resources-   s   z TestRegistry.test_with_resourcesc                 C   s8   d}t |dd}|t  }|t ||ksJ d S )Nzurn:example:resource   r	   r2   )r?   r   r   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s+J d S )Nzurn:example:onerE   rF   zurn:example:two)r?   r   r   r@   )r9   one_urirB   two_urirC   r:   r   r   r   test_matmul_many_resourcesH   s   z'TestRegistry.test_matmul_many_resourcesc                 C   s`   t jddid}ttj}|t   W d    n1 sw   Y  |jtj|dks.J d S )Nr2   r3   r   r   )r   r6   pytestraisesr   NoInternalIDr   value)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J d S )Nr4   $schema,https://json-schema.org/draft/2020-12/schemar<   )r   with_contentsr   r   )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J d S )Nr4   r2   r3   default_specification)r   rU   r   OPAQUEr   r6   )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sJ d S )N   c                 S      g | ]
}t |d difqS r2   r3   strr   ir   r   r   r   m       z)TestRegistry.test_len.<locals>.<listcomp>rY   )r   rU   ranger   r[   r!   )r9   totalr:   r   r   r   test_lenj   s   zTestRegistry.test_lenc                 C   s   t  rJ d S N)r   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J d S )Nc                 S   r^   r_   r`   rb   r   r   r   r   w   rd   z4TestRegistry.test_bool_not_empty.<locals>.<listcomp>   rY   )r   rU   re   r   r[   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J d S )Nc                 S   r^   r_   r`   rb   r   r   r   r   ~   rd   z*TestRegistry.test_iter.<locals>.<listcomp>   rY   c                 S   s   h | ]}t |qS r   r`   rb   r   r   r   	<setcomp>   s    z)TestRegistry.test_iter.<locals>.<setcomp>)r   rU   re   r   r[   setrl   r   r   r   	test_iter|   s
   "zTestRegistry.test_iterc                 C   s6   t ddi}d}t||i }|| |u sJ d S Nr2   r3   r4   )r   r6   r   crawlr8   r   r   r   'test_crawl_still_has_top_level_resource   s   z4TestRegistry.test_crawl_still_has_top_level_resourcec                 C   s   d}t d|ddgd}|t  }tt ||  W d    n1 s'w   Y  t |dd}| | |ks>J d S )N	urn:childurn:rootrE   rF   r	   r   )r?   r   r   rM   rN   LookupErrorrs   )r9   child_idrootr:   rW   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s%J d S )Nurn:barr2   rE   r	   r   r   )r?   r   r   rs   anchoridrP   r   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s$J d S )Nr   r2   rE   rv   r   )r?   r   r   r7   rs   r~   rP   r   r   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sJ d S rr   )r   r6   r   r7   r   r8   r   r   r   test_contents   s   zTestRegistry.test_contentsc                 C   sL   d}t d|d i}|t  }|| ||d    kr!|ks$J  J d S Nhttp://example.com/r	   #)r?   r   r   rG   r   r   r   #test_getitem_strips_empty_fragments   s   
,z0TestRegistry.test_getitem_strips_empty_fragmentsc                 C   sX   d}t d|d i}|t  }||||d   kr'd|d iks*J  J d S r   )r?   r   r   r   rG   r   r   r   $test_contents_strips_empty_fragments   s   

z1TestRegistry.test_contents_strips_empty_fragmentsc                 C   sV   t  }ttj}|d W d    n1 sw   Y  |jtjddks)J d S )Nr4   ref)r   rM   rN   r   NoSuchResourcer   rP   r9   r:   rQ   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s$J |j| ks-J d S )Nr   r2   r3   r4   r   )	r?   r   r   r7   r~   rP   r   r:   rs   )r9   r   r:   	retrievedr   r   r   test_crawled_anchor   s   z TestRegistry.test_crawled_anchorc                 C   sX   t  }ttj}|dd W d    n1 sw   Y  |jtjddks*J d S )Nr4   r2   r   )r   rM   rN   r   r   r~   rP   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s&J d S )Nr   r2   r3   )r=   r>   r=   r>   )r   r6   r?   r   r   r@   rs   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s+J dS )z
        Passing a `dict` to `Registry` gets converted to a `HashTrieMap`.

        So continuing to use the registry works.
        r   r2   r3   r=   r>   N)r   r6   r?   r   r   r7   rs   r@   rA   r   r   r   test_dict_conversion   s    z!TestRegistry.test_dict_conversionc                 C   sT   t  }ttj}|d  W d    n1 sw   Y  |jtjddks(J d S )Nzurn:bigboomr   )r   rM   rN   r   r   rP   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saJ d S )Nr   r2   r3   bazquuxr   rE   r=   r>   r=   http://example.com/bazhttp://example.com/foo/quuxr   )r   r2   r   )r   )
r   r6   r?   r   r   r7   rs   combiner   r   )	r9   rB   rC   threefourfirstsecondthirdfourthr   r   r   test_combine  sB   zTestRegistry.test_combinec                 C   s"   t ddi}|||u sJ 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:foor3   N)r   r   rl   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sBJ | |ksJJ d S )
Nr   r2   r3   r   r   r=   r>   r   r   )r   r6   r?   r   r   r7   r   rs   )	r9   rB   rC   r   r   r   r   rW   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sHJ |||t|d	d|fd
|fd|fgksaJ d S )Nr   r2   r3   r   r   c                 S      d S rh   r   r.   r   r   r   retrieveW     z@TestRegistry.test_combine_with_single_retrieve.<locals>.retriever=   r   http://example.com/2http://example.com/3r   r6   r?   r   r   r7   r   r@   r9   rB   rC   r   r   r   r   r   r   r   r   !test_combine_with_single_retrieveR  s>   	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sLJ |||t|dd	|fd
|fd|fgkseJ d S )Nr   r2   r3   r   r   c                 S   r   rh   r   r.   r   r   r   r   x  r   z@TestRegistry.test_combine_with_common_retrieve.<locals>.retriever   r=   r   r   r   r   r   r   r   !test_combine_with_common_retrieves  sJ   

	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 d    d S 1 sNw   Y  d S )Nr   r2   r3   r   r   c                 S   r   rh   r   r.   r   r   r   foo_retrieve  r   zDTestRegistry.test_combine_conflicting_retrieve.<locals>.foo_retrievec                 S   r   rh   r   r.   r   r   r   bar_retrieve  r   zDTestRegistry.test_combine_conflicting_retrieve.<locals>.bar_retriever   r=   r   r   zconflict.*retrievmatch)
r   r6   r?   r   r   r7   rM   rN   	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s!J d S )Nr   r2   r3   )r   r|   r   r|   )r   r6   r?   r   r   removerA   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s'J d S )Nr   r2   r3   r   r|   )r   r6   r?   r   r   r@   r   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s-J d S )Nr   r   r2   r3   r   r|   )	r   r6   r?   r   r   r@   rs   r   r7   rA   r   r   r   test_remove_with_anchors  s    z%TestRegistry.test_remove_with_anchorsc                 C   sR   t tj}t d W d    n1 sw   Y  |jtjddks'J d S )Nzurn:doesNotExistr   )rM   rN   r   r   r   r   rP   )r9   rQ   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sJ d S )Nr2   r3   c                        S rh   r   r.   r2   r   r   r         z,TestRegistry.test_retrieve.<locals>.<lambda>r   r4   )r   r6   r   get_or_retrieverP   rl   r   r   r   test_retrieve  s   zTestRegistry.test_retrievec                    t   t ddi  fdd}t|d}|dj ksJ ttj |d W d    d S 1 s3w   Y  d S )Nr2   r3   c                    s   | dkr S t d)Nurn:succeedzOh no!)r   r.   r   r   r   r     s   z@TestRegistry.test_retrieve_arbitrary_exception.<locals>.retriever   r   urn:uhoh)	r   r6   r   r   rP   rM   rN   r   Unretrievabler9   r   r:   r   r   r   !test_retrieve_arbitrary_exception     
"z.TestRegistry.test_retrieve_arbitrary_exceptionc                    r   )Nr2   r3   c                    s   | dkr S t j| d)Nr   r   )r   r   r.   r   r   r   r     s   z=TestRegistry.test_retrieve_no_such_resource.<locals>.retriever   r   r   )	r   r6   r   r   rP   rM   rN   r   r   r   r   r   r   test_retrieve_no_such_resource  r   z+TestRegistry.test_retrieve_no_such_resourcec                 C   sN   dd }t |d}ttj |d W d    d S 1 s w   Y  d S )Nc                 S   s
   t i S rh   )r   from_contentsr.   r   r   r   r     s   
zKTestRegistry.test_retrieve_cannot_determine_specification.<locals>.retriever   r   )r   rM   rN   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J |dj|ks!J d S )Nr2   r3   r4   r   )r   r6   r   r0   r   rP   )r9   r2   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s$J d S )Nru   rE   rF   r   r   rv   )r?   r   r   r   r0   r7   rs   )r9   childrz   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sJ d S )Nr   http://example.com)base_urir   r   r6   r   r&   lookupr   )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sJ |dj|jks#J d S )Nr	   r   r   )r?   r   r   resolver_with_rootr   r   r9   rz   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J d S )Nr   r   )r   r6   r   r   r   r   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s!J t| dks+J d S )Nr   r2   r3   r=   r>   z"<Registry (2 uncrawled resources)>z<Registry (2 resources)>)r   r6   r?   r   r   r@   reprrs   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s"J d S )Nr   r2   r3   r=   r>   r5   z%<Registry (2 resources, 1 uncrawled)>)r   r6   r?   r   r   rs   r7   r   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J d S )Nr=   r   r5   z!<Registry (1 uncrawled resource)>)r   r7   r   r6   r   rl   r   r   r   test_repr_one_resource8  s
   
z#TestRegistry.test_repr_one_resourcec                 C   s   t t dks	J d S )Nz<Registry (0 resources)>)r   r   ri   r   r   r   test_repr_empty?  s   zTestRegistry.test_repr_emptyN)2__name__
__module____qualname__r;   rD   rH   rK   rR   rX   r\   rg   rj   rm   rq   rt   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   r   r   r1   "   s`    
	

&!'r1   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J d S )NrS   rT   r<   )r   r   r   r9   rV   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 d   dS 1 sw   Y  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.
        r2   r3   N)rM   rN   r   r   r   r   ri   r   r   r   2test_from_contents_with_no_discernible_informationI  s   "z?TestResource.test_from_contents_with_no_discernible_informationc                 C   0   t jdditjd}|t jddidksJ d S Nr2   r3   rY   r   r   r   r   r[   r6   r9   r   r   r   r   >test_from_contents_with_no_discernible_information_and_defaultS  
   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J d S )NrS   rT   rY   r<   )r   r   r   r[   r   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J d S )NTrY   r<   )r   r   r?   r   r   r   r   test_non_mapping_from_contentse  s   z+TestResource.test_non_mapping_from_contentsc                 C   r   r   r   r   r   r   r    test_from_contents_with_fallbacko  r   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s!J d S )N c                 S      dS Nurn:fixedIDr   r   r   r   r   r   y  r   zATestResource.test_id_delegates_to_specification.<locals>.<lambda>c                 S      g S rh   r   r   r   r   r   r   z  r   c                 S   r   rh   r   r   r   r   r   r   {  r   c                 S      |S rh   r   r$   r   r   r   r   }  r   r(   r2   r   r<   r   )r   r   r   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J d S r   )r?   r   r   )r9   r/   rz   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sJ 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subresourcesr   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sJ d S )NrS   rT   r   )r?   r   r   r   r   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s-J d S )	Nr      r   )r2   r3   r   r2   r   r3   r   )r?   r   r   r   r   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sJ d S )Nr2   r   r   z/foor&   r   r6   r   r&   pointerr   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sJ d S )Nr2   r3   rk   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sJ d S )Nr2   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J d S )Nr2   r3   r<   )r   r6   r   r[   )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J d S )Nr2   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s"J d S )Nr   zhttp://example.com/arE   rF   rw   r?   r   r   r&   r   r   r9   rz   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J d S )Nr   r2   rE   r}   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J d S )Nr   r2   rE   r4   zurn:example#foo)r?   r   r   r7   r&   r   r   )r9   rz   r&   r  r   r   r   test_lookup_anchor_without_id  s   
z*TestResolver.test_lookup_anchor_without_idc                 C   s^   t   }d}ttj}|| W d    n1 sw   Y  |jtj|dks-J d S )Nz!http://example.com/does/not/existr   )r   r&   rM   rN   r   Unresolvabler   rP   )r9   r&   r   rQ   r   r   r   test_lookup_unknown_reference  s   
z*TestResolver.test_lookup_unknown_referencec                 C   s   t di i}td|i }d}ttj}|| W d    n1 s'w   Y  |j	tj
d|dks8J t|j	dksAJ d S )Nr2   r=   zhttp://example.com/1#/foo/barz/foo/barr   r   z,'/foo/bar' does not exist within {'foo': {}})r   r6   r   r&   rM   rN   r   r
  r   rP   PointerToNowherera   r9   r   r&   r   rQ   r   r   r    test_lookup_non_existent_pointer  s   z-TestResolver.test_lookup_non_existent_pointerc                 C   st   t g d}td|i }d}ttj}|| W d    n1 s'w   Y  |j	tj
d|dks8J d S )N)r   r      rn   r=   zhttp://example.com/1#/10z/10r  )r   r6   r   r&   rM   rN   r   r
  r   rP   r  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J d S )Nr   #/)r   r   r   r6   r   r   )r9   r&   r   r   r   #test_lookup_pointer_to_empty_string  s   z0TestResolver.test_lookup_pointer_to_empty_stringc                 C   sp   t di i}t |}tjtjdd}|d W d    n1 s%w   Y  |j	tj
d|dks6J d S )Nr2   z+^'/' does not exist within {'foo': {}}.*'#'r   r  /r  )r   r6   r   r   rM   rN   r   r
  r   rP   r  )r9   r   r&   rQ   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J d}tt	j
}|| W d    n1 s5w   Y  dt|jv sCJ |jt	jd|ddksPJ d S )Nr   r4   zurn:example#noSuchAnchorz'noSuchAnchor' does not existnoSuchAnchor)r   r   r~   )r?   r   r   r7   r&   r   r   rM   rN   r   r
  ra   rP   NoSuchAnchor)r9   rz   r&   r  r   rQ   r   r   r   test_lookup_non_existent_anchor  s   
z,TestResolver.test_lookup_non_existent_anchorc                 C   s   t  tdddid}|d}|jdksJ ttj	}|d W d    n1 s0w   Y  dt
|jv s>J d S )Nr   r3   rE   rF   z	#/foo/barz#foo/barz '#/foo/bar')r   r   r?   r   r   r   rM   rN   r   InvalidAnchorra   rP   )r9   r&   validrQ   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 J d S )Nr2   r   r   c                    r   rh   r   r.   rL   r   r   r   1  r   z=TestResolver.test_lookup_retrieved_resource.<locals>.<lambda>r   r   r   )r9   r&   r  r   rL   r   test_lookup_retrieved_resource/  s   
z+TestResolver.test_lookup_retrieved_resourcec                 C   s   d}t td}ttj || W d   n1 sw   Y  | }ttj |	| W d   dS 1 s=w   Y  dS )zL
        Unretrievable exceptions are also wrapped in Unresolvable.
        r   r   N)
r   r0   rM   rN   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s%J |jd}|j|jks3J dS )	zk
        A (custom-)retrieved resource is added to the registry returned by
        looking it up.
        r2   r   r   c                          S rh   popr.   oncer   r   r   L     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s%J |jd}|j|jks3J d S )	Nr2   r   r   c                    r  rh   r  r.   r!  r   r   r   Z  r#  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s%J ttj	 |jd W d    n1 s<w   Y  |j
t |jd d }|d}|jddiks]J d S )Nr   child/r	   
grandchildrw   r   r   )r?   r   r   r&   r   r   rM   rN   r   r
  r#   )r9   rz   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s%J ttj	 |jd W d    n1 s<w   Y  |jd}|jd}|jddiksVJ d S )Nr   r'  r	   r(  rw   #/children/0)
r?   r   r   r&   r   r   rM   rN   r   r
  )r9   rz   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sVJ t|j d
|jjfd|jjfgkskJ t|j d|jjfgks{J t|j g ksJ d S )Nr   r'  r	   r(  rw   zhttp://example.com/twoz
two-child/r+  z#http://example.com/child/grandchildzhttp://example.com/child/)r?   r   r   r&   r   r   dynamic_scope	_registry)	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s%J | dks-J d S )Nr   c                 S   r   r   r   r   r   r   r   r     r   z8TestSpecification.test_create_resource.<locals>.<lambda>c                 S   r   rh   r   r   r   r   r   r     r   c                 S   r   rh   r   r   r   r   r   r     r   c                 S   r   rh   r   r$   r   r   r   r     r   r(   r2   r   r   r<   r   )r   r   r   r   r   r   r   r   test_create_resource  s   	z&TestSpecification.test_create_resourcec                 C   s"   ddi}t |}|tksJ d S NrS   rT   )r   detectr   r9   rV   r   r   r   r   test_detect_from_json_schema  s   
z.TestSpecification.test_detect_from_json_schemac                 C   @   t tj tddi W d    d S 1 sw   Y  d S Nr2   r3   rM   rN   r   r   r   r3  ri   r   r   r   +test_detect_with_no_discernible_information     "z=TestSpecification.test_detect_with_no_discernible_informationc                 C   r6  )NrS   %   r8  ri   r   r   r   test_detect_with_non_URI_schema  r:  z1TestSpecification.test_detect_with_non_URI_schemac                 C   "   t jddi}|t ju sJ d S r7  r   r[   r3  r9   r   r   r   r   7test_detect_with_no_discernible_information_and_default     zITestSpecification.test_detect_with_no_discernible_information_and_defaultc                 C   s$   ddi}t j|}|tksJ d S r2  )r   r[   r3  r   r4  r   r   r   test_detect_unneeded_default  s   z.TestSpecification.test_detect_unneeded_defaultc                 C   s<   t tj td W d    d S 1 sw   Y  d S NTr8  ri   r   r   r   test_non_mapping_detect  s   "z)TestSpecification.test_non_mapping_detectc                 C   s   t d}|t u sJ d S rC  )r?   r3  r?  r   r   r   $test_non_mapping_detect_with_default  s   
z6TestSpecification.test_non_mapping_detect_with_defaultc                 C   r=  r7  r>  r?  r   r   r   test_detect_with_fallback  rA  z+TestSpecification.test_detect_with_fallbackc                 C   s   t tdksJ d S )Nz&<Specification name='id-and-children'>)r   r?   ri   r   r   r   r     s   zTestSpecification.test_reprN)r   r   r   r1  r5  r9  r<  r@  rB  rD  rE  rF  r   r   r   r   r   r0    s    r0  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 )TestOpaqueSpecificationr2   r3   Tr;  thingc                 C   s   t j|du s
J dS )z/
        An arbitrary thing has no ID.
        N)r   r[   r)   r9   rH  r   r   r   
test_no_id   s   z"TestOpaqueSpecification.test_no_idc                 C      t tj|g ksJ dS )z9
        An arbitrary thing has no subresources.
        N)r   r   r[   r*   rI  r   r   r   test_no_subresources     z,TestOpaqueSpecification.test_no_subresourcesc                 C   rK  )z4
        An arbitrary thing has no anchors.
        N)r   r   r[   r+   rI  r   r   r   test_no_anchors  rM  z'TestOpaqueSpecification.test_no_anchorsN)r   r   r   objectTHINGSrM   markparametrizerJ  rL  rN  r   r   r   r   rG    s    

rG  clsc                 C   sD   t jtdd G dd d| }W d    d S 1 sw   Y  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    rT  )rM   rN   r   )rS  rT  r   r   r   test_nonsubclassable  s   "rU  )rpdsr   rM   referencingr   r   r   r   r   referencing.jsonschemar   r?   r0   r1   r   r  r0  rG  rQ  rR  r  rU  r   r   r   r   <module>   s8        %w  :