B
    u9a+  ã               @   s€   d dl Z d dlZd dlZd dlZd dlZd dlmZ d dlm	Z	 d dl
mZ e	jZdd„ ZG dd„ dejƒZedkr|e ¡  dS )	é    N)Úsupport)Úpartialc           	   C   s:   x4dD ],} yt  t|  ¡ W q tk
r0   Y qX qW d S )N)z.dirz.datz.bak)ÚosÚunlinkÚ_fnameÚOSError)Zext© r   ú#/usr/lib/python3.7/test_dbm_dumb.pyÚ_delete_files   s
    
r
   c               @   sJ  e Zd Zddddddddd	d
ddddd d¡diZdd„ Ze ee	dƒd¡e ee	dƒd¡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d0d1„ Zd2d3„ Zd4d5„ Zd6d7„ Zd8d9„ Zd:d;„ Zd<d=„ Zd>d?„ Zd@dA„ Ze ee	dƒd¡dBdC„ ƒZe e j!dD¡dEdF„ ƒZ"dGdH„ Z#dIdJ„ Z$dKS )LÚDumbDBMTestCaseó   0ó    ó   as   Python:ó   bs   Programmingó   cs   theó   ds   wayó   fs   Guidoó   gs   intendedõ   Ã¼zutf-8ó   !c             C   sT   t  td¡}|  t| ¡ ƒg ¡ x| jD ]}| j| ||< q(W |  |¡ | ¡  d S )NÚc)	ÚdumbdbmÚopenr   ÚassertEqualÚlistÚkeysÚ_dictÚread_helperÚclose)ÚselfÚfÚkeyr   r   r	   Útest_dumbdbm_creation"   s    
z%DumbDBMTestCase.test_dumbdbm_creationÚumaskztest needs os.umask()Úchmodztest needs os.chmod()c          	   C   s”   z$t  d¡}t tdd¡}| ¡  W d t  |¡ X d}t jdkrDd}dd l}t  td ¡}|  | 	|j
¡|¡ t  td	 ¡}|  | 	|j
¡|¡ d S )
Né   r   iŸ  i  Úposixi¶  r   z.datz.dir)r   r#   r   r   r   r   ÚnameÚstatr   ÚS_IMODEÚst_mode)r   Z	old_umaskr    Zexpected_moder(   Ústr   r   r	   Útest_dumbdbm_creation_mode*   s    

z*DumbDBMTestCase.test_dumbdbm_creation_modec             C   s6   t  t¡}d|d< |  |d d¡ | ¡  | ¡  d S )Nr   r   )r   r   r   r   r   )r   r    r   r   r	   Útest_close_twice@   s
    
z DumbDBMTestCase.test_close_twicec             C   s`   |   ¡  t td¡}d | jd< |d< |  |¡ |  | dd¡d¡ |  |d d¡ | ¡  d S )NÚws   indentedr   s   xxxs   foo)	Úinit_dbr   r   r   r   r   r   Ú
setdefaultr   )r   r    r   r   r	   Útest_dumbdbm_modificationG   s    
z)DumbDBMTestCase.test_dumbdbm_modificationc          	   C   sÂ   |   ¡  t td¡}|  |¡ |  td¡ d|d< W d Q R X |  td¡ |d= W d Q R X |  | d¡| j	d ¡ |  | dd¡d¡ |  
| d¡¡ |  t¡ |d  W d Q R X | ¡  d S )	NÚrz'The database is opened for reading onlyó   xr   r   r   s   xxxs   foo)r/   r   r   r   r   ÚassertWarnsRegexÚDeprecationWarningr   Úgetr   ZassertIsNoneÚassertRaisesÚKeyErrorr   )r   r    r   r   r	   Útest_dumbdbm_readQ   s    
z!DumbDBMTestCase.test_dumbdbm_readc             C   s(   |   ¡  t t¡}|  |¡}| ¡  d S )N)r/   r   r   r   Úkeys_helperr   )r   r    r   r   r   r	   Útest_dumbdbm_keysc   s    

z!DumbDBMTestCase.test_dumbdbm_keysc             C   s*   t  t¡}d|d< |  d|¡ | ¡  d S )Ns   helloó   1)r   r   r   ÚassertInr   )r   r    r   r   r	   Útest_write_containsi   s    
z#DumbDBMTestCase.test_write_containsc             C   sH   t  t¡}d|d< d|d< | ¡  t  t¡}|  |d d¡ | ¡  d S )Ns   hellor<   s   hello2)r   r   r   r   r   )r   r    r   r   r	   Útest_write_write_reado   s    

z%DumbDBMTestCase.test_write_write_readc             C   s4   |   ¡  t td¡}|  |d | jd d¡ ¡ d S )Nr2   r   zutf-8)r/   r   r   r   r   r   Úencode)r   r    r   r   r	   Útest_str_ready   s    zDumbDBMTestCase.test_str_readc             C   sx   |   ¡  t t¡}d|d< d|d< | ¡  t td¡}|  d|¡ |  |d d¡ | jd d¡ ¡ |  |d d¡ d S )	Nr   r   ÚaÚ1r2   zutf-8r<   r   )	r/   r   r   r   r   r=   r   r@   r   )r   r    r   r   r	   Útest_str_write_contains~   s    
z'DumbDBMTestCase.test_str_write_containsc          	   C   s¾   t  t¡}d|d< d|d< | ¡  t td d¡}| ¡ }W d Q R X tjdkr^| dd	¡}n| d	d¡}t td d
¡}| 	|¡ W d Q R X t  t¡}|  
|d d¡ |  
|d d¡ d S )Ns   hellor<   s   hello2ó   2z.dirÚrbÚ
ó   
s   
Úwb)r   r   r   r   ÚioÚreadr   ÚlinesepÚreplaceÚwriter   )r   r    ÚfileÚdatar   r   r	   Útest_line_endingsŠ   s    


z!DumbDBMTestCase.test_line_endingsc             C   s4   |   |¡}x$| jD ]}|  | j| || ¡ qW d S )N)r:   r   r   )r   r    r   r!   r   r   r	   r   ¡   s    
zDumbDBMTestCase.read_helperc             C   s6   t  td¡}x| jD ]}| j| ||< qW | ¡  d S )NÚn)r   r   r   r   r   )r   r    Úkr   r   r	   r/   ¦   s    zDumbDBMTestCase.init_dbc             C   s*   t | ¡ ƒ}t | j ¡ ƒ}|  ||¡ |S )N)Úsortedr   r   r   )r   r    r   Zdkeysr   r   r	   r:   ¬   s    zDumbDBMTestCase.keys_helperc       	      C   sæ   dd l }i }xÔtdƒD ]È}t t¡}xptdƒD ]d}| d¡}|  ¡ dk r^||kr’||= ||= q.| d¡| d¡ }|||< |||< |  || |¡ q.W | ¡  t t¡}t	dd	„ | 
¡ D ƒƒ}t	| 
¡ ƒ}|  ||¡ | ¡  qW d S )
Nr   é   éd   Zabcdefghijklmgš™™™™™É?)r   r   r   i'  c             s   s    | ]\}}|  d ¡|fV  qdS )zlatin-1N)r@   )Ú.0rS   Úvr   r   r	   ú	<genexpr>Ç   s    z.DumbDBMTestCase.test_random.<locals>.<genexpr>)ÚrandomÚranger   r   r   ÚchoiceZ	randranger   r   rT   Úitems)	r   rZ   ÚdZdummyr    rS   rX   ZexpectedZgotr   r   r	   Útest_random´   s(    


zDumbDBMTestCase.test_randomc          	   C   sr   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 d S )Nr   zcontext managerzdumbdbm context managerr2   s   dumbdbm context manager)r   r   r   r   r   r   r7   Úerror)r   Údbr   r   r	   Útest_context_managerÌ   s     z$DumbDBMTestCase.test_context_managerc          
   C   sÜ   t  td¡}| ¡  xhttj|ƒttj|dƒttj|ƒttj	|ƒfD ]6}|  
t j¡}|dƒ W d Q R X |  t|jƒd¡ qDW xXt d¡t d¡t d¡tfD ]6}|  
t j¡}||ƒ W d Q R X |  t|jƒd¡ qžW d S )Nr   ÚbÚtestz"DBM object has already been closedr   Ziterkeysr]   )r   r   r   r   r   ÚoperatorÚdelitemÚsetitemÚgetitemÚcontainsr7   r`   r   ÚstrZ	exceptionÚmethodcallerÚlen)r   r    ZmethÚcmr   r   r	   Útest_check_closedÖ   s$    



z!DumbDBMTestCase.test_check_closedc          	   C   sb   t  td¡$}x| jD ]}| j| ||< qW W d Q R X t  td¡}|  | ¡ g ¡ W d Q R X d S )NrR   )r   r   r   r   r   r   )r   r    rS   r   r   r	   Útest_create_newì   s
    zDumbDBMTestCase.test_create_newc             C   sx   t td dƒ}| d¡ W d Q R X t ¡ B}|  t¡ t  t¡}W d Q R X W d Q R X |  | 	¡ d¡ W d Q R X d S )Nz.dirr.   zstr(print('Hacked!')), 0
Ú )
r   r   rN   r   Zcaptured_stdoutr7   Ú
ValueErrorr   r   Úgetvalue)r   ÚstreamÚstdoutr    r   r   r	   Ú	test_evalô   s    
zDumbDBMTestCase.test_evalc          
   C   sD   x>dD ]6}t ƒ  |  td¡ t t|¡}W d Q R X | ¡  qW d S )N)r2   r.   zIThe database file is missing, the semantics of the 'c' flag will be used.)r
   r4   r5   r   r   r   r   )r   Úvaluer    r   r   r	   Útest_warn_on_ignored_flagsý   s    
z*DumbDBMTestCase.test_warn_on_ignored_flagsc          
   C   s–   t  td¡}W d Q R X t td ¡ xjdD ]b}|  td¡ t  t|¡}W d Q R X | ¡  |  tj	 
td ¡|dk¡ |  tj	 
td ¡¡ q,W d S )NrR   z.dir)r2   r.   zFThe index file is missing, the semantics of the 'c' flag will be used.r.   z.bak)r   r   r   r   r   r4   r5   r   r   ÚpathÚexistsZassertFalse)r   r    rv   r   r   r	   Útest_missing_index  s    

z"DumbDBMTestCase.test_missing_indexc          
   C   s>   x8dD ]0}|   td¡ t t|¡}W d Q R X | ¡  qW d S )N)ÚxZrfNz)Flag must be one of 'r', 'w', 'c', or 'n')r4   r5   r   r   r   r   )r   Úflagr    r   r   r	   Útest_invalid_flag  s
    
z!DumbDBMTestCase.test_invalid_flagc          
   C   sâ   t  ¡ Ð}tj |d¡}t |d¡8}|  t| 	¡ ƒg ¡ x| j
D ]}| j
| ||< qBW W d Q R X t |d tj¡ t |d tj¡ t |tjtjB ¡ t |d¡(}|  t| 	¡ ƒt| j
ƒ¡ | ¡  W d Q R X W d Q R X d S )Nra   rR   z.dirz.datr2   )r   Ztemp_dirr   rx   Újoinr   r   r   r   r   r   r$   r(   ÚS_IRUSRÚS_IXUSRrT   r   )r   ÚdirZfnamer    r!   r   r   r	   Útest_readonly_files  s    
z#DumbDBMTestCase.test_readonly_filesz*requires OS support of non-ASCII encodingsc          	   C   sÂ   t j}xdD ]}|  t j|| ¡ qW t |d¡}d|d< W d Q R X |  tj 	|d ¡¡ |  tj 	|d ¡¡ t |d¡:}|  
t| ¡ ƒdg¡ |  d|k¡ |  
|d d¡ W d Q R X d S )N)z.dirz.datz.bakr   s   values   keyz.datz.dirr2   )r   ÚTESTFN_NONASCIIZ
addCleanupr   r   r   Z
assertTruer   rx   ry   r   r   r   )r   ÚfilenameÚsuffixra   r   r   r	   Útest_nonascii_filename,  s    
z&DumbDBMTestCase.test_nonascii_filenamec             C   s
   t ƒ  d S )N)r
   )r   r   r   r	   ÚtearDown;  s    zDumbDBMTestCase.tearDownc             C   s
   t ƒ  d S )N)r
   )r   r   r   r	   ÚsetUp>  s    zDumbDBMTestCase.setUpN)%Ú__name__Ú
__module__Ú__qualname__r@   r   r"   ÚunittestZ
skipUnlessÚhasattrr   r,   r-   r1   r9   r;   r>   r?   rA   rD   rQ   r   r/   r:   r_   rb   rn   ro   ru   rw   rz   r}   r‚   r   rƒ   r†   r‡   rˆ   r   r   r   r	   r      sH   


	
r   Ú__main__)rJ   re   r   r(   rŒ   Zdbm.dumbZdumbr   rd   r   Ú	functoolsr   ZTESTFNr   r
   ZTestCaser   r‰   Úmainr   r   r   r	   Ú<module>   s     -