o
    1iJ                     @   s   d dl Z d dlmZ d dlmZ d dlmZmZmZ d dl	m
Z
 d dlmZ dZg dZG d	d
 d
eZG dd deZG dd deZG dd deZG dd deZdS )    N)TestCase)translation)	CountriesCountryTuple	countries)settings)custom_countries   ))AFAfghanistan)AXu   Åland Islands)ALAlbaniac                   @   s   e Zd Zdd Zdd ZdS )BaseTestc                 C      t ` d S Nr   self r   f/var/www/html/humari/django-venv/lib/python3.10/site-packages/django_countries/tests/test_countries.pysetUp      zBaseTest.setUpc                 C   r   r   r   r   r   r   r   tearDown   r   zBaseTest.tearDownN)__name__
__module____qualname__r   r   r   r   r   r   r      s    r   c                   @   sD  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d Zejjej dd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/d0 Zd1d2 Zd3d4 Z d5d6 Z!d7d8 Z"d9d: Z#d;d< Z$ejjej ddd=d> Z%d?d@ Z&dAdB Z'dCdD Z(dEdF Z)dGS )HTestCountriesObjectc                 C   s   |  ttt d S r   )assertEquallenr   EXPECTED_COUNTRY_COUNTr   r   r   r   test_countries_len   s   z&TestCountriesObject.test_countries_lenc                 C   s   |  ttd d t d S )N   )r   listr   FIRST_THREE_COUNTRIESr   r   r   r   test_countries_sorted   s   z)TestCountriesObject.test_countries_sortedc                 C   sH   t td }td }| |j|d  | |j|d  | |t d S )Nr      )r#   r   r$   r   codenameassertIsInstancer   )r   countryfirst_countryr   r   r   test_countries_namedtuple    s
   z-TestCountriesObject.test_countries_namedtuplec                 C   s\   | j dddd | ttddg | ttd W d    d S 1 s'w   Y  d S )NNew Zealand	NeverlandNZNVCOUNTRIES_ONLYr1   r.   r0   r-      r   r   r#   r   r   r   r   r   r   test_countries_limit'   s   "z(TestCountriesObject.test_countries_limitc                 C   sZ   | j ddgd | ttddg | ttd W d    d S 1 s&w   Y  d S )Nr0   r4   r2   r5   r6   r7   r   r   r   r   test_countries_limit_codes.   s   "z.TestCountriesObject.test_countries_limit_codesc                 C   sJ   | j dd id | tttd  W d    d S 1 sw   Y  d S )NAUCOUNTRIES_OVERRIDEr&   r   r   r   r   r    r   r   r   r   !test_countries_custom_removed_len5      "z5TestCountriesObject.test_countries_custom_removed_lenc                 C   sJ   | j ddid | tttd  W d    d S 1 sw   Y  d S )NXXr.   r;   r&   r=   r   r   r   r   test_countries_custom_added_len9   r?   z3TestCountriesObject.test_countries_custom_added_lenc                 C   s   t d  d S )Nr   r   r   r   r   r   test_countries_getitem=   s   z*TestCountriesObject.test_countries_getitemc                 C   s"   t ddd }| t|d d S )N
      r6      )r   r   r   )r   slicedr   r   r   test_countries_slice@   s   z(TestCountriesObject.test_countries_slicec                 C   sL   G dd d}| j d| id tj W d    d S 1 sw   Y  d S )Nc                   @      e Zd Zdd ZdS )zUTestCountriesObject.test_countries_custom_gettext_evaluation.<locals>.FakeLazyGetTextc                 S   s   t d)NzCan't evaluate lazy_gettext yet)
ValueErrorr   r   r   r   __bool__F   r   z^TestCountriesObject.test_countries_custom_gettext_evaluation.<locals>.FakeLazyGetText.__bool__N)r   r   r   rJ   r   r   r   r   FakeLazyGetTextE       rK   r:   r;   )r   r   )r   rK   r   r   r   (test_countries_custom_gettext_evaluationD   s   "z<TestCountriesObject.test_countries_custom_gettext_evaluationc                 C      ddl m} |dd d S )Nr6   )check_ioc_countriesr   	verbosity)ioc_datarO   )r   rO   r   r   r   test_ioc_countriesL      z&TestCountriesObject.test_ioc_countriesc                 C   s   t t  d S r   )dictr   r   r   r   r   test_initial_iterQ   s   z%TestCountriesObject.test_initial_iterc                 C   rN   )Nr6   )check_flagsr   rP   )datarW   )r   rW   r   r   r   
test_flagsU   rT   zTestCountriesObject.test_flagsc                 C   s   ddl m} |  d S )Nr6   )check_common_names)rX   rZ   )r   rZ   r   r   r   test_common_namesZ   s   
z%TestCountriesObject.test_common_namesNo i18nreasonc              	   C   sT   t  }t d z| tdd | tdd W t | d S t | w )NdeBOBolivienVE	Venezuela)r   get_languageactivater   r   r(   )r   langr   r   r   test_common_name_translation_   s   
z0TestCountriesObject.test_common_name_translationc                 C   s^   |  tdd |  tdd |  tdd |  tdd |  tdd d S )Nr0   nZNzli*  554r   r   alpha2r   r   r   r   test_alpha2i   s
   zTestCountriesObject.test_alpha2c                 C      |  tdd d S )Nr@    rk   r   r   r   r   test_alpha2_invalidp      z'TestCountriesObject.test_alpha2_invalidc                 C   sH   | j dd id | tdd W d    d S 1 sw   Y  d S )Nr:   r;   ro   r   r   r   rl   r   r   r   r   test_alpha2_overrides      "z(TestCountriesObject.test_alpha2_overridec                 C   sx   | j d ddiddidd$ | tdd | tdd | tdd W d    d S 1 s5w   Y  d S )	Nalpha3ro   XXXr:   r0   USr;   r:   r0   rx   )r   r   r   ru   r   r   r   r   test_alpha3_overridew      "z(TestCountriesObject.test_alpha3_overridec                 C   sx   | j d dd iddidd$ | tdd  | tdd  | tdd W d    d S 1 s5w   Y  d S )Nnumerici  rw   r;   r:   r0   rx   )r   r   r   r{   r   r   r   r   test_numeric_override   rz   z)TestCountriesObject.test_numeric_overridec                 C   sH   | j ddid | tdd W d    d S 1 sw   Y  d S )Nr@   r.   r;   rr   r   r   r   r   test_alpha2_override_new   rt   z,TestCountriesObject.test_alpha2_override_newc                 C   s   | j ddgddddd? | tdd | tdd | td	d
 | td	d
 | tdd | tdd W d   dS 1 sPw   Y  dS )zTest that 3-character custom country codes work correctly.

        This reproduces issue #474 where a custom country code "IND" would be
        incorrectly resolved to "IN" (India) because it matched India's alpha3 code.
        N	IndonesiaINAid)namesioc_code	flag_code)IDINDr;   r   r   ro   INIndia)r   r   r   rl   r(   r   r   r   r   $test_alpha2_override_three_char_code   s   "z8TestCountriesObject.test_alpha2_override_three_char_codec                 C   rn   )NBSBAH)r   r   r   r   r   r   r   test_ioc_code   rq   z!TestCountriesObject.test_ioc_codec              	   C   s   | j dd ddidddidddd	d
E | tddd | tddd | tddd | tddd | tddd | tddd W d    d S 1 s\w   Y  d S )NzBahamas in Pajamasr   ro   r.   rv   KosovoKOS)r(   r   )r   r:   r0   r@   rx   XKr;   r   r   z3Should still use built-in code if only name changedr:   z4Should be empty since country was marked not presentr0   z9Should be empty since country exists but IOC code clearedr@   z5Should be empty for a custom country with no IOC coderx   z#Should use provided custom IOC coder   z=Should use IOC code for a custom country that provides a code)r   r   r   r   r   r   r   r   test_ioc_code_override   sL   
"z*TestCountriesObject.test_ioc_code_overridec                 C      t d}| |d d S )NBruneiBNr   by_namer   r   r'   r   r   r   test_fetch_by_name      
z&TestCountriesObject.test_fetch_by_namec                 C   r   )Nzbrunei darussalamr   r   r   r   r   r   test_fetch_by_name_official   r   z/TestCountriesObject.test_fetch_by_name_officialc                 C   r   )NbRuNeIr   r   r   r   r   r   #test_fetch_by_name_case_insensitive   r   z7TestCountriesObject.test_fetch_by_name_case_insensitivec                 C   r   )NzCzech RepublicCZr   r   r   r   r   test_fetch_by_name_old   r   z*TestCountriesObject.test_fetch_by_name_oldc                 C   r   )NzCzech republicr   r   r   r   r   r   'test_fetch_by_name_old_case_insensitive   r   z;TestCountriesObject.test_fetch_by_name_old_case_insensitivec                 C   s   t jddd}| |d d S )NzEstados Unidoses)languagerx   r   r   r   r   r   test_fetch_by_name_i18n   s   z+TestCountriesObject.test_fetch_by_name_i18nc                 C   rn   )Nr.   ro   )r   r   r   r   r   r   r   test_fetch_by_name_no_match   rq   z/TestCountriesObject.test_fetch_by_name_no_matchc                 C   sz   | j ddidddgidd$ | tdd | tdd	 | tdd	 W d    d S 1 s6w   Y  d S )
Nr(   Ozr   r-   Hobbiton)r:   r0   r2   r:   r0   )r   r   r   r   r   r   r   r   test_fetch_by_name_custom   s   
"z-TestCountriesObject.test_fetch_by_name_customc                 C   s&   t jddd}| t|h d d S )Nz([ao])\1T)regex>   CKCMSX)r   r   r   set)r   codesr   r   r   test_fetch_by_name_regex   s   z,TestCountriesObject.test_fetch_by_name_regexc                 C   s\   | j dddgiddiddd tt}W d    n1 sw   Y  | |g d	 d S )
Nr   r-   r   r(   r   r.   )r0   r:   r1   r2   )r0   r   r4   r5   )r:   r   r   r#   r   r   )r   list_countriesr   r   r   test_multiple_labels  s   

z(TestCountriesObject.test_multiple_labelsN)*r   r   r   r!   r%   r,   r8   r9   r>   rA   rB   rG   rM   rS   rV   rY   r[   pytestmarkskipifr   USE_I18Nrg   rm   rp   rs   ry   r|   r}   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r      sJ    
	(
r   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ejjej dddd Zejjej dddd Zejjej dddd Zejjej dddd Zejjej dddd  Zd!d" Zd#S )$CountriesFirstTestc                 C   sV   | j ddgd | ttd d ddgt  W d    d S 1 s$w   Y  d S )Nr0   r:   COUNTRIES_FIRSTrE   r5   r:   	Australiar   r   r#   r   r$   r   r   r   r   test_countries_first  s   
"z'CountriesFirstTest.test_countries_firstc                 C   sX   | j ddgdd | ttd d g dt  W d    d S 1 s%w   Y  d S )Nr0   r:   ------r   COUNTRIES_FIRST_BREAK   r5   r   ro   r   r   r   r   r   r   test_countries_first_break   s   "z-CountriesFirstTest.test_countries_first_breakc                 C   sn   | j g ddd tt}W d    n1 sw   Y  | |d d g dt  | t|td  d S )N)r@   r0   r:   r   r   r   r   r&   r   r#   r   r   r$   r   r    r   countries_listr   r   r   test_countries_first_some_valid*  s   

z2CountriesFirstTest.test_countries_first_some_validc                 C   s`   | j dgdd tt}W d    n1 sw   Y  | |d d t | t|t d S )Nr@   r   r   r"   r   r   r   r   r   test_countries_first_no_valid6  s
   
z0CountriesFirstTest.test_countries_first_no_validc                 C   s|   | j ddgdd tt}W d    n1 sw   Y  | t|td  dd |dd  D }|d |d d S )Nr0   r:   T)r   COUNTRIES_FIRST_REPEATr6   c                 S      g | ]}|d  qS r   r   .0itemr   r   r   
<listcomp>@      zBCountriesFirstTest.test_countries_first_repeat.<locals>.<listcomp>)r   r#   r   r   r   r    indexr   r   sorted_codesr   r   r   test_countries_first_repeat<  s   

z.CountriesFirstTest.test_countries_first_repeatc                 C   sJ   | j g dd | tttd  W d    d S 1 sw   Y  d S )Nr0   r:   r@   r   r6   r=   r   r   r   r   test_countries_first_lenD  r?   z+CountriesFirstTest.test_countries_first_lenc                 C   sL   | j g ddd | tttd  W d    d S 1 sw   Y  d S )Nr   r   r   r"   r=   r   r   r   r   test_countries_first_break_lenH  s
   "z1CountriesFirstTest.test_countries_first_break_lenc                 C   sF   | j dgdd | ttt W d    d S 1 sw   Y  d S )Nr@   r   r   r=   r   r   r   r   'test_countries_first_break_len_no_validN  s   "z:CountriesFirstTest.test_countries_first_break_len_no_validc                 C   sf   | j g ddd  tt}dd |d d D }| g d| W d    d S 1 s,w   Y  d S )Nr0   CAYETr   COUNTRIES_FIRST_SORTc                 S   r   r   r   r   r   r   r   r   W  r   zJCountriesFirstTest.test_sorted_countries_first_english.<locals>.<listcomp>r"   )r   r0   r   r   r   r   r   r   #test_sorted_countries_first_englishR  s   "z6CountriesFirstTest.test_sorted_countries_first_englishc                 C   sf   | j g ddd  tt}dd |d d D }| g d| W d    d S 1 s,w   Y  d S )Nr   Fr   c                 S   r   r   r   r   r   r   r   r   `  r   zLCountriesFirstTest.test_unsorted_countries_first_english.<locals>.<listcomp>r"   r   )r   r   unsorted_codesr   r   r   %test_unsorted_countries_first_english[  s   "z8CountriesFirstTest.test_unsorted_countries_first_englishr\   r]   c              
   C   s   | j g ddd7 t }td ztt}dd |d d D }| g d| W t| nt| w W d    d S 1 sCw   Y  d S )	Nr   Tr   eoc                 S   r   r   r   r   r   r   r   r   l  r   zMCountriesFirstTest.test_sorted_countries_first_translated.<locals>.<listcomp>r"   )r   r   r0   )r   r   rd   re   r#   r   r   )r   rf   r   r   r   r   r   &test_sorted_countries_first_translatedc  s   
"z9CountriesFirstTest.test_sorted_countries_first_translatedc              	   C   f   t  }t }z$td | |dd td | |dd W t| d S t| w Nr   r0   Nov-Zelandoen@testEndorr   TranslationFallbackCountriesr   rd   re   r   r(   r   trans_fall_countriesrf   r   r   r   *test_translation_fallback_from_common_namer  s   

z=CountriesFirstTest.test_translation_fallback_from_common_namec              	   C   r   r   r   r   r   r   r   'test_translation_fallback_from_old_name~  s   

z:CountriesFirstTest.test_translation_fallback_from_old_namec              	   C   s   t  }zTt d | tdd t d | tdd t j}| jd|did t`| tdd W d    n1 sDw   Y  W t | d S W t | d S t | w )	Nr   r0   r   r   r-   Middle Earthr;   r   )r   rd   re   r   r   r(   gettext_lazyr   )r   rf   gtlr   r   r   "test_translation_fallback_override  s   

z5CountriesFirstTest.test_translation_fallback_overridec              
   C   s   t j}| jdd|dt dgiid7 t  }z#t d | tdd t d | tdd	 W t | nt | w W d    d S 1 sNw   Y  d S )
Nr0   r   r   r-   r;   r   r   r   r   )r   r   r   rd   re   r   r   r(   )r   r   rf   r   r   r   (test_translation_fallback_override_names  s$   


"z;CountriesFirstTest.test_translation_fallback_override_namesc                 C   s\   | j dgddddgiddd tt}W d    n1 sw   Y  | |g d	 d S )
Nr0   r   r   r-   r   r.   r/   )r   r   r3   )r5   r   r   r4   r   r   r   r   r   test_first_multiple_labels  s   

	z-CountriesFirstTest.test_first_multiple_labelsN)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   r   r   r     s,    
	




r   c                   @   s0   e Zd Zdd Zejjej dddd Z	dS )TestCountriesPerformancec                 C   s   ddl }|  }tt}|  | }|  }tt}|  | }| || |dkr.|| ntd}| |dd|dd|dd	|dd
 dS )zFTest that Countries.__iter__ caches results per language (issue #454).r   Ninfg      @z&Caching not working: speedup was only z.1fz
x (first: z.4fzs, second: zs))timer#   r   r   floatassertGreater)r   r   startfirst_result
first_timesecond_resultsecond_timespeedupr   r   r   test_iter_caching  s$   
z*TestCountriesPerformance.test_iter_cachingr\   r]   c              	   C   s   t  }z?t d tt}dd |D d }t d tt}dd |D d }| ||d | |d | |d	 W t | d
S t | w )z9Test that cache is per-language, not global (issue #454).enc                 S      g | ]
\}}|d kr|qS DEr   r   r'   r(   r   r   r   r         zKTestCountriesPerformance.test_iter_caching_per_language.<locals>.<listcomp>r   r_   c                 S   r   r   r   r  r   r   r   r     r  zACache should be per-language, but got same name in both languagesGermanyDeutschlandN)r   rd   re   r#   r   assertNotEqualr   )r   rf   countries_en
de_name_encountries_de
de_name_der   r   r   test_iter_caching_per_language  s    

z7TestCountriesPerformance.test_iter_caching_per_languageN)
r   r   r   r   r   r   r   r   r   r  r   r   r   r   r     s    r   c                   @   rH   )TestCountriesCustomc                 C   s0   t  }| t|ddg | t|d d S )Nr4   r5   r6   )r   FantasyCountriesr   r#   r   )r   fantasy_countriesr   r   r   r8     s
   z(TestCountriesCustom.test_countries_limitN)r   r   r   r8   r   r   r   r   r     rL   r  )r   django.testr   django.utilsr   django_countriesr   r   r   django_countries.confr   django_countries.testsr   r    r$   r   r   r   r   r  r   r   r   r   <module>   s        27