o
    QDi7                     @   s  d dl Z d dlZd dlZd dlZd dlmZ d dlmZ d dlZd dl	Z	d dl
mZmZmZ dZdddd	d
dd	giZddddidd
dd	ddd	ddd	ddd	gg ddgdgddgddgdZdd Zdd Zdd Zdd Zejjejjejd k d!d"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,ej-dKg dLdMdN Z.ej-dKg dOdPdQ Z/ejj0dRd"dSdT Z1ejj0dRd"dUdV Z2dWdX Z3dYdZ Z4d[d\ Z5d]d^ Z6ej-d_d`dagdbdc Z7ddde Z8dfdg Z9dhdi Z:djdk Z;dldm Z<dndo Z=dpdq Z>dS )r    N)Decimal)import_module)FieldFrictionlessExceptionSchemazLhttps://raw.githubusercontent.com/frictionlessdata/frictionless-py/master/%sfieldsidstringnametypeheightintegerrequiredT)r   r   constraintsnumberager   
occupation -nullr   resourcer   r   	reference)r   missingValues
primaryKeyforeignKeysc                   C   s4   t tsJ t tsJ t dsJ t dsJ d S )Ndata/schema-valid-full.jsondata/schema-valid-simple.json)r   DESCRIPTOR_MINDESCRIPTOR_MAX r#   r#   b/var/www/Datamplify/venv/lib/python3.10/site-packages/frictionless/schema/__spec__/test_general.pytest_schema'   s   r%   c                   C   s8   t t tg  W d    d S 1 sw   Y  d S N)pytestraisesr   r   r#   r#   r#   r$   "test_schema_extract_metadata_error.   s   
"r)   c                   C   s   t t tks
J d S r&   )r   r"   to_descriptorr#   r#   r#   r$   test_schema_descriptor3   s   r+   c                  C   sX   d} t | }tj| dd}t|}W d    n1 sw   Y  | |ks*J d S )Nr    zutf-8)encoding)r   ioopenjsonloadr*   )pathschemafile
descriptorr#   r#   r$   test_schema_descriptor_path7   s   r5   )   
   zpytest-vcr bug in Python3.8/9)reasonc                  C   s2   t d } t| }t|  }| |ksJ d S )Nzdata/schema.json)BASEURLr   requestsgetr/   r*   )urlr2   r4   r#   r#   r$   test_schema_descriptor_url?   s   r=   c                  C   P   t t} g d}dtddddg}| |\}}||ksJ t|dks&J d S )N)r	   10.01r	   r	   r	         $@      r   r"   r   
read_cellslenr2   sourcetargetcellsnotesr#   r#   r$   test_schema_read_cellsH      rL   c                  C   sF   t t} g d}g d}| |\}}||ksJ t|dks!J d S )N)r	   r   r   r	   r   )r	   NNr	   NrC   )r   r"   rE   rF   rG   r#   r#   r$   "test_schema_read_cells_null_valuesQ   s   rN   c                  C   sP   t t} g d}dtdddd g}| |\}}||ksJ t|dks&J d S )N)r	   r?   r@   r	   r	   rA   rB   rC   rD   rG   r#   r#   r$    test_schema_read_cells_too_shortZ   rM   rO   c                  C   r>   )N)r	   r?   r@   r	   r	   r	   r	   rA   rB   rC   rD   rG   r#   r#   r$   test_schema_read_cells_too_longc   rM   rP   c                  C   s^   t t} g d}g d}| |\}}||ksJ |d ddiks#J |d ddiks-J d S )N)r	   
notdecimalz10.6r	   r	   )r	   NNr	   r	   rB   r   ztype is "number/default"   ztype is "integer/default")r   r"   rE   rG   r#   r#   r$   !test_schema_read_cells_wrong_typel   s   rS   c                   C   s.   t tjdgks
J t tjg dksJ d S )Nr   r   )r   r!   missing_valuesr"   r#   r#   r#   r$   test_schema_missing_valuesv   s   rU   c                  C   s,   ddg} dd t tjD }| |ksJ d S )Nr   r   c                 S   s   g | ]}|j qS r#   )r   ).0fieldr#   r#   r$   
<listcomp>}   s    z&test_schema_fields.<locals>.<listcomp>)r   r!   r   )expectactualr#   r#   r$   test_schema_fields{   s   r[   c                  C   s4   t t} | djdksJ | djdksJ d S Nr   r   )r   r!   	get_fieldr   r2   r#   r#   r$   test_schema_get_field   s   r_   c                  C   d   t t} tt}| d W d    n1 sw   Y  |jj}|jdks)J |j	dks0J d S Nbadschema-errorzfield "bad" does not exist)
r   r!   r'   r(   r   r]   valueerrorr   noter2   excinfore   r#   r#   r$   %test_schema_get_field_error_not_found      ri   c                  C   sL   t t} | dd | dd | djdksJ | djdks$J d S )Nr   r   r   )r   r!   set_field_typer]   r   r^   r#   r#   r$   test_schema_update_field   s
   rl   c                  C   s6   t t} | dsJ | dsJ | drJ d S )Nr   r   	undefined)r   r!   	has_fieldr^   r#   r#   r$   test_schema_has_field   s   ro   c                  C   s*   t t} | dsJ | jdgksJ d S )Nr   r   )r   r!   remove_fieldfield_namesr^   r#   r#   r$   test_schema_remove_field   s   rr   c                  C   r`   ra   )
r   r!   r'   r(   r   rp   rd   re   r   rf   rg   r#   r#   r$   (test_schema_remove_field_error_not_found   rj   rs   c                   C   s   t tjddgksJ d S r\   )r   r!   rq   r#   r#   r#   r$   test_schema_field_names   s   rt   c                   C   s*   t tjg ks	J t tjdgksJ d S )Nr   )r   r!   primary_keyr"   r#   r#   r#   r$   test_schema_primary_key   s   rv   c                   C   s,   t tjg ks	J t tjtd ksJ d S )Nr   )r   r!   foreign_keysr"   r#   r#   r#   r$   test_schema_foreign_keys   s   rx   c                  C   s8   t  } | tddd | d}|jdksJ d S )Nr   r	   r
   )r   	add_fieldr   from_descriptorrp   r   )r2   rW   r#   r#   r$   !test_schema_add_then_remove_field   s   
r{   c                  C   sl   ddddddgdgdgddgddgd	} t | }|jdgks$J |jdgddgddgks4J d S 
Nr   r	   r
   	parent_idr   r   r   r   )r   r   r   r   ru   rw   r4   r2   r#   r#   r$   )test_schema_primary_foreign_keys_as_array   s   
r   c                  C   sf   ddddddgddddddgd	} t | }|jdgks!J |jdgddgddgks1J d S r|   r~   r   r#   r#   r$   *test_schema_primary_foreign_keys_as_string   s   
r   rH   )r    r   zdata/schema-valid-pk-array.jsonzdata/schema-valid-fk-array.jsonc                 C   s   t | sJ d S r&   r   )rH   r#   r#   r$   test_schema_metadata_valid   s   
r   )zdata/schema-invalid-empty.jsonz"data/schema-invalid-pk-string.jsonz!data/schema-invalid-pk-array.jsonz"data/schema-invalid-fk-string.jsonz(data/schema-invalid-fk-no-reference.jsonz!data/schema-invalid-fk-array.jsonz,data/schema-invalid-fk-string-array-ref.jsonz,data/schema-invalid-fk-array-string-ref.jsonc                 C   sZ   t t}t|  W d    n1 sw   Y  |jj}|jdks$J |jdks+J d S )Nrc   descriptor is not validr'   r(   r   r   rd   re   r   rf   )rH   rh   re   r#   r#   r$   test_schema_metadata_not_valid   s   
r   z
issue-1222c                  C   s    t d} t|  dksJ d S )Nz(data/schema-invalid-multiple-errors.jsonrC   )r   rF   list_metadata_errorsr^   r#   r#   r$   .test_schema_metadata_not_valid_multiple_errors  s   r   c                  C   sN   t t} td W d    n1 sw   Y  | jj}t|dks%J d S )Nz)data/schema-invalid-pk-is-wrong-type.jsonr6   )r'   r(   r   r   rd   reasonsrF   )rh   r   r#   r#   r$   6test_schema_metadata_not_valid_multiple_errors_with_pk  s
   
r   c                  C   sf   t t} tddddgi W d    n1 sw   Y  | jj}|jdks*J |jdks1J d S )Nr   r   otherr
   field-errorz#field type "other" is not supportedr   rh   re   r#   r#   r$   "test_schema_metadata_error_message  s   r   c                  C   s   t t} tdddddddgi W d    n1 sw   Y  | jj}| jj}|jdks2J |jd	ks9J |d
 jdksBJ |d
 j	dsLJ d S )Nr   r   booleanYesNo)
trueValuesfalseValuesr   r   formatrc   r   r   r   z,is not of type 'string' at property 'format')
r'   r(   r   r   rd   re   r   r   rf   countrh   re   r   r#   r#   r$   ,test_schema_metadata_error_bad_schema_format"  s"   r   c                  C   s8   t dddddddddgi} | djdksJ d S )	Nr   r   r	   Johnr   r   exampler   r   *   )r   r]   r   r^   r#   r#   r$   test_schema_valid_examples7  s   

r   c                  C   s   t t} tdddddgi W d    n1 sw   Y  | jj}| jj}|jdks/J |jdks6J |d jd	ks?J |d jd
ksHJ d S )Nr   r   r   rb   r   rc   r   r   r   z1example value "bad" for field "name" is not valid)	r'   r(   r   r   rd   re   r   r   rf   r   r#   r#   r$   test_schema_invalid_exampleC  s"   r   create_descriptor)F)Tc                 C   sz   t d}tg g g g d}| stdi |n	t|jdi |}|jg ks&J |jg ks-J |jg ks4J |jg ks;J d S )Nzfrictionless.helpers)r   rT   ru   rw   r#   )r   dictr   r   r   rT   ru   rw   )r   helpersoptionsr2   r#   r#   r$   %test_schema_standard_specs_propertiesX  s   r   c                  C   s\   dddddddddddddgi} t | }d}tt| t|t| ks,J d S )	Nr   test_1r	   defaultr   test_2test_3z
    {'fields': [{'name': 'test_1', 'type': 'string', 'format': 'default'},
                {'name': 'test_2', 'type': 'string', 'format': 'default'},
                {'name': 'test_3', 'type': 'string', 'format': 'default'}]}
    r   printreprtextwrapdedentstrip)r4   r2   expectedr#   r#   r$   test_schema_pprintl  s   


r   c                  C   s6   t d} d}tt|  t| t| ksJ d S )Nzdata/schema-valid.jsonar  
    {'fields': [{'name': 'id',
                 'type': 'integer',
                 'title': 'ID',
                 'description': 'The id.',
                 'constraints': {'required': True}},
                {'name': 'name',
                 'type': 'string',
                 'title': 'Name',
                 'description': 'The name.',
                 'constraints': {'required': True}},
                {'name': 'age',
                 'type': 'integer',
                 'title': 'Age',
                 'description': 'The age.',
                 'constraints': {'required': True}}],
     'primaryKey': ['id']}
    r   )r2   r   r#   r#   r$   #test_schema_pprint_with_constraints~  s   r   c                  C   &   dddddgi} t | }|sJ d S )Nr   myfielddatez%d/%m/%yr   r   r   r#   r#   r$   'test_schema_field_date_format_issue_177     r   c                  C   r   )Nr   r   timez%H:%M:%Sr   r   r   r#   r#   r$   'test_schema_field_time_format_issue_177  r   r   c                  C   sT   dddddddddddddgi} t | }|d |tdddd d S )	Nr   r   r	   r   r   r   r   test_4)r   rp   ry   r   rz   )r4   r%   r#   r#   r$   &test_schema_add_remove_field_issue_218  s   



r   c                  C   sl   t t} tdddidddgi W d    n1 sw   Y  | jj}|jdks-J |jdks4J d S )Nr   r   r   rb   r
   r   z!field type "bad" is not supportedr   r   r#   r#   r$   3test_schema_not_supported_type_issue_goodatbles_304  s   	r   c                  C   sf   t t} tt}d| d_W d    n1 sw   Y  |jj}|jdks*J |j	dks1J d S )Nr   r   r   z=Use "schema.set_field_type()" to update the type of the field)
r   r!   r'   r(   r   r]   r   rd   re   rf   rg   r#   r#   r$   <test_schema_updating_field_type_by_setter_warning_issue_1466  s   r   )?r-   r/   sysr   decimalr   	importlibr   r'   r:   frictionlessr   r   r   r9   r!   r"   r%   r)   r+   r5   markvcrskipifversion_infor=   rL   rN   rO   rP   rS   rU   r[   r_   ri   rl   ro   rr   rs   rt   rv   rx   r{   r   r   parametrizer   r   skipr   r   r   r   r   r   r   r   r   r   r   r   r   r   r#   r#   r#   r$   <module>   s    				
		
	



