U
    d\                     @   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   C/tmp/pip-unpacked-wheel-dnjn05s9/django_countries/tests/test_drf.py
<listcomp>   s     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-   r0   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   r/   r   r   
assertTrueis_validr.   validated_datar1   r2   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 )	Nr9   ZGBZAnythingr6   r:   r;   r<   r   r=   rA   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=   rA   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 )NrD   zNo Such ZealandrE   r;   r<   r   z("No Such Zealand" is not a valid choice.r   assertFalser?   r.   errorsrA   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   r4   r)   r   r&   r.   r/   r1   Zmcr2   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   rK   rL   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)   r4   r<   r&   r>   r?   r.   r@   rA   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,   r5   r<   r)   r4   rO   rA   r   r   r   test_multi_deserialize_by_name   s
    
z&TestDRF.test_multi_deserialize_by_namec                 C   sP   t ddddgid}| |  |jd }t|tr>|d }| |dg d S )Nr   r)   BADr4   r<      z"BAD" is not a valid choice.)r&   rH   r?   rI   
isinstancedictr.   )r1   r2   rI   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)   r4   r<   )pk)r&   r>   r?   saver   ZobjectsgetrW   r.   r   r
   )r1   r2   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 )NrD   r+   r*   r<   r   z"" is not a valid choice.rG   rA   r   r   r   test_deserialize_blank_invalid   s    z&TestDRF.test_deserialize_blank_invalidN)r   r   r    r3   r8   rB   rC   rF   rJ   rM   rN   rP   rQ   rV   rZ   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& ||d}||d}	| ||	 W 5 Q R X 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   )r1   requestr   r   r   post   s    z6TestDRFMetadata.test_actions.<locals>.ExampleView.postc                 S   s   t  S r^   )r   )r1   r   r   r   get_serializer   s    z@TestDRFMetadata.test_actions.<locals>.ExampleView.get_serializerN)r   r   r    __doc__r`   ra   r   r   r   r   ExampleView   s   rc   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/)r_   r   r      fr)ZLANGUAGE_CODE)r   ZAPIViewZas_viewr   optionsr.   status_coder   r   r   r   ZassertNotEqual)
r1   rc   ri   viewfactoryr_   rg   Zcountry_choicesZfantasy_choices_enZfantasy_choices_frr   r   r   test_actions   s    	





zTestDRFMetadata.test_actionsN)
r   r   r    rb   pytestmarkZskipifr	   ZUSE_I18Nrq   r   r   r   r   r\      s   r\   )rr   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