a
    Lcòf4=  ã                   @   sz   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 d dlmZ d	ZG d
d„ dƒZdS )é    N)ÚStringIO)Úapps)Úsettings)Úserializers)Úrouter)Úatomic)Úimport_stringZtest_c                   @   s¼   e Zd ZdZdd„ Zdd„ Zdd„ Zd.dd„Zdd„ Zdd„ Z	dd„ Z
dd„ Zdd„ Zd/dd„Zd0dd„Zd1dd„Zdd„ Zd2dd „Zd3d"d#„Zd$d%„ Zd&d'„ Zd(d)„ Zd*d+„ Zd,d-„ Zd!S )4ÚBaseDatabaseCreationzs
    Encapsulate backend-specific differences pertaining to creation and
    destruction of the test database.
    c                 C   s
   || _ d S ©N)Ú
connection)Úselfr   © r   ú]/home/httpd/docs/test/DocsMgr/lib/python3.9/site-packages/django/db/backends/base/creation.pyÚ__init__   s    zBaseDatabaseCreation.__init__c                 C   s
   | j  ¡ S r
   )r   Ú_nodb_cursor©r   r   r   r   r      s    z!BaseDatabaseCreation._nodb_cursorc                 C   s   t j |tj ¡ d S r
   )ÚsysÚstderrÚwriteÚosÚlinesep)r   Úmsgr   r   r   Úlog   s    zBaseDatabaseCreation.logé   FTc           	      C   sN  ddl m} |  ¡ }|dkrBd}|r(d}|  d||  ||¡f ¡ |  |||¡ | j ¡  |tj	| jj
 d< || jjd< zl| jjd d	 d
u r¨tj}dd„ t ¡ D ƒt_|dt|d dƒd
| jj
dd� W | jjd d	 d
u rä|t_n| jjd d	 d
u �r|t_0 |�r|  ¡ | j_|d| jj
d� | j ¡  tj d¡dk�rJ|  ¡  |S )z£
        Create a test database, prompting the user for confirmation if the
        database already exists. Return the name of the test database created.
        r   )Úcall_commandr   ZCreatingzUsing existingú %s test database for alias %s...ÚNAMEÚTESTZMIGRATEFc                 S   s   i | ]}|j d “qS r
   )Úlabel)Ú.0Zappr   r   r   Ú
<dictcomp>H   s   z7BaseDatabaseCreation.create_test_db.<locals>.<dictcomp>ZmigrateT)Ú	verbosityÚinteractiveÚdatabaseZ
run_syncdbZcreatecachetable)r#   ZRUNNING_DJANGOS_TEST_SUITEÚtrue)Zdjango.core.managementr   Ú_get_test_db_namer   Ú_get_database_display_strÚ_create_test_dbr   Úcloser   Ú	DATABASESÚaliasÚsettings_dictZMIGRATION_MODULESr   Úget_app_configsÚmaxÚserialize_db_to_stringZ_test_serialized_contentsZensure_connectionr   ÚenvironÚgetÚ mark_expected_failures_and_skips)	r   r!   ÚautoclobberÚ	serializeÚkeepdbr   Útest_database_nameÚactionZold_migration_modulesr   r   r   Úcreate_test_db    sR    
þÿÿ
ÿûÿ
z#BaseDatabaseCreation.create_test_dbc                 C   s   |d | j jd< dS )z€
        Set this database up to be used in testing as a mirror of a primary
        database whose settings are given.
        r   N)r   r+   )r   Zprimary_settings_dictr   r   r   Úset_as_test_mirrorj   s    z'BaseDatabaseCreation.set_as_test_mirrorc                    s.   ‡ fdd„}t ƒ }tjd|ƒ d|d� | ¡ S )z¤
        Serialize all data in the database into a JSON string.
        Designed only for test runner usage; will not handle large
        amounts of data.
        c                  3   s    ddl m}  | ˆ jƒ}t ¡ D ]|}|jd ur|j|jv r|jt	j
vr| ¡ D ]L}|j ˆ j¡rLt ˆ jj|¡rL|j ˆ jj¡ |jjj¡}| ¡ E d H  qLqd S )Nr   )ÚMigrationLoader)Zdjango.db.migrations.loaderr9   r   r   r,   Zmodels_moduler   Zmigrated_appsÚnamer   ZTEST_NON_SERIALIZED_APPSZ
get_modelsÚ_metaZcan_migrater   Zallow_migrate_modelr*   Z_base_managerÚusingZorder_byÚpkÚiterator)r9   ÚloaderZ
app_configÚmodelZquerysetr   r   r   Úget_objectsy   s*    
ÿ
þ
ýÿþÿþz@BaseDatabaseCreation.serialize_db_to_string.<locals>.get_objectsÚjsonN)ÚindentÚstream)r   r   r3   Úgetvalue)r   rA   Úoutr   r   r   r.   q   s    z+BaseDatabaseCreation.serialize_db_to_stringc              	   C   s¬   t |ƒ}tƒ }t| jjd��| | j ¡ �D tjd|| jjd�D ]}| ¡  | 	|j
jjj¡ q>W d  ƒ n1 sr0    Y  | jj|d� W d  ƒ n1 sž0    Y  dS )zw
        Reload the database with data from a string generated by
        the serialize_db_to_string() method.
        )r<   rB   N)Útable_names)r   Úsetr   r   r*   Zconstraint_checks_disabledr   ZdeserializeÚsaveÚaddÚobjectÚ	__class__r;   Zdb_tableZcheck_constraints)r   ÚdatarG   Úobjr   r   r   Údeserialize_db_from_string‘   s    
ÿ
2z/BaseDatabaseCreation.deserialize_db_from_stringc                 C   s    d| j j|dkrd| ndf S )zR
        Return display string for a database for use in various actions.
        z'%s'%sé   z ('%s')Ú )r   r*   )r   r!   Zdatabase_namer   r   r   r&   ¦   s    þz.BaseDatabaseCreation._get_database_display_strc                 C   s0   | j jd d r | j jd d S t| j jd  S )zû
        Internal implementation - return the name of the test DB that will be
        created. Only useful when called from create_test_db() and
        _create_test_db() and when no external munging is done with the 'NAME'
        settings.
        r   r   )r   r+   ÚTEST_DATABASE_PREFIXr   r   r   r   r%   ¯   s    z&BaseDatabaseCreation._get_test_db_namec                 C   s   |  d| ¡ d S )Nz%CREATE DATABASE %(dbname)s %(suffix)s)Úexecute)r   ÚcursorÚ
parametersr4   r   r   r   Ú_execute_create_test_dbº   s    z,BaseDatabaseCreation._execute_create_test_dbc           	      C   sp  |   ¡ }| jj |¡|  ¡ dœ}|  ¡ ��0}z|  |||¡ W �n
 t�yJ } zð|rt|W  Y d}~W  d  ƒ S |  d| ¡ |s’t	d| ƒ}|s |dk�r"z@|dkrÂ|  d|  
||¡f ¡ | d| ¡ |  |||¡ W n> t�y } z$|  d	| ¡ t d
¡ W Y d}~n
d}~0 0 n|  d¡ t d¡ W Y d}~n
d}~0 0 W d  ƒ n1 �sb0    Y  |S )zF
        Internal implementation - create the test db tables.
        )ZdbnameÚsuffixNz+Got an error creating the test database: %szXType 'yes' if you would like to try deleting the test database '%s', or 'no' to cancel: Úyesr   z,Destroying old test database for alias %s...zDROP DATABASE %(dbname)sz-Got an error recreating the test database: %srP   zTests cancelled.)r%   r   ÚopsÚ
quote_nameÚsql_table_creation_suffixr   rV   Ú	Exceptionr   Úinputr&   rS   r   Úexit)	r   r!   r2   r4   r5   Ztest_db_paramsrT   ÚeZconfirmr   r   r   r'   ½   sH    þÿÿÿÿÿÿ"
@z$BaseDatabaseCreation._create_test_dbc                 C   sL   | j jd }|dkr:d}|r d}|  d||  ||¡f ¡ |  |||¡ dS )z(
        Clone a test database.
        r   r   zCloning test databasezUsing existing clonez%s for alias %s...N)r   r+   r   r&   Ú_clone_test_db)r   rW   r!   r2   r4   Zsource_database_namer6   r   r   r   Úclone_test_dbì   s    
þÿÿ
z"BaseDatabaseCreation.clone_test_dbc                 C   s$   | j j}i |¥dd |d |¡i¥S )zX
        Return a modified connection settings dict for the n-th clone of a DB.
        r   z{}_{})r   r+   Úformat)r   rW   Zorig_settings_dictr   r   r   Úget_test_db_clone_settings  s    ÿþz/BaseDatabaseCreation.get_test_db_clone_settingsc                 C   s   t dƒ‚dS )zI
        Internal implementation - duplicate the test db tables.
        znThe database backend doesn't support cloning databases. Disable the option to run tests in parallel processes.N)ÚNotImplementedError)r   rW   r!   r4   r   r   r   r`     s    ÿz#BaseDatabaseCreation._clone_test_dbNc                 C   s–   | j  ¡  |du r | j jd }n|  |¡d }|dkr\d}|rBd}|  d||  ||¡f ¡ |sl|  ||¡ |dur’|tj| j j	 d< || j jd< dS )zv
        Destroy a test database, prompting the user for confirmation if the
        database already exists.
        Nr   r   Z
DestroyingZ
Preservingr   )
r   r(   r+   rc   r   r&   Ú_destroy_test_dbr   r)   r*   )r   Zold_database_namer!   r4   rW   r5   r6   r   r   r   Údestroy_test_db  s(    

þÿÿ
z$BaseDatabaseCreation.destroy_test_dbc                 C   sD   |   ¡ �(}| d| jj |¡ ¡ W d  ƒ n1 s60    Y  dS )zF
        Internal implementation - remove the test db tables.
        zDROP DATABASE %sN)r   rS   r   rY   rZ   )r   r5   r!   rT   r   r   r   re   ;  s    
ÿz%BaseDatabaseCreation._destroy_test_dbc                 C   sÞ   ddl m}m} | jjjD ]N}| d¡\}}}| d¡d }|tj	v rt
|ƒ}t||ƒ}	t||||	ƒƒ q| jjj ¡ D ]`\}
}|D ]R}| d¡\}}}| d¡d }|tj	v r„t
|ƒ}t||ƒ}	t||||
ƒ|	ƒƒ q„qxdS )z›
        Mark tests in Django's test suite which are expected failures on this
        database and test which should be skipped on this database.
        r   )ÚexpectedFailureÚskipÚ.N)Zunittestrg   rh   r   ÚfeaturesZdjango_test_expected_failuresÚ
rpartitionÚsplitr   ZINSTALLED_APPSr   ÚgetattrÚsetattrZdjango_test_skipsÚitems)r   rg   rh   Z	test_nameZtest_case_nameÚ_Ztest_method_nameZtest_appZ	test_caseZtest_methodÚreasonÚtestsr   r   r   r1   H  s     



z5BaseDatabaseCreation.mark_expected_failures_and_skipsc                 C   s   dS )zQ
        SQL to append to the end of the test table creation statements.
        rQ   r   r   r   r   r   r[   b  s    z.BaseDatabaseCreation.sql_table_creation_suffixc                 C   s$   | j j}|d |d |d |  ¡ fS )zÇ
        Return a tuple with elements of self.connection.settings_dict (a
        DATABASES setting value) that uniquely identify a database
        accordingly to the RDBMS particularities.
        ZHOSTZPORTZENGINE)r   r+   r%   )r   r+   r   r   r   Útest_db_signatureh  s    üz&BaseDatabaseCreation.test_db_signaturec                 C   s*   |   t|ƒ¡}| jj |¡ | j ¡  d S r
   )rc   Ústrr   r+   Úupdater(   )r   Z
_worker_idr+   r   r   r   Úsetup_worker_connectionv  s    z,BaseDatabaseCreation.setup_worker_connection)r   FTF)F)F)r   FF)F)Nr   FN)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   r   r7   r8   r.   rO   r&   r%   rV   r'   ra   rc   r`   rf   re   r1   r[   rs   rv   r   r   r   r   r	      s.    ÿ
J 	

/


 ÿ
#r	   )r   r   Úior   Zdjango.appsr   Zdjango.confr   Zdjango.corer   Z	django.dbr   Zdjango.db.transactionr   Zdjango.utils.module_loadingr   rR   r	   r   r   r   r   Ú<module>   s   