U
    jS                     @  s  d Z ddlmZ ddlmZmZmZ ddlZddlmZm	Z	 ddl
mZ zddlmZ W n  ek
rx   ddlmZ Y nX ddlmZmZmZmZmZ dd	lmZ d
dddddZejeegddd Zejedddddedddedddddeddgddd Zdd Zdd Zdd  Zd!d" Zejeeeegdd#d$ Z d%d& Z!d'd( Z"e Z#e#$deddddd e#$deddddd) e%e#& Z'e Z(e($d*ed+d,d,ed+d-d,f ee#Z)ee'Z*ee(Z+ej,-d.e#e'e(e)e*e+gd/d0 Z.d1d2 Z/ejeddddd)eddd3edddd4deddgdd5d6 Z0d7d8 Z1d9d: Z2d;d< Z3d=d> Z4eje1e4e2e3gdd?d@ Z5dAdB Z6dCdD Z7ej,-dEd*dFgdGdH Z8dIdJdKdLdMZ9dIdJdKdNdOZ:dIdJdKdPdQZ;eje9e:e;gddRdS Z<dTdU Z=e Z>e>$d*edVd,d,dWdX e>$dFedVd,d, e Z?e?$dFedVd,d,dWdX e?$d*edVd,d, e Z@e@$dFedVd,d,dWdX e@$d*edVd,d,dWdX e@$dYed,dZ e ZAeA$d*edVd,d, eA$dYed,d[ e ZBeB$d*edVd,d,dWdX eB$d\edVd,d, e ZCeC$d\edVd,d,dWdX eC$d*edVd,d, e ZDeD$d\edVd,d,dWdX eD$d*edVd,d,dWdX eD$dYed,dZ e ZEeE$d*edVd,d, eE$dYed,d[ ej,-d]e>d^fe?d^fe@d_feAd`feBdafeCdafeDdbfeEd`fgej,-dEdcddgdedf ZFdgdh ZGdidj ZHdkdl ZIdmdn ZJe ZKe ZLeL$dYed,d[ e ZMe ZNeN$dYed,d[ ej,-doeKeLeMeNgej,-dEdcdddSgdpdq ZOej,-dreP ed,dZedVdWdsdWdXed,dZfgej,-dtedcfeddfed*fedFfedcfeddfed*fedcfeddfed*fed\fgdudv ZQdwdxdydzZRej,-dreP dedVdWdsdWdXed,dZfed{d-d-edd-d-gd|d} ZSd~d ZTdd ZUej,-dd*edVdWdsdWdXfdFedVdWdsdWdXfdYed,dZfgZVeVdd ZWeVdd ZXej,-dEd*dFdYgdd ZYdd ZZej,-dEd*dddcgej,-dcedVdWdsdWdXedVdWdsedVdWdsdWdXeddgdd Z[ej,-dEdcddgdd Z\dd Z]ej,-dde%ddfdeddZdfdeddZdfgdd Z^dd Z_dd Z`dd Zadd Zbej,-dddde%de%de%de%de%dfffddeddZfdffdeddZfddffgdd Zcdd Zded,d[Zedd Zfdd Zgej,-dddde%dfffddeddZfdffdeddZfddffgej,-dSeddZeegej,-dd,dgdd ZhdS )z8This tests the properties of components and their types.    )annotations)datedatetime	timedeltaN)IncompleteComponentInvalidCalendar)Alarm)ZoneInfo)EventJournalTodo	vDDDTypes	vDatetime	vDurationzEvent | Todostr)	componentpropreturnc                 C  s   t | tr| dkrdS |S )z]Translate the end property.

    This allows us to run the same tests on Event and Todo.
    DTENDDUE)
isinstancer   upper)r   r    r   W/tmp/pip-unpacked-wheel-g40nyxg1/icalendar/tests/test_issue_662_component_properties.pyr      s    r   )paramsc                 C  s   |   S )zThe event to test.paramrequestr   r   r   start_end_component$   s    r    i              zEurope/Paris)tzinfoc                 C  s   ||| j  | j S )zStart of the event.r   )r   set_component_startr    r   r   r   dtstart*   s    	r'   c                 C  s2   t | }t||d< |   | t| | dS )*Create the event with the __init__ method.r'   N)dictr   clearupdatetype)r   startdr   r   r   _set_component_start_init7   s    r/   c                 C  s
   || _ dS z+Create the event with the dtstart property.N)DTSTARTr   r-   r   r   r   _set_component_dtstart?   s    r3   c                 C  s
   || _ dS r0   )r-   r2   r   r   r   _set_component_start_attrD   s    r4   c                 C  s@   |  d| |   }t| |   | t| | dS )z)Create the event with the start property.r'   N)addto_icaldecodeprintr*   r+   r,   	from_ical)r   r-   icsr   r   r   _set_component_start_icsI   s
    r;   c                 C  s   | j S zCreate a new event.r   r   r   r   r   r&   R   s    
r&   c                 C  s   |j | kstdS zTest the start of events.N)r1   AssertionErrorr'   r    r   r   r   test_component_dtstart_   s    r@   c                 C  s   |j | kstdS r=   )r-   r>   r?   r   r   r   test_event_startd   s    rA      r1   i        invalid_eventc              	   C  s<   t t | j W 5 Q R X t t | j W 5 Q R X dS )"Check that we get the right error.N)pytestraisesr   r-   r1   )rE   r   r   r   test_multiple_dtstartt   s    rI   c              	   C  s.   | j dksttt | j W 5 Q R X dS )zDTSTART is optional.

    The following is REQUIRED if the component
    appears in an iCalendar object that doesn't
    specify the "METHOD" property; otherwise, it
    is OPTIONAL; in any case, it MUST NOT occur
    more than once.
    N)r1   r>   rG   rH   r   r-   r    r   r   r   test_no_dtstart   s    	rK         c                 C  s   ||| j  | j S )zend of the event.r   )r   set_component_endr    r   r   r   dtend   s    	rO   c                 C  s8   t | }t||t| d< |   | t| | dS )r(   rO   N)r)   r   r   r*   r+   r,   )r   endr.   r   r   r   _set_component_end_init   s    rQ   c                 C  s   t | t| d| dS ))Create the event with the dtend property.r   N)setattrr   r   rP   r   r   r   _set_component_end_property   s    rU   c                 C  s
   || _ dS )rR   N)rP   rT   r   r   r   _set_component_end_attr   s    rV   c                 C  sF   |  t| d| |   }t| |   | t| | dS )z'Create the event with the end property.r   N)	r5   r   r6   r7   r8   r*   r+   r,   r9   )r   rP   r:   r   r   r   _set_component_end_ics   s
    rW   c                 C  s   | j S r<   r   r   r   r   r   rN      s    
rN   c                 C  s    t |d}t||| kstdS )Test the end of events.r   N)r   getattrr>   )rO   r    attrr   r   r   test_component_end_property   s    
r[   c                 C  s   |j | kstdS )rX   N)rP   r>   )rO   r    r   r   r   test_component_end   s    r\   rZ   r   c                 C  s4   t | |}t| | t| |d ks&tt| | d S N)r   delattrrY   r>   )r    r'   rO   rZ   r   r   r   test_delete_attr   s    

r_   r
   r   eventdurationc                 C  s   t || d< dS zSet the vDDDTypes valueDURATIONN)r   r`   r   r   r   _set_duration_vdddtypes   s    re   c                 C  s   |  d| dS rc   )r5   r`   r   r   r   _set_duration_add   s    rf   c                 C  s   t || d< dS rc   r   r`   r   r   r   _set_duration_vduration   s    rg   c                 C  s.   t |trtddntdd}|| | |S )z... events have a DATE value type for the "DTSTART" property ...
    If such a "VEVENT" has a "DURATION"
    property, it MUST be specified as a "dur-day" or "dur-week" value.
    rC   hoursrD   days)r   r   r   r   )r    r'   r   rb   r   r   r   rb      s    	rb   c                 C  s*   | j | j }||kst| j|ks&tdS )z'Check calculation of end with duration.N)rP   r-   r>   rb   )r    r'   rb   Zdurr   r   r   test_start_and_duration   s    rl     
      rd   rj   rh   r   )invalid_componentmessagezDDTSTART and DTEND must be of the same type, either date or datetime.z<Only one of DTEND and DURATION may be in a VEVENT, not both.z:When DTSTART is a date, DURATION must be of days or weeks.zBDTSTART and DUE must be of the same type, either date or datetime.z9Only one of DUE and DURATION may be in a VTODO, not both.r-   rP   c              	   C  s8   t t}t| | W 5 Q R X |jjd |ks4tdS )z3Test that the end and start throuw the right error.r   N)rG   rH   r   rY   valueargsr>   )rp   rq   rZ   er   r   r   test_invalid_event$  s    (ru   c                  C  s@   t  } tddddd| _| j| jks(t| jtddks<tdS )z
    For cases where a "VEVENT" calendar component
    specifies a "DTSTART" property with a DATE-TIME value type but no
    "DTEND" property, the event ends on the same calendar date and
    time of day specified by the "DTSTART" property.
    rm   rn      ro   r   rj   N)r
   r   r-   rP   r>   rb   r   ra   r   r   r   test_event_duration_zeroQ  s    rx   c                  C  sF   t  } tddd| _| j| jtdd ks.t| jtddksBtdS )z
    For cases where a "VEVENT" calendar component
    specifies a "DTSTART" property with a DATE value type but no
    "DTEND" nor "DURATION" property, the event's duration is taken to
    be one day
    rm   rn   rv   rC   rj   Nr
   r   r-   rP   r   r>   rb   rw   r   r   r   test_event_duration_one_day^  s    rz   c                  C  s@   t  } tddddd| _| j| jks(t| jtddks<tdS )z3We do not know about the duration of a todo really.rm   rn   rv   ro   r   rj   N)r   r   r-   rP   r>   rb   r   )todor   r   r   test_todo_duration_zerok  s    r|   c                  C  sF   t  } tddd| _| j| jtdd ks.t| jtddksBtdS )a  The end is at the end of the day, excluding midnight.

    RFC 5545:
    The following is an example of a "VTODO" calendar
    component that needs to be completed before May 1st, 2007.  On
    midnight May 1st, 2007 this to-do would be considered overdue.
    rm   rn   rv   rC   rj   Nry   rw   r   r   r   test_todo_duration_one_days  s    r}   incomplete_event_endc              	   C  s$   t t t| | W 5 Q R X dS )z)Test that the end throws the right error.N)rG   rH   r   rY   )r~   rZ   r   r   r   test_incomplete_event  s    r   invalid_valuerv   )	ComponentrZ   c              	   C  sN   | }t t}t|||  W 5 Q R X |jjd dt| j dksJtdS )z^Check that we get the right error.

    - other types that vDDDTypes accepts
    - object
    r   zUse datetime or date, not .N)	rG   rH   	TypeErrorrS   rr   rs   r,   __name__r>   )r   rZ   r   r   rt   r   r   r   test_set_invalid_start  s
    r   r)   )r.   c                 C  s   || |< d S r]   r   )r.   keyrr   r   r   r   setitem  s    r   i  c              	   C  sJ   || d< t t}| j W 5 Q R X |jjd dt|j dksFtdS )rF   rd   r   z"DURATION must be a timedelta, not r   N)	rG   rH   r   rd   rr   rs   r,   r   r>   )r    r   rt   r   r   r   test_check_invalid_duration  s    
r   c              	   C  s   t | d}tddddd| _tdd| _t| |tddddd d	| ksLt| jd
ksZtt| |}|tdddddks|td
S )z+Setting the end should not break the event.r   rm   rn   rv   ro   rC   rj      rd   N)r   r   r1   r   rd   rS   r>   rY   )r    r   rP   r   r   r   )test_setting_the_end_deletes_the_duration  s    

r   c              	   C  sv   t | d}tddddd| _t| |tddddd tdd| _|| ksLtt| |d	ks^t| jtddksrtd	S )
z0Setting the duration should not break the event.r   rm   rn   rv   ro   r   rC   rj   N)r   r   r1   rS   r   rd   r>   rY   )r    r   r   r   r   %test_setting_duration_deletes_the_end  s    
r   )rZ   rr   c                 C  sP   t | |}t| || || ks"tt| ||ks4tt| |d || ksLtdS )z(Setting attributes to None deletes them.N)r   rS   r>   rY   r    rZ   rr   r   r   r   "test_setting_to_none_deletes_value  s    
r   c                 C  sB   t | |}t| ||tdd  t| || t| ||ks>tdS )z'Setting attributes twice replaces them.rC   rj   N)r   rS   r   rY   r>   r   r   r   r   test_setting_a_value_twice  s    
r   c              	   C  s6   t | |}d| |< tt t| | W 5 Q R X dS )zSpecial case for None.N)r   rG   rH   r   rY   )r    rZ   r   r   r   test_invalid_none  s    
r   c                 C  s"   t dd| _| `| jdkstdS )zTest the del command.rC   rj   N)r   rd   r>   rJ   r   r   r   test_delete_duration  s    r   c                 C  sR   t  }t|||  | |jks t|j| ks.t|j| ks<t|jtdksNtdS )z,Test that we can set the start of a journal.r   N)r   rS   r1   r>   r-   rP   rb   r   )r-   rZ   jr   r   r   test_journal_start  s    r   c              	   C  s\   t  }tddddd|_d|_|jdks,td|ks8ttt t||  W 5 Q R X dS )z Delete the start of the journal.i  rv   r#   r$   rM   Nr1   )	r   r   r-   r1   r>   rG   rH   r   rY   )rZ   r   r   r   r   test_delete_journal_start0  s    r   c                  C  sh   t  } tddd| _tddd| _tddd| jks8t| jtdddksNt| jtdddksdtd S )Nrm   rC      )r   r   r1   r>   r-   rP   )r   r   r   r   *setting_twice_does_not_duplicate_the_entry<  s    r   )filetriggerrelatedZrfc_5545_absolute_alarm_exampleZ19970317T133000ZSTARTZrfc_5545_endENDZ
start_datec                 C  s(   | | }|j |kst|j|ks$tdS Get the trigger property.N)TRIGGERr>   TRIGGER_RELATED)alarmsr   r   r   alarmr   r   r   test_get_alarm_trigger_propertyE  s    r   c                  C  s8   t  } tdd| _| jtddks&t| jdks4tdS )Set the alarm trigger.rC   rh   r   N)r   r   r   r>   r   ar   r   r   test_set_alarm_triggerX  s    r   c                  C  s>   t  } tdd| _d| _| jtddks,t| jdks:tdS )r   rC   rh   r   N)r   r   r   r   r>   r   r   r   r   test_set_alarm_trigger_related`  s
    r   c                   C  s   t  jdkstdS )zThe default is startr   N)r   r   r>   r   r   r   r    test_get_related_without_triggeri  s    r   c               	   C  s:   t t} t }d|_W 5 Q R X | jjd dks6tdS )z)TRIGGER must be set to set the parameter.r   r   z<You must set a TRIGGER before setting the RELATED parameter.N)rG   rH   
ValueErrorr   r   rr   rs   r>   )rt   r   r   r   r   'test_cannot_set_related_without_triggern  s    
r   )r   triggersr   Z19970317T134500ZZ19970317T140000ZZ19970317T141500ZZ19970317T143000Zc                 C  s0   | | }t t|j t | |j|ks,tdS r   )r8   tupler   r>   )r   r   r   r   r   r   r   test_get_alarm_triggersy  s    r   c                   C  s   t  jdkstdS )z)An alarm with no trigger has no triggers.)r   r   r   N)r   r   r>   r   r   r   r   test_triggers_emtpy_alarm  s    r   c                  C  s,   t  } t| _t| _| jtfddfks(tdS )Check incomplete values.r   N)r   h1r   rd   r   r>   r   r   r   r   "test_triggers_emtpy_with_no_repeat  s    r   c                  C  s,   t  } t| _d| _| jtfddfks(tdS )r   rn   r   N)r   r   r   REPEATr   r>   r   r   r   r   $test_triggers_emtpy_with_no_duration  s    r   repeatr   c           	      C  s   | |   }||_||_t||jD ]v\}}|s>|dks$tq$t|d| ksRt|d |d ksftt|dd |dd D ]\}}|| |kstqq$dS )r   r   rC   r   Nr   )copyr   rd   zipr   r>   len)	r   r   r   rb   r   r   expectedxyr   r   r    test_get_alarm_triggers_repeated  s    "r   )i__doc__
__future__r   r   r   r   rG   Zicalendar.errorr   r   Zicalendar.calr   Zzoneinfor	   ImportErrorZbackports.zoneinfoZ	icalendarr
   r   r   r   r   Zicalendar.propr   r   Zfixturer    r'   r/   r3   r4   r;   r&   r@   rA   Zinvalid_start_event_1r5   r9   r6   Zinvalid_start_event_2Zinvalid_start_event_3Zinvalid_start_todo_1Zinvalid_start_todo_2Zinvalid_start_todo_3markZparametrizerI   rK   rO   rQ   rU   rV   rW   rN   r[   r\   r_   re   rf   rg   rb   rl   Zinvalid_event_end_1Zinvalid_event_end_2Zinvalid_event_end_3Zinvalid_event_end_4Zinvalid_todo_end_1Zinvalid_todo_end_2Zinvalid_todo_end_3Zinvalid_todo_end_4ru   rx   rz   r|   r}   Zincomplete_event_1Zincomplete_event_2Zincomplete_todo_1Zincomplete_todo_2r   objectr   r   r   r   r   Zvalid_valuesr   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   <module>   s$  



	
 


	


%	











	
	

