B
    u9aÙ1  ã               @   sŒ   d dl mZmZmZ d dlZd dlZd dlmZ d dlZG dd„ dej	ƒZ
G dd„ dƒZG dd	„ d	eƒZG d
d„ deƒZG dd„ deƒZdS )é    )ÚfindfileÚTESTFNÚunlinkN)Úmockc               @   s   e Zd Zdd„ Zdd„ ZdS )ÚUnseekableIOc             C   s
   t j‚d S )N)ÚioÚUnsupportedOperation)Úself© r
   ú /usr/lib/python3.7/audiotests.pyÚtell	   s    zUnseekableIO.tellc             O   s
   t j‚d S )N)r   r   )r	   ÚargsÚkwargsr
   r
   r   Úseek   s    zUnseekableIO.seekN)Ú__name__Ú
__module__Ú__qualname__r   r   r
   r
   r
   r   r      s   r   c               @   s(   e Zd ZdZdd„ Zdd„ Zdd„ ZdS )	Ú
AudioTestsFc             C   s   d  | _ | _d S )N)ÚfÚfout)r	   r
   r
   r   ÚsetUp   s    zAudioTests.setUpc             C   s4   | j d k	r| j  ¡  | jd k	r(| j ¡  ttƒ d S )N)r   Úcloser   r   r   )r	   r
   r
   r   ÚtearDown   s
    



zAudioTests.tearDownc          	   C   s  |   | ¡ |¡ |   | ¡ |¡ |   | ¡ |¡ |   | ¡ |¡ |   | ¡ |¡ |   | ¡ |¡ | ¡ }|   |||||||f¡ |   |j|¡ |   |j	|¡ |   |j
|¡ |   |j|¡ |   |j|¡ |   |j|¡ x4ttjd ƒD ]"}	t ||	¡}
|   t |
¡|¡ qäW d S )Né   )ÚassertEqualÚgetnchannelsÚgetsampwidthÚgetframerateÚ
getnframesZgetcomptypeZgetcompnameÚ	getparamsÚ	nchannelsÚ	sampwidthÚ	framerateÚnframesÚcomptypeÚcompnameÚrangeÚpickleZHIGHEST_PROTOCOLÚdumpsÚloads)r	   r   r    r!   r"   r#   r$   r%   ÚparamsÚprotoÚdumpr
   r
   r   Úcheck_params   s$    zAudioTests.check_paramsN)r   r   r   Úclose_fdr   r   r-   r
   r
   r
   r   r      s   r   c               @   s   e Zd Zdd„ ZdS )ÚAudioMiscTestsc          
   C   s`   d}d}t  | jj› d¡:}|  t¡$ | jj||d |j||d W d Q R X W d Q R X d S )NÚargÚmodez.open)r1   )r   ZpatchÚmoduler   ZassertWarnsÚDeprecationWarningZopenfpZassert_called_with)r	   r0   r1   Z	mock_openr
   r
   r   Útest_openfp_deprecated7   s    z%AudioMiscTests.test_openfp_deprecatedN)r   r   r   r4   r
   r
   r
   r   r/   5   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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 )$ÚAudioWriteTestsc             C   sL   | j  |d¡ }| _| | j¡ | | j¡ | | j¡ | 	| j
| j¡ |S )NÚwb)r2   Úopenr   Úsetnchannelsr    Úsetsampwidthr!   Úsetframerater"   Úsetcomptyper$   r%   )r	   Útestfiler   r
   r
   r   Úcreate_fileB   s    zAudioWriteTests.create_filec          	   C   sv   | j  |d¡^}|  | ¡ | j¡ |  | ¡ | j¡ |  | ¡ | j¡ |  | 	¡ |¡ |  | 
|¡|¡ W d Q R X d S )NÚrb)r2   r7   r   r   r    r   r!   r   r"   r   Ú
readframes)r	   r<   r#   Úframesr   r
   r
   r   Ú
check_fileJ   s    zAudioWriteTests.check_filec          	   C   sP   |   t¡}| | j¡ | | j¡ |  || j| j| j	| j| j
| j¡ | ¡  d S )N)r=   r   Ú
setnframesr#   Úwriteframesr@   r-   r    r!   r"   r$   r%   r   )r	   r   r
   r
   r   Útest_write_paramsR   s    
z!AudioWriteTests.test_write_paramsc             C   s€   |   | jj¡  | j td¡ W d Q R X W d Q R X |   | jj¡4 ttdƒ}| j |¡ W d Q R X W d Q R X W d Q R X d S )Nr6   )ÚassertRaisesr2   ÚErrorr7   r   )r	   r<   r
   r
   r   Ú&test_write_context_manager_calls_closeZ   s    z6AudioWriteTests.test_write_context_manager_calls_closec          
   C   s  t tdƒb}| j  |¡:}| | j¡ | | j¡ | | j¡ | 	| j
| j¡ W d Q R X |  |j| j¡ W d Q R X t tdƒŠ}| j  |¡N}|  | ¡ j¡ | ¡ }|  |j| j¡ |  |j| j¡ |  |j| j¡ W d Q R X | jsò|  | ¡ ¡ |  |j| j¡ W d Q R X d S )Nr6   r>   )r7   r   r2   r8   r    r9   r!   r:   r"   r;   r$   r%   r   Úclosedr.   ÚassertFalseÚgetfpr   ÚassertIsNone)r	   r<   r   r*   r
   r
   r   Ú#test_context_manager_with_open_filee   s"    z3AudioWriteTests.test_context_manager_with_open_filec          	   C   sÆ   | j  td¡:}| | j¡ | | j¡ | | j¡ | 	| j
| j¡ W d Q R X | j  t¡N}|  | ¡ j¡ | ¡ }|  |j| j¡ |  |j| j¡ |  |j| j¡ W d Q R X | jsÂ|  | ¡ ¡ d S )Nr6   )r2   r7   r   r8   r    r9   r!   r:   r"   r;   r$   r%   rI   rJ   rH   r   r   r.   rK   )r	   r   r*   r
   r
   r   Ú"test_context_manager_with_filenamex   s    z2AudioWriteTests.test_context_manager_with_filenamec             C   s@   |   t¡}| | j¡ | | j¡ | ¡  |  t| j| j¡ d S )N)r=   r   rB   r#   rC   r@   r   rA   )r	   r   r
   r
   r   Ú
test_write‰   s
    
zAudioWriteTests.test_writec             C   sD   |   t¡}| | j¡ | t| jƒ¡ | ¡  |  t| j| j¡ d S )N)	r=   r   rB   r#   rC   Ú	bytearrayr@   r   rA   )r	   r   r
   r
   r   Útest_write_bytearray‘   s
    
z$AudioWriteTests.test_write_bytearrayc             C   sH   |   t¡}| | j¡ | t d| j¡¡ | ¡  |  t| j| j¡ d S )NÚh)	r=   r   rB   r#   rC   Úarrayr@   r   rA   )r	   r   r
   r
   r   Útest_write_array™   s
    
z AudioWriteTests.test_write_arrayc             C   sD   |   t¡}| | j¡ | t| jƒ¡ | ¡  |  t| j| j¡ d S )N)	r=   r   rB   r#   rC   Ú
memoryviewr@   r   rA   )r	   r   r
   r
   r   Útest_write_memoryview¡   s
    
z%AudioWriteTests.test_write_memoryviewc          	   C   sŒ   t tdƒ>}| d¡ |  |¡}| | jd ¡ | | j¡ | ¡  W d Q R X t tdƒ*}|  	| 
d¡d¡ |  || j| j¡ W d Q R X d S )Nr6   s   ababagalamagar   r>   é   )r7   r   Úwriter=   rB   r#   rC   r@   r   r   ÚreadrA   )r	   r<   r   r
   r
   r   Útest_incompleted_write©   s    

z&AudioWriteTests.test_incompleted_writec          	   C   s´   t tdƒf}| d¡ |  |¡}| | j¡ | j| j }| | j	d | … ¡ | | j	| d … ¡ | 
¡  W d Q R X t tdƒ*}|  | d¡d¡ |  || j| j	¡ W d Q R X d S )Nr6   s   ababagalamagar>   rV   )r7   r   rW   r=   rB   r#   r    r!   rC   r@   r   r   rX   rA   )r	   r<   r   Ú	framesizer
   r
   r   Útest_multiple_writesµ   s    

z$AudioWriteTests.test_multiple_writesc          	   C   sŒ   t tdƒ>}| d¡ |  |¡}| | jd ¡ | | j¡ | ¡  W d Q R X t tdƒ*}|  	| 
d¡d¡ |  || j| j¡ W d Q R X d S )Nr6   s   ababagalamagar   r>   rV   )r7   r   rW   r=   rB   r#   rC   r@   r   r   rX   rA   )r	   r<   r   r
   r
   r   Útest_overflowed_writeÃ   s    

z%AudioWriteTests.test_overflowed_writec          	   C   sZ   |   t¡}| | j¡ | | j¡ W d Q R X ttdƒ}|  || j| j¡ W d Q R X d S )Nr>   )r=   r   rB   r#   rC   r@   r   rA   )r	   r   r<   r
   r
   r   Útest_unseekable_readÏ   s
    z$AudioWriteTests.test_unseekable_readc          
   C   sZ   t tdƒ4}|  |¡}| | j¡ | | j¡ W d Q R X W d Q R X |  t| j| j¡ d S )Nr6   )r   r   r=   rB   r#   rC   r@   rA   )r	   r<   r   r
   r
   r   Útest_unseekable_write×   s
     z%AudioWriteTests.test_unseekable_writec          	   C   sÄ   t tdƒr}| d¡ |  |¡}| | jd ¡ y| | j¡ W n tk
rT   Y nX y| 	¡  W n tk
rv   Y nX W d Q R X t
tdƒ.}|  | d¡d¡ |  || jd | j¡ W d Q R X d S )Nr6   s   ababagalamagar   r>   rV   )r   r   rW   r=   rB   r#   rC   r@   ÚOSErrorr   r7   r   rX   rA   )r	   r<   r   r
   r
   r   Ú!test_unseekable_incompleted_writeß   s    

z1AudioWriteTests.test_unseekable_incompleted_writec          	   C   sÚ   t tdƒr}| d¡ |  |¡}| | jd ¡ y| | j¡ W n tk
rT   Y nX y| 	¡  W n tk
rv   Y nX W d Q R X t
tdƒD}|  | d¡d¡ | j| j }|  || jd | jd | … ¡ W d Q R X d S )Nr6   s   ababagalamagar   r>   rV   )r   r   rW   r=   rB   r#   rC   r@   r_   r   r7   r   rX   r    r!   rA   )r	   r<   r   rZ   r
   r
   r   Ú test_unseekable_overflowed_writeñ   s     

z0AudioWriteTests.test_unseekable_overflowed_writeN)r   r   r   r=   rA   rD   rG   rL   rM   rN   rP   rS   rU   rY   r[   r\   r]   r^   r`   ra   r
   r
   r
   r   r5   @   s"   r5   c               @   s@   e Zd Zedd„ ƒZdd„ Zdd„ Zdd„ Zd	d
„ Zdd„ Z	dS )ÚAudioTestsWithSourceFilec             C   s   t | jdd| _d S )NZ	audiodata)Zsubdir)r   ZsndfilenameÚsndfilepath)Úclsr
   r
   r   Ú
setUpClass  s    z#AudioTestsWithSourceFile.setUpClassc          	   C   s:   | j  | j¡ }| _|  || j| j| j| j| j	| j
¡ d S )N)r2   r7   rc   r   r-   r    r!   r"   Úsndfilenframesr$   r%   )r	   r   r
   r
   r   Útest_read_params  s    z)AudioTestsWithSourceFile.test_read_paramsc          
   C   sÂ   t | jdƒ<}| j  |¡ }| _|  |j¡ | ¡  |  |j| j¡ W d Q R X t t	dƒ`}| j  |d¡ }| _
|  |j¡ |  | jj¡ | ¡  W d Q R X |  |j| j¡ | ¡  W d Q R X d S )Nr>   r6   )r7   rc   r2   r   rI   rH   r   r   r.   r   r   rE   rF   )r	   r<   r   r   r
   r
   r   Ú
test_close  s    z#AudioTestsWithSourceFile.test_closec          	   C   s\  | j | j }| jd d| … }| jd| d| … }| j | j¡ }| _|  | d¡d¡ |  | 	¡ d¡ |  | d¡|¡ | 
¡  | 	¡ }|  |d¡ |  | d¡|¡ | 	¡ }|  |d¡ |  | d¡|¡ | |¡ |  | d¡|¡ | |¡ |  | d¡|¡ |  | jj¡ | d¡ W d Q R X |  | jj¡ | | ¡ d ¡ W d Q R X d S )Né   é   r   ó    éÿÿÿÿr   )r    r!   r@   r2   r7   rc   r   r   r?   r   ÚrewindZsetposrE   rF   r   )r	   rZ   Zchunk1Zchunk2r   Zpos0Zpos2r
   r
   r   Ú	test_read  s,    

z"AudioTestsWithSourceFile.test_readc             C   sÌ   | j  | j¡ }| _| j  td¡ }| _| | ¡ ¡ d}| ¡ }x,|dkrn|d7 }| 	| 
|¡¡ ||8 }qDW | ¡  | j  td¡ }| _| ¡  |  | ¡ | ¡ ¡ |  | 
| ¡ ¡| 
| ¡ ¡¡ d S )Nr6   r   r   r>   )r2   r7   rc   r   r   r   Z	setparamsr   r   rC   r?   r   rm   r   )r	   r   r   ÚiÚnr
   r
   r   Ú	test_copy7  s    
z"AudioTestsWithSourceFile.test_copyc          
   C   sê   t tdƒ6}| d¡ t | jdƒ}| | ¡ ¡ W d Q R X W d Q R X t tdƒ}|  | d¡d¡ | j  |d¡d}|  | ¡ | j¡ |  | 	¡ | j
¡ |  | ¡ | j¡ |  | ¡ | j¡ |  | | j¡| j¡ W d Q R X W d Q R X d S )Nr6   s   ababagalamagar>   rV   )r7   r   rW   rc   rX   r   r2   r   r    r   r!   r   r"   r   rf   r?   r#   r@   )r	   r<   r   r
   r
   r   Útest_read_not_from_startH  s    
"z1AudioTestsWithSourceFile.test_read_not_from_startN)
r   r   r   Úclassmethodre   rg   rh   rn   rq   rr   r
   r
   r
   r   rb     s   rb   )Ztest.supportr   r   r   rR   r   Zunittestr   r'   ÚFileIOr   r   r/   r5   rb   r
   r
   r
   r   Ú<module>   s   % F