a
    Dg+(                     @   s   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lmZ d dlmZmZmZmZmZ dd	d
Ze
ddG dd de	Ze
ddG dd de	ZG dd de	ZdS )    )patch)settings)checks)models)TestCaseoverride_settings)Locale)ClusterableTestModelClusterableTestModelChild%ClusterableTestModelTranslatableChildInheritedTestModel	TestModelNc                 K   s   | d u rt } | jjf i |S N)r   objectscreate)modelkwargs r   c/var/www/lab.imftr.de/x/nb_venv/lib/python3.9/site-packages/wagtail/tests/test_translatablemixin.pymake_test_instance   s    r   T)ZWAGTAIL_I18N_ENABLEDc                   @   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dd Zdd Zdd ZdS )TestTranslatableMixinc                 C   sv   t tj }|D ]}tjj|d qtjjdd| _tjjdd| _	t
| jdd| _t
| j	| jjdd| _t
  d S )Nlanguage_codeenfr
Main ModellocaletitlezTranslated Modelr   translation_keyr   )dictr   Z	LANGUAGESkeysr   r   Zget_or_creategetr   another_localer   main_instancer    translated_model)selfZlanguage_codesr   r   r   r   setUp   s    zTestTranslatableMixin.setUpc                 C   s   |  t| j | jg d S r   )ZassertSequenceEquallistr%   get_translationsr&   r'   r   r   r   %test_get_translations_inclusive_false2   s    z;TestTranslatableMixin.test_get_translations_inclusive_falsec                 C   s&   |  t| jjdd| j| jg d S )NT)Z	inclusive)assertEqualr)   r%   r*   r&   r+   r   r   r   $test_get_translations_inclusive_true7   s    
z:TestTranslatableMixin.test_get_translations_inclusive_truec                 C   s   |  | j| j| j d S r   )r-   r%   get_translationr   r+   r   r   r   test_get_translation=   s    z*TestTranslatableMixin.test_get_translationc                 C   s   |  | j| jj| j d S r   )r-   r%   r/   r   idr+   r   r   r   $test_get_translation_using_locale_idB   s    z:TestTranslatableMixin.test_get_translation_using_locale_idc                 C   sR   t | jd0}| j|_| | j| j| j W d    n1 sD0    Y  d S Nr/   )r   objectr%   r&   Zreturn_valuer-   get_translation_or_noner$   r'   Zmock_get_translationr   r   r   /test_get_translation_or_none_return_translationG   s    zETestTranslatableMixin.test_get_translation_or_none_return_translationc                 C   sZ   | j   t| jd.}| jj|_| | j| j	 W d    n1 sL0    Y  d S r3   )
r&   deleter   r4   r%   ZDoesNotExistZside_effectZassertIsNoner5   r$   r6   r   r   r   (test_get_translation_or_none_return_noneQ   s    

z>TestTranslatableMixin.test_get_translation_or_none_return_nonec                 C   s   |  | j| j d S r   )
assertTruer%   has_translationr   r+   r   r   r    test_has_translation_when_exists[   s    z6TestTranslatableMixin.test_has_translation_when_existsc                 C   s   |  | j| jj d S r   )r:   r%   r;   r   r1   r+   r   r   r   0test_has_translation_when_exists_using_locale_id^   s    zFTestTranslatableMixin.test_has_translation_when_exists_using_locale_idc                 C   s"   | j   | | j| j d S r   )r&   r8   ZassertFalser%   r;   r$   r+   r   r   r   %test_has_translation_when_none_existsa   s    
z;TestTranslatableMixin.test_has_translation_when_none_existsc                 C   s\   | j   | jj| jd}| || j | |j| jj | |j| j | d|j	 d S )Nr   r   )
r&   r8   r%   copy_for_translationr$   assertNotEqualr-   r    r   r   )r'   copyr   r   r   test_copy_for_translatione   s    
z/TestTranslatableMixin.test_copy_for_translationc                 C   s0   |  | j t ttd}|  | t d S )Nr   )r-   r%   Zget_translation_modelr   r   r   )r'   Zinherited_modelr   r   r   test_get_translation_modeln   s    
z0TestTranslatableMixin.test_get_translation_modelc                 C   sH   t td}|j| jd}| || | |j|j | |j| j d S )NrD   r?   )r   r   r@   r$   rA   r-   r    r   )r'   instancerB   r   r   r   )test_copy_inherited_model_for_translationu   s
    
z?TestTranslatableMixin.test_copy_inherited_model_for_translationc                 C   s   t jjdtddgtddgd}|j| jd}|j }|j }|j	 }|j	 }| 
|| | |j|j | |j| j | 
|| | |jd | 
|| | |jd | |j|j | |j| j d S )NzA test clusterable modelzA non-translatable child object)fieldzA translatable child object)r   childrentranslatable_childrenr?   )r	   r   r   r
   r   r@   r$   rI   r#   rJ   rA   r-   r    r   rH   )r'   rF   rB   Zinstance_childZ
copy_childZinstance_translatable_childZcopy_translatable_childr   r   r   +test_copy_clusterable_model_for_translation}   s4    



zATestTranslatableMixin.test_copy_clusterable_model_for_translationN)__name__
__module____qualname__r(   r,   r.   r0   r2   r7   r9   r<   r=   r>   rC   rE   rG   rK   r   r   r   r   r      s   

	r   c                   @   s$   e Zd Zdd Zdd Zdd ZdS )TestLocalizedc                 C   sF   t j | _t jjdd| _t| jdd| _t| j| jjdd| _	d S )Nr   r   r   r   r   )
r   r   r#   Z	en_localer   Z	fr_localer   en_instancer    fr_instancer+   r   r   r   r(      s    zTestLocalized.setUpc                 C   sD   |  d | jj}W d    n1 s(0    Y  | || j d S )N   )assertNumQueriesrP   	localizedr-   r'   rF   r   r   r   test_localized_same_language   s    &z*TestLocalized.test_localized_same_languagec                 C   sD   |  d | jj}W d    n1 s(0    Y  | || j d S )N   )rS   rQ   rT   r-   rP   rU   r   r   r   !test_localized_different_language   s    &z/TestLocalized.test_localized_different_languageN)rL   rM   rN   r(   rV   rX   r   r   r   r   rO      s   rO   c                   @   s,   e Zd Zdd Zdd Zdd Zdd Zd	S )
TestSystemChecksc                 C   s   t  }| t|d d S )Nr   )r   checkr-   lenr'   errorsr   r   r   $test_unique_together_raises_no_error   s    z5TestSystemChecks.test_unique_together_raises_no_errorc                 C   sl   t jj}z<g t j_tjddgddgt j_t  }W |t j_g t j_n|t j_g t j_0 | t|d d S )Nr    r   5unique_translation_key_locale_%(app_label)s_%(class)sfieldsnamer   )	r   _metaunique_togetherr   UniqueConstraintconstraintsrZ   r-   r[   r'   Zprevious_unique_togetherr]   r   r   r   &test_unique_constraint_raises_no_error   s    


z7TestSystemChecks.test_unique_constraint_raises_no_errorc                 C   s   t jj}zg t j_t  }W |t j_n
|t j_0 | t|d | |d tj | |d j	d | |d j
d | |d jd d S )NrR   r   wagtailcore.E003z[i18n.TestModel is missing a UniqueConstraint for the fields: ('translation_key', 'locale').zAdd models.UniqueConstraint(fields=('translation_key', 'locale'), name='unique_translation_key_locale_i18n_testmodel') to TestModel.Meta.constraints.)r   rc   rd   rZ   r-   r[   assertIsInstancer   Errorr1   msghintrg   r   r   r   Ktest_raises_error_if_both_unique_constraint_and_unique_together_are_missing   s     
z\TestSystemChecks.test_raises_error_if_both_unique_constraint_and_unique_together_are_missingc                 C   s   z,t jddgddgtj_t }W g tj_n
g tj_0 | t|d | |d t	j
 | |d jd | |d jd | |d jd	 d S )
Nr    r   r_   r`   rR   r   ri   zli18n.TestModel should not have both UniqueConstraint and unique_together for: ('translation_key', 'locale').z4Remove unique_together in favor of UniqueConstraint.)r   re   r   rc   rf   rZ   r-   r[   rj   r   rk   r1   rl   rm   r\   r   r   r   :test_error_with_both_unique_constraint_and_unique_together   s&    
zKTestSystemChecks.test_error_with_both_unique_constraint_and_unique_togetherN)rL   rM   rN   r^   rh   rn   ro   r   r   r   r   rY      s   rY   )N)Zunittest.mockr   Zdjango.confr   Zdjango.corer   Z	django.dbr   Zdjango.testr   r   Zwagtail.modelsr   Zwagtail.test.i18n.modelsr	   r
   r   r   r   r   r   rO   rY   r   r   r   r   <module>   s   	
 
