o
    .jD                     @   s  d Z ddlmZ ddlZddlmZmZmZ ddlm	Z	 er$ddlm
Z
 deddfd	d
ZdeddfddZG dd dZejdd Zejdd Zdd Zdd Zdd Zdd Zdd Zejdg ddd  Zd!d" Zd#d$ Zd%d& Zd'd( Zd)d*d+d,d-d.Zejd/d0d1gejd2g d3ejd4d0d1gd5d6 Zd7d8 Zd9d: Z d;d< Z!ejd=g d>ejd4d0d1gd?d@ Z"dAdB Z#dCdD Z$dS )EzcTest lazy parsing of calendar components.

See https://github.com/collective/icalendar/issues/1050
    )TYPE_CHECKINGN)AlarmEventLazyCalendarLazySubcomponent)Calendarcalendarreturnc                 C   s,   t | tsJ | j sJ |  sJ dS )z,Assert that the calendar is a lazy calendar.N
isinstancer   _subcomponentsis_lazyr	    r   a/home/thesage/.local/lib/python3.10/site-packages/icalendar/tests/test_issue_1050_lazy_parsing.pyassert_is_lazy_calendar      r   c                 C   s,   t | tsJ | j rJ |  rJ dS )z.Assert that the calendar is a parsed calendar.Nr   r   r   r   r   assert_is_parsed_calendar   r   r   c                   @   s   e Zd Zdd Zdd ZdS )
MockParserc                 C   s   || _ d| _d S )Nr   )
_component_calls)self	componentr   r   r   __init__    s   
zMockParser.__init__c                 C   s   |  j d7  _ | jgS )zReturn the component.   )r   r   )r   r   r   r   parse$   s   zMockParser.parseN)__name__
__module____qualname__r   r   r   r   r   r   r      s    r   c                   C   s   t dS )z-A mock parser that returns a fixed component.mock_component)r   r   r   r   r   mock_parser*   s   r!   c                 C   s
   t d| S )z.A lazy subcomponent that uses the mock parser.VEVENTr   )r!   r   r   r   lazy_subcomponent0   s   
r#   c                 C   s,   |  rJ |j| u sJ | jdksJ dS )z&The component is initially not parsed.r   N)	is_parsed_parserr   )r!   r#   r   r   r   1test_lazy_subcomponent_is_not_parsed_when_created6   s   r&   c                 C   s2   |   }|dks
J |jdksJ |  sJ dS )z!We retrieve the component inside.r    r   N)r   r   r$   r#   r!   r   r   r   r   (test_lazy_subcomponent_returns_component=   s   r(   c                 C   s.   |   }|   }||u sJ |jdksJ dS )z)Parsing twice returns the same component.r   N)r   r   )r#   r!   
component1
component2r   r   r   )test_parsing_twice_returns_same_componentE   s   r+   c                 C   s&   |   }|dks
J | jdu sJ dS )z8Test that we can remove references to the content lines.r    N)r   r%   r'   r   r   r    test_when_parsed_parser_is_freedM   s   r,   c                 C   s   t | jtsJ dS )z7LazyCalendar.from_ical returns a LazyCalendar instance.N)r   emptyr   )lazy_calendarsr   r   r   %test_lazy_calendar_returns_a_calendarT   s   r/   component_name)r"   VTODOVALARMzX-COMPONENT	VTIMEZONEc                 C   s4   t d|  d|  }|j| ksJ | rJ dS )zLLazyCalendar.from_ical() returns components as Calendar and as Component do.zBEGIN:z
END:N)r   	from_icalnamer   )r0   r   r   r   r   1test_non_calendar_components_are_parsed_correctlyY   s   r6   c                 C   s   | j }t| dS )z,Test that the BigCalendar is initially lazy.N)exampler   r.   r	   r   r   r   #test_big_calendar_is_initially_lazyc   s   r9   c                 C   s   | j }|t  t| dS )z4Test that we can add a component to a lazy calendar.N)r-   add_componentr   r   r8   r   r   r   *test_adding_a_component_to_a_lazy_calendari   s   r;   c                 C   s8   | j }|j}t|dksJ t| |j j|ksJ dS )z:Test that we can get the subcomponents of a lazy calendar.   N)r7   subcomponentslenr   )r.   	calendarsr	   r=   r   r   r   1test_getting_the_subcomponents_of_a_lazy_calendarp   s
   r@   c                 C   s*   | j }|j}|j}||u sJ t| dS )zCTest that parsing a lazy calendar twice does not change the result.N)r7   r=   r   )r.   r	   subcomponents1subcomponents2r   r   r   "test_parsing_a_lazy_calendar_twicey   s
   rC   	timezoneseventstodosjournalsavailabilities)r3   r"   r1   VJOURNALVAVAILABILITYwalkTF)r5   parsed))r3   r3   )r"   r3   r"   )r1   r3   r1   )rI   r3   rI   )rJ   r3   rJ   fully_parsedc                 C   sv   |j }|r|j | r||nt|t| }t|dks J d|d j|ks)J t|tr7|s9t	|| dS dS dS )zCheck that only timezones are parsed.

    Skipping FREEBUSY for now is ok as they are for requests and need the whole calendar anyway.
    r   z8We only have one component of each type in the calendar.r   N)
issue_1050_all_componentsr=   rK   getattrname2getterr>   r5   r   r   assert_only_parsed)rK   r?   r5   rL   rR   r	   
componentsr   r   r   test_accessing_timezones   s   rX   c                 C   sT   |   sJ ddd | jjD }|t|ks(J dd| dd| ddS )	z6Check that only certain types of components are parsedz(The calendar should not be fully parsed.c                 S   s   h | ]	}|  r|jqS r   )r$   r5   ).0lcr   r   r   	<setcomp>   s
    z%assert_only_parsed.<locals>.<setcomp>zWe expect only z, z to be parsed, not .N)r   r   _componentssetjoin)r	   rL   parsed_in_calendarr   r   r   rV      s   rV   c                 C   s.   | j }| }t|dksJ | rJ dS )zWalk goes through them all.	   N)rS   rK   r>   r   r?   r	   rW   r   r   r   #test_walk_parses_the_whole_calendar   s   rc   c                 C   s6   | j d}t|dksJ |d }|jdksJ dS )z%Walking and finding the alarm inside.r2   r   r   N)rS   rK   r>   r5   )r?   alarmsalarmr   r   r   test_walk_and_find_an_alarm   s   rf   )uidrL   ))zevent-1rN   )z
calendar-1rM   )ztodo-1rO   )z	journal-1rP   )zavailability-1rQ   )zalarm-1rN   c                 C   s`   | j }|r|j ||}t|dksJ |d j|ksJ t|tr,|s.t|| d S d S d S )Nr   r   )rS   r=   with_uidr>   rg   r   r   rV   )r?   rg   rL   rR   r	   rW   r   r   r   test_get_with_uid   s   
ri   c                 C   s$   | j }|d}t|dksJ d S )Nznon-existent-uidr   rS   rh   r>   rb   r   r   r   test_absent_uid   s   
rk   c                 C   s    | j d}t|dksJ dS )zCheck that even if the UID is present somewhere the component is not
    returned.

    See https://github.com/collective/icalendar/pull/1201/changes#r2869287074
    re   r   Nrj   )r?   rW   r   r   r   /test_uid_is_present_but_not_in_the_uid_property   s   rl   )%__doc__typingr   pytest	icalendarr   r   r   icalendar.parser.ical.lazyr   r   r   r   r   fixturer!   r#   r&   r(   r+   r,   r/   markparametrizer6   r9   r;   r@   rC   rU   rX   rV   rc   rf   ri   rk   rl   r   r   r   r   <module>   sf    


	
	
