o
    >eC                     @   s   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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lmZ d dlmZ d d	lmZmZ d d
l	mZmZmZmZ dZdZdZdZG dd dej Z!G dd dej Z"dS )    N)mkdtemp)mock)Image)DataOverflowError)	BaseImage)
PyPNGImage)StyledPilImage)
colormasksmoduledrawers)MODE_8BIT_BYTEMODE_ALPHA_NUMMODE_NUMBERQRDatau   αβγ)   r   r   )r   r   r   )r   r   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ee ddd Zee dd d! Zee dd"d# Zee dd$d% Zee dd&d' Zd(d) Zd*d+ Zd,d- Zd.d/ Zd0d1 Zd2d3 Zee dd4d5 Zee dd6d7 Z ee dd8d9 Z!ee dd:d; Z"ee dd<d= Z#ee dd>d? Z$ee dd@dA Z%ee ddBdC Z&ee ddDdE Z'ee ddFdG Z(ee ddHdI Z)ee ddJdK Z*ee ddLdM Z+ee ddNdO Z,ee ddPdQ Z-ee ddRdS Z.ee ddTdU Z/dVdW Z0dXdY Z1dZd[ Z2d\d] Z3d^d_ Z4d`da Z5dbdc Z6ddde Z7dfdg Z8dhdi Z9djdk Z:dldm Z;dndo Z<dpS )qQRCodeTestsc                 C   s   t  | _d S N)r   tmpdirself r   Y/var/www/html/humari/django-venv/lib/python3.10/site-packages/qrcode/tests/test_qrcode.pysetUp   s   zQRCodeTests.setUpc                 C   s   t | j d S r   )osrmdirr   r   r   r   r   tearDown   s   zQRCodeTests.tearDownc                 C   &   t jdd}|d |jdd d S N   versionaFfitqrcodeQRCodeadd_datamaker   qrr   r   r   
test_basic!      
zQRCodeTests.test_basicc                 C   r   )N   r   r    Fr!   r#   r(   r   r   r   
test_large&   r+   zQRCodeTests.test_largec                 C      | j ttjdd d S )N)   r   assertRaises
ValueErrorr$   r%   r   r   r   r   test_invalid_version+      z QRCodeTests.test_invalid_versionc                 C   r.   )Nborderr0   r   r   r   r   test_invalid_border.   r4   zQRCodeTests.test_invalid_borderc                 C   s,   t jdd}|d | jt|jdd d S )Nr   r   abcdefghijklmnoFr!   )r$   r%   r&   r1   r   r'   r(   r   r   r   test_overflow1      
zQRCodeTests.test_overflowc                 C   s.   t jdd}td}|| |jdd d S r   )r$   r%   r   r&   r'   )r   r)   datar   r   r   test_add_qrdata6   s   
zQRCodeTests.test_add_qrdatac                 C   sL   t  }|d |  | |jd |d |  | |jd d S )Nr    r   bcdefghijklmno   r$   r%   r&   r'   assertEqualr   r(   r   r   r   test_fit<   s   

zQRCodeTests.test_fitc                 C   D   t  }|jddd |  | |jd | |jd jt d S )N"1234567890123456789012345678901234r   optimizer   )	r$   r%   r&   r'   rA   r   	data_listmoder   r(   r   r   r   test_mode_numberE   
   zQRCodeTests.test_mode_numberc                 C   rC   )NABCDEFGHIJ1234567890r   rE   r   )	r$   r%   r&   r'   rA   r   rG   rH   r   r(   r   r   r   test_mode_alphaL   rJ   zQRCodeTests.test_mode_alphac                 C   6   t  }|jddd |  | |jd jt d S )N,r   rE   r$   r%   r&   r'   rA   rG   rH   r   r(   r   r   r   test_regression_mode_commaS      z&QRCodeTests.test_regression_mode_commac                 C   sH   t  }|jdt dd |  | |jd | |jd jt	 d S )NabcABCr   rE   r   )
r$   r%   r&   UNICODE_TEXTr'   rA   r   rG   rH   r   r(   r   r   r   test_mode_8bitY   s
   zQRCodeTests.test_mode_8bitc                 C   rM   )NzABCDEFGHIJ1234567890
r   rE   rO   r(   r   r   r   test_mode_8bit_newline`   rQ   z"QRCodeTests.test_mode_8bit_newlineRequires PILc                 C   s>   t  }|t | }|t  | |	 t
j d S r   )r$   r%   r&   rS   
make_imagesaveioBytesIOassertIsInstance	get_image	pil_Imager   r   r)   imgr   r   r   test_render_pilf   s
   
zQRCodeTests.test_render_pilc                 C   0   t  }|t |jdd}|t  d S )NTransParent
back_colorr$   r%   r&   rS   rW   rX   rY   rZ   r^   r   r   r   +test_render_pil_with_transparent_backgroundn      
z7QRCodeTests.test_render_pil_with_transparent_backgroundc                 C   ra   )Nredrc   re   r^   r   r   r   #test_render_pil_with_red_backgroundu   rg   z/QRCodeTests.test_render_pil_with_red_backgroundc                 C   s2   t  }|t |jddd}|t  d S )N)r         )7   _   #   )rd   
fill_colorre   r^   r   r   r   %test_render_pil_with_rgb_color_tuples|   s   
z1QRCodeTests.test_render_pil_with_rgb_color_tuplesc                 C   s0   t jdd}|t | }|t  d S )N   mask_patternre   r^   r   r   r   test_render_with_pattern   s   
z$QRCodeTests.test_render_with_patternc                 C   s   |  t tjdd W d    n1 sw   Y  |  t tjdd W d    n1 s1w   Y  |  t tjdd W d    d S 1 sMw   Y  d S )Nstring patternrr   r5   *   )r1   	TypeErrorr$   r%   r2   r   r   r   r   "test_make_image_with_wrong_pattern   s   "z.QRCodeTests.test_make_image_with_wrong_patternc                 C   s   t  }| t d|_W d    n1 sw   Y  | t d|_W d    n1 s/w   Y  | t d|_W d    d S 1 sHw   Y  d S )Nru   r5      )r$   r%   r1   rw   rs   r2   r(   r   r   r   test_mask_pattern_setter   s   "z$QRCodeTests.test_mask_pattern_setterc                 C   sr   |  t tjdd W d    n1 sw   Y  |  t tjtd W d    d S 1 s2w   Y  d S )Nnot_BaseImageimage_factory)r1   rw   r$   r%   AssertionErrordictr   r   r   r   test_qrcode_bad_factory   s   "z#QRCodeTests.test_qrcode_bad_factoryc                 C   sN   G dd dt }tj|d}|t |  | |jj | |j	j d S )Nc                   @   s   e Zd Ze Ze ZdS )z4QRCodeTests.test_qrcode_factory.<locals>.MockFactoryN)__name__
__module____qualname__r   Mockdrawrect	new_imager   r   r   r   MockFactory   s    r   r|   )
r   r$   r%   r&   rS   rW   
assertTruer   calledr   )r   r   r)   r   r   r   test_qrcode_factory   s   
zQRCodeTests.test_qrcode_factoryc                 C   sT   t  }|t |jtd}| | tj	 t
|j|j|j |t  d S )Nr|   )r$   r%   r&   rS   rW   r   r[   r\   pngWriterprintwidthbox_sizer7   rX   rY   rZ   r^   r   r   r   test_render_pypng   s   
zQRCodeTests.test_render_pypngc                 C   s   t  }|t |jtd}| | tj	 t
 }t
jd|dd |d W d    n1 s4w   Y  |dd |ddj  d S )Nr|   zqrcode.image.pure.openT)createztest_file.pngwb)r$   r%   r&   rS   rW   r   r[   r\   r   r   r   	mock_openpatchrX   assert_called_once_withwriteassert_called)r   r)   r_   r   r   r   r   test_render_pypng_to_str   s   
z$QRCodeTests.test_render_pypng_to_strc                 C   s6   t jt jd}|t |jtd}|t	  d S )Nerror_correctionr|   )
r$   r%   ERROR_CORRECT_Lr&   rS   rW   r   rX   rY   rZ   r^   r   r   r   test_render_styled_Image   s   
z$QRCodeTests.test_render_styled_Imagec                 C   sH   t jdddd}tjtjd}|t |jt|d}|	t
  d S )NRGB
   r   rh   colorr   )r}   embeded_image)r]   newr$   r%   r   r&   rS   rW   r   rX   rY   rZ   )r   embeded_imgr)   r_   r   r   r   %test_render_styled_with_embeded_image   s
   
z1QRCodeTests.test_render_styled_with_embeded_imagec                 C   sl   t j| jd}tjdddd}|| tjtj	d}|
t |jt|d}|t  t | d S )Nztest.pngr   r   rh   r   r   )r}   embeded_image_path)r   pathjoinr   r]   r   rX   r$   r%   r   r&   rS   rW   r   rY   rZ   remove)r   tmpfiler   r)   r_   r   r   r   *test_render_styled_with_embeded_image_path   s   

z6QRCodeTests.test_render_styled_with_embeded_image_pathc                 C   <   t jt jd}|t |jtt d}|	t
  d S Nr   )r}   module_drawer)r$   r%   r   r&   rS   rW   r   r
   SquareModuleDrawerrX   rY   rZ   r^   r   r   r   ,test_render_styled_with_square_module_drawer      
z8QRCodeTests.test_render_styled_with_square_module_drawerc                 C   r   r   )r$   r%   r   r&   rS   rW   r   r
   GappedSquareModuleDrawerrX   rY   rZ   r^   r   r   r   ,test_render_styled_with_gapped_module_drawer   r   z8QRCodeTests.test_render_styled_with_gapped_module_drawerc                 C   r   r   )r$   r%   r   r&   rS   rW   r   r
   CircleModuleDrawerrX   rY   rZ   r^   r   r   r   ,test_render_styled_with_circle_module_drawer   r   z8QRCodeTests.test_render_styled_with_circle_module_drawerc                 C   r   r   )r$   r%   r   r&   rS   rW   r   r
   RoundedModuleDrawerrX   rY   rZ   r^   r   r   r   -test_render_styled_with_rounded_module_drawer   r   z9QRCodeTests.test_render_styled_with_rounded_module_drawerc                 C   r   r   )r$   r%   r   r&   rS   rW   r   r
   VerticalBarsDrawerrX   rY   rZ   r^   r   r   r   3test_render_styled_with_vertical_bars_module_drawer	  r   z?QRCodeTests.test_render_styled_with_vertical_bars_module_drawerc                 C   r   r   )r$   r%   r   r&   rS   rW   r   r
   HorizontalBarsDrawerrX   rY   rZ   r^   r   r   r   5test_render_styled_with_horizontal_bars_module_drawer  r   zAQRCodeTests.test_render_styled_with_horizontal_bars_module_drawerc                 C   s@   t jt jd}|t t }|jt|d}|	t
  d S )Nr   r}   
color_mask)r$   r%   r   r&   rS   r	   SolidFillColorMaskrW   r   rX   rY   rZ   r   r)   maskr_   r   r   r   0test_render_styled_with_default_solid_color_mask  s
   
z<QRCodeTests.test_render_styled_with_default_solid_color_maskc                 C   sF   t jt jd}|t tjttd}|j	t
|d}|t  d S )Nr   rd   front_colorr   )r$   r%   r   r&   rS   r	   r   WHITEREDrW   r   rX   rY   rZ   r   r   r   r   (test_render_styled_with_solid_color_mask%  s
   
z4QRCodeTests.test_render_styled_with_solid_color_maskc                 C   sT   t jt jd}|t tjdtd}|jt	|d}|
t  |jdks(J d S )Nr   )r   r   r   r   r   r   RGBA)r$   r%   r   r&   rS   r	   r   r   rW   r   rX   rY   rZ   rH   r   r   r   r   4test_render_styled_with_color_mask_with_transparency-  s   
z@QRCodeTests.test_render_styled_with_color_mask_with_transparencyc                 C   H   t jt jd}|t tjttt	d}|j
t|d}|t  d S Nr   )rd   center_color
edge_colorr   )r$   r%   r   r&   rS   r	   RadialGradiantColorMaskr   BLACKr   rW   r   rX   rY   rZ   r   r   r   r   2test_render_styled_with_radial_gradient_color_mask8     
z>QRCodeTests.test_render_styled_with_radial_gradient_color_maskc                 C   r   r   )r$   r%   r   r&   rS   r	   SquareGradiantColorMaskr   r   r   rW   r   rX   rY   rZ   r   r   r   r   2test_render_styled_with_square_gradient_color_maskB  r   z>QRCodeTests.test_render_styled_with_square_gradient_color_maskc                 C   r   )Nr   )rd   
left_colorright_colorr   )r$   r%   r   r&   rS   r	   HorizontalGradiantColorMaskr   r   r   rW   r   rX   rY   rZ   r   r   r   r   6test_render_styled_with_horizontal_gradient_color_maskL  r   zBQRCodeTests.test_render_styled_with_horizontal_gradient_color_maskc                 C   r   )Nr   )rd   	top_colorbottom_colorr   )r$   r%   r   r&   rS   r	   VerticalGradiantColorMaskr   r   r   rW   r   rX   rY   rZ   r   r   r   r   4test_render_styled_with_vertical_gradient_color_maskV  r   z@QRCodeTests.test_render_styled_with_vertical_gradient_color_maskc                 C   sV   t jdddd}tjtjd}|t tjt	|d}|j
t|d}|t  d S )Nr   r   rh   r   r   )rd   color_mask_imager   )r]   r   r$   r%   r   r&   rS   r	   ImageColorMaskr   rW   r   rX   rY   rZ   )r   img_maskr)   r   r_   r   r   r   (test_render_styled_with_image_color_mask`  s   
z4QRCodeTests.test_render_styled_with_image_color_maskc                 C   sV   t  }d}|j|dd |  | dd |jD tttttg | |j	d d S )NA1abc12345def1HELLOa   rE   c                 S      g | ]}|j qS r   rH   .0dr   r   r   
<listcomp>o      z-QRCodeTests.test_optimize.<locals>.<listcomp>r?   )
r$   r%   r&   r'   rA   rG   r   r   r   r   r   r)   textr   r   r   test_optimizei  s   
zQRCodeTests.test_optimizec                 C   sd   t  }d}|j|dd |  | t|jd | dd |jD tttg | |j	d d S )NA1abc1234567def1HELLOa   rE   rq   c                 S   r   r   r   r   r   r   r   r     r   z3QRCodeTests.test_optimize_short.<locals>.<listcomp>r?   )
r$   r%   r&   r'   rA   lenrG   r   r   r   r   r   r   r   test_optimize_shortz  s   zQRCodeTests.test_optimize_shortc                 C   sD   t  }d}|j|dd | t|jd | |jd jt d S )NABCDEFGHIJK   rE   r   r   )r$   r%   r&   rA   r   rG   rH   r   r   r   r   r   test_optimize_longer_than_data  s
   z*QRCodeTests.test_optimize_longer_than_datac                 C   s\   d}t  }|| |  | |jd t  }|j|dd |  | |jd d S )NA1abc12345123451234512345def1HELLOHELLOHELLOHELLOaA1abc12345123451234512345def1HELLOHELLOHELLOHELLOaA1abc12345123451234512345def1HELLOHELLOHELLOHELLOaA1abc12345123451234512345def1HELLOHELLOHELLOHELLOaA1abc12345123451234512345def1HELLOHELLOHELLOHELLOar   r   rE      r@   )r   r   r)   r   r   r   test_optimize_size  s   
zQRCodeTests.test_optimize_sizec                 C   s(   d}t j|}| t|t| d S )Ns   hello)r$   utilr   rA   repr)r   r<   data_objr   r   r   test_qrdata_repr  s   zQRCodeTests.test_qrdata_reprc                 C   s`   t  }td}d |j_| jt|jdd | 	|jj
 W d    d S 1 s)w   Y  d S )N
sys.stdoutT)tty)r$   r%   r   r   isattyreturn_valuer1   OSErrorprint_asciir   r   r   r)   fake_stdoutr   r   r   test_print_ascii_stdout  s   "z#QRCodeTests.test_print_ascii_stdoutc                 C   s   t jdd}t }|j|d | }|  d}| |d t| | t }dd |_	|j|dd | }|  d	}| |d t| | d S )
Nr   r6   outu   █▀▀▀▀▀█c                   S      dS NTr   r   r   r   r   <lambda>      z.QRCodeTests.test_print_ascii.<locals>.<lambda>T)r  r   u)   [48;5;232m[38;5;255m ▄▄▄▄▄ )
r$   r%   rY   StringIOr   getvaluecloserA   r   r   )r   r)   fprintedexpectedr   r   r   test_print_ascii  s   
zQRCodeTests.test_print_asciic                 C   s\   t  }td}d |j_| t|j | 	|jj
 W d    d S 1 s'w   Y  d S )Nr   )r$   r%   r   r   r   r   r1   r   	print_ttyr   r   r   r   r   r   test_print_tty_stdout  s   "z!QRCodeTests.test_print_tty_stdoutc           	      C   s   t  }t }dd |_|j|d | }|  d}d}|d | }d}|d | | d	 | }| |d t	| | d S )
Nc                   S   r  r  r   r   r   r   r   r    r  z,QRCodeTests.test_print_tty.<locals>.<lambda>r  z[1;47mz[40mz  z[0m
z.                                              z              )
r$   r%   rY   r  r   r  r	  r
  rA   r   )	r   r)   r  r  BOLD_WHITE_BGBLACK_BGWHITE_BLOCKEOLr  r   r   r   test_print_tty  s   
zQRCodeTests.test_print_ttyc                 C   s,   t jdd}|d | | |j d S )Nr   r6   1)r$   r%   r&   rA   
get_matrixmodulesr(   r   r   r   test_get_matrix  r;   zQRCodeTests.test_get_matrixc                 C   sB   t jdd}|d dd | dd D }| ||j d S )Nr   r6   r  c                 S   s   g | ]}|d d qS )r   r5   r   )r   rowr   r   r   r     s    z6QRCodeTests.test_get_matrix_border.<locals>.<listcomp>r5   )r$   r%   r&   r  rA   r  )r   r)   matrixr   r   r   test_get_matrix_border  s   
z"QRCodeTests.test_get_matrix_borderc                 C   r.   )Nr5   )r   r0   r   r   r   r   "test_negative_size_at_construction  r4   z.QRCodeTests.test_negative_size_at_constructionc                 C   s    t  }d|_| t|j d S )Nr5   )r$   r%   r   r1   r2   rW   r(   r   r   r   test_negative_size_at_usage  s   z'QRCodeTests.test_negative_size_at_usageN)=r   r   r   r   r   r*   r-   r3   r8   r:   r=   rB   rI   rL   rP   rT   rU   unittestskipIfr]   r`   rf   ri   rp   rt   rx   rz   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r  r  r  r  r  r  r  r  r   r   r   r   r      s    	





	




	
	
	
	
	
	




	
	
	
	
r   c                   @   s"   e Zd Zee ddd ZdS )ShortcutTestrV   c                 C   s   t d d S )Nimage)r$   r'   r   r   r   r   runTest  s   zShortcutTest.runTestN)r   r   r   r  r   r]   r#  r   r   r   r   r!    s    r!  )#rY   r   r  warningstempfiler   r   r   r$   qrcode.utilqrcode.compat.pilr   r]   qrcode.exceptionsr   qrcode.image.baser   qrcode.image.purer   qrcode.image.styledpilr   qrcode.image.stylesr	   r
   r   r   r   r   rS   r   r   r   TestCaser   r!  r   r   r   r   <module>   s2       M