o
    
ic                      @   s  d dl Z d dlmZ d dlmZ d dlZd dlZddlmZm	Z	m
Z
mZmZ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 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"e j#$d'g d(d)fd*d+d,d)fd-d.d/ d0fgd1d2 Z%e j#$d'g d3d4d5 Z&e j#$d6g d7d8d9 Z'd:d; Z(d<d= Z)d>d? Z*d@dA Z+dS )B    N)StringIO)Path   )	read_json
write_json
read_jsonlwrite_jsonlread_gzip_jsonlwrite_gzip_jsonl)write_gzip_json
json_dumpsis_json_serializable
json_loads)force_string   )make_tempdirc                  C   s(   dddd} t | dd}|dksJ d S )Nr      r   )acbT)	sort_keysz{"a":1,"b":2,"c":3})r   )dataresult r   M/home/ubuntu/.local/lib/python3.10/site-packages/srsly/tests/test_json_api.pytest_json_dumps_sort_keys   s   r   c                  C   sp   d} t d| i}|d }| sJ t|}W d    n1 s!w   Y  t|dks.J |d dks6J d S )N{
    "hello": "world"
}tmp.jsonr   helloworld)r   existsr   lenfile_contentstemp_dir	file_pathr   r   r   r   test_read_json_file   s   
r'   c               	   C   s   d} t d| i4}|d }| sJ tt t| W d    n1 s'w   Y  W d    d S W d    d S 1 s?w   Y  d S )Nz{
    "hello": world
}r   )r   r!   pytestraises
ValueErrorr   )r$   r%   r&   r   r   r   test_read_json_file_invalid%   s   
"r+   c                 C   s@   d}|  dt| td}t|dksJ |d dksJ d S )Nr   	sys.stdin-r   r   r    )setattrr   r   r"   monkeypatch
input_datar   r   r   r   test_read_json_stdin.   s
   r2   c               	   C   s   ddd} ddg}t  ;}|d }t||  t|jdd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   test#{
  "hello":"world",
  "test":123
}#{
  "test":123,
  "hello":"world"
}r   rutf8encoding)r   r   r   openreadr   expectedr%   r&   fr   r   r   test_write_json_file6   s   

"rA   c               	   C   s   ddd} ddg}t  =}t|d }t||  t|d}| d|v s*J W d    n1 s4w   Y  W d    d S W d    d S 1 sLw   Y  d S )	Nr    r3   r4   r6   r7   r   r8   r9   )r   r   r   gzipr<   r=   decoder>   r   r   r   test_write_json_file_gzipD   s   

"rD   c                 C   s6   ddd}ddg}t d| |  }|j|v sJ d S )Nr    r3   r4   z${
  "hello":"world",
  "test":123
}
z${
  "test":123,
  "hello":"world"
}
r-   )r   
readouterrout)capsysr   r?   capturedr   r   r   test_write_json_stdoutR   s   

rI   c                  C   s   d} t d| i!}|d }| sJ t|}t|drJ t|}W d    n1 s,w   Y  t|dks9J t|d dksCJ t|d dksMJ |d d dksWJ |d d	 d
ksaJ d S )N {"hello": "world"}
{"test": 123}r   __len__r   r   r   r   r    r5   r3   )r   r!   r   hasattrlistr"   r#   r   r   r   test_read_jsonl_file^   s   
rN   c               	   C   s   d} t d| i5}|d }| sJ tt tt|}W d    n1 s)w   Y  tt|dd}W d    n1 s@w   Y  t|dksMJ t|d dksWJ |d d dksaJ d S )	Nz{"hello": world}
{"test": 123}r   T)skipr   r   r5   r3   )r   r!   r(   r)   r*   rM   r   r"   r#   r   r   r   test_read_jsonl_file_invalidn   s   rP   c                 C   s   d}|  dt| td}t|drJ t|}t|dks!J t|d dks+J t|d dks5J |d d d	ks?J |d d
 dksIJ d S )NrJ   r,   r-   rK   r   r   r   r   r    r5   r3   )r.   r   r   rL   rM   r"   r/   r   r   r   test_read_jsonl_stdin{   s   rQ   c               	   C   s   ddiddig} t  ;}|d }t||  t|jddd}| d	ks'J W d    n1 s1w   Y  W d    d S W d    d S 1 sIw   Y  d S )
Nr   r    r5   r3   r   r8   r9   r:   {"hello":"world"}
{"test":123}
r   r   r   r<   r=   )r   r%   r&   r@   r   r   r   test_write_jsonl_file   s   
"rT   c               	   C   s   ddiddig} d}t  B}|d }t||  t|| dd t|jd	d
d}| |ks0J W d    n1 s:w   Y  W d    d S W d    d S 1 sRw   Y  d S )Nr   r    r5   r3   z?{"hello":"world"}
{"test":123}

{"hello":"world"}
{"test":123}
r   Tappendr8   r9   r:   rS   r>   r   r   r   test_write_jsonl_file_append   s   
"rW   c               	   C   s   ddiddig} d}t  C}|d }t||  t|| ddd	 t|jd
dd}| |ks1J W d    n1 s;w   Y  W d    d S W d    d S 1 sSw   Y  d S )Nr   r    r5   r3   z>{"hello":"world"}
{"test":123}
{"hello":"world"}
{"test":123}
r   TF)rV   append_new_liner8   r9   r:   rS   r>   r   r   r   (test_write_jsonl_file_append_no_new_line   s   
"rY   c                 C   s4   ddiddig}t d| |  }|jdksJ d S )Nr   r    r5   r3   r-   rR   )r   rE   rF   )rG   r   rH   r   r   r   test_write_jsonl_stdout   s   
rZ   zobj,expected)r   r   r   r   Tr   r3   )r   r   )r   Tc                 C   s   | S Nr   )xr   r   r   <lambda>   s    r]   Fc                 C      t | |ksJ d S r[   )r   objr?   r   r   r   test_is_json_serializable      
ra   ))z-32i)32    )0r   )z-0r   c                 C   r^   r[   r   r_   r   r   r   test_json_loads_number_string   rb   rf   r`   )HIr-   z-?z?!zTHIS IS A STRINGc                 C   s8   t t t|  W d    d S 1 sw   Y  d S r[   )r(   r)   r*   r   )r`   r   r   r   test_json_loads_raises   s   
"rh   c                  C   s@   t  } tt t| }W d    d S 1 sw   Y  d S r[   )numpyfloat32r(   r)   	TypeErrorr   )r@   sr   r   r   test_unsupported_type_error   s   
"rm   c               	   C   s   ddiddig} ddg}t  =}|d }t||  t|d}d	d
 | D |ks-J W d   n1 s7w   Y  W d   dS W d   dS 1 sOw   Y  dS )z,Tests writing data to a gzipped .jsonl file.r   r    r5   r3   {"hello":"world"}
{"test":123}
r   r8   c                 S      g | ]}| d qS r9   rC   .0liner   r   r   
<listcomp>       z)test_write_jsonl_gzip.<locals>.<listcomp>Nr   r
   rB   r<   	readlinesr>   r   r   r   test_write_jsonl_gzip   s   
"rz   c               	   C   s   ddiddig} g d}t  D}|d }t||  t|| dd t|d	}d
d | D |ks4J W d   n1 s>w   Y  W d   dS W d   dS 1 sVw   Y  dS )z.Tests appending data to a gzipped .jsonl file.r   r    r5   r3   )rn   ro   
rn   ro   r   TrU   r8   c                 S   rp   rq   rr   rs   r   r   r   rv      rw   z0test_write_jsonl_gzip_append.<locals>.<listcomp>Nrx   r>   r   r   r   test_write_jsonl_gzip_append   s   
"r|   c               	   C   s  ddiddig} t  A}|d }t|d}|dd | D  W d	   n1 s+w   Y  | s6J t|}t|d
rAJ t|}W d	   n1 sOw   Y  t|dks\J t|d dksfJ t|d dkspJ |d d dkszJ |d d dksJ d	S )z.Tests reading data from a gzipped .jsonl file.r   r    r5   r3   r   wc                 S   s   g | ]}t |d  dqS )r{   zutf-8)r   encoders   r   r   r   rv      s    z(test_read_jsonl_gzip.<locals>.<listcomp>NrK   r   r   r   )	r   rB   r<   
writelinesr!   r	   rL   rM   r"   )r$   r%   r&   r@   r   r   r   r   test_read_jsonl_gzip   s$   
r   ),r(   ior   pathlibr   rB   ri   	_json_apir   r   r   r   r	   r
   r   r   r   r   utilr   r   r   r'   r+   r2   rA   rD   rI   rN   rP   rQ   rT   rW   rY   rZ   markparametrizera   rf   rh   rm   rz   r|   r   r   r   r   r   <module>   s^     
		


	
	
