o
    mi                      @  sJ  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mZmZ dd	lmZ ddlZe
r^dd
lmZmZmZ ddlmZ ddlZddlmZ ddlZd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&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Sequence)Schema)_VALIDATORSzhttp://localhost:1234z[\W\- ]+c                  C  sB   t jd} | d u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   Q/home/kim/smarthome/.venv/lib/python3.10/site-packages/jsonschema/tests/_suite.py_find_suite"   s   r   c                   @  s4   e Zd ZU eedZded< dddZdd
dZdS )Suite)factoryr   _rootrunnerpyperf.Runnerc                 C  s,   t  D ]\}}| j|dj||d qd S )N)name)r    	Validator)r   itemsversion	benchmark)selfr    r"   r#   r   r   r   r&   :   s   zSuite.benchmarkreturnVersionc                 C  sX   t | }||j}tj|}t jt| j	d ||d|d}t
|| j	d | |dS )Nremotes)r   r"   uri)Zdefault_specificationtests)r"   pathr*   )r   ZID_OFZMETA_SCHEMAreferencingr   Zspecification_withr
   Zwith_contents
remotes_inr   r)   )r'   r"   r#   r+   specificationregistryr   r   r   r%   A   s   zSuite.versionNr    r!   )r(   r)   )	__name__
__module____qualname__r   r   r   __annotations__r&   r%   r   r   r   r   r   4   s   
 
r   c                   @  s^   e Zd ZU ded< ded< ded< dd ZdddZdddZdddZdd ZdddZ	dS )r)   r   _path%referencing.jsonschema.SchemaRegistry_remotesstrr"   c                 K  s"   |   D ]
}|jdi | qd S Nr   )casesr&   )r'   kwargscaser   r   r   r&   Y   s   zVersion.benchmarkr(   Iterable[_Case]c                 C     | j | jddS )N*.jsonpaths	_cases_inr7   globr'   r   r   r   r<   ]      zVersion.casesc                 C  r@   )Nzoptional/format/*.jsonrB   rD   rG   r   r   r   format_cases`   rH   zVersion.format_casesc                 C  s   | j | jd | d gdS )Noptionalz.jsonrB   )rE   r7   )r'   r"   r   r   r   optional_cases_ofc   s   zVersion.optional_cases_ofc                   s     dd| j dd }dd  fdd|D D }t|tjf|}tt t	 |_
W d    |S 1 s9w   Y  |S )	Nr"   ZTest- c                 S  s   i | ]}|j |qS r   )r3   ).0methodr   r   r   
<dictcomp>h   s    z0Version.to_unittest_testcase.<locals>.<dictcomp>c                 3  s6    | ]}|D ]}|j D ]}|jdi  V  qqqd S r;   )r,   to_unittest_method)rN   groupr>   testr=   r   r   	<genexpr>j   s    z/Version.to_unittest_testcase.<locals>.<genexpr>)popr"   titlereplacetypeunittestZTestCaser   	Exception)_someone_save_us_the_module_of_the_callerr4   )r'   groupsr=   r"   methodsclsr   rT   r   to_unittest_testcasef   s   
	


zVersion.to_unittest_testcaserC   Iterable[Path]c                 c  s@    |D ]}t |jddD ]}tj|| |j| jdV  qqd S )Nzutf-8)encoding)r%   subjectr*   )r   loads	read_text_Case	from_dictstemr9   )r'   rC   r-   r>   r   r   r   rE   {   s   
zVersion._cases_inN)r(   r?   )r"   r:   r(   r?   )rC   ra   r(   r?   )
r3   r4   r5   r6   r&   r<   rI   rK   r`   rE   r   r   r   r   r)   Q   s   
 


r)   c                   @  sd   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< dZded< edd ZdddZd
S )rf   r)   r%   r:   rc   descriptionMapping[str, Any] | boolschemazlist[_Test]r,   N
str | Nonecommentr   zSequence[dict[str, str]]r0   c                   s6     |  fdd dD }| dd|i S )Nc              
     s6   g | ]}t d d   d  d  d d|qS )r%   rc   ri   rk   )r%   rc   case_descriptionrk   r*   r   )_Test)rN   rS   datar*   r   r   
<listcomp>   s    z#_Case.from_dict.<locals>.<listcomp>r,   r   )updaterV   )r_   rq   r*   r=   r,   r   rp   r   rg      s
   

z_Case.from_dictr    r!   c                 K  s,   | j D ]}||jt|jfi | qd S N)r,   Z
bench_funcfully_qualified_namer   validate_ignoring_errors)r'   r    r=   rS   r   r   r   r&      s   
z_Case.benchmarkr2   )	r3   r4   r5   r6   rm   r0   classmethodrg   r&   r   r   r   r   rf      s   
 
rf   r   r   r"   r:   r+   r(   Iterable[tuple[str, Schema]]c                 c  s~    |  dD ]6}t| }t|| dd}|dv r#|jdks1d|vr2|dr2||s2qt	 d| |fV  qd S )NrA   \/>   Zdraft3Zdraft4treez$schemaZdraft)
rglobr   rd   re   r:   relative_torX   rh   
startswithMAGIC_REMOTE_URL)r   r"   r+   Zeachrk   relativer   r   r   r/      s   

r/   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 )ro   r)   r%   r:   rc   rn   ri   r   rq   rj   rk   boolvalidr8   r9   Nrl   rm   c                 C  s   d| j  dS )Nz<Test >)ru   rG   r   r   r   __repr__   s   z_Test.__repr__c                 C  s   d | jj| j| j| jgS )Nz > )joinr%   r"   rc   rn   ri   rG   r   r   r   ru      s   z_Test.fully_qualified_namec                 C  s   d S rt   r   )rS   r   r   r   <lambda>   s    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 u s<tj	
dddkr>|S tj	
dddkrLt|S t||S )	Nc                   s   j di   d S r;   )validatethisr=   r'   r   r   fn   rH   z$_Test.to_unittest_method.<locals>.fnc                   sB   |  tj jdi   W d    d S 1 sw   Y  d S r;   )ZassertRaisesr   ValidationErrorr   r   r   r   r   r      s   "_rS   JSON_SCHEMA_DEBUG0ZJSON_SCHEMA_EXPECTED_FAILURES)r   r   _DELIMITERSsubrc   rn   ri   r3   r   r   r   rZ   ZexpectedFailureskip)r'   r   r=   r   reasonr   r   r   rQ      s    
z_Test.to_unittest_methodc                 K  sN   | | j |d| j| jd|}tjdddkrt  |j| jd d S )N)rk   r1   r   r   )instancer   )	Zcheck_schemark   r9   r   r   r   
breakpointr   rq   )r'   r#   r=   	validatorr   r   r   r      s   z_Test.validatec                 C  s<   t tj | j|d W d    d S 1 sw   Y  d S )N)r#   )r   r   r   r   )r'   r#   r   r   r   rv     s   "z_Test.validate_ignoring_errors)r3   r4   r5   r6   rm   r   propertyru   rQ   r   rv   r   r   r   r   ro      s    
 

ro   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.
       r3   )sys	_getframe	f_globalsr   r   r   r   r\     s   r\   )r   r   r"   r:   r+   r:   r(   rx   )*__doc__
__future__r   
contextlibr   	functoolsr   pathlibr   typingr   r   r   r   rer   rZ   attrsr   r	   r.   r
   Zreferencing.jsonschemacollections.abcr   r   r   r   ZpyperfZjsonschema.validatorsr   r   r   compiler   r   r   r)   rf   r/   ro   r\   r   r   r   r   <module>   sB    
4
"H