o
    
i                     @   s   d dl Z d dlmZ d dlZd dlmZ d dlZddlmZm	Z	 ddlm
Z
m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eddedddd Zdd ZdS )    N)Path)patch   )read_msgpackwrite_msgpack)msgpack_loadsmsgpack_dumps)msgpack_encodersmsgpack_decoders   )make_tempdirc                  C   s*   ddd} ddg}t | }||v sJ d S )Nworld{   hellotest   helloworldtest{   test{helloworld)r   )dataexpectedmsg r   P/home/ubuntu/.local/lib/python3.10/site-packages/srsly/tests/test_msgpack_api.pytest_msgpack_dumps   s   
r   c                  C   s@   d} t | }t|dksJ |d dksJ |d dksJ d S )Nr   r   r   r   r   r   )r   len)r   r   r   r   r   test_msgpack_loads   s
   r   c                  C   st   d} t d| idd}|d }| sJ t|}W d    n1 s#w   Y  t|dks0J |d dks8J d S )Ns   helloworldtmp.msgwbmoder   r   r   )r   existsr   r   )file_contentstemp_dir	file_pathr   r   r   r   test_read_msgpack_file   s   
r$   c               	   C   s   d} t d| idd4}|d }| sJ tt t| W d    n1 s)w   Y  W d    d S W d    d S 1 sAw   Y  d S )Ns   helloworldr   r   r   )r   r    pytestraises
ValueErrorr   )r!   r"   r#   r   r   r   test_read_msgpack_file_invalid&   s   
"r(   c               	   C   s   ddd} ddg}t dd9}|d }t||  t|d	}| |v s(J W d    n1 s2w   Y  W d    d S W d    d S 1 sJw   Y  d S )
Nr   r   r   r   r   r   r   r   rb)r   r   r   openread)r   r   r"   r#   fr   r   r   test_write_msgpack_file/   s   

"r-   zsrsly.msgpack._msgpack_numpy.npz&srsly.msgpack._msgpack_numpy.has_numpyFc                   C   sB   t t tttj  W d   dS 1 sw   Y  dS )zTest that msgpack works without numpy and raises correct errors (e.g.
    when serializing datetime objects, the error should be msgpack's TypeError,
    not a "'np' is not defined error").N)r%   r&   	TypeErrorr   r   datetimenowr   r   r   r   test_msgpack_without_numpy9   s   "r1   c                     s:  G dd d d fdd	} d fdd	}d dd	id
}t t t| W d    n1 s2w   Y  tjd| d tjd|d t|}t|}|d dksUJ t|d  s^J |d j	dd	iksiJ t
d dd	id
}t|}t|}t|d t
jsJ t|d  sJ |d j	dd	iksJ d S )Nc                   @   s   e Zd Zdd ZdS )z9test_msgpack_custom_encoder_decoder.<locals>.CustomObjectc                 S   s
   || _ d S N)value)selfr3   r   r   r   __init__E   s   
zBtest_msgpack_custom_encoder_decoder.<locals>.CustomObject.__init__N)__name__
__module____qualname__r5   r   r   r   r   CustomObjectD   s    r9   c                    s(   t |  r
d| jiS |d u r| S || S N
__custom__)
isinstancer3   objchainr9   r   r   serialize_objH   s   

z:test_msgpack_custom_encoder_decoder.<locals>.serialize_objc                    s(   d| v r
 | d S |d u r| S || S r:   r   r=   r@   r   r   deserialize_objM   s   z<test_msgpack_custom_encoder_decoder.<locals>.deserialize_objr   foobar)abcustom_object)funcrE   rF   )r   r      r2   )r%   r&   r.   r   r	   registerr
   r   r<   r3   numpyzerosndarray)rA   rB   r   
bytes_datanew_datar   r@   r   #test_msgpack_custom_encoder_decoderC   s(   
rP   )r%   pathlibr   r/   mockr   rK   _msgpack_apir   r   r   r   r	   r
   utilr   r   r   r$   r(   r-   r1   rP   r   r   r   r   <module>   s$    
	
