o
    ;Di!                     @   s  d dl mZ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
 d dlmZmZmZ d dlmZmZ edZedZdd	 Zejje du d
dZdd Zdd Zdd Zdd Zdd ZdBddZdd Zddgddgddgd d!gd"d#gd$d%gd&d'ggZ ed(d) Z!ed*d+ Z"ed,d- Z#ed.d/ Z$ed0d1 Z%eejj&e'd2d3d4d5 Z(ed6d7 Z)ed8d9 Z*d:d; Z+ed<d= Z,ed>d? Z-ed@dA Z.dS )C    )absolute_importprint_functiondivisionN	text_type)
fromgsheettogsheetappendgsheet)ieqget_env_vars_namedgspreaduuidc                  C   sd   t dd } | d urt j| r| S td}|d ur|S t jd}t j|r0t j|r0|S d S )NPETL_GCP_JSON_PATHPETL_GCP_CREDS_z&~/.config/gspread/service_account.json)osgetenvpathexistsr   
expanduserisfile)	json_path
json_props	user_path r   Q/var/www/Datamplify/venv/lib/python3.10/site-packages/petl/test/io/test_gsheet.py_get_gspread_credentials   s   r   a  SKIPPED. to/from gspread needs json credentials for testing.
In order to run google spreadsheet tests, follow the steps bellow: 
1. Create a json authorization file, following the steps described at
   http://gspread.readthedocs.io/en/latest/oauth2.html, and save to a local path
2. Point the envvar `PETL_GCP_JSON_PATH` to the json authorization file path
2. Or fill the properties inside the json authorization file in envrionment 
   variables named with prefix PETL_GCP_CREDS_: PETL_GCP_CREDS_project_id=petl
3. Or else save the file in one of the following paths:
      unix: ~/.config/gspread/service_account.json
   windows: %APPDATA%\gspread\service_account.json)reasonc                  C   s\   t  } t| tr
| S t| tr,t| dd}t|} | W  d    S 1 s'w   Y  d S )Nzutf-8)encoding)r   
isinstancedictr   openjsonload)creds	json_filer   r   r   _get_env_credentials1   s   


 r%   c               
   C   sf   t  } z| d u rt }W |S t| }W |S  tjjy2 } ztd|  W Y d }~d S d }~ww )Nz1SKIPPED. to/from gspread authentication error: %s)r%   r   service_accountservice_account_from_dict
exceptionsAPIErrorpytestskip)credentialsgspread_clientexr   r   r   _get_gspread_client<   s   
r/   c                  C   s$   t ddd} | d urt|  S g S )NPETL_GSHEET_EMAILF)remove_prefix)r   listvalues)emailsr   r   r   _get_env_sharing_emailsI   s   r5   c                  C   s(   d tt } t }t }| ||fS )Nztest-{})formatstrr   uuid4r/   r5   )filenamer-   r4   r   r   r   _get_gspread_test_paramsP   s   
r:   c           	   	   C   sV   t  \}}}t| ||||d}zt||||d}t|| W || d S || w )N	worksheetshare_emails)r<   
cell_range)r:   r   r   r
   del_spreadsheet)	table	sheetnamer>   expectedr9   r-   r4   	spread_idresultr   r   r   _test_to_fromg_sheetW   s   
rE   c              	   C   s   t  \}}}| d }| dd  }t|||||d}z#|D ]}	t|	|||dd qt|||dd}
t||
 W || d S || w )Nr      r;   T)r<   open_by_key)r:   r   r	   r   r
   r?   )
table_listrB   rA   r9   r-   r4   table1other_tablesrC   tableNrD   r   r   r   _test_append_from_gsheeth   s"   
rL   c                   C   s   t d d S )N   )timesleepr   r   r   r   teardown_function   s   rP   foobarA1B2C3D   random_stuff-in+_名字   éz3/4/2012F6c                   C   s"   t td d  d d td d   d S NrE   
TEST_TABLEr   r   r   r   test_tofromgsheet_01_basic      "ra   c                  C   sB   dd t td d  D } dd | d d  D }t| d d | d S )Nc                 S   s&   g | ]\}}|d v r|dg n|qS ))   rX   r   .0ixr   r   r   
<listcomp>      & z3test_tofromgsheet_02_uneven_row.<locals>.<listcomp>c                 S   s&   g | ]}t |d k r|dg n|qS )rM    )lenre   rg   r   r   r   rh      ri   	enumerater`   rE   )test_table_t1test_table_f1r   r   r   test_tofromgsheet_02_uneven_row   s   rq   c                   C   s   t dd d d d S )Nr   )rE   r   r   r   r    test_tofromgsheet_03_empty_table   s   rr   c                  C   s0   dd t dd D } tt d d  d d|  d S )Nc                 S   s   g | ]}|d  gqS )rF   r   rl   r   r   r   rh      s    z3test_tofromgsheet_04_cell_range.<locals>.<listcomp>r      B1:B4)r`   rE   )test_table_f2r   r   r   test_tofromgsheet_04_cell_range   s   rv   c                   C   s"   t td d  dd td d   d S )NrZ   r_   r   r   r   r    test_tofromgsheet_05_sheet_title   rb   rw   zBWhen this stop failing, uncomment datetime.date in TEST1 and TEST2)raisesr   c                  C   s<   dd t td d  D } t| d d  d d| d d   d S )Nc                 S   s2   g | ]\}}|d v r|d t dddgn|qS ))   r   i  ry      )datetimedaterd   r   r   r   rh      s   2 z6test_tofromgsheet_06_datetime_date.<locals>.<listcomp>rt   rm   )test_table_dtr   r   r   "test_tofromgsheet_06_datetime_date   s   "r~   c               	   C   s^   t  \} }}td d  }t||| |d}zt||dd}t|| W || d S || w )Nr=   TrG   )r:   r`   r   r   r
   r?   )r9   r-   r4   r@   rC   rD   r   r   r    test_tofromgsheet_07_open_by_key   s   r   c            	   	   C   s   t  \} }}td d  }dd t|D }t||| |d}z6t||dd}t|| t||| |d}zt||dd}t|| W || n|| w W || d S || w )Nc                 S   s,   g | ]\}}|d kr|d  t |gn|qS )r   r   rd   r   r   r   rh      s   , z1test_tofromgsheet_08_recreate.<locals>.<listcomp>r   Tr   )r:   r`   rn   r   r   r
   r?   )	r9   r-   r4   rI   table2rC   result1
spread_id2result2r   r   r   test_tofromgsheet_08_recreate   s   
r   c                  C   s8   t d d  t d d  g} t d d  t dd   }| |fS )NrF   )r`   rH   rB   r   r   r   _get_testcase_for_append   s   r   c                  C   s   t  \} }t| | d S r^   r   rL   r   r   r   r   test_appendgsheet_10_double   s   
r   c                  C   s   t  \} }t| |dd d S )Npetl_append)rA   r   r   r   r   r    test_appendgsheet_11_named_sheet   s   
r   c               	   C   s   t  \} }}td d  }tdd  }t||| |d}z(t||| dd t|| d d}t|| t|| dd}t|| W || d S || w )NrF   r   petl)r<   )r:   r`   r   r	   r   r
   r?   )r9   r-   r4   r@   r   rC   r   r   r   r   r    test_appendgsheet_12_other_sheet   s   
r   r^   )/
__future__r   r   r   r{   r   r!   rN   r*   petl.compatr   petl.io.gsheetr   r   r	   petl.test.helpersr
   r   importorskipr   r   r   markskipiffound_gcp_credentialsr%   r/   r5   r:   rE   rL   rP   r`   ra   rq   rr   rv   rw   xfail	TypeErrorr~   r   r   r   r   r   r   r   r   r   r   <module>   st   












