o
    h6#                     @   sD  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
 d dlmZ d dlmZmZ d dlmZ G d	d
 d
eZejdddedddiidG dd deeZejdddedddiidG dd deeZejdddedddiidG dd deeZejdddedddiidG dd deeZdS )    )date)mock)TestCaseoverride_settings)Page)index)models)AdvertWithCustomUUIDPrimaryKey
SimplePage)WagtailTestUtilsc                   @   s*   e Zd ZdgZdd Zdd Zdd ZdS )	TestGetIndexedInstancesearchc                 C   s*   t jjjdd}t|}| || d S N   id)r   Authorobjectsgetr   get_indexed_instanceassertEqualselfobjindexed_instance r   i/var/www/html/ndineBlogger/venv/lib/python3.10/site-packages/wagtail/search/tests/test_index_functions.pytest_gets_instance   s   
z)TestGetIndexedInstance.test_gets_instancec                 C   s,   t jjjdd}t|j}| || d S r   )r   Novelr   r   r   r   book_ptrr   r   r   r   r   test_gets_specific_class   s   z/TestGetIndexedInstance.test_gets_specific_classc                 C   s:   t jdtddddd}|  t|j}| | d S )NzDon't index me!  
      d   titlepublication_datenumber_of_pages)r   r   r   saver   r   r   assertIsNoner   r   r   r   "test_blocks_not_in_indexed_objects   s   
z9TestGetIndexedInstance.test_blocks_not_in_indexed_objectsN)__name__
__module____qualname__fixturesr   r    r+   r   r   r   r   r      s
    r   z'wagtail.search.tests.DummySearchBackendT)createdefaultBACKEND)WAGTAILSEARCH_BACKENDSc                   @   s,   e Zd Zdd Zdd Zdd Zdd Zd	S )
TestInsertOrUpdateObjectc                 C   B   t jjjdtddddd}|   t| | j	| d S NTestr!   r"   r#   r$   r%   )
r   Bookr   r0   r   
reset_mockr   insert_or_update_objectaddassert_called_withr   backendr   r   r   r   test_inserts_object3      

z,TestInsertOrUpdateObject.test_inserts_objectc                 C   s@   t jdtddddd}|   t| | | jj d S r6   )	r   r8   r   r9   r   r:   assertFalser;   
mock_callsr=   r   r   r   !test_doesnt_insert_unsaved_object=   s   

z:TestInsertOrUpdateObject.test_doesnt_insert_unsaved_objectc                 C   sP   t jjdd}|jtddddd}|j}|   t| | j	
| d S )Nr   r   test)r&   slugcontent)instance)r   r   r   	add_childr
   page_ptrr9   r   r:   r;   r<   )r   r>   	root_pagepageunspecific_pager   r   r   test_converts_to_specific_pageG   s   

z7TestInsertOrUpdateObject.test_converts_to_specific_pagec                 C   s   t jjjdtddddd}td| j_|   | j	ddd	}t
| W d    n1 s1w   Y  | t|jd
 | d|jd  | d|jd  | d|jd  d S )Nr7   r!   r"   r#   r$   r%   wagtail.search.indexERRORlevelr   zLException raised while adding <Book: Test> into the 'default' search backendr   "Traceback (most recent call last):ValueError: Test)r   r8   r   r0   r   
ValueErrorr;   side_effectr9   
assertLogsr   r:   r   lenoutputassertInr   r>   r   cmr   r   r   test_catches_index_errorW   s   
z1TestInsertOrUpdateObject.test_catches_index_errorN)r,   r-   r.   r?   rC   rM   r\   r   r   r   r   r4   ,   s
    

r4   c                   @   s$   e Zd Zdd Zdd Zdd ZdS )TestRemoveObjectc                 C   r5   r6   )
r   r8   r   r0   r   r9   r   remove_objectdeleter<   r=   r   r   r   test_removes_objectr   r@   z$TestRemoveObject.test_removes_objectc                 C   s>   t jdtddddd}|   t| | j| d S r6   )r   r8   r   r9   r   r^   r_   r<   r=   r   r   r   test_removes_unsaved_object|   s   

z,TestRemoveObject.test_removes_unsaved_objectc                 C   s   t jjjdtddddd}|   td| j_| j	ddd	}t
| W d    n1 s1w   Y  | t|jd
 | d|jd  | d|jd  | d|jd  d S )Nr7   r!   r"   r#   r$   r%   rN   rO   rP   r   zNException raised while deleting <Book: Test> from the 'default' search backendr   rR   rS   )r   r8   r   r0   r   r9   rT   r_   rU   rV   r   r^   r   rW   rX   rY   rZ   r   r   r   r\      s   
z)TestRemoveObject.test_catches_index_errorN)r,   r-   r.   r`   ra   r\   r   r   r   r   r]   k   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S )TestSignalHandlersc                 C   sd   |    | jdd tjjjdtddddd}W d    n1 s$w   Y  | j| d S )	NTexecuter7   r!   r"   r#   r$   r%   )	r9   captureOnCommitCallbacksr   r8   r   r0   r   r;   r<   r=   r   r   r   test_index_on_create   s   
z'TestSignalHandlers.test_index_on_createc                 C   sV   |    | jdd tjjdd}W d    n1 sw   Y  | j| d S )NTrc   r7   text)r9   re   r	   r   r0   r;   r<   r=   r   r   r   *test_index_on_create_with_uuid_primary_key   s
   
z=TestSignalHandlers.test_index_on_create_with_uuid_primary_keyc                 C   s   t jjjdtddddd}|   d|_| jdd	 |  W d    n1 s+w   Y  | 	| j
jd
 | j
jd d }| 	|jd d S )Nr7   r!   r"   r#   r$   r%   Updated testTrc   r   r   )r   r8   r   r0   r   r9   r&   re   r)   r   r;   
call_count	call_argsr   r>   r   indexed_objectr   r   r   test_index_on_update   s   

z'TestSignalHandlers.test_index_on_updatec                 C   s   t jjdd}|   d|_| jdd |  W d    n1 s$w   Y  | | jj	d | jj
d d }| |jd d S )Nr7   rg   rj   Trc   r   r   )r	   r   r0   r9   rh   re   r)   r   r;   rk   rl   rm   r   r   r   *test_index_on_update_with_uuid_primary_key   s   

z=TestSignalHandlers.test_index_on_update_with_uuid_primary_keyc                 C   sl   t jjjdtddddd}|   | jdd |  W d    n1 s(w   Y  | j| d S )	Nr7   r!   r"   r#   r$   r%   Trc   )	r   r8   r   r0   r   r9   re   r_   r<   r=   r   r   r   test_index_on_delete   s   

z'TestSignalHandlers.test_index_on_deletec                 C   s^   t jjdd}|   | jdd |  W d    n1 s!w   Y  | j| d S )Nr7   rg   Trc   )r	   r   r0   r9   re   r_   r<   r=   r   r   r   *test_index_on_delete_with_uuid_primary_key   s   

z=TestSignalHandlers.test_index_on_delete_with_uuid_primary_keyc                 C   s   t jjjdtddddd}|   d|_tddd	|_| jd
d |j	dgd W d    n1 s5w   Y  | 
| jjd | jjd d }| 
|jd | 
|jtddd d S )Nr7   r!   r"   r#   r$   r%   rj   i     Trc   r&   )update_fieldsr   r   )r   r8   r   r0   r   r9   r&   r'   re   r)   r   r;   rk   rl   rm   r   r   r   3test_do_not_index_fields_omitted_from_update_fields   s   
zFTestSignalHandlers.test_do_not_index_fields_omitted_from_update_fieldsN)
r,   r-   r.   rf   ri   ro   rp   rq   rr   ru   r   r   r   r   rb      s    
rb   c                   @   s   e Zd Zdd ZdS ) TestSignalHandlersSearchDisabledc                 C   s|   t jjjdtddddd}| | jjd | | jj	 | 
  d|_|  | | jjd | | jj	 d S )	Nr7   r!   r"   r#   r$   r%   r   rj   )r   UnindexedBookr   r0   r   r   r;   rk   r*   rl   r9   r&   r)   r=   r   r   r   test_index_on_create_and_update   s   
z@TestSignalHandlersSearchDisabled.test_index_on_create_and_updateN)r,   r-   r.   rx   r   r   r   r   rv      s    rv   N)datetimer   unittestr   django.testr   r   wagtail.modelsr   wagtail.searchr   wagtail.test.searchr   wagtail.test.testapp.modelsr	   r
   wagtail.test.utilsr   r   patchr4   r]   rb   rv   r   r   r   r   <module>   sB    9)L