
    P}bg39                         d dl Z d dlZd dlmZ d dlmZ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          ZdS )    N)StringIO)expectedFailureskip)apps)settings)serializers)router)atomic)import_stringtest_c                       e Zd ZdZd Zd Zd ZddZd	 Zd
 Z	d Z
d Zd ZddZddZddZd ZddZddZd Zd Zd Zd ZdS )BaseDatabaseCreationzs
    Encapsulate backend-specific differences pertaining to creation and
    destruction of the test database.
    c                     || _         d S N)
connection)selfr   s     s/home/e360mart.nyusoft.in/public_html/e360mart_env/lib/python3.11/site-packages/django/db/backends/base/creation.py__init__zBaseDatabaseCreation.__init__   s    $    c                 4    | j                                         S r   )r   _nodb_cursorr   s    r   r   z!BaseDatabaseCreation._nodb_cursor   s    ++---r   c                 ^    t           j                            |t          j        z              d S r   )sysstderrwriteoslinesep)r   msgs     r   logzBaseDatabaseCreation.log   s%    
rz)*****r      FTc                    ddl m} |                                 }|dk    r5d}|rd}|                     |d|                     ||          d           |                     |||           | j                                         |t          j	        | j        j
                 d<   || j        j        d<   	 | j        j        d	         d
         du r3t          j        }d t          j                    D             t          _         |dt          |dz
  d          d| j        j
        d           | j        j        d	         d
         du r|t          _        n+# | j        j        d	         d
         du r|t          _        w xY w|r|                                 | j        _         |d| j        j
                   | j                                         t&          j                            d          dk    r|                                  |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!   CreatingzUsing existing test database for alias ...NAMETESTMIGRATEFc                     i | ]
}|j         d S r   )label).0apps     r   
<dictcomp>z7BaseDatabaseCreation.create_test_db.<locals>.<dictcomp>C   s,     . . . It. . .r   migrateT)	verbosityinteractivedatabase
run_syncdbcreatecachetable)r2   RUNNING_DJANGOS_TEST_SUITEtrue)django.core.managementr#   _get_test_db_namer    _get_database_display_str_create_test_dbr   closer   	DATABASESaliassettings_dictMIGRATION_MODULESr   get_app_configsmaxserialize_db_to_string_test_serialized_contentsensure_connectionr   environget mark_expected_failures_and_skips)	r   r0   autoclobber	serializekeepdbr#   test_database_nameactionold_migration_moduless	            r   create_test_dbz#BaseDatabaseCreation.create_test_db    s=    	877777!3355>>F *)HH..y:LMMMM    	YV<<<<N4?01&90B%f-	C,V4Y?5HH(0(B%. .#355. . .* Li!mQ//!.    ,V4Y?5HH-B* ,V4Y?5HH-B*BBBB  	V8<8S8S8U8UDO5'$/2GHHHH 	))+++:>>6776AA11333!!s   5A8E (E<c                 0    |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   primary_settings_dicts     r   set_as_test_mirrorz'BaseDatabaseCreation.set_as_test_mirrorf   s    
 1Ff0M%f---r   c                       fd}t                      }t          j        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     K   ddl m}   | j                  }t          j                    D ]}|j        |j        |j        v r|j        t          j
        vr|                                D ]}|j                            j                  rt          j        j        j        |          r`|j                            j        j                                      |j        j        j                  }|                                E d {V  d S )Nr   )MigrationLoader)django.db.migrations.loaderrT   r   r   r@   models_moduler+   migrated_appsnamer   TEST_NON_SERIALIZED_APPS
get_models_metacan_migrater	   allow_migrate_modelr=   _base_managerusingorder_bypkiterator)rT   loader
app_configmodelquerysetr   s        r   get_objectsz@BaseDatabaseCreation.serialize_db_to_string.<locals>.get_objectst   s&     CCCCCC$_T_55F"244 ; ;
,8$(<<<O8+LLL!+!6!6!8!8 ; ;!K33DODD;"6t7LeTT; (-':'@'@ $ 5( (&hu{~':;; % (0'8'8':'::::::::; ;r   jsonN)indentstream)r   r   rI   getvalue)r   rg   outs   `  r   rB   z+BaseDatabaseCreation.serialize_db_to_stringm   sU    	; 	; 	; 	; 	;& jjfkkmmDMMMM||~~r   c                    t          |          }t                      }t          | j        j                  5  | j                                        5  t          j        d|| j        j                  D ]?}|                                 |	                    |j
        j        j        j                   @	 ddd           n# 1 swxY w Y   | j                            |           ddd           dS # 1 swxY w Y   dS )zw
        Reload the database with data from a string generated by
        the serialize_db_to_string() method.
        )r_   rh   N)table_names)r   setr
   r   r=   constraint_checks_disabledr   deserializesaveaddobject	__class__r[   db_tablecheck_constraints)r   datarn   objs       r   deserialize_db_from_stringz/BaseDatabaseCreation.deserialize_db_from_string   s   
 ~~ee$//000 		G 		G ;;== I I&264tG\]]] I ICHHJJJOOCJ$8$>$GHHHHII I I I I I I I I I I I I I I O--+-FFF		G 		G 		G 		G 		G 		G 		G 		G 		G 		G 		G 		G 		G 		G 		G 		G 		G 		Gs6   C4A"C5C4C	C4C		C44C8;C8c                 <    d| j         j        d|dk    rd|z  ndS )zR
        Return display string for a database for use in various actions.
        '   z ('%s') )r   r=   )r   r0   database_names      r   r9   z.BaseDatabaseCreation._get_database_display_str   s6     
 O!!!+4>>Y&&rA
 	
r   c                     | j         j        d         d         r| j         j        d         d         S t          | j         j        d         z   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   s    r   r8   z&BaseDatabaseCreation._get_test_db_name   sF     ?(08 	A?08@@#do&CF&KKKr   c                 6    |                     d|z             d S )Nz%CREATE DATABASE %(dbname)s %(suffix)s)execute)r   cursor
parametersrJ   s       r   _execute_create_test_dbz,BaseDatabaseCreation._execute_create_test_db   s     >KLLLLLr   c           	         |                                  }| j        j                            |          |                                 d}|                                 5 }	 |                     |||           n1# t          $ r#}|r|cY d}~cddd           S |                     d|z             |st          d|z            }|s|dk    r	 |dk    r-|                     d| 
                    ||          d           |                    d	|z             |                     |||           nl# t          $ r6}|                     d
|z             t          j        d           Y d}~n1d}~ww xY w|                     d           t          j        d           Y d}~nd}~ww xY wddd           n# 1 swxY w Y   |S )zF
        Internal implementation - create the test db tables.
        )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 r&   zDROP DATABASE %(dbname)sz-Got an error recreating the test database: %sr}   zTests cancelled.)r8   r   ops
quote_namesql_table_creation_suffixr   r   	Exceptionr    inputr9   r   r   exit)	r   r0   rH   rJ   rK   test_db_paramsr   econfirms	            r   r:   z$BaseDatabaseCreation._create_test_db   s    "3355o)445GHH4466
 

    	 F ,,V^VLLLL        .------	  	  	  	  	  	  	  	  FJKKK" S#=?QRS SG   'U"2"2	$$>> HHH $ > >yJ\ ] ] ] ]&    'AN'RSSS44V^VTTTT$ $ $ $!PST!TUUU$ HH/000HQKKK1 	  	  	  	  	  	  	  	  	  	  	  	  	  	  	 : "!sy   F5A86F58F&F!F&F54F!A"D0/F!0
E0:,E+&F!+E00,F!F5!F&&F55F9<F9c                     | j         j        d         }|dk    r5d}|rd}|                     |d|                     ||          d           |                     |||           dS )z(
        Clone a test database.
        r'   r!   zCloning test databasezUsing existing clonez for alias r&   N)r   r>   r    r9   _clone_test_db)r   r   r0   rH   rJ   source_database_namerL   s          r   clone_test_dbz"BaseDatabaseCreation.clone_test_db   s      $<VD>>,F 0/HH..y:NOOOO    	FIv66666r   c                 ^    | 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   r   orig_settings_dicts      r   get_test_db_clone_settingsz/BaseDatabaseCreation.get_test_db_clone_settings   s;     "_:a$afgnn=OPV=WY_.`.`aaar   c                      t          d          )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.)NotImplementedError)r   r   r0   rJ   s       r   r   z#BaseDatabaseCreation._clone_test_db   s     "EF F 	Fr   Nc                    | j                                          || j         j        d         }n|                     |          d         }|dk    r5d}|rd}|                     |d|                     ||          d           |s|                     ||           |0|t          j        | j         j	                 d<   || j         j        d<   dS dS )zv
        Destroy a test database, prompting the user for confirmation if the
        database already exists.
        Nr'   r!   
Destroying
Preservingr%   r&   )
r   r;   r>   r   r    r9   _destroy_test_dbr   r<   r=   )r   old_database_namer0   rJ   r   rK   rL   s          r   destroy_test_dbz$BaseDatabaseCreation.destroy_test_db  s    
 	>!%!>v!F!%!@!@!H!H!P>>!F &%HH..y:LMMMM     	A!!"4i@@@ (@QHt45f=4EDO)&111 )(r   c                     |                                  5 }|                    d| j        j                            |          z             ddd           dS # 1 swxY w Y   dS )zF
        Internal implementation - remove the test db tables.
        zDROP DATABASE %sN)r   r   r   r   r   )r   rK   r0   r   s       r   r   z%BaseDatabaseCreation._destroy_test_db!  s        	QFNN-!_0;;<NOOP Q Q Q	Q 	Q 	Q 	Q 	Q 	Q 	Q 	Q 	Q 	Q 	Q 	Q 	Q 	Q 	Q 	Q 	Q 	Qs   6AAAc           
         | j         j        j        D ]}|                    d          \  }}}|                    d          d         }|t
          j        v r=t          |          }t          ||          }t          ||t          |                     | j         j        j                                        D ]\  }}	|	D ]}|                    d          \  }}}|                    d          d         }|t
          j        v rFt          |          }t          ||          }t          || t          |          |                     d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   N)r   featuresdjango_test_expected_failures
rpartitionsplitr   INSTALLED_APPSr   getattrsetattrr   django_test_skipsitemsr   )
r   	test_nametest_case_name_test_method_nametest_app	test_casetest_methodreasontestss
             r   rG   z5BaseDatabaseCreation.mark_expected_failures_and_skips-  sa   
 1O 	S 	SI2;2F2Fs2K2K/NA/ s++A.H8222).99	%i1ABB	#3_[5Q5QRRR!_5GMMOO 	T 	TMFE" T T	6?6J6J36O6O3#3$??3//2x666 -n = =I"))5E"F"FKI'7fk9R9RSSST	T 	Tr   c                     dS )zQ
        SQL to append to the end of the test table creation statements.
        r~    r   s    r   r   z.BaseDatabaseCreation.sql_table_creation_suffixD  s	     rr   c                 n    | 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.
        HOSTPORTENGINE)r   r>   r8   )r   r>   s     r   test_db_signaturez&BaseDatabaseCreation.test_db_signatureJ  s?     5&!&!(#""$$	
 	
r   )r!   FTF)F)r!   FF)Nr!   FN)__name__
__module____qualname____doc__r   r   r    rN   rQ   rB   rz   r9   r8   r   r:   r   r   r   r   r   rG   r   r   r   r   r   r   r      s}        % % %. . .+ + +D" D" D" D"LN N N  <G G G&
 
 
	L 	L 	LM M M M'" '" '" '"R7 7 7 7&b b bF F F FF F F F<
Q 
Q 
QT T T.  
 
 
 
 
r   r   )r   r   ior   unittestr   r   django.appsr   django.confr   django.corer   	django.dbr	   django.db.transactionr
   django.utils.module_loadingr   r   r   r   r   r   <module>r      s    				 



       * * * * * * * *                   # # # # # #       ( ( ( ( ( ( 5 5 5 5 5 5  D
 D
 D
 D
 D
 D
 D
 D
 D
 D
r   