
    @i7                     R    d Z ddlmZ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 models.
    )date	timedelta)TestCase)timezone)CronExecutionc                       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d Zd Zd Zd Zd Zd Zd Zd Zd Zd Zy)CronExecutionModelTestszTests for CronExecution model.c           	      0   t         j                  j                  ddt        ddd      dd      }| j	                  |j
                         | j                  |j                  d       | j                  |j                  d       | j                  |j                  t        ddd             | j                  |j                         | j                  |j                         | j                  |j                  d       | j	                  |j                         y	)
z"Test creating an execution record.A001	Test Task        F	task_code	task_nameexecution_date	completedsuccessr   N)r   objectscreater   assertIsNotNoneidassertEqualr   r   r   assertFalser   r   retry_countstartedself	executions     A/home/cursorai/projects/django-cronjob-utils/tests/test_models.pytest_create_executionz-CronExecutionModelTests.test_create_execution   s    !))00!a, 1 
	 	Y\\*,,f5,,k:114a3DE,,-**+..2Y../    c           	         t         j                  j                  ddt        ddd            }| j	                  |j
                         | j	                  |j                         | j                  |j                  d       | j                  |j                  d       | j                  |j                  d       | j                  |j                         y	)
z%Test execution record default values.r   r   r   r   r   r   r   r   r    N)r   r   r   r   r   r   r   r   r   message
error_codeassertIsNoneendedr   s     r!   test_execution_defaultsz/CronExecutionModelTests.test_execution_defaults    s    !))00!a, 1 
	 	,,-**+..2**B/--r2)//*r#   c           	         t         j                  j                  ddt        ddd      d      }| j	                  dt        |             | j	                  dt        |             | j	                  dt        |             |j                  d	d
       | j	                  dt        |             t         j                  j                  ddt        ddd      d	d      }| j	                  dt        |             y)z%Test execution string representation.r   r   r   r   r   Fr   r   r   r   u   →TSuccess)r   r'   u   ✓A002zFailed Taskr   u   ✗N)r   r   r   r   assertInstrmark_completed)r   r    
execution2s      r!   $test_execution_string_representationz<CronExecutionModelTests.test_execution_string_representation/   s     "))00!a,	 1 
	 	eS^,k3y>2fc)n- 	  y AeS^, #**11#a, 2 

 	eS_-r#   c           	      t   t         j                  j                  ddt        ddd            }t	        j
                         t        d      z
  }||_        |j                          |j                  d	
       |j                  }| j                  |       | j                  |d       | j                  |d       y)z2Test duration calculation for completed execution.r   r   r   r   r   r%   d   secondsT)r   n   N)r   r   r   r   r   nowr   r   saver2   durationr   assertGreaterEqual
assertLessr   r    started_timer<   s       r!    test_duration_property_completedz8CronExecutionModelTests.test_duration_property_completedJ   s    !))00!a, 1 
	  ||~	#(>>(	 	   .%%X&#.#&r#   c           	      R   t         j                  j                  ddt        ddd      d      }t	        j
                         t        d	      z
  }||_        |j                          |j                  }| j                  |       | j                  |d       | j                  |d
       y)z0Test duration calculation for running execution.r   r   r   r   r   Fr-   2   r7   <   N)r   r   r   r   r   r:   r   r   r;   r<   r   r=   r>   r?   s       r!   test_duration_property_runningz6CronExecutionModelTests.test_duration_property_running_   s    !))00!a,	 1 
	  ||~	"(==(	%%X&"-"%r#   c           	          t         j                  j                  ddt        ddd            }| j	                  |j
                         |j                  }| j	                  |       | j                  |d       y)	a=  Test duration calculation when started is None (edge case).
        
        Note: Since started has auto_now_add=True, it cannot actually be None.
        This test verifies that the duration property handles the theoretical
        case where started might be None (though it shouldn't happen in practice).
        r   r   r   r   r   r%   r   N)r   r   r   r   r   r   r<   r=   )r   r    r<   s      r!   !test_duration_property_no_startedz9CronExecutionModelTests.test_duration_property_no_startedr   sr     "))00!a, 1 
	 	Y../ %%X&!,r#   c           	         t         j                  j                  ddt        ddd            }|j	                  ddd	
       |j                          | j                  |j                         | j                  |j                         | j                  |j                  d       | j                  |j                  d	       | j                  |j                         y)z1Test marking execution as completed successfully.r   r   r   r   r   r%   TzTask completedr&   r   r'   r(   N)r   r   r   r   r2   refresh_from_db
assertTruer   r   r   r'   r(   r   r*   r   s     r!   test_mark_completed_successz3CronExecutionModelTests.test_mark_completed_success   s    !))00!a, 1 
	 	  7GTV W!!#	++,	))***,<=--r2Y__-r#   c           	         t         j                  j                  ddt        ddd            }|j	                  ddd	
       |j                          | j                  |j                         | j                  |j                         | j                  |j                  d       | j                  |j                  d	       | j                  |j                         y)z1Test marking execution as completed with failure.r   r   r   r   r   r%   FzTask failed	ERROR_001rI   N)r   r   r   r   r2   rJ   rK   r   r   r   r   r'   r(   r   r*   r   s     r!   test_mark_completed_failurez3CronExecutionModelTests.test_mark_completed_failure   s    !))00!a, 1 
	 	  R] ^!!#	++,**+**M:--{;Y__-r#   c           	      N   t         j                  j                  ddt        ddd      dd      }|j	                  d	d
d
       |j                          | j                  |j                         | j                  |j                         | j                  |j                  d
       y)z7Test marking completed with partial message/error_code.r   r   r   r   r   zOriginal messageORIGINAL_ERROR)r   r   r   r'   r(   Tr&   rI   N)r   r   r   r   r2   rJ   rK   r   r   r   r'   r   s     r!   "test_mark_completed_partial_updatez:CronExecutionModelTests.test_mark_completed_partial_update   s    !))00!a,&' 1 
	 	  rb I!!#	++,	))***B/r#   c           	         t         j                  j                  ddt        ddd            }|j	                  dd	       |j                          | j                  |j                         | j                  |j                         | j                  |j                  d       | j                  |j                  d       y
)z$Test mark_failed convenience method.r   r   r   r   r   r%   FailedFAIL)r'   r(   N)r   r   r   r   mark_failedrJ   rK   r   r   r   r   r'   r(   r   s     r!   test_mark_failedz(CronExecutionModelTests.test_mark_failed   s    !))00!a, 1 
	 	h6B!!#	++,**+**H5--v6r#   c           	      P   t         j                  j                  ddt        ddd      d      }|j	                          |j                          | j                  |j                  d       |j	                          |j                          | j                  |j                  d       y	)
zTest incrementing retry count.r   r   r   r   r   r   r   r   r   r      N)r   r   r   r   increment_retryrJ   r   r   r   s     r!   test_increment_retryz,CronExecutionModelTests.test_increment_retry   s    !))00!a,	 1 
	 	!!#!!#..2!!#!!#..2r#   c           	          t         j                  j                  ddt        ddd      d      }t	        d      D ]A  }|j                          |j                          | j                  |j                  |dz          C y	)
z-Test incrementing retry count multiple times.r   r   r   r   r   r   rY      N)	r   r   r   r   ranger[   rJ   r   r   )r   r    is      r!   #test_increment_retry_multiple_timesz;CronExecutionModelTests.test_increment_retry_multiple_times   st    !))00!a,	 1 
	 qA%%'%%'Y22AE: r#   c           	          ddl }t        j                  j                  ddt	        ddd      |j                               }| j                  |j                  |j                                y)	z$Test PID field for timeout handling.r   Nr   r   r   r   r   r   r   r   pid)osr   r   r   r   getpidr   rd   )r   re   r    s      r!   test_pid_fieldz&CronExecutionModelTests.test_pid_field   sW    !))00!a,			 1 
	 			4r#   c           	          t         j                  j                  ddt        ddd      d      }| j	                  |j
                         y)zTest PID field can be null.r   r   r   r   r   Nrc   )r   r   r   r   r)   rd   r   s     r!   test_pid_field_nullz+CronExecutionModelTests.test_pid_field_null   sF    !))00!a,	 1 
	 	)--(r#   c                 (   t        d      D ]8  }t        j                  j                  d| d| t	        ddd|z                : t        j                  j                  dt	        ddd	      
      }| j                  |j                         d       y)z7Test that database indexes are created (indirect test).r^   A00zTask r   r   r   r%   r      )r   r   N)r_   r   r   r   r   filterr   count)r   r`   
executionss      r!   test_indexes_existz*CronExecutionModelTests.test_indexes_exist  s     qA!!((s)!!+#D!R!V4 )   #**11a, 2 

 	))+Q/r#   c                 H   t        ddd      t        ddd      t        ddd      g}|D ]$  }t        j                  j                  dd|       & t	        t        j                  j                               }| j                  |d	   j                  |d   j                         y
)z*Test default ordering (most recent first).r   r   r   rl      r   r   r%   r   N)r   r   r   r   listallr=   r   )r   datesdro   s       r!   test_orderingz%CronExecutionModelTests.test_ordering  s    dAr"Dq"$5tD!R7HIA!!(( %  )   -//3356

1 5 5z!}7L7LMr#   c           	         t         j                  j                  ddt        ddd            }t         j                  j                  ddt        ddd            }| j	                  |j
                  |j
                         | j                  |j                  |j                         | j                  |j                  |j                         y)z0Test multiple executions for same task and date.r   r   r   r   r   r%   N)	r   r   r   r   assertNotEqualr   r   r   r   )r   
execution1r3   s      r!   'test_multiple_executions_same_task_datez?CronExecutionModelTests.test_multiple_executions_same_task_date%  s    "**11!a, 2 

 #**11!a, 2 

 	JMM:==9--z/C/CD22J4M4MNr#   c           	          ddz  }t         j                  j                  ddt        ddd      |      }|j	                          | j                  t        |j                        d       y	)
z2Test execution with very long message (edge case).Ai'  r   r   r   r   r   r   r   r   r'   N)r   r   r   r   rJ   r   lenr'   )r   long_messager    s      r!   test_long_messagez)CronExecutionModelTests.test_long_message7  sd    U{!))00!a, 	 1 
	 	!!#Y../7r#   c           	          d}t         j                  j                  ddt        ddd      |      }|j	                          | j                  |j                  |       y)	z2Test execution with special characters in message.z4Error: "Test" <test@example.com> & more
New line	Tabr   r   r   r   r   r~   N)r   r   r   r   rJ   r   r'   )r   special_messager    s      r!   "test_special_characters_in_messagez:CronExecutionModelTests.test_special_characters_in_messageD  s[    R!))00!a,#	 1 
	 	!!#**O<r#   c                     t        ddd      }t        j                  j                  dd|      }| j	                  |j
                  |       y)z,Test execution with future date (edge case).i        r   r   r%   Nr   r   r   r   r   r   )r   future_dater    s      r!   test_future_execution_datez2CronExecutionModelTests.test_future_execution_dateQ  sL    4R(!))00!& 1 
	 	11;?r#   c                     t        ddd      }t        j                  j                  dd|      }| j	                  |j
                  |       y)zTest execution with past date.i  r   r   r   r%   Nr   )r   	past_dater    s      r!   test_past_execution_datez0CronExecutionModelTests.test_past_execution_date\  sL    q!$	!))00!$ 1 
	 	119=r#   c           	          t         j                  j                  ddt        ddd            }| j	                  |j
                  d       y)zBTest execution with empty task code (edge case - should validate).r&   r   r   r   r   r%   Nr   r   r   r   r   r   r   s     r!   test_empty_task_codez,CronExecutionModelTests.test_empty_task_codeg  sI     "))00!a, 1 
	 	,,b1r#   c           	          d}t         j                  j                  |dt        ddd            }| j	                  |j
                  |       y)z4Test execution with very long task code (edge case).2AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAr   r   r   r   r%   Nr   )r   	long_coder    s      r!   test_very_long_task_codez0CronExecutionModelTests.test_very_long_task_coder  sL    	!))00!a, 1 
	 	,,i8r#   c           	          d}t         j                  j                  d|t        ddd            }| j	                  |j
                  |       y)z4Test execution with very long task name (edge case).dAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAr   r   r   r   r%   N)r   r   r   r   r   r   )r   	long_namer    s      r!   test_very_long_task_namez0CronExecutionModelTests.test_very_long_task_name}  sL    	!))00a, 1 
	 	,,i8r#   N)__name__
__module____qualname____doc__r"   r+   r4   rA   rE   rG   rL   rO   rR   rW   r\   ra   rg   ri   rp   rw   r{   r   r   r   r   r   r   r    r#   r!   r	   r	   
   s    (0&+.6'*&&-0."."0&7 3";
5	)0"NO$8=	@	>	2	9	9r#   r	   N)r   datetimer   r   django.testr   django.utilsr   django_cronjob_utils.modelsr   r	   r   r#   r!   <module>r      s&    %   ! 5|9h |9r#   