a
    GbKv                     @   s   d dl Z d dlZd dlZd dlmZmZmZmZ d dl	m
Z d dlmZ ejdd Zejdd Zejdd	 Zejd
d Zejdd ZG dd dZG dd dZdS )    N)	DataFrameIndexSeriesjson_normalizenested_to_recordc                   C   sl   ddddddddgdd	d
dddddgdgddddddgddddddddgdgdgS )NUSA
CaliforniaSan Francisco90  )namepopLos Angeles:0  )r   citiesOhioColumbus  	Cleveland  )countrystatesGermanyBayernMunich;0  Nordrhein-WestfalenDuesseldorf  Koeln   r!   r!   r!   Blib/python3.9/site-packages/pandas/tests/io/json/test_normalize.pydeep_nested   s.    r#   c                   C   sL   dddddddddgdd	id
ddddddddgddidddgS )NDader   r   
populationBroward@  
Palm Beach`  governor
Rick ScottFLFlorida)countiesinfo	shortnamestateSummitr   Cuyahoga9  John KasichOHr   r!   r!   r!   r!   r"   
state_data8   s     r8   c                   C   s   dd idddddddgS )	Nr0   
11/08/1993
26/05/2012Z
created_atZlast_updatedJaneDoefirst	last_namer0   Zauthor_namer!   r!   r!   r!   r"   author_missing_dataQ   s
    rB   c                   C   sF   dddddddgdd	d
igidddddddgdd	digidgS )NAliceZ%  
Morris St.	Massillonr7   f  )numberstreetcityr2   zipr   	city_nameFoo York City)r   	addressesprevious_residences!  
Spring St.ElizabethtonTN  
Barmingham)rN   rO   r!   r!   r!   r!   r"   missing_metadata\   s(    		rV   c                   C   s"   ddiddddddd	id
gS )z@
    input data to test json_normalize with max_level param
    NameUser001	Some textID001Name001ZIdrW   	TextField	UserFieldab	CreatedByLookupImager!   r!   r!   r!   r"   max_level_test_input_data{   s    rf   c                   @   s  e Zd Zdd Zdd Zdd Zejddd	idd
igddfddd	idd
igiddfdde	fdde	fg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ejd'd	d(d)d*d+d,d-idd.id/d(d)d*d+d,d-idd.id/gfd
d(d)d*d,d-idd.id0d(d)d*d,d-idd.id0gfgd1d2 Zd3d4 Zd5d6 Zejd7g d8d9d: Zd;d< Zd=d> ZdS )?TestJSONNormalizec                 C   sL   dddddddddd	d
dddddg}t |}t|}t|| d S )N         r`   ra   c               	   
         r   r   tmassert_frame_equalselfrecsresultexpectedr!   r!   r"   test_simple_records   s    



z%TestJSONNormalize.test_simple_recordsc                 C   s   t |d d}t|d d }t|| t |d}g }|D ]}||d  q<t|}t|| t |ddd}tddgddg|d< t|| d S )	Nr   r/   r2   metar.   r   rj   ri   )r   r   rw   rx   extendnparrayrepeatrz   r8   r|   r}   recr!   r!   r"   test_simple_normalize   s    
z'TestJSONNormalize.test_simple_normalizec                 C   s   t g }t }t|| d S )Nrv   rz   r|   r}   r!   r!   r"   test_empty_array   s    z"TestJSONNormalize.test_empty_arrayz!data, record_path, exception_typer`   r   rh   Nz{"a": [{"a": 0}, {"a": 1}]}c                 C   st   |d urFt j|tjd t||d W d    qp1 s:0    Y  n*t||d}tddgdgd}t|| d S )Nmatchrecord_pathr   rh   r`   columns)pytestraisesrw   ZEMPTY_STRING_PATTERNr   r   rx   )rz   datar   Zexception_typer|   r}   r!   r!   r"   test_accepted_input   s    
,z%TestJSONNormalize.test_accepted_inputc                 C   s   t ddddi}tddggddgd}t||| t ddddidd	}tddggd
dgd}t||| t ddddidd	}tddggddgd}t||| t |ddgdddggdd}tg d }|j |sJ d S )NArh   ri   )r   BzA.AzA.Br   _sepZA_AZA_Bu   σu   AσAu   AσBr   r   r   r   )r   r   )r   r   r   Zstates_name)	r   r   rw   rx   Zreindex_liker   Zsort_valuesr   Zequals)rz   r#   r|   r}   r!   r!   r"   $test_simple_normalize_with_separator   s"    
z6TestJSONNormalize.test_simple_normalize_with_separatorc                 C   sL   ddgdddd}t |dd}tddgddggg dd	}t|| d S )
Nrh   ri   )rj   rm   )Zb_1Zb_2)r`   ra   __r   )r`   Zb__b_1Zb__b_2r   rv   rz   r   r|   r}   r!   r!   r"   'test_normalize_with_multichar_separator   s    z9TestJSONNormalize.test_normalize_with_multichar_separatorc                 C   s<   t dddgiddd}tdgdggdgd}t|| d S )Nr   rh   ri   zPrefix.)record_prefixzPrefix.0r   rv   r   r!   r!   r"   test_value_array_record_prefix   s    z0TestJSONNormalize.test_value_array_record_prefixc                 C   sj   dddddddddd	dgd
d}t |ddgd}tddgddgdd	ggddgd}t|| d S )Nr.   r,   r$   r   r%   r'   r(   r)   r*   )r+   r/   r2   r0   r0   r/   r   r   r&   r   rv   r   r!   r!   r"   test_nested_object_record_path   s    z0TestJSONNormalize.test_nested_object_record_pathc                 C   sb   t |ddgdddggd}dgd dgd	  g d
g dg dd}t||jd}t|| d S )Nr   r   r   r   r   r   rm   r   rj   )r	   r	   r   r   r   r   r   )r
   r   r   r   r   r   r   )r   r   r   r   r   r   r    )r   zstates.namer   r   r   r   r   r   rw   rx   )rz   r#   r|   ex_datar}   r!   r!   r"   test_more_deeply_nested   s    		z)TestJSONNormalize.test_more_deeply_nestedc                 C   s   ddddiddddd	dd
ddgdddddiddddddgdg}t |dddddgg}g ddgd dgd  g ddgd dgd  g dd}t||jd}t|| d S )Nr.   r-   r+   r,   r$   r   r%   r'   r(   r)   r*   )r2   r1   r0   r/   r   r7   r6   r3   r   r4   r5   r/   r2   r1   r0   r$   r'   r)   r3   r4   rj   ri   )r-   r-   r-   r7   r7   r   r(   r*   r   r5   )r   r2   r1   info.governorr&   r   r   )rz   r   r|   r   r}   r!   r!   r"   test_shallow_nested  s6    z%TestJSONNormalize.test_shallow_nestedc                 C   s|   t |dgddddggdd}g dg d	d
gd dgd  dgd dgd  dgd dgd  d}t|}t|| d S )Nr/   r2   r1   r0   r+   ignorer   r   r   errorsr   r   r.   rj   r   ri   r-   r7   r,   r6   )r   r&   r2   r1   r   rv   )rz   r8   r|   r   r}   r!   r!   r"   -test_nested_meta_path_with_nested_record_path?  s    z?TestJSONNormalize.test_nested_meta_path_with_nested_record_pathc                 C   s   ddddddddgdg}d	}t jt|d
" t|dddgd W d    n1 sX0    Y  t|dddgdd}dD ]}||v szJ qzd S )Nhellothere	somethingelsefoobar
something2else2r   r   r   z?Conflicting metadata name (foo|bar), need distinguishing prefixr   r   r   r   r   r   r   Zmeta_prefixZmetafooZmetabarr   r   )r   r   
ValueErrorr   )rz   r   msgr|   valr!   r!   r"   test_meta_name_conflictS  s    0z)TestJSONNormalize.test_meta_name_conflictc                 C   s`   ddddddddgdg}d	d
g}t |d|dd}|d	d
gksFJ dD ]}||v sJJ qJd S )Nr   r   r   r   r   r   r   r   r   r   r   r   r   r   r   )rz   r   ZCOLUMNSr|   r   r!   r!   r"    test_meta_parameter_not_modifiedh  s    z2TestJSONNormalize.test_meta_parameter_not_modifiedc                 C   s   t |d d}t|d d }t|| t |dddd}g }|D ]}||d  qBt|}|jdd d}td	d
gddg|d< t|| d S )Nr   r/   r2   county_)r   r   c                 S   s   d|  S )Nr   r!   )xr!   r!   r"   <lambda>      z6TestJSONNormalize.test_record_prefix.<locals>.<lambda>r   r.   r   rj   ri   )	r   r   rw   rx   r   renamer   r   r   r   r!   r!   r"   test_record_prefix|  s    z$TestJSONNormalize.test_record_prefixc                 C   sR   d d}d dddgdddgdd	d
gi}t|}tt|}t|| d S )NsK   [{"Ünicøde":0,"sub":{"A":1, "B":2}},{"Ünicøde":1,"sub":{"A":3, "B":4}}]utf8s	   Ünicøder   rh   zsub.Arj   zsub.Bri   rm   )decoder   r   jsonloadsrw   rx   )rz   ZtestjsonZtestdatar}   r|   r!   r!   r"   test_non_ascii_key  s    z$TestJSONNormalize.test_non_ascii_keyc                 C   sJ   t |}tjtjtjtjtjdd dddddg}t|}t|| d S )N)r0   info.created_atinfo.last_updatedauthor_name.firstauthor_name.last_namer9   r:   r<   r=   r   r   nanr   rw   rx   )rz   rB   r|   r   r}   r!   r!   r"   test_missing_field  s     z$TestJSONNormalize.test_missing_fieldzmax_level,expectedrY   rZ   r[   r\   rW   rX   ra   )r^   r_   rc   re   )r^   zUserField.IdzUserField.Namerc   re   c                 C   s~   ddiddddddddddgdd	id
dddddgdg}t |dgdgdgg|d}t||jjd}t|| d S )NrW   rX   rY   rZ   r[   r\   r]   r`   ra   r   r   r   r   r   )rc   rd   re   Ztagsrd   rc   re   )r   r   	max_level)r   r   )r   r   r   valuesrw   assert_equal)rz   r   r}   
test_inputr|   Zexpected_dfr!   r!   r"    test_max_level_with_records_path  s,    -

z2TestJSONNormalize.test_max_level_with_records_pathc                 C   s:   t dddiig}t ddddiigid}t|| d S )Nr   r   rh   Zdummy)r   rw   rx   )rz   Zdf1Zdf2r!   r!   r"   !test_nested_flattening_consistent  s    z3TestJSONNormalize.test_nested_flattening_consistentc                 C   sF   t d|ddddigdgdgd}tddidgd	}t|| d S )
NTexasr   r.   iri   r0   r   r   )index)r   r   rw   r   )rz   Znulls_fixturer|   r}   r!   r!   r"   test_nonetype_record_path  s    z+TestJSONNormalize.test_nonetype_record_pathvalue)Zfalsetruez{}1z"text"c                 C   sp   t |}d|d}d}| d| d| d}tjt|d  t|g|gd W d    n1 sb0    Y  d S )	Nr   r   r0   z has non list value z
 for path z. Must be list or null.r   r   )r   r   r   r   	TypeErrorr   )rz   r   Zparsed_valuer   Z	test_pathr   r!   r!   r"    test_non_list_record_path_errors  s    

z2TestJSONNormalize.test_non_list_record_path_errorsc                 C   sL   d}t t|dgdgd}tdgdgtjdgtdd	}t|| d S )
Nz,[{"id": 99, "data": [{"one": 1, "two": 2}]}]r   id)r   r   rh   ri   c   )Zdtype)ZoneZtwor   )	r   r   r   r   r   r   objectrw   rx   r   r!   r!   r"   test_meta_non_iterable  s    z(TestJSONNormalize.test_meta_non_iterablec                    s6    fdd}t | }t d d }t|| d S )Nc                   3   s    d d E d H  d S )Nr   r/   r!   r!   r8   r!   r"   generator_data,  s    z8TestJSONNormalize.test_generator.<locals>.generator_datar   r/   rv   )rz   r8   r   r|   r}   r!   r   r"   test_generator)  s    
z TestJSONNormalize.test_generator)__name__
__module____qualname__r~   r   r   r   markparametrizeNotImplementedErrorr   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r!   r!   r!   r"   rg      s~   	
	$)
 

rg   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ejddddddddgfdd didddd!d"d#did$gfd%ddddd!dd&gfgd'd( Zd)d* Zd+d, Zd-d. ZdS )/TestNestedToRecordc                 C   s0   ddddddg}t |}|}||ks,J d S )Nrh   ri   )flat1flat2rj   rm   )Zflat3r   r   ry   r!   r!   r"   test_flat_stays_flat6  s    z'TestNestedToRecord.test_flat_stays_flatc                 C   s4   ddddd}t |}dddd}||ks0J d S )Nrh   ri   rl   d)r   dict1)dict1.cdict1.dr   r   r   r!   r!   r"   test_one_level_deep_flattens<  s    z/TestNestedToRecord.test_one_level_deep_flattensc                 C   sH   dddddddddd}t |}ddddddd}||ksDJ d S )Nrh   ri   r   )er   )r   r   nested)r   r   r   znested.dz
nested.e.cz
nested.e.dr   r   r!   r!   r"   test_nested_flattensD  s    	z'TestNestedToRecord.test_nested_flattensc                 C   sF   d}t jt|d  t|dddd W d    n1 s80    Y  d S )N^Key 'name' not found. To replace missing values of 'name' with np.nan, pass in errors='ignore'r   rN   r   raiser   r   r   KeyErrorr   rz   rV   r   r!   r!   r"   test_json_normalize_errorsW  s    z-TestNestedToRecord.test_json_normalize_errorsc                 C   sN   t |dddd}g ddddd	d
tjgg}g d}t||d}t|| d S )NrN   r   r   r   )rD   rE   rF   r7   rG   rC   rP   rQ   rR   rS   rT   )rH   rI   rJ   r2   rK   r   r   r   rz   rV   r|   r   r   r}   r!   r!   r"   test_missing_metah  s    z$TestNestedToRecord.test_missing_metac                 C   s   dd ddiddigd}t |dddd	ggd
d}ddtjgddtjgg}g d}t||ddti}t|| tj	t
dd( t |dddd	ggdd W d    n1 s0    Y  d S )Nr   r   rh   ri   )r   nested_metar   r   r   r  Zleafr   )r   r   r   )r   r   nested_meta.leafr   r  z'leaf' not foundr   r   )r   r   r   r   Zastyper   rw   rx   r   r   r   )rz   r   r|   r   r   r}   r!   r!   r"   test_missing_nested_metaw  s(    

z+TestNestedToRecord.test_missing_nested_metac                 C   sJ   d}t jt|d$ t|ddgddd W d    n1 s<0    Y  d S )Nr   r   rO   r   r   r   r   r   r   r!   r!   r"   5test_missing_meta_multilevel_record_path_errors_raise  s    zHTestNestedToRecord.test_missing_meta_multilevel_record_path_errors_raisec                 C   sJ   t |ddgddd}ddgdtjgg}d	dg}t||d
}t|| d S )NrO   r   r   r   r   rM   rC   rU   rL   r   r   r   r!   r!   r"   6test_missing_meta_multilevel_record_path_errors_ignore  s    zITestNestedToRecord.test_missing_meta_multilevel_record_path_errors_ignorec                 C   sX   d ddddddddd	ddg}t |}d ddd
dd	dddg}||ksTJ d S )NZSmithZ	Appleseedr>   rA   r9   r:   r;   r<   r=   )r0   r   r   )r   r   r   r   r   r   r!   r!   r"   test_donot_drop_nonevalues  s"    z-TestNestedToRecord.test_donot_drop_nonevaluesc              
   C   sP   d ddd d d dddddiid}t |}d d d d dddd	}||ksLJ d S )
Nr   r2       gH@   @   ;@)r   regionr   yzr   z	town.infor   location)r   location.country.state.idz#location.country.state.town.info.id'location.country.state.town.info.region"location.country.state.town.info.x"location.country.state.town.info.y"location.country.state.town.info.zr   r   r!   r!   r"   $test_nonetype_top_level_bottom_level  s0    	z7TestNestedToRecord.test_nonetype_top_level_bottom_levelc              	   C   sT   d d d d d dddddddd}t |}d d d d d dddd	}||ksPJ d S )
Nr  r  r	  )r
  r   r  r  r  )r   r2   )r   r   r  )r   zlocation.idzlocation.country.idr  r  r  r  r  r   r   r!   r!   r"   test_nonetype_multiple_levels  s0    
z0TestNestedToRecord.test_nonetype_multiple_levelszmax_level, expectedNrX   rY   rZ   r[   ra   )CreatedBy.NameLookup.TextFieldzLookup.UserField.IdzLookup.UserField.NameImage.ar   rW   r\   r]   r`   rb   rh   )r  r  zLookup.UserFieldr  c                 C   s   t ||d}||ksJ d S )Nr   r   )rz   r   r}   rf   outputr!   r!   r"   test_with_max_level  s    +z&TestNestedToRecord.test_with_max_levelc              	   C   sh   d}dddddddd	d
ddidddidiig}dddd	d
ddg}t ||d}||ksdJ d S )Nd   rc   userZLeoZThomson)Z	firstnameZLastNamefatherZ	Father001Z	Father002Z	Father003rW   Z	Father004)r   r  )rW   r  )r   Zfamily_tree)zCreatedBy.user.name.firstnamezCreatedBy.user.name.LastNamez&CreatedBy.user.family_tree.father.namez-CreatedBy.user.family_tree.father.father.Namez4CreatedBy.user.family_tree.father.father.father.namez;CreatedBy.user.family_tree.father.father.father.father.Namer  r   )rz   r   Z
input_datar}   r  r!   r!   r"   test_with_large_max_level>  s6    
z,TestNestedToRecord.test_with_large_max_levelc                 C   sZ   t t< ddlm} ddddddd	dg}|| W d    n1 sL0    Y  d S )
Nr   r   rh   ri   rj   rk   rm   rn   ro   )rw   Zassert_produces_warningFutureWarningZpandas.io.jsonr   )rz   r   r{   r!   r!   r"   test_deprecated_importc  s    z)TestNestedToRecord.test_deprecated_importc                 C   s   ddddidddddiddd	d
didd}t |}g d|_t|}tg dg ddtjtjgtjdtjgtjtjdgd}t|| d S )Nrh   Foor`   )r   r   elementsri   Barra   rj   Bazrl   )r   rh   ri   )rh   ri   rj   )r#  r%  r&  g      ?g       @g      @)r   r   z
elements.az
elements.bz
elements.c)r   r   r   r   r   r   rw   rx   )rz   r   sr|   r}   r!   r!   r"   test_series_non_zero_indexj  s     
	z-TestNestedToRecord.test_series_non_zero_index)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   5  sZ    ")
%r   )r   Znumpyr   r   Zpandasr   r   r   r   Zpandas._testingZ_testingrw   Zpandas.io.json._normalizer   Zfixturer#   r8   rB   rV   rf   rg   r   r!   r!   r!   r"   <module>   s(   
&




   ,