o
    h+(                     @   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di |S )N )r   objectscreate)modelkwargsr   r   d/var/www/html/ndineBlogger/venv/lib/python3.10/site-packages/wagtail/tests/test_translatablemixin.pymake_test_instance   s   r   T)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   	LANGUAGESkeysr   r   get_or_creategetr   another_localer   main_instancer    translated_model)selflanguage_codesr   r   r   r   setUp   s   
zTestTranslatableMixin.setUpc                 C   s   |  t| j | jg d S N)assertSequenceEquallistr'   get_translationsr(   r)   r   r   r   %test_get_translations_inclusive_false2      z;TestTranslatableMixin.test_get_translations_inclusive_falsec                 C   s&   |  t| jjdd| j| jg d S )NT)	inclusive)assertEqualr.   r'   r/   r(   r0   r   r   r   $test_get_translations_inclusive_true7   s   
z:TestTranslatableMixin.test_get_translations_inclusive_truec                 C   s   |  | j| j| j d S r,   )r4   r'   get_translationr   r0   r   r   r   test_get_translation=   s   z*TestTranslatableMixin.test_get_translationc                 C   s   |  | j| jj| j d S r,   )r4   r'   r6   r   idr0   r   r   r   $test_get_translation_using_locale_idB   r2   z:TestTranslatableMixin.test_get_translation_using_locale_idc                 C   sT   t | jd}| j|_| | j| j| j W d    d S 1 s#w   Y  d S Nr6   )r   objectr'   r(   return_valuer4   get_translation_or_noner&   r)   mock_get_translationr   r   r   /test_get_translation_or_none_return_translationG   s   "zETestTranslatableMixin.test_get_translation_or_none_return_translationc                 C   s\   | j   t| jd}| jj|_| | j| j	 W d    d S 1 s'w   Y  d S r:   )
r(   deleter   r;   r'   DoesNotExistside_effectassertIsNoner=   r&   r>   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   r0   r   r   r    test_has_translation_when_exists[   s   z6TestTranslatableMixin.test_has_translation_when_existsc                 C   s   |  | j| jj d S r,   )rF   r'   rG   r   r8   r0   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(   rA   assertFalser'   rG   r&   r0   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(   rA   r'   copy_for_translationr&   assertNotEqualr4   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   )r4   r'   get_translation_modelr   r   r   )r)   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 )NrQ   rL   )r   r   rM   r&   rN   r4   r    r   )r)   instancerO   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_childrenrL   )r	   r   r   r
   r   rM   r&   rX   r%   rY   rN   r4   r    r   rW   )r)   rU   rO   instance_child
copy_childinstance_translatable_childcopy_translatable_childr   r   r   +test_copy_clusterable_model_for_translation}   s4   



zATestTranslatableMixin.test_copy_clusterable_model_for_translationN)__name__
__module____qualname__r+   r1   r5   r7   r9   r@   rE   rH   rI   rK   rP   rT   rV   r^   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%   	en_localer   	fr_localer   en_instancer    fr_instancer0   r   r   r   r+      s   zTestLocalized.setUpc                 C   sD   |  d | jj}W d    n1 sw   Y  | || j d S )N   )assertNumQueriesre   	localizedr4   r)   rU   r   r   r   test_localized_same_language   s   
z*TestLocalized.test_localized_same_languagec                 C   sD   |  d | jj}W d    n1 sw   Y  | || j d S )N   )rh   rf   ri   r4   re   rj   r   r   r   !test_localized_different_language   s   
z/TestLocalized.test_localized_different_languageN)r_   r`   ra   r+   rk   rm   r   r   r   r   rb      s    rb   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   checkr4   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_w | t|d d S )Nr    r   5unique_translation_key_locale_%(app_label)s_%(class)sfieldsnamer   )	r   _metaunique_togetherr   UniqueConstraintconstraintsro   r4   rp   r)   previous_unique_togetherrr   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_w | t|d | |d tj | |d j	d | |d j
d | |d jd d S )Nrg   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   rx   ry   ro   r4   rp   assertIsInstancer   Errorr8   msghintr|   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_w | t|d | |d t	j
 | |d jd | |d jd | |d jd	 d S )
Nr    r   rt   ru   rg   r   r   zli18n.TestModel should not have both UniqueConstraint and unique_together for: ('translation_key', 'locale').z4Remove unique_together in favor of UniqueConstraint.)r   rz   r   rx   r{   ro   r4   rp   r   r   r   r8   r   r   rq   r   r   r   :test_error_with_both_unique_constraint_and_unique_together   s&   
zKTestSystemChecks.test_error_with_both_unique_constraint_and_unique_togetherN)r_   r`   ra   rs   r~   r   r   r   r   r   r   rn      s
    rn   r,   )unittest.mockr   django.confr   django.corer   	django.dbr   django.testr   r   wagtail.modelsr   wagtail.test.i18n.modelsr	   r
   r   r   r   r   r   rb   rn   r   r   r   r   <module>   s    
	 
