U
    |h$                     @   s   d dl 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 d d	lmZmZ d d
lmZ G dd deZG dd de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)timezone)StreamValue)migrate_operation)RenameStreamChildrenOperationRenameStructChildrenOperation)InvalidBlockDefErrorapply_changes_to_raw_data)#disable_reference_index_auto_update)	factoriesmodels)MigrationTestMixinc                   @   s(   e Zd ZdZdd Zdd Zdd ZdS )	TestExceptionRaisedInRawDataa  Directly test whether an exception is raised by apply_changes_to_raw_data for invalid defs.

    This would happen in a situation where the user gives a block path which contains a block name
    which is not present in the block definition in the project state at which the migration is
    applied. (There should also be a block in the stream data with the said name for this to happen)
    c                 C   s`   t jdddjj}|dddddddd	ddddg d
dddg|d d d< || _d S )NChar Block 1nestedstructZcontent__0__char1__valueZ
content__1invalid_name10001foochar1Zchar2typeidvalue0002r   Z0003)r   r   r      r   Zinvalid_name2)r   ZSampleModelFactorycontentraw_dataextend)selfr    r!   z/var/www/dating/data/www/fatepal.com/env/lib/python3.8/site-packages/wagtail/tests/streamfield_migrations/test_bad_data.pysetUp   s$    

z"TestExceptionRaisedInRawData.setUpc              	   C   s:   |  td$ t| jdtdddtjjd W 5 Q R X dS )zLTest whether Exception is raised in when recursing through stream block dataz(No current block def named invalid_name1r   r   renamed1Zold_namenew_namer   Zblock_path_strZ	operationZstreamfieldN)assertRaisesMessager   r	   r   r   r   SampleModelr   r    r!   r!   r"    test_rename_invalid_stream_child5   s      z=TestExceptionRaisedInRawData.test_rename_invalid_stream_childc              	   C   s:   |  td$ t| jdtdddtjjd W 5 Q R X dS )zLTest whether Exception is raised in when recursing through struct block dataz(No current block def named invalid_name2znestedstruct.invalid_name2r   r$   r%   r'   N)r(   r   r	   r   r   r   r)   r   r*   r!   r!   r"    test_rename_invalid_struct_childD   s      z=TestExceptionRaisedInRawData.test_rename_invalid_struct_childN)__name__
__module____qualname____doc__r#   r+   r,   r!   r!   r!   r"   r      s   r   c                   @   sH   e Zd ZejZeddddfgZdZdd Z	dd	 Z
d
d Zdd ZdS )BadDataMigrationTestCaser   r$   r%   r   Zstreamfield_migration_testsc                 C   s   t jddd}|| _d S )Nr   r   r   )r   ZSamplePageFactoryinstance)r    r2   r!   r!   r"   create_instance^   s
    z(BadDataMigrationTestCase.create_instancec                 C   s^   | j jj}|ddddddddddddg | j jj}t||dd| j _| j   d S )	Nr   r   r   r   r   r   Tstream_blockZstream_dataZis_lazy)r2   r   r   r   r5   r   save)r    r   r5   r!   r!   r"   append_invalid_instance_datae   s&    

  
z5BadDataMigrationTestCase.append_invalid_instance_datac                 C   sZ   |    | |}| jjj}|d d }| jjj}t||dd| j_| j  |j|j	fS )N   Tr4   )
r7   create_revisionr2   r   r   r5   r   r6   r   
created_at)r    deltaZinvalid_revisionr   r5   r!   r!   r"   create_invalid_revision{   s    


  

z0BadDataMigrationTestCase.create_invalid_revisionc                 C   s,   | j  }t tj|d |_|  |S )Ndays)r2   Zsave_revisionr   nowdatetime	timedeltar:   r6   )r    r;   revisionr!   r!   r"   r9      s    
z(BadDataMigrationTestCase.create_revisionN)r-   r.   r/   r   
SamplePagemodelr   Z default_operation_and_block_pathZapp_namer3   r7   r<   r9   r!   r!   r!   r"   r1   T   s   
r1   c                   @   s    e Zd ZdZdd Zdd ZdS )TestExceptionRaisedForInstancezlException should always be raised when applying migration if it occurs while migrating the
    instance datac              	   C   s&   t   |   |   W 5 Q R X d S )N)r
   r3   r7   r*   r!   r!   r"   r#      s    z$TestExceptionRaisedForInstance.setUpc              	   C   sJ   |  td| jjj| jj" | jt	 t
jdd d W 5 Q R X d S )Nz#Invalid block def in {} object ({})r8   r=   Zrevisions_from)r(   r   formatr2   	__class__r-   r   apply_migrationr   r?   r@   rA   r*   r!   r!   r"   test_migrate   s     z+TestExceptionRaisedForInstance.test_migrateNr-   r.   r/   r0   r#   rJ   r!   r!   r!   r"   rE      s   rE   c                   @   s    e Zd ZdZdd Zdd ZdS )$TestExceptionRaisedForLatestRevisionzsException should always be raised when applying migration if it occurs while migrating the
    latest revision datac              	   C   sL   t  < |   tdD ]}| d|  q| d\| _| _W 5 Q R X d S )N      r   )r
   r3   ranger9   r<   invalid_revision_idinvalid_revision_created_atr    ir!   r!   r"   r#      s    z*TestExceptionRaisedForLatestRevision.setUpc              	   C   sB   |  td| jjj| jj| j| j | j	d d W 5 Q R X d S NzFInvalid block def in {} object ({}) for revision id ({}) created at {}rF   
r(   r   rG   r2   rH   r-   r   rP   rQ   rI   r*   r!   r!   r"   rJ      s    	z1TestExceptionRaisedForLatestRevision.test_migrateNrK   r!   r!   r!   r"   rL      s   rL   c                   @   s    e Zd ZdZdd Zdd ZdS )"TestExceptionRaisedForLiveRevisionzqException should always be raised when applying migration if it occurs while migrating the
    live revision datac              	   C   sb   t  R |   | d\| _| _| j| j_| j  tddD ]}| 	d|  q@W 5 Q R X d S NrN   r   )
r
   r3   r<   rP   rQ   r2   Zlive_revision_idr6   rO   r9   rR   r!   r!   r"   r#      s    

z(TestExceptionRaisedForLiveRevision.setUpc              	   C   sB   |  td| jjj| jj| j| j | j	d d W 5 Q R X d S rT   rU   r*   r!   r!   r"   rJ      s    	z/TestExceptionRaisedForLiveRevision.test_migrateNrK   r!   r!   r!   r"   rV      s   rV   c                   @   s&   e Zd ZdZejZdd Zdd ZdS )%TestExceptionIgnoredForOtherRevisionszException should not be be raised when applying migration if it occurs while migrating
    revision data which is not of a live or latest revision. Instead an exception should be loggedc              	   C   sN   t  > |   | d\| _| _tddD ]}| d|  q,W 5 Q R X d S rW   )r
   r3   r<   rP   rQ   rO   r9   rR   r!   r!   r"   r#      s    z+TestExceptionIgnoredForOtherRevisions.setUpc                 C   s   | j ddv}| jd d | |jd  d dtj| jj	j| jj
| j| j | |jd  d dtjd tj  W 5 Q R X d S )	NERROR)levelrF   r   zOERROR:{}:Invalid block def in {} object ({}) for revision id ({}) created at {}z,{}: No current block def named invalid_name1.)Z
assertLogsrI   ZassertEqualoutput
splitlinesrG   r   r-   r2   rH   r   rP   rQ   r   r.   )r    cmr!   r!   r"   rJ      s,    z2TestExceptionIgnoredForOtherRevisions.test_migrateN)	r-   r.   r/   r0   r   rC   rD   r#   rJ   r!   r!   r!   r"   rX      s   rX   )r@   Zdjango.testr   Zdjango.utilsr   Zwagtail.blocksr   Zwagtail.blocks.migrationsr   Z$wagtail.blocks.migrations.operationsr   r   Zwagtail.blocks.migrations.utilsr   r	   Zwagtail.signal_handlersr
   Z#wagtail.test.streamfield_migrationsr   r   Z-wagtail.test.streamfield_migrations.testutilsr   r   r1   rE   rL   rV   rX   r!   r!   r!   r"   <module>   s   ?=