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	 d dl
mZmZ d dlmZmZ d dlmZmZmZmZmZ d d	lmZ d d
lmZ d dlmZmZmZmZ d dlm Z  G dd deZ!G dd deZ"G dd deZ#G dd de eZ$G dd de eZ%G dd deZ&dS )    )StringIO)mock)get_user_model)ContentType)
management)CountQ)TestCaseTransactionTestCase)LocalePagePageViewRestrictionSiteWorkflow)	MATCH_ALL)page_unpublished)	EventPage
SimplePageSingleEventPage
StreamPage)WagtailTestUtilsc                   @   sb  e Zd Zdg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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d0d1 Zd2d3 Zd4d5 Zd6d7 Zd8d9 Zd:d; Z d<d= Z!d>d? Z"d@dA Z#dBdC Z$dDdE Z%dFdG Z&dHdI Z'dJdK Z(dLdM Z)dNdO Z*dPdQ Z+dRdS Z,dTdU Z-dVS )WTestPageQuerySet	test.jsonc                 C   sJ   t j }|D ]}| |j qt jjdd}| |j|jd  d S )N/home/url_pathid)r   objectslive
assertTruegetfilterr   exists)selfpagespagehomepage r(   `/var/www/html/ndineBlogger/venv/lib/python3.10/site-packages/wagtail/tests/test_page_queryset.py	test_live   
   
zTestPageQuerySet.test_livec                 C   sJ   t j }|D ]}| |j qt jjdd}| |j|jd	  d S )N!/home/events/someone-elses-event/r   r   )
r   r   not_liveassertFalser   r!   r    r"   r   r#   r$   r%   r&   eventr(   r(   r)   test_not_live$   r+   zTestPageQuerySet.test_not_livec                 C   sJ   t j }|D ]}| |j qt jjdd}| |j|jd  d S N/home/events/r   r   )	r   r   in_menur    show_in_menusr!   r"   r   r#   )r$   r%   r&   events_indexr(   r(   r)   test_in_menu/   r+   zTestPageQuerySet.test_in_menuc                 C   s:   t j }|D ]}| |j q| |jdd  d S )N   r   )r   r   not_in_menur.   r5   r    r"   r#   r$   r%   r&   r(   r(   r)   test_not_in_menu:   s   
z!TestPageQuerySet.test_not_in_menuc                 C   s>   t jjdd}t j|}| | d | | | d S Nr   r   r8   )r   r   r!   r&   assertEqualcountfirst)r$   r'   r%   r(   r(   r)   	test_pageD   s   zTestPageQuerySet.test_pagec                 C   sR   t jjdd}t j|}| | t j  d  |D ]}| || qd S r<   )r   r   r!   not_pager=   r>   allassertNotEqualr$   r'   r%   r&   r(   r(   r)   test_not_pageL   s   zTestPageQuerySet.test_not_pagec                 C   sD   t jjdd}t j|}|D ]}| | j|jd  qd S r2   	r   r   r!   descendant_ofr    get_ancestorsr"   r   r#   r$   r6   r%   r&   r(   r(   r)   test_descendant_ofU   s
   z#TestPageQuerySet.test_descendant_ofc                 C   sh   t jjdd}t jj|dd}|D ]}| ||kp#| j|jd  q| |j|jd  d S Nr3   r   T	inclusiver   rF   rI   r(   r(   r)   test_descendant_of_inclusive]   s   z-TestPageQuerySet.test_descendant_of_inclusivec                 C   s\   t jjdd}t j|}|D ]}| | j|jd  q| 	|j|jd  d S r2   )
r   r   r!   not_descendant_ofr.   rH   r"   r   r#   r    rI   r(   r(   r)   test_not_descendant_ofk   s
   z'TestPageQuerySet.test_not_descendant_ofc                 C   s`   t jjdd}t jj|dd}|D ]}| | j|jd  q| |j|jd  d S rK   )	r   r   r!   rO   r.   rH   r"   r   r#   rI   r(   r(   r)    test_not_descendant_of_inclusivev   s
   z1TestPageQuerySet.test_not_descendant_of_inclusivec                 C   8   t jjdd}t j|}|D ]
}| | | qd S )Nr   r   )r   r   r!   child_ofr=   
get_parentrD   r(   r(   r)   test_child_of   
   zTestPageQuerySet.test_child_ofc                 C   rR   )Nr3   r   )r   r   r!   not_child_ofrC   rT   rI   r(   r(   r)   test_not_child_of   rV   z"TestPageQuerySet.test_not_child_ofc                 C   sj   t jjdd}t jjdd}t jjdd}t j|}| | d | |d | | |d | d S )Nr8   r   r   r   r3      r   r   r   r!   ancestor_ofr=   r>   r$   	root_pager'   r6   r%   r(   r(   r)   test_ancestor_of   s   z!TestPageQuerySet.test_ancestor_ofc                 C   s~   t jjdd}t jjdd}t jjdd}t jj|dd}| | d | |d	 | | |d | | |d
 | d S )Nr8   r   r   r   r3   TrL      r   rY   rZ   r\   r(   r(   r)   test_ancestor_of_inclusive   s   z+TestPageQuerySet.test_ancestor_of_inclusivec                 C   st   t jjdd}t jjdd}t jjdd}t j|}|D ]}| || | || q| |j|jd  d S )Nr8   r   r   r   r3   )	r   r   r!   not_ancestor_ofrC   r    r"   r   r#   r$   r]   r'   r6   r%   r&   r(   r(   r)   test_not_ancestor_of   s   z%TestPageQuerySet.test_not_ancestor_ofc                 C   sl   t jjdd}t jjdd}t jjdd}t jj|dd}|D ]}| || | || | || qd S )Nr8   r   r   r   r3   TrL   )r   r   r!   ra   rC   rb   r(   r(   r)   test_not_ancestor_of_inclusive   s   z/TestPageQuerySet.test_not_ancestor_of_inclusivec                 C   sL   t jjdd}t jjdd}t j|}| | d | |d | d S )Nr   r   r3   r8   r   )r   r   r!   	parent_ofr=   r>   )r$   r'   r6   r%   r(   r(   r)   test_parent_of   s
   zTestPageQuerySet.test_parent_ofc                 C   sZ   t jjdd}t jjdd}t j|}|D ]}| || q| |j|jd  d S )Nr   r   r3   r   )	r   r   r!   not_parent_ofrC   r    r"   r   r#   )r$   r'   r6   r%   r&   r(   r(   r)   test_not_parent_of   s   z#TestPageQuerySet.test_not_parent_ofc                 C   s^   t jjdd}t jjdd}t j|}|D ]
}| | | q| |j|jd	  dS )zx
        sibling_of should default to an inclusive definition of sibling
        if 'inclusive' flag not passed
        r3   r   /home/events/christmas/r   N
r   r   r!   
sibling_ofr=   rT   r    r"   r   r#   r$   r6   r0   r%   r&   r(   r(   r)   test_sibling_of_default   s   z(TestPageQuerySet.test_sibling_of_defaultc                 C   b   t jjdd}t jjdd}t jj|dd}|D ]
}| | | q| |j|jd	  d S Nr3   r   ri   FrL   r   )
r   r   r!   rk   r=   rT   r.   r"   r   r#   rl   r(   r(   r)   test_sibling_of_exclusive      z*TestPageQuerySet.test_sibling_of_exclusivec                 C   rn   Nr3   r   ri   TrL   r   rj   rl   r(   r(   r)   test_sibling_of_inclusive   rq   z*TestPageQuerySet.test_sibling_of_inclusivec                 C   sv   t jjdd}t jjdd}t j|}|D ]
}| | | q| |j|jd	  | 
|j|jd	  dS )z
        not_sibling_of should default to an inclusive definition of sibling -
        i.e. eliminate self from the results as well -
        if 'inclusive' flag not passed
        r3   r   ri   r   Nr   r   r!   not_sibling_ofrC   rT   r.   r"   r   r#   r    rl   r(   r(   r)   test_not_sibling_of_default   s   z,TestPageQuerySet.test_not_sibling_of_defaultc                 C   s   t jjdd}t jjdd}t jj|dd}|D ]}||kr&| | | q| |j|jd	  | |j|jd	  d S ro   )
r   r   r!   ru   rC   rT   r    r"   r   r#   rl   r(   r(   r)   test_not_sibling_of_exclusive  s   z.TestPageQuerySet.test_not_sibling_of_exclusivec                 C   sz   t jjdd}t jjdd}t jj|dd}|D ]
}| | | q| |j|jd	  | 
|j|jd	  d S rr   rt   rl   r(   r(   r)   test_not_sibling_of_inclusive!  s   z.TestPageQuerySet.test_not_sibling_of_inclusivec                 C   s\   t jt}|D ]	}| |jt qt jjdd}| || t jjdd}| || d S Nr,   r   /home/events/saint-patrick/)r   r   typer   assertIsInstancespecificr!   assertInr/   r(   r(   r)   	test_type0     zTestPageQuerySet.test_typec                 C   s|   t jtt}|D ]}| |jttf q	t jjdd}| || t jjdd}| || t jjdd}| || d S )Nr,   r   rz   /home/about-us/)	r   r   r{   r   r   r|   r}   r!   r~   r$   r%   r&   r0   about_usr(   r(   r)   test_type_with_multiple_models@  s   z/TestPageQuerySet.test_type_with_multiple_modelsc                 C   \   t jt}|D ]	}| |jt qt jjdd}| || t jjdd}| || d S Nr   r   r   )r   r   not_typer   assertNotIsInstancer}   r!   r~   r$   r%   r&   r   r'   r(   r(   r)   test_not_typeT  s   zTestPageQuerySet.test_not_typec                 C   sb   t jtt}|D ]}| |jttf q	t jjdd}| || t jjdd}| 	|| d S r   )
r   r   r   r   r   r   r}   r!   assertNotInr~   r   r(   r(   r)   "test_not_type_with_multiple_modelsc  s   z3TestPageQuerySet.test_not_type_with_multiple_modelsc                 C   s\   t jt}|D ]	}| |jt qt jjdd}| || t jjdd}| || d S ry   )	r   r   
exact_typer   assertIsspecific_classr!   r~   r   )r$   r%   r&   r0   single_eventr(   r(   r)   test_exact_typer  r   z TestPageQuerySet.test_exact_typec                 C   s   t jtt }|D ]}| |jtt f q	t jjdd}| || t jjdd}| || t jjdd}| || t jjdd}| || d S )Nr,   r   rz   r   r   )r   r   r   r   r~   r   r!   r   )r$   r%   r&   r0   r   r'   r   r(   r(   r)   $test_exact_type_with_multiple_models  s   z5TestPageQuerySet.test_exact_type_with_multiple_modelsc                 C   r   )Nr   r   rz   )r   r   not_exact_typer   assertIsNotr   r!   r~   )r$   r%   r&   r'   r0   r(   r(   r)   test_not_exact_type  r   z$TestPageQuerySet.test_not_exact_typec                 C   sb   t jtt }|D ]}| |jtt f q	t jjdd}| || t jjdd}| || d S )Nrz   r   r   )r   r   r   r   r   r   r!   r~   r   r(   r(   r)   (test_not_exact_type_with_multiple_models  s   z9TestPageQuerySet.test_not_exact_type_with_multiple_modelsc                 C      t jjdd}t jjdd}t jjdd}tjj|dd | d2 t j }| |j|j	d
  | |j|j	d
  | |j|j	d
  W d    d S 1 sWw   Y  d S 	Nr3   r   ri   r   hello)r&   password   r   )r   r   r!   r   createassertNumQueriespublicr    r"   r   r#   r.   r$   r6   r0   r'   r%   r(   r(   r)   test_public     
"zTestPageQuerySet.test_publicc                 C   r   r   )r   r   r!   r   r   r   
not_publicr.   r"   r   r#   r    r   r(   r(   r)   test_not_public  r   z TestPageQuerySet.test_not_publicc                 C   r   r   )r   r   r!   r   r   r   privater.   r"   r   r#   r    r   r(   r(   r)   test_private  r   zTestPageQuerySet.test_privatec                 C   s,   t j   tj  }| |d d S Nr   )r   r   rB   deleter   r   r>   r=   )r$   r>   r(   r(   r)   !test_private_with_no_private_page  s   z2TestPageQuerySet.test_private_with_no_private_pagec                 C   s4   t jt}t }||O }| t j|  d S N)r   r   type_qr   r   r    r"   r#   )r$   r   queryr(   r(   r)   test_merge_queries  s   z#TestPageQuerySet.test_merge_queriesc                 C   s$   t j   | t j d d S r   )r   r   rB   r   r=   r>   r$   r(   r(   r)   test_delete_queryset	  s   z%TestPageQuerySet.test_delete_querysetc                 C   s:   |  t tj  W d    d S 1 sw   Y  d S r   )assertRaisesAttributeErrorr   r   r   r   r(   r(   r)   'test_delete_is_not_available_on_manager  s   "z8TestPageQuerySet.test_delete_is_not_available_on_managerc                 C   s   t jjdd}tjjdd}t jjdd}|jt dd||jd	d
}| d t j|}| 	t
||g W d    n1 sAw   Y  | d t jj|ddd}| 	t
|||g W d    d S 1 slw   Y  d S )Nr   r   frlanguage_coder8   depthFrench homepagehome-frtitlesluglocaletranslation_keyinstanceTrL   r   )r   r   r!   r   r   	add_childr   r   translation_ofassertListEquallistorder_by)r$   en_homepage	fr_localer]   fr_homepagetranslationsr(   r(   r)   test_translation_of  s.   	"z$TestPageQuerySet.test_translation_ofc                 C   s*  t jjdd}tjjdd}t jjdd}|jt dd||jd	d
}| d tt j	|}W d    n1 s:w   Y  t j
 D ]}||fv rR| || qD| || qD| d tt jj	|dd}W d    n1 ssw   Y  t j
 D ]}|||fv r| || q}| || q}d S )Nr   r   r   r   r8   r   r   r   r   r   TrL   )r   r   r!   r   r   r   r   r   r   not_translation_ofrB   r   r~   )r$   r   r   r]   r   r   r&   r(   r(   r)   test_not_translation_of+  s8   	
z(TestPageQuerySet.test_not_translation_ofc                 C   sd  t jjdd}t jjdd}t j }tj }|j|jg}i }i }|D ].}|  |||}	|	j	}
|
j
j|
d dd |||}|  |||j< |j	j
||j< q%t jj|j|jfd}d|dgd	| d
gg}|D ]\}}}| j|d | | dd | D }W d    n1 sw   Y  |D ]}||j }| d | |j||j g W d    n1 sw   Y  | d | |jd j	j
||j  W d    n1 sw   Y  | d | |j W d    n1 sw   Y  | d | |j||j  W d    n	1 sw   Y  qW d    n	1 s*w   Y  qmd S )Nr   r   r3   approve)useraction_name)pk__inbaserY   r}   r   )casec                 S   s   i | ]}|j |qS r(   pk).0r&   r(   r(   r)   
<dictcomp>o  s    zBTestPageQuerySet.test_prefetch_workflow_states.<locals>.<dictcomp>r   )r   r   r!   r   r?   r   r}   save_revisionstartcurrent_task_statetask	on_actionrefresh_from_dbr   r"   subTestr   prefetch_workflow_statesr=   _current_workflow_statesr    workflow_in_progresscurrent_workflow_state)r$   homeevent_indexr   workflow
test_pagesworkflow_statescurrent_tasksr&   approved_workflow_state
task_stateworkflow_stater   queriesr   num_queriesqueried_pagesr@   r(   r(   r)   test_prefetch_workflow_statesQ  sj   



z.TestPageQuerySet.test_prefetch_workflow_statesN).__name__
__module____qualname__fixturesr*   r1   r7   r;   r@   rE   rJ   rN   rP   rQ   rU   rX   r^   r`   rc   rd   rf   rh   rm   rp   rs   rv   rw   rx   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r(   r(   r(   r)   r      sX    
	
	&r   c                   @   s"   e Zd ZdgZdd Zdd ZdS )TestPageQueryInSiter   c                 C   sz   t dddd| _t j| jd t dddd| _| jj| jd tjjd	d
tjj	| jj
ddd| _t jj	dd| _d S )NzSite 2 pagesite_2_pageHello)r   r   contentr   zSite 2 subpagesite_2_subpagezHello againzexample.comi  r   F)hostnameportr]   is_default_siter   r   )r   r   r   get_first_root_noder   r   r   r   r   r!   r   site_2about_us_pager   r(   r(   r)   setUp  s&   zTestPageQueryInSite.setUpc                 C   s<   t j| j}| | j| | | j| | | j| d S r   )	r   r   in_siter   r~   r   r   r   r   )r$   site_2_pagesr(   r(   r)   test_in_site  s   z TestPageQueryInSite.test_in_siteN)r   r   r   r   r   r   r(   r(   r(   r)   r     s    r   c                   @   2   e Zd ZdgZdd Zdd Zdd Zdd	 Zd
S )TestPageQuerySetSearchr   c                 C   sV   t jjddgd}| | d | tjjddj| | tjjddj| d S )Nmoonlocation)fieldsrY   )/home/events/tentative-unpublished-event/r   r,   )	r   r   searchr=   r>   r~   r   r!   r}   r$   r%   r(   r(   r)   test_search  s   z"TestPageQuerySetSearch.test_searchc                 C   sz   t jjddd}| t|tjjddjg t jjddd}t|dd d	}| |tjjd
djtjjddjg d S )Nzmoon poniesand)operatorr  r   orc                 S   s   | j S r   r   )r&   r(   r(   r)   <lambda>  s    z7TestPageQuerySetSearch.test_operators.<locals>.<lambda>)keyr,   )	r   r   r  r=   r   r   r!   r}   sorted)r$   resultssorted_resultsr(   r(   r)   test_operators  s(   	z%TestPageQuerySetSearch.test_operatorsc                 C   s   t jdjddgdd}| t|tjjddjtjjddjg t jd	jddgdd}| t|tjjddjtjjddjg d S )
Nr   r  r  F)r  order_by_relevancer,   r   r  	-url_path)	r   r   r   r  r=   r   r   r!   r}   r  r(   r(   r)   test_custom_order  s0   
z(TestPageQuerySetSearch.test_custom_orderc              	      s   g   fdd}t | zNtjjdd}|   tjjdd}tjjdd}tjjdd}| 	|j
 | 	|j
 | t|f  | t|f  | t|f  W t | d S t | w )Nc                    s     | |f d S r   )append)senderr   kwargsunpublish_signals_firedr(   r)   page_unpublished_handler  s   zGTestPageQuerySetSearch.test_unpublish.<locals>.page_unpublished_handlerr3   r   ri   rz   r  )r   connectr   r   r!   get_children	unpublishr   r   r.   r   r~   r   
disconnect)r$   r  r6   	christmassaint_patrickunpublished_eventr(   r  r)   test_unpublish  s&   
z%TestPageQuerySetSearch.test_unpublishN)r   r   r   r   r	  r  r  r#  r(   r(   r(   r)   r    s    r  c                   @   s   e Zd ZdZdg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d  Zd!d" Zd#d$ Zd%d& Zd'd( Zd)d* Zd+d, Zd-S ).TestSpecificQuerya  
    Test the .specific() queryset method. This is isolated in its own test case
    because it is sensitive to database changes that might happen for other
    tests.

    The fixture sets up a page structure like:

    =========== =========================================
    Type        Path
    =========== =========================================
    Page        /
    Page        /home/
    SimplePage  /home/about-us/
    EventIndex  /home/events/
    EventPage   /home/events/christmas/
    EventPage   /home/events/someone-elses-event/
    EventPage   /home/events/tentative-unpublished-event/
    SimplePage  /home/other/
    EventPage   /home/other/special-event/
    =========== =========================================
    test_specific.jsonc                 C   s0   t j  | _t j  jtdd| _d S )Nr   r>   )r   r   r   r}   
live_pagesannotater   live_pages_with_annotationsr   r(   r(   r)   r   0  s   zTestSpecificQuery.setUpc              	   C   s   t jjdd}| d |  }W d    n1 sw   Y  | d t|}W d    n1 s6w   Y  | |t | t	|d |D ]+}|j
}| }| || | d | ||j W d    n1 sqw   Y  qKd S )Nr   r   r   r      )r   r   r!   r   get_descendantsr}   r   r|   r=   lencontent_typemodel_classr   )r$   rootqsr%   r&   r-  modelr(   r(   r)   test_specific6  s$   
zTestSpecificQuery.test_specificc                 C   s   |  d tj dd d  }W d    n1 sw   Y  |  d t|}W d    n1 s7w   Y  | t|d | |tjj	ddjtjj	ddjtjj	ddjg d S )Nr   r  r_   z/home/other/special-event/r   /home/other/ri   )
r   r   r   r   r   r}   r   r=   r,  r!   r$   r0  r%   r(   r(   r)   test_filtering_before_specificQ  s   
z0TestSpecificQuery.test_filtering_before_specificc              	   C   s   |  d tj   dd d }W d    n1 s w   Y  |  d t|}W d    n1 s9w   Y  | t	|d | |tjj
ddjtjj
ddjtjj
ddjtjj
ddjg d S )	Nr   r  r   r3  r   ri   r3   r   )r   r   r   r}   r   r4   r   r   r=   r,  r!   r4  r(   r(   r)   test_filtering_after_specificg  s   "
z/TestSpecificQuery.test_filtering_after_specificc                 C   s   |  d t| j}| t|d W d    n1 sw   Y  |  d t| j}| t|d W d    d S 1 s@w   Y  d S )N   r*  )r   r   r'  r=   r,  r)  r  r(   r(   r)   Ctest_specific_query_with_annotations_performs_no_additional_queries~  s   

"zUTestSpecificQuery.test_specific_query_with_annotations_performs_no_additional_queriesc                 C   s|   t j }|  }| jj|dd | jj|dd t j  j	t
dd}| | jd | | jd d S )NFr   comment_notificationssubscriberssubscribers_countr8   )r   r   r   create_test_userr?   r;  r   lastr}   r(  r   r=   r=  )r$   r%   r   r  r(   r(   r)   #test_specific_query_with_annotation  s   
z5TestSpecificQuery.test_specific_query_with_annotationc                 C   s   |  d ttjt jddd}W d    n1 sw   Y  | t	|d |  d |D ]}| 
|j q4W d    d S 1 sHw   Y  d S )NrY   
feed_imageTfor_specific_subqueriesr   r   )r   r   r   r   r{   r   r}   select_relatedr=   r,  r    rA  r:   r(   r(   r)   %test_specific_subquery_select_related  s   
"z7TestSpecificQuery.test_specific_subquery_select_relatedc                 C   B   |  t tj jdd W d    d S 1 sw   Y  d S NTrB  )r   
ValueErrorr   r   rB   rD  r   r(   r(   r)   4test_specific_subquery_select_related_without_fields  s   "zFTestSpecificQuery.test_specific_subquery_select_related_without_fieldsc                 C   s   |  d ttjt jdddjd dd}W d    n1 s$w   Y  |  d |D ]}| |j	 q1W d    d S 1 sEw   Y  d S )NrY   rA  TrB  r   )
r   r   r   r   r{   r   r}   rD  r    rA  r:   r(   r(   r)   .test_specific_subquery_select_related_negation  s    
	"z@TestSpecificQuery.test_specific_subquery_select_related_negationc                 C   s   |  d ttjt jddd}W d    n1 sw   Y  | t	|d |  d |D ]
}| 
|j  q4W d    d S 1 sJw   Y  d S )Nr_   
categoriesTrB  r   r   r   r   r   r   r{   r   r}   prefetch_relatedr=   r,  r.   rK  rB   r:   r(   r(   r)   'test_specific_subquery_prefetch_related  s   
"z9TestSpecificQuery.test_specific_subquery_prefetch_relatedc                 C   rF  rG  )r   rH  r   r   rB   rM  r   r(   r(   r)   7test_specific_subquery_prefetch_related_without_lookups  s   "zITestSpecificQuery.test_specific_subquery_prefetch_related_without_lookupsc                 C   s   |  d ttjt jdddjd dd}W d    n1 s$w   Y  | t	|d |  d |D ]
}| 
|j  q9W d    d S 1 sOw   Y  d S )NrY   rK  TrB  r   rL  r:   r(   r(   r)   0test_specific_subquery_prefetch_related_negation  s"   
	"zBTestSpecificQuery.test_specific_subquery_prefetch_related_negationc                 C   s   |  d ttjt jdddjddd}W d    n1 s$w   Y  | 	t
|d |  d |D ]}| |j | |jj  q9W d    d S 1 sVw   Y  d S )Nr_   rA  TrB  feed_image__renditionsr   r   )r   r   r   r   r{   r   r}   rD  rM  r=   r,  r    rA  r.   
renditionsrB   r:   r(   r(   r)   :test_specific_subquery_select_related_and_prefetch_related  s$   
	"zLTestSpecificQuery.test_specific_subquery_select_related_and_prefetch_relatedc                 C   sb   t j }|  }| jj|dd | jj|dd tt j 	 j
tddd dS )z
        Ensure alias() works with specific() queries.
        See https://github.com/wagtail/wagtail/issues/11285 for more details
        Fr9  r;  r<  r=  N)r   r   r   r>  r?   r;  r   r?  iterr}   aliasr   r   )r$   r%   r   r(   r(   r)   test_specific_query_with_alias  s   
z0TestSpecificQuery.test_specific_query_with_aliasc                 C   sx   t jjddd}tjjddj|d ttjjdd 	 }| 
|tjjddtjjddj	tjjd	dj	g d S )
Ntestsmissingpage)	app_labelr1  r3   r   )r-  r   r   r3  )r   r   r   r   r"   updater   r!   r  r}   r=   )r$   missing_page_content_typer%   r(   r(   r)   /test_specific_gracefully_handles_missing_models  s   zATestSpecificQuery.test_specific_gracefully_handles_missing_modelsc              	   C   s   t jdtjtdD | td tt	jj
dd  }W d    n1 s*w   Y  | |t	jj
ddt	jj
ddt	jj
ddg W d    d S 1 sRw   Y  d S )	Nz,wagtail.query.ContentType.objects.get_for_id)return_valuez;Specific versions of the following items could not be foundr   r   r3   r   r3  )r   patchr   r   get_for_modelr   assertWarnsRegexRuntimeWarningr   r   r!   r  r}   r=   r  r(   r(   r)   -test_specific_gracefully_handles_missing_rows  s(   
	"z?TestSpecificQuery.test_specific_gracefully_handles_missing_rowsc              	   C   sP  t jjdd}tdddd}|j|d | d | jd	d
}W d    n1 s,w   Y  | d t|}W d    n1 sEw   Y  | 	|t | 
t|d |D ]+}|j}| }| 	|| | d | ||j W d    n1 sw   Y  qZ| d |d j |d j W d    d S 1 sw   Y  d S )Nr   r   stream pagestream-page"[{"type": "text", "value": "foo"}]r   r   bodyr   r   T)deferr8      rY   )r   r   r!   r   r   r   r+  r}   r   r|   r=   r,  r-  r.  r   rg  )r$   r/  stream_pager0  r%   r&   r-  r1  r(   r(   r)   test_deferred_specific_query.  s6   

"z.TestSpecificQuery.test_deferred_specific_queryc                 C   s,  | j }| d t| }| t|d W d    n1 s!w   Y  | d t|  }| || W d    n1 sDw   Y  | d t| jdd}| || W d    n1 siw   Y  | d t| jdd}| || W d    d S 1 sw   Y  d S )Nr7  r*  
chunk_size   rY   r)  r   r   rB   r=   r,  iterator)r$   querysetbenchmark_resultresult_1result_2result_3r(   r(   r)   !test_specific_query_with_iteratorX  s"   "z3TestSpecificQuery.test_specific_query_with_iteratorc                 C   s   | j dd  }| d t| }| t|d W d    n1 s%w   Y  | d t|  }| || W d    n1 sHw   Y  | d t| jdd}| || W d    d S 1 snw   Y  d S )NrY   r   r7  ro  r8   rm  rp  r$   rr  rs  rt  ru  r(   r(   r)   /test_bottom_sliced_specific_query_with_iteratort     "zATestSpecificQuery.test_bottom_sliced_specific_query_with_iteratorc                 C   s   | j d d }| d t| }| t|d W d    n1 s%w   Y  | d t|  }| || W d    n1 sHw   Y  | d t| jdd}| || W d    d S 1 snw   Y  d S )Nro  r7  r*  r8   rm  rp  rx  r(   r(   r)   ,test_top_sliced_specific_query_with_iterator  rz  z>TestSpecificQuery.test_top_sliced_specific_query_with_iteratorc                 C   s   | j dd }| d t| }| t|d W d    n1 s%w   Y  | d t|  }| || W d    n1 sHw   Y  | d t| jdd}| || W d    d S 1 snw   Y  d S )NrY   ro  r   r7  r_   rm  rp  rx  r(   r(   r)   7test_top_and_bottom_sliced_specific_query_with_iterator  rz  zITestSpecificQuery.test_top_and_bottom_sliced_specific_query_with_iteratorN)r   r   r   __doc__r   r   r2  r5  r6  r8  r@  rE  rI  rJ  rN  rO  rP  rS  rV  r\  rb  rl  rw  ry  r{  r|  r(   r(   r(   r)   r$    s0    *r$  c                   @   r  )TestSpecificQuerySearchr%  c                 C   sD   t jddt dd tj  | _tj  jt	dd| _
d S )Nupdate_indexdefault2   )backend_namestdoutrn  r   r&  )r   call_commandr   r   r   r   r}   r'  r(  r   r)  r   r(   r(   r)   r     s   zTestSpecificQuerySearch.setUpc                 C   sH   t j  td}| t|d |D ]
}| 	t
|d qd S )N_scorer   )r   r   r   r}   r  r   annotate_scoreassertGreaterr,  r    hasattrr$   r  resultr(   r(   r)   8test_specific_query_with_match_all_search_and_annotation     zPTestSpecificQuerySearch.test_specific_query_with_match_all_search_and_annotationc                 C   sH   t j  dd}| t|d |D ]
}| t	|d qd S )Nr0   r  r   )
r   r   r   r}   r  r  r  r,  r    r  r  r(   r(   r)   3test_specific_query_with_real_search_and_annotation  r  zKTestSpecificQuerySearch.test_specific_query_with_real_search_and_annotationc                 C   s   t tj   jtdd}| t	|d | 
tjjddj| | 
tjjddj| | 
tjjddj| | 
tjjddj| d S )	Nz wagtail.search.backends.database)backendr   r3  r   ri   r3   r   )r   r   r   r}   r   r4   r  r   r=   r,  r~   r!   r  r(   r(   r)   test_specific_query_with_search  s   	z7TestSpecificQuerySearch.test_specific_query_with_searchN)r   r   r   r   r   r  r  r  r(   r(   r(   r)   r~    s    r~  c                   @   s   e Zd ZdZdg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d  Zd!S )"TestFirstCommonAncestorza
    Uses the same fixture as TestSpecificQuery. See that class for the layout
    of pages.
    r%  c                 C   s8   t jjdd| _t jt| _t jtjdd| _d S )Nr   r   z/other/)url_path__contains)	r   r   r!   r]   r{   r   
all_eventsexcluderegular_eventsr   r(   r(   r)   r     s
   zTestFirstCommonAncestor.setUpc                 C   s    t dddd}| jj|d d S )Nrc  rd  re  rf  r   )r   r]   r   )r$   rk  r(   r(   r)   _create_streampage  s   z*TestFirstCommonAncestor._create_streampagec                 C   s(   |  | j d |  | j d d S )Nr   r_   )r=   r  r>   r  r   r(   r(   r)   test_bookkeeping  s   z(TestFirstCommonAncestor.test_bookkeepingc                 C       |  tjjdd| j  dS )zCommon ancestor for EventPagesr   r   N)r=   r   r   r!   r  first_common_ancestorr   r(   r(   r)   test_event_pages  s   z(TestFirstCommonAncestor.test_event_pagesc                 C   r  )z8Common ancestor for EventPages, excluding /other/ eventseventsr  Nr=   r   r   r!   r  r  r   r(   r(   r)   test_normal_event_pages  s   z/TestFirstCommonAncestor.test_normal_event_pagesc                 C   s$   |  tjjdd| jjdd dS )zj
        Common ancestor for EventPages, excluding /other/ events, with
        include_self=True
        r  r  Tinclude_selfNr  r   r(   r(   r)   $test_normal_event_pages_include_self  s   z<TestFirstCommonAncestor.test_normal_event_pages_include_selfc                 C   s(   |  tjjddtjjdd  dS )z4Test getting a single page, with include_self=False.r  r  	Christmasr   Nr=   r   r   r!   r"   r  r   r(   r(   r)    test_single_page_no_include_self#  s   z8TestFirstCommonAncestor.test_single_page_no_include_selfc                 C   s,   |  tjjddtjjddjdd dS )z3Test getting a single page, with include_self=True.r  r  Tr  Nr  r   r(   r(   r)   test_single_page_include_self*  s   z5TestFirstCommonAncestor.test_single_page_include_selfc                 C   s   |  t tj  d S r   r=   r   r   r   r  r   r(   r(   r)   test_all_pages3  s   z&TestFirstCommonAncestor.test_all_pagesc                 C   s@   |  tj tjjdd W d    d S 1 sw   Y  d S NT)strict)r   r   DoesNotExistr   r  r   r(   r(   r)   test_all_pages_strict8  s   "z-TestFirstCommonAncestor.test_all_pages_strictc                 C   s    |  t tjjddd d S )NT)r  r  r  r   r(   r(   r)   "test_all_pages_include_self_strict<  s   z:TestFirstCommonAncestor.test_all_pages_include_self_strictc                 C   s   |  t tj   d S r   )r=   r   r   r   noner  r   r(   r(   r)   test_empty_querysetB  s   z+TestFirstCommonAncestor.test_empty_querysetc                 C   sD   |  tj tj jdd W d    d S 1 sw   Y  d S r  )r   r   r  r   r  r  r   r(   r(   r)   test_empty_queryset_strictG  s   "z2TestFirstCommonAncestor.test_empty_queryset_strictc              	   C   s^   |    tj  D ]!}| d|j | d |j W d    n1 s'w   Y  qd S Nrg  r8   )	r  r   r   rB   defer_streamfieldsr   __dict__r   rg  r$   r&   r(   r(   r)   (test_defer_streamfields_without_specificK  s   z@TestFirstCommonAncestor.test_defer_streamfields_without_specificc              	   C   sd   |    tjt  D ]!}| d|j | 	d |j
 W d    n1 s*w   Y  qd S r  )r  r   r   r   r   r  r}   r   r  r   rg  r  r(   r(   r)   %test_defer_streamfields_with_specificR  s   z=TestFirstCommonAncestor.test_defer_streamfields_with_specificN)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  N)'ior   unittestr   django.contrib.authr   "django.contrib.contenttypes.modelsr   django.corer   django.db.modelsr   r   django.testr	   r
   wagtail.modelsr   r   r   r   r   wagtail.search.queryr   wagtail.signalsr   wagtail.test.testapp.modelsr   r   r   r   wagtail.test.utilsr   r   r   r  r$  r~  r  r(   r(   r(   r)   <module>   s2        y!k    <