B
    u9a                 @   s   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 dd Z	G dd deZ
G d	d
 d
e jZd dlmZ G dd dejZG dd deZG dd deZG dd deZG dd deZG dd deZG dd deZdd Zedkre  dS )    N)support)MutableMapping)dbm_iteratorc             C   s
   |  dS )Nzlatin-1)decode)s r   !/usr/lib/python3.7/test_shelve.pyL1   s    r	   c               @   sT   e Zd ZdZdd Zdd Zdd Zdd	 Zd
d Zdd Z	e	Z
dd Zdd ZdS )byteskeydictz Mapping that supports bytes keysc             C   s
   i | _ d S )N)d)selfr   r   r   __init__   s    zbyteskeydict.__init__c             C   s   | j t| S )N)r   r	   )r   keyr   r   r   __getitem__   s    zbyteskeydict.__getitem__c             C   s   || j t|< d S )N)r   r	   )r   r   valuer   r   r   __setitem__   s    zbyteskeydict.__setitem__c             C   s   | j t|= d S )N)r   r	   )r   r   r   r   r   __delitem__   s    zbyteskeydict.__delitem__c             C   s
   t | jS )N)lenr   )r   r   r   r   __len__   s    zbyteskeydict.__len__c             c   s$   x| j  D ]}|dV  qW d S )Nzlatin-1)r   keysencode)r   kr   r   r   iterkeys   s    zbyteskeydict.iterkeysc             C   s   t |  S )N)listr   )r   r   r   r   r   #   s    zbyteskeydict.keysc             C   s
   t | jS )N)r
   r   )r   r   r   r   copy&   s    zbyteskeydict.copyN)__name__
__module____qualname____doc__r   r   r   r   r   r   __iter__r   r   r   r   r   r   r
      s   r
   c               @   sh   e Zd 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S )TestCasezshelftemp.dbc             C   s(   x"t  | jd D ]}t| qW d S )N*)globfnr   unlink)r   fr   r   r   tearDown.   s    zTestCase.tearDownc             C   s   i }t j|ddd}ddddg|d< | |d ddddg | t|d |  | tt| y|d  W n tk
r   Y nX | d d S )	N   F)protocol	writeback         key1z"Closed shelf should not find a key)shelveShelfassertEqualr   closeassertRaises
ValueErrorfail)r   d1r   r   r   r   
test_close2   s    zTestCase.test_closec             C   s<   t j| jdd}zd|d< | |d d W d |  X d S )Nr   )r(   )r*   r'   r+   r,   r-   )r.   openr#   r0   r1   )r   r   r   r   r   test_ascii_file_shelfA   s
    zTestCase.test_ascii_file_shelfc             C   s<   t j| jdd}zd|d< | |d d W d |  X d S )Nr*   )r(   )r*   r'   r+   r,   r-   )r.   r7   r#   r0   r1   )r   r   r   r   r   test_binary_file_shelfI   s
    zTestCase.test_binary_file_shelfc             C   s<   t j| jdd}zd|d< | |d d W d |  X d S )Nr'   )r(   )r*   r'   r+   r,   r-   )r.   r7   r#   r0   r1   )r   r   r   r   r   test_proto2_file_shelfQ   s
    zTestCase.test_proto2_file_shelfc             C   s   t  }tj|dd}d|d< | |d d |  t  }tj|dd}d|d< | |d d |  | t|d | t|d | | |  d S )Nr   )r(   )r*   r'   r+   r,   r-   r*   )r
   r.   r/   r0   r1   r   assertNotEqualitems)r   r5   r   d2r   r   r   test_in_memory_shelfY   s    zTestCase.test_in_memory_shelfc             C   s   t  }tj|ddd}ddddg|d< | |d ddddg |d d | |d ddddg |  t  }tj|dd	d}ddddg|d< | |d ddddg |d d | |d dddddg |  | t|d | t|d d S )
Nr'   F)r(   r)   r*   r+   r,   r-      T)r
   r.   r/   r0   appendr1   r   )r   r5   r   r=   r   r   r   test_mutable_entryi   s     zTestCase.test_mutable_entryc             C   sv   i }d}dgt ||< | |d| dgt j|dd|< | |d| t j|dd}| t|j|dg d S )Nu   PÃ¶pr*   zutf-8zlatin-1)Zkeyencodingascii)r.   r/   ZassertInr   r2   UnicodeEncodeErrorr   )r   r   r   r   r   r   r   test_keyencoding}   s    zTestCase.test_keyencodingc             C   s`   i }d}| d}tj|dd}dg||< || }|d d |  || }| || d S )Nr   zutf-8T)r)   r*   r'   )r   r.   r/   r@   r1   r;   )r   r   r   
encodedkeyr   Zp1Zp2r   r   r   &test_writeback_also_writes_immediately   s    

z/TestCase.test_writeback_also_writes_immediatelyc          	   C   s   i }t j|ddd>}ddddg|d< | |d ddddg | t|d W d Q R X | tt| y|d  W n tk
r   Y nX | d d S )	Nr'   F)r(   r)   r*   r+   r,   r-   z"Closed shelf should not find a key)r.   r/   r0   r   r2   r3   r4   )r   r5   r   r   r   r   	test_with   s    zTestCase.test_withc          	   C   s(   t i }| |jd W d Q R X d S )Nr+   )r.   r/   r0   Z	_protocol)r   r   r   r   r   test_default_protocol   s    zTestCase.test_default_protocolN)r   r   r   r#   r&   r6   r8   r9   r:   r>   rA   rD   rF   rG   rH   r   r   r   r   r    *   s   r    )mapping_testsc               @   s:   e Zd ZdZdZdd ZejZdd Z	dd Z
d	d
 ZdS )TestShelveBasezshelftemp.dbr   c             O   s   g | _ tjj| f|| d S )N)_dbrI   BasicTestMappingProtocolr   )r   argskwr   r   r   r      s    zTestShelveBase.__init__c             C   s   ddddS )NZvalue1r'   )r*   r'   r+   )r-   Zkey2Zkey3r   )r   r   r   r   
_reference   s    zTestShelveBase._referencec             C   sT   | j rtjt f| j}n*|  jd7  _tj| jt| j f| j}| j	
| |S )Nr*   )_in_memr.   r/   r
   _argscounterr7   r#   strrK   r@   )r   xr   r   r   _empty_mapping   s    zTestShelveBase._empty_mappingc             C   sL   x| j D ]}|  qW g | _ | jsHx"t| jd D ]}t| q6W d S )Nr!   )rK   r1   rP   r"   r#   r   r$   )r   Zdbr%   r   r   r   r&      s    zTestShelveBase.tearDownN)r   r   r   r#   rR   r   r.   r/   Z	type2testrO   rU   r&   r   r   r   r   rJ      s   rJ   c               @   s   e Zd ZddiZdZdS )TestAsciiFileShelver(   r   FN)r   r   r   rQ   rP   r   r   r   r   rV      s   rV   c               @   s   e Zd ZddiZdZdS )TestBinaryFileShelver(   r*   FN)r   r   r   rQ   rP   r   r   r   r   rW      s   rW   c               @   s   e Zd ZddiZdZdS )TestProto2FileShelver(   r'   FN)r   r   r   rQ   rP   r   r   r   r   rX      s   rX   c               @   s   e Zd ZddiZdZdS )TestAsciiMemShelver(   r   TN)r   r   r   rQ   rP   r   r   r   r   rY      s   rY   c               @   s   e Zd ZddiZdZdS )TestBinaryMemShelver(   r*   TN)r   r   r   rQ   rP   r   r   r   r   rZ      s   rZ   c               @   s   e Zd ZddiZdZdS )TestProto2MemShelver(   r'   TN)r   r   r   rQ   rP   r   r   r   r   r[      s   r[   c           
   C   s*   x$t  D ]} tttttttt	 qW d S )N)
r   r   Zrun_unittestrV   rW   rX   rY   rZ   r[   r    )moduler   r   r   	test_main   s    r]   __main__)Zunittestr.   r"   Ztestr   Zcollections.abcr   Ztest.test_dbmr   r	   r
   r    rI   rL   rJ   rV   rW   rX   rY   rZ   r[   r]   r   r   r   r   r   <module>   s&   