o
    1ik5                     @   s  d dl Z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 ejddu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 G dd deZ!ej"j#e ddG dd deZ$dS )    N)TestCaseoverride_settings)serializersviews)APIRequestFactory	countries)settings)Country)CountryFieldMixin)FantasyCountries)MultiCountryPersondrf_spectacularc                 C   s   dd | D S )z>
    Convert Countries into a DRF-OPTIONS formatted dict.
    c                 S   s   g | ]	\}}||d qS ))display_namevalue ).0kvr   r   `/var/www/html/humari/django-venv/lib/python3.10/site-packages/django_countries/tests/test_drf.py
<listcomp>   s    z%countries_display.<locals>.<listcomp>r   r   r   r   r   countries_display   s   r   c                   @      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_countrycountry_dictT	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          r   c                   @   r   )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,   *   r+   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selfperson
serializerr   r   r   test_serialize1   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   r1   	Australiacoder   r2   r   r3   r6   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TesterUSNVr   r   r    r5   r   r   
assertTrueis_validr4   validated_datar7   r9   r   r   r   test_deserializeM   s
   
zTestDRF.test_deserializec                 C   s<   t ddddddd}| |  | |jd d d S )	Nr@   GBAnythingr=   rB   rC   rD   r   rE   rI   r   r   r   test_deserialize_country_dictT   s   z%TestDRF.test_deserialize_country_dictc                 C   sH   t ddddd}| |  | |jd d | |jd d	 d S )
NChrisr2   	NeverlandrC   rD   r   r/   r    rB   rE   rI   r   r   r   test_deserialize_by_name_   s   z TestDRF.test_deserialize_by_namec                 C   s8   t ddddd}| |  | |jd dg d S )NrN   zNo Such ZealandrO   rC   rD   r   z("No Such Zealand" is not a valid choice.r   assertFalserG   r4   errorsrI   r   r   r   test_deserialize_invalidk   s   z TestDRF.test_deserialize_invalidc                 C   s,   t dd}t|}| |jdddgi d S )NzNZ,AUr   r   r;   r/   r   r,   r4   r5   r7   mcr9   r   r   r   test_multi_serializex   s   
zTestDRF.test_multi_serializec                 C   s(   t dd}t|}| |jdg i d S )Nr1   r   r   rU   rV   r   r   r   test_multi_serialize_empty}   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/   r;   rD   r,   rF   rG   r4   rH   rI   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   r2   r<   rD   r/   r;   rZ   rI   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/   BADr;   rD      z"BAD" is not a valid choice.)r,   rR   rG   rS   
isinstancedictr4   )r7   r9   rS   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/   r;   rD   )pk)r,   rF   rG   saver   objectsgetrb   r4   r   r
   )r7   r9   saved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 )NrN   r1   r0   rD   r   z"" is not a valid choice.rQ   rI   r   r   r   test_deserialize_blank_invalid   s   z&TestDRF.test_deserialize_blank_invalidN)r#   r$   r%   r:   r?   rJ   rM   rP   rT   rX   rY   r[   r\   ra   rh   ri   r   r   r   r   r-   0   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 d    d S 1 saw   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   )r7   requestr   r   r   post   s   z6TestDRFMetadata.test_actions.<locals>.ExampleView.postc                 S   s   t  S rm   )r   )r7   r   r   r   get_serializer   s   z@TestDRFMetadata.test_actions.<locals>.ExampleView.get_serializerN)r#   r$   r%   __doc__ro   rp   r   r   r   r   ExampleView   s    rr   c                 S   s   | j d d | d S )z*Helper method for unpacking response JSON.actionsPOSTchoicesrD   )responsekeyr   r   r   _choices   s   z.TestDRFMetadata.test_actions.<locals>._choices/)rn   r   r       fr)LANGUAGE_CODE)r   APIViewas_viewr   optionsr4   status_coder   r   r   r   assertNotEqual)
r7   rr   rx   viewfactoryrn   rv   country_choicesfantasy_choices_enfantasy_choices_frr   r   r   test_actions   s    	





"zTestDRFMetadata.test_actionsN)
r#   r$   r%   rq   pytestmarkskipifr	   USE_I18Nr   r   r   r   r   rj      s    rj   c                   @   s8   e Zd ZdZdd Zdd Zdd Zdd	 Zd
d ZdS )TestDRFSchemaGenerationz@
    Tests for OpenAPI schema generation with CountryField.
    c                 C   s   ddl m} |dd}| t|d |j}| | | d| |d }| |d d | d	| | d
|d	  | d|d	  | |d	 d
 d d | |d	 d d d | |d d
dg dS )z
        Test that CountryField with country_dict=True provides correct schema
        annotation. This is used by drf-spectacular for OpenAPI schema generation.
        r   CountryFieldTr!   _spectacular_annotationfieldtypeobject
propertiesr>   r   stringrequiredN)"django_countries.serializer_fieldsr   rF   hasattrr   assertIsNotNoneassertInr4   r7   r   r   
annotationschemar   r   r   #test_country_dict_schema_annotation   s   

z;TestDRFSchemaGeneration.test_country_dict_schema_annotationc                 C   s   ddl m} |ddd}| t|d |j}| d| |d }| | | d| | t|d d t	d	d
 |d D }| d| | d|d  t	dd
 |d D }| |d d dS )za
        Test that nullable CountryField with country_dict=True includes null in schema.
        r   r   T)r!   
allow_nullr   r   oneOf   c                 s   "    | ]}| d dkr|V  qdS )r   r   Nre   r   sr   r   r   	<genexpr>        zWTestDRFSchemaGeneration.test_country_dict_schema_annotation_nullable.<locals>.<genexpr>r   r>   c                 s   r   )r   nullNr   r   r   r   r   r      r   r   r   N)
r   r   rF   r   r   r   r   r4   lennext)r7   r   r   r   r   object_schemanull_schemar   r   r   ,test_country_dict_schema_annotation_nullable   s   
zDTestDRFSchemaGeneration.test_country_dict_schema_annotation_nullablec                 C   s^   ddl m} |dd}| t|d |j}| d| |d }| | | |d d d	S )
z`
        Test that CountryField with name_only=True provides correct schema annotation.
        r   r   Tr"   r   r   r   r   N)r   r   rF   r   r   r   r   r4   r   r   r   r    test_name_only_schema_annotation   s   

z8TestDRFSchemaGeneration.test_name_only_schema_annotationc                 C   sp   ddl m} |ddd}| t|d |j}| d| |d }| | | d| | t|d d d	S )
z^
        Test that nullable CountryField with name_only=True includes null in schema.
        r   r   T)r"   r   r   r   r   r   N)	r   r   rF   r   r   r   r   r4   r   r   r   r   r   )test_name_only_schema_annotation_nullable
  s   
zATestDRFSchemaGeneration.test_name_only_schema_annotation_nullablec                 C   s&   ddl m} | }| t|d dS )z
        Test that standard CountryField doesn't set annotation.
        This allows the default ChoiceField enum handling to work.
        r   r   r   N)r   r   rR   r   )r7   r   r   r   r   r   %test_standard_field_schema_annotation  s   z=TestDRFSchemaGeneration.test_standard_field_schema_annotationN)	r#   r$   r%   rq   r   r   r   r   r   r   r   r   r   r      s    r   zdrf-spectacular not installedrk   c                   @   s(   e Zd ZdZdd Zdd Zdd ZdS )	TestDRFSpectacularIntegrationzO
    Integration tests with drf-spectacular for OpenAPI schema generation.
    c                    s   ddl m} ddlm  G  fdddtj}| }| jd }||dd}| |	d	d
 | 
d| | 
d|d  | 
d|d  | d| dS )zp
        Test that drf-spectacular generates an object schema (not enum)
        for country_dict=True.
        r   
AutoSchemar   c                          e Zd Z ddZdS )zkTestDRFSpectacularIntegration.test_spectacular_country_dict_generates_object_schema.<locals>.TestSerializerTr   Nr#   r$   r%   r   r   r   r   r   TestSerializer4      r   r   rn   Nr   r   r   r>   r   enum)drf_spectacular.openapir   r   r   r   
Serializerr'   _map_serializer_fieldr4   re   r   assertNotInr7   r   r   auto_schemar   r   r   r   r   5test_spectacular_country_dict_generates_object_schema+  s   zSTestDRFSpectacularIntegration.test_spectacular_country_dict_generates_object_schemac                    sp   ddl m} ddlm  G  fdddtj}| }| jd }||dd}| |	d	d
 | 
d| dS )zl
        Test that drf-spectacular generates a string schema (not enum)
        for name_only=True.
        r   r   r   c                       r   )zhTestDRFSpectacularIntegration.test_spectacular_name_only_generates_string_schema.<locals>.TestSerializerTr   Nr   r   r   r   r   r   L  r   r   r   rn   Nr   r   r   )r   r   r   r   r   r   r'   r   r4   re   r   r   r   r   r   2test_spectacular_name_only_generates_string_schemaC  s   zPTestDRFSpectacularIntegration.test_spectacular_name_only_generates_string_schemac                    s~   ddl m} ddlm  G  fdddtj}| }| jd }||dd}| d	| | d
|d	  | d|d	  dS )z_
        Test that drf-spectacular generates an enum schema for standard CountryField.
        r   r   r   c                       s   e Zd Z  ZdS )zkTestDRFSpectacularIntegration.test_spectacular_standard_field_generates_enum_schema.<locals>.TestSerializerNr   r   r   r   r   r   _  s    
r   r   rn   Nr   r/   rA   )	r   r   r   r   r   r   r'   r   r   r   r   r   r   5test_spectacular_standard_field_generates_enum_schemaW  s   zSTestDRFSpectacularIntegration.test_spectacular_standard_field_generates_enum_schemaN)r#   r$   r%   rq   r   r   r   r   r   r   r   r   %  s
    r   )%importlib.util	importlibr   django.testr   r   rest_frameworkr   r   rest_framework.testr   django_countriesr   django_countries.confr	   django_countries.fieldsr
   django_countries.serializersr   'django_countries.tests.custom_countriesr   django_countries.tests.modelsr   r   util	find_specHAS_DRF_SPECTACULARr   ModelSerializerr   r,   r-   rj   r   r   r   r   r   r   r   r   <module>   s(    u%[