U
    kuf                     @  s4  d 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
mZ ddlZddlZddl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Ze
rdd
lmZmZ ddlZddlmZ ddlZedZ dd Z!eG dd dZ"eG dd dZ#eG dd dZ$eddG dd dZ%dd Z&dS )z=
Python representations of the JSON Schema Test Suite tests.
    )annotations)suppress)partial)Path)TYPE_CHECKINGAnyN)fieldfrozen)Registry)IterableMapping)_VALIDATORSz[\W\- ]+c                  C  sB   t jd} | d k	rt| S ttjjjd } |  s>td| S )NZJSON_SCHEMA_TEST_SUITEjsonzCan't find the JSON-Schema-Test-Suite directory. Set the 'JSON_SCHEMA_TEST_SUITE' environment variable or run the tests from alongside a checkout of the suite.)	osenvirongetr   
jsonschema__file__parentis_dir
ValueError)root r   ;/tmp/pip-unpacked-wheel-k94ccfep/jsonschema/tests/_suite.py_find_suite    s    r   c                   @  sV   e Zd ZU eedZded< eddZded< dd	 Zd
dddZ	ddddZ
dS )Suite)factoryr   _rootF)init%referencing.jsonschema.SchemaRegistry_remotesc                 C  s   | j dd}tjt|dg}t|d}t	|}d}d}t
 |tjjj||df|tjjj||dfgj| tjjd}t| d	| d S )
Nbinjsonschema_suiteremotesutf-8z?http://localhost:1234/locationIndependentIdentifierPre2019.jsonz>http://localhost:1234/locationIndependentIdentifierDraft4.json)contents)Zdefault_specificationr    )r   joinpathsys
executablestr
subprocesscheck_outputdecoder   loadsr
   Zwith_resourcesreferencingr   ZDRAFT7Zcreate_resourcepopZDRAFT4Zwith_contentsitemsZDRAFT202012object__setattr__)selfr"   argvr#   	resourcesliZli4registryr   r   r   __attrs_post_init__8   s.    
zSuite.__attrs_post_init__pyperf.Runnerrunnerc                 C  s,   t  D ]\}}| j|dj||d qd S )N)name)r;   	Validator)r   r0   version	benchmark)r3   r;   r<   r=   r   r   r   r?   W   s
    zSuite.benchmarkVersionreturnc                 C  s   t || jd | | jdS )Ntests)r<   pathr#   )r@   r   r    r3   r<   r   r   r   r>   ^   s
    zSuite.versionN)__name__
__module____qualname__r   r   r   __annotations__r    r8   r?   r>   r   r   r   r   r   2   s
   
r   c                   @  sr   e Zd ZU ded< ded< ded< dd Zd	d
ddZd	d
ddZdd	dddZdd Zdd	dddZ	dS )r@   r   _pathr   r    r)   r<   c                 K  s   |   D ]}|jf | qd S N)casesr?   )r3   kwargscaser   r   r   r?   n   s    zVersion.benchmarkzIterable[_Case]rA   c                 C  s   | j | jddS )Nz*.jsonpaths	_cases_inrJ   globr3   r   r   r   rL   r   s    zVersion.casesc                 C  s   | j | jddS )Nzoptional/format/*.jsonrO   rQ   rT   r   r   r   format_casesu   s    zVersion.format_cases)r<   rB   c                 C  s   | j | jd | d gdS )Noptionalz.jsonrO   )rR   rJ   rE   r   r   r   optional_cases_ofx   s    zVersion.optional_cases_ofc              	     sj     dd| j dd }dd  fdd|D D }t|tjf|}tt t	 |_
W 5 Q R X |S )	Nr<   ZTest- c                 S  s   i | ]}|j |qS r   )rF   ).0methodr   r   r   
<dictcomp>}   s    z0Version.to_unittest_testcase.<locals>.<dictcomp>c                 3  s0   | ](}|D ]}|j D ]}|jf  V  qq
qd S rK   )rC   to_unittest_method)rZ   grouprN   testrM   r   r   	<genexpr>   s     z/Version.to_unittest_testcase.<locals>.<genexpr>)r/   r<   titlereplacetypeunittestZTestCaser   	Exception)_someone_save_us_the_module_of_the_callerrG   )r3   groupsrM   r<   methodsclsr   r`   r   to_unittest_testcase{   s    
	
zVersion.to_unittest_testcasezIterable[Path])rP   rB   c                 c  s>   |D ]4}t |jddD ]}tj|| |j| jdV  qqd S )Nr$   )encoding)r>   subjectr#   )r   r-   	read_text_Case	from_dictstemr    )r3   rP   rD   rN   r   r   r   rR      s    zVersion._cases_inN)
rF   rG   rH   rI   r?   rL   rU   rW   rk   rR   r   r   r   r   r@   f   s   
r@   c                   @  s\   e Zd ZU ded< ded< ded< ded< ded	< d
Zded< edd ZddddZd
S )ro   r@   r>   r)   rm   descriptionMapping[str, Any] | boolschemazlist[_Test]rC   N
str | Nonecommentc                   s6     |  fdd dD }| f d|i S )Nc              
     s6   g | ].}t f  d   d  d  d d|qS )r>   rm   rr   rt   )r>   rm   case_descriptionrt   r#   )_Test)rZ   r_   datar#   r   r   
<listcomp>   s   z#_Case.from_dict.<locals>.<listcomp>rC   )updater/   )rj   rz   r#   rM   rC   r   ry   r   rp      s
    

z_Case.from_dictr9   r:   c                 K  s(   | j D ]}||jt|jf| qd S rK   )rC   Z
bench_funcfully_qualified_namer   validate_ignoring_errors)r3   r;   rM   r_   r   r   r   r?      s
    
z_Case.benchmark)rF   rG   rH   rI   rv   classmethodrp   r?   r   r   r   r   ro      s   

ro   F)reprc                   @  s   e Zd ZU ded< ded< ded< ded< ded< d	ed
< ded< ded< dZded< dd Zedd Zdd fddZdd Z	dd Z
dS )rx   r@   r>   r)   rm   rw   rr   r   rz   rs   rt   boolvalidr   r    Nru   rv   c                 C  s   d| j  dS )Nz<Test >)r}   rT   r   r   r   __repr__   s    z_Test.__repr__c                 C  s   d | jj| j| j| jgS )Nz > )joinr>   r<   rm   rw   rr   rT   r   r   r   r}      s    z_Test.fully_qualified_namec                 C  s   d S rK   r   )r_   r   r   r   <lambda>       z_Test.<lambda>c              	     s   j r fdd}n fdd}ddtdjtdjtdjg|_|}|d ksxtj	
dddkr||S tj	
dddkrt|S t||S d S )	Nc                   s   j f   d S rK   )validatethisrM   r3   r   r   fn   s    z$_Test.to_unittest_method.<locals>.fnc              	     s(   |  tj jf   W 5 Q R X d S rK   )assertRaisesr   ValidationErrorr   r   r   r   r   r      s    _r_   JSON_SCHEMA_DEBUG0ZJSON_SCHEMA_EXPECTED_FAILURES)r   r   _DELIMITERSsubrm   rw   rr   rF   r   r   r   re   ZexpectedFailureskip)r3   r   rM   r   reasonr   r   r   r]      s     
z_Test.to_unittest_methodc                 K  sN   | | j |f | j| jd|}tjdddkr<t  |j| jd d S )N)rt   r7   r   r   )instance)	Zcheck_schemart   r    r   r   r   
breakpointr   rz   )r3   r=   rM   Z	validatorr   r   r   r      s    z_Test.validatec              	   C  s&   t tj | j|d W 5 Q R X d S )N)r=   )r   r   r   r   )r3   r=   r   r   r   r~     s    z_Test.validate_ignoring_errors)rF   rG   rH   rI   rv   r   propertyr}   r]   r   r~   r   r   r   r   rx      s   


rx   c                   C  s   t djd S )a  
    The FQON of the module 2nd stack frames up from here.

    This is intended to allow us to dynamically return test case classes that
    are indistinguishable from being defined in the module that wants them.

    Otherwise, trial will mis-print the FQON, and copy pasting it won't re-run
    the class that really is running.

    Save us all, this is all so so so so so terrible.
       rF   )r'   	_getframe	f_globalsr   r   r   r   rg     s    rg   )'__doc__
__future__r   
contextlibr   	functoolsr   pathlibr   typingr   r   r   r   rer*   r'   re   attrsr   r	   r.   r
   Zreferencing.jsonschemacollections.abcr   r   ZpyperfZjsonschema.validatorsr   r   compiler   r   r   r@   ro   rx   rg   r   r   r   r   <module>   s<   
34!H