B
    u9aà  ã               @   sd   d dl mZ e d¡Zd dlZd dlZd dlmZmZm	Z	 eZ
G dd„ dejƒZedkr`e ¡  dS )é    )Úsupportzdbm.gnuN)ÚTESTFNÚTESTFN_NONASCIIÚunlinkc               @   sh   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e ed¡dd„ ƒZdS )ÚTestGdbmc             C   s
   d | _ d S )N)Úg)Úself© r	   ú"/usr/lib/python3.7/test_dbm_gnu.pyÚsetUp   s    zTestGdbm.setUpc             C   s    | j d k	r| j  ¡  ttƒ d S )N)r   Úcloser   Úfilename)r   r	   r	   r
   ÚtearDown   s    

zTestGdbm.tearDownc          	   C   sN  t  td¡| _|  | j ¡ g ¡ d| jd< d| jd< d| jd< t| j ¡ ƒ}|  |tddd	gƒ¡ |  d| j¡ |  d| j¡ |  | jd d¡ | j ¡ }x*|rÄ|  ||¡ | 	|¡ | j 
|¡}qœW |  | j d¡d
¡ |  | j d¡¡ |  | j dd¡d¡ |  t¡ | jd  W d Q R X |  | j dd¡d¡ |  | jd d¡ d S )NÚcÚbÚaZ019237410982340912840198242Z12345678910s   datas   bytesó   as   12345678910ó   bs   xxxs   fooZxxx)ÚgdbmÚopenr   r   ÚassertEqualÚkeysÚsetZassertInZfirstkeyÚremoveZnextkeyÚgetZassertIsNoneÚassertRaisesÚKeyErrorÚ
setdefault)r   Zkey_setÚkeyr	   r	   r
   Útest_key_methods   s,    




zTestGdbm.test_key_methodsc                s`   t tƒ ˆ  tjtjtd¡ t td¡ˆ _ˆ j ¡  ˆ  tj‡ fdd„¡ ˆ  tjdd„ ¡ d S )NÚrr   c                  s
   ˆ j d S )Nr   )r   r	   )r   r	   r
   Ú<lambda>3   ó    z0TestGdbm.test_error_conditions.<locals>.<lambda>c               S   s   t  td¡ ¡ S )NÚrx)r   r   r   r   r	   r	   r	   r
   r!   5   r"   )r   r   r   r   Úerrorr   r   r   )r   r	   )r   r
   Útest_error_conditions,   s    
zTestGdbm.test_error_conditionsc             C   sˆ   t tjƒ}|t dƒ }x(t|ƒD ]}t t|¡| _| j ¡  q W |t dƒ }x6|D ].}x(|D ] }t t|| ¡| _| j ¡  q\W qRW d S )NZfsuZcrwn)r   r   Z
open_flagsÚsortedr   r   r   r   )r   ÚallZmodesÚmodeÚflagsÚflagr	   r	   r
   Ú
test_flags7   s    


zTestGdbm.test_flagsc             C   s˜   t  td¡| _tj t¡}t|dƒ}d| | jd< tj t¡}|  ||¡ | jd= |  	tj t¡|¡ | j 
¡  tj t¡}|  ||¡ |  ||¡ d S )Nr   i'  Úx)r   r   r   r   ÚosÚpathÚgetsizeÚmaxZassertGreaterr   Z
reorganizeZ
assertLessZassertGreaterEqual)r   Zsize0Z
value_sizeZsize1Zsize2r	   r	   r
   Útest_reorganizeG   s    

zTestGdbm.test_reorganizec          	   C   s„   t  td¡}d|d< W d Q R X t  td¡}|  t| ¡ ƒdg¡ W d Q R X |  t j¡}| ¡  W d Q R X |  t|j	ƒd¡ d S )Nr   zcontext managerzgdbm context managerr    s   gdbm context managerz#GDBM object has already been closed)
r   r   r   r   Úlistr   r   r$   ÚstrZ	exception)r   ÚdbÚcmr	   r	   r
   Útest_context_manager\   s     zTestGdbm.test_context_managerc          	   C   sp   t  td¡}d|d< W d Q R X t  td¡:}|  t| ¡ ƒdg¡ |  d|k¡ |  |d d¡ W d Q R X d S )Nr   s   bytes value ½s   bytes key ½r    )r   r   r   r   r2   r   Ú
assertTrue)r   r4   r	   r	   r
   Ú
test_bytesh   s    zTestGdbm.test_bytesc          	   C   s¢   t  td¡}d|d< W d Q R X t  td¡l}|  t| ¡ ƒd ¡ g¡ |  d ¡ |k¡ |  d|k¡ |  |d ¡  d ¡ ¡ |  |d d ¡ ¡ W d Q R X d S )Nr   u   Unicode value ðŸu   Unicode key ðŸr    )r   r   r   r   r2   r   Úencoder7   )r   r4   r	   r	   r
   Útest_unicodep   s    

zTestGdbm.test_unicodez*requires OS support of non-ASCII encodingsc          	   C   s’   t }|  t|¡ t |d¡}d|d< W d Q R X |  tj |¡¡ t |d¡:}|  	t
| ¡ ƒdg¡ |  d|k¡ |  	|d d¡ W d Q R X d S )Nr   s   values   keyr    )r   Z
addCleanupr   r   r   r7   r-   r.   Úexistsr   r2   r   )r   r   r4   r	   r	   r
   Útest_nonascii_filename|   s    zTestGdbm.test_nonascii_filenameN)Ú__name__Ú
__module__Ú__qualname__r   r   r   r%   r+   r1   r6   r8   r:   ÚunittestZ
skipUnlessr   r<   r	   r	   r	   r
   r   
   s   r   Ú__main__)Ztestr   Úimport_moduler   r@   r-   Ztest.supportr   r   r   r   ZTestCaser   r=   Úmainr	   r	   r	   r
   Ú<module>   s   
 