o
    3i%                     @   s   d dl Z d dlZd dlZd dlmZ d dlmZmZmZ d dlm	Z	 d dl
mZ d dlmZ d dl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 dZdZG dd dejZG dd dejZdS )    N)	dataclass)datedatetime	timedelta)Decimal)Optional)UUID)tzutc)parameterized)	BaseModel)utils)FeatureFlagResult+kOOlRy2QlMY9jHZQv0bKz0FZyazBUoY8Arj0lFVNjs4
random_keyc                   @   s   e Zd ZeddgdedefddZdd Zd	d
 Z	dd Z
dd Zdd Zeg ddd Zdd ZdddZdd ZdS )	TestUtils)znaive datetime should be naiveT)z+timezone-aware datetime should not be naiveF_nameexpected_naivec                 C   s2   |rt  }nt jt d}t||u sJ d S )Ntz)r   nowr	   r   is_naive)selfr   r   dt r   e/lsinfo/ai/hellotax_ai/llm_service/venv_embed/lib/python3.10/site-packages/posthog/test/test_utils.pytest_is_naive   s   
zTestUtils.test_is_naivec                 C   sL   t  }t jt d}t|}t|du sJ t|}||ks$J d S )Nr   F)r   r   r	   r   guess_timezoner   )r   r   utcnowfixedshouldnt_be_editedr   r   r   test_timezone_utils&   s   

zTestUtils.test_timezone_utilsc              
   C   s   t dtdt dddddd d	}tdtd	d
g dd}t| }|	|  |
 }t| |
 |ks>J ttddksIJ d S )Nz0.142857wooi    g       @T)	decimalunicoder   longintegerfloatboolstrnonezThis should show up   )microseconds)r"         )	exceptionr   list 12345678123456781234567812345678$12345678-1234-5678-1234-567812345678)r   sixur   r   	Exceptionr   dictitemsupdatekeysr   cleanr   )r   simplecomplicatedcombinedpre_clean_keysr   r   r   
test_clean0   s,   
zTestUtils.test_cleanc                 C   s2   t dddtjt dd}|t|ksJ d S )Ni  r"   r   )	birthdateregistration)r   r   r   r	   r   r:   )r   dict_with_datesr   r   r   test_clean_with_datesQ   s   
zTestUtils.test_clean_with_datesc                 C   s(   t d}t| t|dksJ d S )N
   z
          )bytesr   r:   )r   itemr   r   r   
test_bytesX   s   
zTestUtils.test_bytesc                 C   s*   t dd dd}|d ddksJ d S )Nc                 S   s   | S Nr   )xr   r   r   <lambda>^   s    z)TestUtils.test_clean_fn.<locals>.<lambda>   )fnnumberr   r:   )r   cleanedr   r   r   test_clean_fn]   s   zTestUtils.test_clean_fn))zhttp://posthog.io/http://posthog.io)rQ   rQ   )zhttps://example.com/path/https://example.com/path)rR   rR   c                 C   s   |t |ks	J d S rH   )r   remove_trailing_slash)r   	input_urlexpected_urlr   r   r   test_remove_slasha   s   	zTestUtils.test_remove_slashc                    s   G dd dt  G dd dt}G  fdddt }t ddd	ddd d
ks+J tjdk r@t|ddd	ddd	ks@J t| dddd
dddddd
iksWJ d S )Nc                   @   s.   e Zd ZU eed< eed< dZee ed< dS )z.TestUtils.test_clean_pydantic.<locals>.ModelV2foobarNbaz)__name__
__module____qualname__r)   __annotations__intrY   r   r   r   r   r   ModelV2m   s   
 r_   c                   @   s   e Zd ZU eed< eed< dS )z.TestUtils.test_clean_pydantic.<locals>.ModelV1rW   rX   N)rZ   r[   r\   r^   r]   r)   r   r   r   r   ModelV1r   s   
 r`   c                       s   e Zd ZU  ed< dS )z2TestUtils.test_clean_pydantic.<locals>.NestedModelrW   N)rZ   r[   r\   r]   r   r_   r   r   NestedModelv   s   
 rb   1r-   )rW   rX   )rW   rX   rY   )r.      r"   23)rW   rW   )r   BaseModelV1r   r:   sysversion_info)r   r`   rb   r   ra   r   test_clean_pydanticl   s   
 zTestUtils.test_clean_pydanticreturnNc                 C   s.   G dd d}t d| idd iksJ d S )Nc                   @   s   e Zd ZdedefddZdS )z7TestUtils.test_clean_pydantic_like_class.<locals>.Dummyrequired_paramrk   c                 S   s   i S rH   r   )r   rl   r   r   r   
model_dump   s   zBTestUtils.test_clean_pydantic_like_class.<locals>.Dummy.model_dumpN)rZ   r[   r\   r)   r6   rm   r   r   r   r   Dummy   s    rn   testrN   )r   rn   r   r   r   test_clean_pydantic_like_class   s    z(TestUtils.test_clean_pydantic_like_classc                    s|   t G dd d t G  fddd}t|dd ddtd	td
dddddddddtd
ddd ddks<J d S )Nc                   @   s>   e Zd ZU eed< eed< eed< eed< dZe	e ed< dS )z6TestUtils.test_clean_dataclass.<locals>.InnerDataClass	inner_foo	inner_bar
inner_uuid
inner_dateNinner_optional)
rZ   r[   r\   r)   r]   r^   r   r   ru   r   r   r   r   r   InnerDataClass   s   
 rv   c                       s&   e Zd ZU eed< eed<  ed< dS )z5TestUtils.test_clean_dataclass.<locals>.TestDataClassrW   rX   nestedN)rZ   r[   r\   r)   r]   r^   r   rv   r   r   TestDataClass   s   
 ry   rc   r-   rf   rK   r1   i  r"   )rq   rr   rs   rt   )rW   rX   rw   r2   )rq   rr   rs   rt   ru   )r   r   r:   r   r   )r   ry   r   rx   r   test_clean_dataclass   s4   

zTestUtils.test_clean_dataclass)rk   N)rZ   r[   r\   r
   expandr)   r(   r   r    r?   rC   rG   rP   rV   rj   rp   rz   r   r   r   r   r      s&    
!

r   c                   @   s<   e Zd Zdd Zdd Zdd Zdd Zd	d
 Zdd ZdS )TestFlagCachec                 C   s$   t jddd| _tddd | _d S )Nr.   r"   )max_sizedefault_ttl	test-flagT)r   	FlagCachecacher   from_value_and_payloadflag_result)r   r   r   r   setUp   s   
zTestFlagCache.setUpc                 C   sh   d}d}d}| j |||}|d u sJ | j ||| j| | j |||}|d us,J | s2J d S )Nuser123r   r"   )r   get_cached_flagset_cached_flagr   	get_valuer   distinct_idflag_keyflag_versionresultr   r   r   test_cache_basic_operations   s   
z)TestFlagCache.test_cache_basic_operationsc                 C   sf   d}d}d}| j ||| j| | j |||}|d usJ td | j |||}|d u s1J d S Nr   r   r"   g?)r   r   r   r   timesleepr   r   r   r   test_cache_ttl_expiration   s   

z'TestFlagCache.test_cache_ttl_expirationc                 C   s   d}d}d}d}| j ||| j| | j |||}|d us J | j |||}|d u s.J | j | | j |||}|d u sBJ d S )Nr   r   r"   r-   )r   r   r   r   invalidate_version)r   r   r   old_versionnew_versionr   r   r   r   test_cache_version_invalidation   s   z-TestFlagCache.test_cache_version_invalidationc                 C   sp   d}d}d}| j ||| j| td | j |||}|d u s#J | j ||}|d us0J | s6J d S r   )r   r   r   r   r   r   get_stale_cached_flagr   )r   r   r   r   r   stale_resultr   r   r   test_stale_cache_functionality   s   

z,TestFlagCache.test_stale_cache_functionalityc                 C   s   d}t dD ]}d| }| j|d| j| q| jdd| | jdd| j| | jdd|}|d us8J | jdd|}|d usFJ | jdd|}|d usTJ d S )Nr"   r.   userr   user0user3user2)ranger   r   r   r   )r   r   iuser_idr   r   r   r   test_lru_eviction  s   

zTestFlagCache.test_lru_evictionN)	rZ   r[   r\   r   r   r   r   r   r   r   r   r   r   r|      s    r|   )rh   r   unittestdataclassesr   r   r   r   r#   r   typingr   uuidr   r3   dateutil.tzr	   r
   pydanticr   pydantic.v1rg   posthogr   posthog.typesr   TEST_API_KEYFAKE_TEST_API_KEYTestCaser   r|   r   r   r   r   <module>   s(     !