a
    YÒOc[  ã                   @   sÔ   d dl Z d dlmZmZ d dlmZm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 d d	lmZ d d
lmZmZ dd„ ZG dd„ deejƒZG dd„ deejƒZG dd„ deƒZG dd„ deƒZdS )é    N)ÚTestCaseÚoverride_settings)ÚserializersÚviews)ÚAPIRequestFactory©Ú	countries)Úsettings)ÚCountry)ÚCountryFieldMixin)ÚFantasyCountries)ÚMultiCountryÚPersonc                 C   s   dd„ | D ƒS )z>
    Convert Countries into a DRF-OPTIONS formatted dict.
    c                 S   s   g | ]\}}||d œ‘qS ))Zdisplay_nameÚvalue© )Ú.0ÚkÚvr   r   úV/var/www/html/gsl.tools/lib/python3.9/site-packages/django_countries/tests/test_drf.pyÚ
<listcomp>   ó    z%countries_display.<locals>.<listcomp>r   r   r   r   r   Úcountries_display   s    r   c                   @   s   e Zd ZG dd„ dƒZdS )ÚPersonSerializerc                   @   s&   e Zd ZeZdZddiddidœZdS )zPersonSerializer.Meta©ÚnameÚcountryÚother_countryÚfavourite_countryÚfantasy_countryZcountry_dictTZ	name_only)r   r   N)Ú__name__Ú
__module__Ú__qualname__r   ÚmodelÚfieldsÚextra_kwargsr   r   r   r   ÚMeta   s
   þr%   N©r   r    r!   r%   r   r   r   r   r      s   r   c                   @   s   e Zd ZG dd„ dƒZdS )ÚMultiCountrySerializerc                   @   s   e Zd ZeZdZdS )zMultiCountrySerializer.Metar   N)r   r    r!   r   r"   r#   r   r   r   r   r%   &   s   r%   Nr&   r   r   r   r   r'   %   s   r'   c                   @   st   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dd„ Zdd„ ZdS )ÚTestDRFc              	   C   s2   t ddd�}t|ƒ}|  |jddddddœ¡ d S )NúChris BeavenÚNZ©r   r   Ú úNew Zealandr   ©r   r   ÚassertEqualÚdata©ÚselfZpersonÚ
serializerr   r   r   Útest_serialize,   s    ûþzTestDRF.test_serializec              	   C   s8   t ddd�}t|ƒ}|  |jdddddœdddœ¡ d S )	Nr)   ÚAU)r   r   r,   Ú	Australia©Úcoder   r-   r   r.   r1   r   r   r   Útest_serialize_country_dict:   s    ûþz#TestDRF.test_serialize_country_dictc                 C   s6   t ddddœd�}|  | ¡ ¡ |  |jd d¡ d S )NÚTesterZUSÚNV©r   r   r   ©r0   r   ©r   Ú
assertTrueÚis_validr/   Úvalidated_data©r2   r3   r   r   r   Útest_deserializeH   s
    
ÿzTestDRF.test_deserializec                 C   s<   t ddddœddœd�}|  | ¡ ¡ |  |jd d¡ d S )	Nr:   ÚGBZAnythingr7   r;   r<   r=   r   r>   rB   r   r   r   Útest_deserialize_country_dictO   s    ýÿz%TestDRF.test_deserialize_country_dictc                 C   sH   t ddddœd�}|  | ¡ ¡ |  |jd d¡ |  |jd d	¡ d S )
NÚChrisr-   Ú	Neverlandr<   r=   r   r*   r   r;   r>   rB   r   r   r   Útest_deserialize_by_nameZ   s    ýÿz TestDRF.test_deserialize_by_namec                 C   s8   t ddddœd�}|  | ¡ ¡ |  |jd dg¡ d S )NrF   zNo Such ZealandrG   r<   r=   r   z("No Such Zealand" is not a valid choice.©r   ÚassertFalser@   r/   ÚerrorsrB   r   r   r   Útest_deserialize_invalidf   s    ýÿÿz TestDRF.test_deserialize_invalidc                 C   s,   t dd�}t|ƒ}|  |jdddgi¡ d S )NzNZ,AUr   r   r5   r*   ©r   r'   r/   r0   ©r2   Zmcr3   r   r   r   Útest_multi_serializes   s    
zTestDRF.test_multi_serializec                 C   s(   t dd�}t|ƒ}|  |jdg i¡ d S )Nr,   r   r   rM   rN   r   r   r   Útest_multi_serialize_emptyx   s    
z"TestDRF.test_multi_serialize_emptyc                 C   s:   t dddgid�}|  | ¡ ¡ |  |jd ddg¡ d S )Nr   r*   r5   r=   ©r'   r?   r@   r/   rA   rB   r   r   r   Útest_multi_deserialize}   s    zTestDRF.test_multi_deserializec                 C   s:   t dddgid�}|  | ¡ ¡ |  |jd ddg¡ d S )Nr   r-   r6   r=   r*   r5   rQ   rB   r   r   r   Útest_multi_deserialize_by_name‚   s
    
ÿz&TestDRF.test_multi_deserialize_by_namec                 C   sN   t dg d¢id�}|  | ¡ ¡ |jd }t|tƒr<|d }|  |dg¡ d S )Nr   )r*   ÚBADr5   r=   é   z"BAD" is not a valid choice.)r'   rJ   r@   rK   Ú
isinstanceÚdictr/   )r2   r3   rK   r   r   r   Útest_multi_deserialize_invalid‰   s    

z&TestDRF.test_multi_deserialize_invalidc                 C   sV   t dddgid�}|  | ¡ ¡ | ¡ }tjj|jd�}|  |j	t
dƒt
dƒg¡ d S )Nr   r*   r5   r=   )Úpk)r'   r?   r@   Úsaver   ÚobjectsÚgetrY   r/   r   r
   )r2   r3   ZsavedZloadedr   r   r   Útest_multi_deserialize_save“   s
    z#TestDRF.test_multi_deserialize_savec                 C   s6   t dddœd�}|  | ¡ ¡ |  |jd dg¡ d S )NrF   r,   r+   r=   r   z"" is not a valid choice.rI   rB   r   r   r   Útest_deserialize_blank_invalidš   s    z&TestDRF.test_deserialize_blank_invalidN)r   r    r!   r4   r9   rC   rE   rH   rL   rO   rP   rR   rS   rX   r]   r^   r   r   r   r   r(   +   s   
r(   c                   @   s,   e Zd ZdZejjej dd�dd„ ƒZ	dS )ÚTestDRFMetadataz>
    Tests against the DRF OPTIONS API metadata endpoint.
    zNo i18n)Úreasonc           
      C   sÎ   G dd„ dt jƒ}dd„ }| ¡ }tƒ }| d¡}||d�}||dƒ}||dƒ}|  |jd	¡ |  |ttƒ¡ |  |tt	ƒ ƒ¡ t
d
d��0 ||d�}||dƒ}	|  ||	¡ W d   ƒ n1 sÀ0    Y  d S )Nc                   @   s    e Zd ZdZdd„ Zdd„ ZdS )z1TestDRFMetadata.test_actions.<locals>.ExampleViewzExample view.c                 S   s   d S ©Nr   )r2   Úrequestr   r   r   Úpostª   s    z6TestDRFMetadata.test_actions.<locals>.ExampleView.postc                 S   s   t ƒ S ra   )r   )r2   r   r   r   Úget_serializer­   s    z@TestDRFMetadata.test_actions.<locals>.ExampleView.get_serializerN)r   r    r!   Ú__doc__rc   rd   r   r   r   r   ÚExampleView§   s   rf   c                 S   s   | j d d | d S )z*Helper method for unpacking response JSON.ÚactionsÚPOSTÚchoicesr=   )ÚresponseÚkeyr   r   r   Ú_choices°   s    z.TestDRFMetadata.test_actions.<locals>._choicesú/)rb   r   r   éÈ   Úfr)ZLANGUAGE_CODE)r   ZAPIViewZas_viewr   Úoptionsr/   Ústatus_coder   r   r   r   ZassertNotEqual)
r2   rf   rl   ÚviewÚfactoryrb   rj   Zcountry_choicesZfantasy_choices_enZfantasy_choices_frr   r   r   Útest_actions¥   s    	





zTestDRFMetadata.test_actionsN)
r   r    r!   re   ÚpytestÚmarkZskipifr	   ZUSE_I18Nrt   r   r   r   r   r_       s   r_   )ru   Zdjango.testr   r   Zrest_frameworkr   r   Zrest_framework.testr   Zdjango_countriesr   Zdjango_countries.confr	   Zdjango_countries.fieldsr
   Zdjango_countries.serializersr   Z'django_countries.tests.custom_countriesr   Zdjango_countries.tests.modelsr   r   r   ZModelSerializerr   r'   r(   r_   r   r   r   r   Ú<module>   s   u