
    ?iM                     f    d 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
 ddlmZ  G d d	e      Zy
)z'
Tests for django_cronjob_utils locks.
    )date)TestCase)transaction)DatabaseLock)CronExecution)ConcurrentExecutionErrorc                   d    e Zd ZdZd Zd Zd Zd Zd Zd Z	d Z
d	 Zd
 Zd Zd Zd Zd Zd Zy)DatabaseLockTestszTests for DatabaseLock.c                 b    t        dt        ddd            }|5  	 ddd       y# 1 sw Y   yxY w)z4Test lock acquires when no running execution exists.A001        N)r   r   selflocks     @/home/cursorai/projects/django-cronjob-utils/tests/test_locks.pytest_lock_acquires_successfullyz1DatabaseLockTests.test_lock_acquires_successfully   s)    FDq"$56  TTs   %.c           	         t         j                  j                  ddt        ddd      d       t	        dt        ddd            }| j                  t              5 }|5  	 ddd       ddd       | j                  dt        j                               | j                  d	t        |j                               y# 1 sw Y   \xY w# 1 sw Y   `xY w)
z(Test lock prevents concurrent execution.r   	Test Taskr   r   r   F	task_code	task_nameexecution_date	completedNz
2024-01-15)
r   objectscreater   r   assertRaisesr   assertInstr	exception)r   r   cms      r   'test_lock_prevents_concurrent_executionz9DatabaseLockTests.test_lock_prevents_concurrent_execution   s     	$$!a,	 	% 	
 FDq"$56 78B  9 	fc",,/0lC$56	  98s$   CB; C;C	 CCc           	          t         j                  j                  ddt        ddd      d       t	        dt        ddd            }|5  	 d	d	d	       y	# 1 sw Y   y	xY w)
z3Test lock allows execution for different task code.r   r   r   r   r   Fr   A002Nr   r   r   r   r   r   s     r   $test_lock_allows_different_task_codez6DatabaseLockTests.test_lock_allows_different_task_code+   Y     	$$!a,	 	% 	
 FDq"$56 TT   AAc           	          t         j                  j                  ddt        ddd      d       t	        dt        ddd            }|5  	 d	d	d	       y	# 1 sw Y   y	xY w)
z8Test lock allows execution for different execution date.r   r   r   r   r   Fr      Nr&   r   s     r   )test_lock_allows_different_execution_datez;DatabaseLockTests.test_lock_allows_different_execution_date:   r(   r)   c           	          t         j                  j                  ddt        ddd      d      }t	        dt        ddd            }|5  	 ddd       y# 1 sw Y   yxY w)	z@Test lock allows execution when previous execution is completed.r   r   r   r   r   Tr   Nr&   r   	executionr   s      r   $test_lock_allows_completed_executionz6DatabaseLockTests.test_lock_allows_completed_executionI   s[     "))00!a,	 1 
	 FDq"$56 TTr)   c           	          t         j                  j                  ddt        ddd      dd      }t	        dt        ddd            }|5  	 ddd       y# 1 sw Y   yxY w)	z=Test lock allows execution when previous execution succeeded.r   r   r   r   r   Tr   r   r   r   successNr&   r.   s      r   %test_lock_allows_successful_executionz7DatabaseLockTests.test_lock_allows_successful_executionX   s\    !))00!a, 1 
	 FDq"$56 TT   	AAc           	          t         j                  j                  ddt        ddd      dd      }t	        dt        ddd            }|5  	 d	d	d	       y	# 1 sw Y   y	xY w)
z:Test lock allows execution when previous execution failed.r   r   r   r   r   TFr2   Nr&   r.   s      r   !test_lock_allows_failed_executionz3DatabaseLockTests.test_lock_allows_failed_executionf   s\    !))00!a, 1 
	 FDq"$56 TTr5   c           	      n   t         j                  j                  ddt        ddd      d       t         j                  j                  ddt        ddd      d       t	        dt        ddd            }| j                  t              5  |5  	 ddd       ddd       y# 1 sw Y   xY w# 1 sw Y   yxY w)	z.Test lock detects multiple running executions.r   r   r   r   r   Fr   Nr   r   r   r   r   r   r   r   s     r   %test_lock_multiple_running_executionsz7DatabaseLockTests.test_lock_multiple_running_executionst   s     	$$!a,	 	% 	
 	$$!a,	 	% 	
 FDq"$5678  98 98s$   	B+BB+B(	$B++B4c           
         t        dt        ddd            }|5  t        j                  j	                  ddt        ddd      d       ddd       t        j                  j                  dt        ddd      	      }|j                  d
       t        dt        ddd            }|5  	 ddd       y# 1 sw Y   kxY w# 1 sw Y   yxY w)z2Test lock is released after context manager exits.r   r   r   r   r   Fr   N)r   r   T)r3   )r   r   r   r   r   getmark_completed)r   r   r/   lock2s       r    test_lock_released_after_contextz2DatabaseLockTests.test_lock_released_after_context   s    FDq"$56!!(( %#D!R0	 )   "))--a, . 
	 	   . VT$2%67 U' T& Us   /B3(B?3B<?Cc                    t        j                         5  t        dt        ddd            }|5  t        j
                  j                  ddt        ddd      d       ddd       ddd       y# 1 sw Y   xY w# 1 sw Y   yxY w)	z,Test lock works within database transaction.r   r   r   r   r   Fr   N)r   atomicr   r   r   r   r   r   s     r   test_lock_within_transactionz.DatabaseLockTests.test_lock_within_transaction   sp    !T1b(9:D%%,,$)#'a#4#	 -   "! "!s"   A;/A/A;/A8	4A;;Bc           	         t         j                  j                  ddt        ddd      d       t	        dt        ddd            }| j                  t              5  |5  	 ddd       ddd       y# 1 sw Y   xY w# 1 sw Y   yxY w)	z1Test lock uses SELECT FOR UPDATE (indirect test).r   r   r   r   r   Fr   Nr9   r   s     r   test_lock_select_for_updatez-DatabaseLockTests.test_lock_select_for_update   s|     	$$!a,	 	% 	
 FDq"$56 78  98 98$   A=A1 A=1A:	6A==Bc           	         t         j                  j                  ddt        ddd      d       t	        dt        ddd            }| j                  t              5  |5  	 ddd       ddd       y# 1 sw Y   xY w# 1 sw Y   yxY w)	z+Test lock with empty task code (edge case). r   r   r   r   Fr   Nr9   r   s     r   #test_lock_edge_case_empty_task_codez5DatabaseLockTests.test_lock_edge_case_empty_task_code   sx    $$!a,	 	% 	
 BT1b 1278  98 98rE   c                     t        ddd      }t        j                  j                  dd|d       t	        d|      }| j                  t              5  |5  	 ddd       ddd       y# 1 sw Y   xY w# 1 sw Y   yxY w)	z'Test lock with future date (edge case).i        r   r   Fr   Nr   r   r   r   r   r   r   )r   future_dater   s      r   test_lock_edge_case_future_datez1DatabaseLockTests.test_lock_edge_case_future_date   st    4R($$!&	 	% 	
 FK078  98 98$   A4A(A4(A1	-A44A=c                     t        ddd      }t        j                  j                  dd|d       t	        d|      }| j                  t              5  |5  	 ddd       ddd       y# 1 sw Y   xY w# 1 sw Y   yxY w)z%Test lock with past date (edge case).i  r   r   r   Fr   NrL   )r   	past_dater   s      r   test_lock_edge_case_past_datez/DatabaseLockTests.test_lock_edge_case_past_date   st    q!$	$$!$	 	% 	
 FI.78  98 98rO   N)__name__
__module____qualname____doc__r   r#   r'   r,   r0   r4   r7   r:   r?   rB   rD   rH   rN   rR        r   r
   r
      sK    !7(,4""rX   r
   N)rV   datetimer   django.testr   	django.dbr   django_cronjob_utils.locksr   django_cronjob_utils.modelsr   django_cronjob_utils.exceptionsr   r
   rW   rX   r   <module>r_      s,       ! 3 5 De erX   