o
    .j                     @   s   d Z ddlmZmZ ddlmZ ddlZddlmZmZ ddl	m
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 )#z:Tests for issue #857: enhanced API usability improvements.    )datetime	timedelta)ZoneInfoN)CalendarEvent)IncompleteComponentc                  C   s0   t jdddd} | jdksJ | jdksJ dS )zLTest Calendar.new() with organization and language. Generates proper prodid.foo.org
My partiesenorganizationnamelanguage-//foo.org//My parties//ENN)r   newcalendar_nameprodidcalendar r   `/home/thesage/.local/lib/python3.10/site-packages/icalendar/tests/test_issue_857_enhanced_api.py0test_calendar_new_with_organization_and_language   s   r   c                  C       t jddd} | jdksJ dS )zFTest Calendar.new() with organization but no language. Defaults to EN.example.comTest Calendarr   r   z!-//example.com//Test Calendar//ENNr   r   r   r   r   r   r   /test_calendar_new_with_organization_no_language      r   c                  C   r   )zCTest Calendar.new() with organization but no name. Uses 'Calendar'.r   fr)r   r   z-//example.com//Calendar//FRNr   r   r   r   r   +test_calendar_new_with_organization_no_name   r   r    c                  C   s$   t jddddd} | jdksJ dS )zBTest that explicit prodid overrides organization-based generation.r   r	   r
   z-//Custom//Product//ID)r   r   r   r   Nr   r   r   r   r   8test_calendar_new_explicit_prodid_overrides_organization"   s   r!   c                  C   s,   t jdd} d| jv sJ d| jv sJ dS )z7Test that without organization, default prodid is used.r   r   
collective	icalendarNr   r   r   r   r   5test_calendar_new_no_organization_uses_default_prodid.   s   r%   c                  C   sX   t jdtdddddd} tdd	| _| jtdd	ksJ | jtdddd
dks*J dS )z@Test that setting event duration. Correctly calculates end time.Test Meeting              summarystart   hours   N)r   r   r   r   durationendeventr   r   r   test_event_duration_setter7   s   r7   c                  C   sf   t dddddtdd} tjd| d	}td
d|_| td
d }|jtd
dks*J |j|ks1J dS )z7Test duration setter. Works with timezone-aware events.r'   r(   r)   r*   r+   zEurope/Londontzinfor&   r,   Z   minutesN)r   r   r   r   r   r3   r4   )
start_timer6   expected_endr   r   r   (test_event_duration_setter_with_timezoneB   s   r?   c                  C   sL   t jdd} tjtdd tdd| _W d   dS 1 sw   Y  dS )z<Test that setting duration without start time. Raises error.zIncomplete Event)r-   z#Cannot set duration without DTSTARTmatch   r0   N)r   r   pytestraisesr   r   r3   r5   r   r   r   5test_event_duration_setter_without_start_raises_errorN   s   "rE   c                  C   r   t  } | dd tjdtdddddd	}| | tjtd
d | 	  W d   dS 1 s2w   Y  dS )z,Test that validate() detects missing PRODID.VERSION2.0Testr'   r(   r)   r*   r+   r,   zCalendar must have a PRODIDr@   N
r   addr   r   r   add_componentrC   rD   r   validater   r6   r   r   r   %test_calendar_validate_missing_prodidX      

"rO   c                  C   rF   )z-Test that validate() detects missing VERSION.PRODID-//Test//Test//ENrI   r'   r(   r)   r*   r+   r,   zCalendar must have a VERSIONr@   NrJ   rN   r   r   r   &test_calendar_validate_missing_versionc   rP   rS   c                  C   sZ   t  } | dd | dd tjtdd |   W d   dS 1 s&w   Y  dS )z,Test that validate() detects empty calendar.rQ   rR   rG   rH   z,Calendar must contain at least one componentr@   N)r   rK   rC   rD   r   rM   r   r   r   r   $test_calendar_validate_no_componentsn   s   
"rT   c               	   C   sj   t jdd} tjdtdddddtdddd	dd
}| | |   |  }d|v s-J d|v s3J dS )z2Test that validate() succeeds with valid calendar.zValid Calendarr"   z
Test Eventr'   r(   r)   r*   r+   r2   )r-   r.   r4   s   BEGIN:VCALENDARs   BEGIN:VEVENTN)r   r   r   r   rL   rM   to_ical)r   r6   ical_outputr   r   r   1test_calendar_validate_passes_with_valid_calendarz   s   
rW   c                  C   s   t jdddd} tddddd	td
d}tj|d}tdd|_d|_| jdks*J | j	dks1J |j
|ks8J |j|tdd ksDJ |jdksKJ |jtddksUJ | | |  }d|v sdJ dS )z:Test the exact workflow desired by the user in issue #857.r   r	   r
   r   r'   r(   r)   r*   r+   UTCr8   )r.      r;   zSome stringr   s!   PRODID:-//foo.org//My parties//ENN)r   r   r   r   r   r   r3   r-   r   r   r.   r4   rL   rU   )cal
mydatetimer6   rV   r   r   r   )test_user_desired_workflow_from_issue_857   s   
r\   c                  C   s   t jddd} tjdtddddddtd	d
d}tdd|_| | |   | 	 
 }d|v s4J d|v s:J d|v s@J d|v sFJ d|v sLJ d|v sRJ dS )zHTest that enhanced API is much simpler than original example from issue.r   zMy calendarr   z Python meeting about calendaringi        r   rX   r8   r,   r/   r0   z-//example.com//My calendar//ENzVERSION:2.0zUID:zDTSTAMP:DTSTARTDURATIONN)r   r   r   r   r   r   r3   rL   add_missing_timezonesrU   decode)rZ   r6   rV   r   r   r   .test_enhanced_api_compared_to_original_example   s   
rc   )__doc__r   r   zoneinfor   rC   r$   r   r   icalendar.errorr   r   r   r    r!   r%   r7   r?   rE   rO   rS   rT   rW   r\   rc   r   r   r   r   <module>   s(    	
