o a39@sddlZddlZddlmZddlmZmZddlmZddl m Z ddl m Z ddl mZddlmZdd lmZd ZGd d d ZdS) N)StringIO)expectedFailureskip)apps)settings) serializers)router)atomic) import_stringtest_c@seZdZdZddZddZddZd,d d Zd dZddZ ddZ ddZ ddZ d-ddZ d-ddZd.ddZddZd-dd Zd/d"d#Zd$d%Zd&d'Zd(d)Zd*d+Zd!S)0BaseDatabaseCreationzs Encapsulate backend-specific differences pertaining to creation and destruction of the test database. cCs ||_dSN) connection)selfrrB/usr/lib/python3/dist-packages/django/db/backends/base/creation.py__init__ zBaseDatabaseCreation.__init__cCs |jSr )r _nodb_cursorrrrrrrz!BaseDatabaseCreation._nodb_cursorcCstj|tjdSr )sysstderrwriteoslinesep)rmsgrrrlogszBaseDatabaseCreation.logFTc CsHddlm}|}|dkr!d}|rd}|d||||f|||||j|tj |jj d<||jj d<z6|jj dd d urTtj }d d t Dt_ |d t|ddd |jj ddW|jj dd d urr|t_ n|jj dd d ur|t_ w|r||j_|d|jj d|jtjddkr||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_commandrCreatingzUsing existing %s test database for alias %s...NAMETESTMIGRATEFcSsi|]}|jdqSr )label).0apprrr Csz7BaseDatabaseCreation.create_test_db..migrateT) verbosity interactivedatabase run_syncdbcreatecachetable)r+RUNNING_DJANGOS_TEST_SUITEtrue)django.core.managementr_get_test_db_namer_get_database_display_str_create_test_dbrcloser DATABASESalias settings_dictMIGRATION_MODULESrget_app_configsmaxserialize_db_to_string_test_serialized_contentsensure_connectionrenvironget mark_expected_failures_and_skips) rr) autoclobber serializekeepdbrtest_database_nameactionold_migration_modulesrrrcreate_test_db sN       z#BaseDatabaseCreation.create_test_dbcCs|d|jjd<dS)z Set this database up to be used in testing as a mirror of a primary database whose settings are given. r!N)rr7)rprimary_settings_dictrrrset_as_test_mirrorfsz'BaseDatabaseCreation.set_as_test_mirrorcs.fdd}t}tjd|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. c3sddlm}|j}tD]>}|jdurN|j|jvrN|jt j vrN| D]&}|j jrMtjj|rM|jjj|j jj}|EdHq'qdS)Nr)MigrationLoader)django.db.migrations.loaderrJrrr9 models_moduler$ migrated_appsnamerTEST_NON_SERIALIZED_APPS get_models_meta can_migraterallow_migrate_modelr6 _base_managerusingorder_bypkiterator)rJloader app_configmodelquerysetrrr get_objectsts(         z@BaseDatabaseCreation.serialize_db_to_string..get_objectsjsonN)indentstream)rrrBgetvalue)rr]outrrrr;ms z+BaseDatabaseCreation.serialize_db_to_stringc Cst|}t}t|jjd?|j"tjd||jjdD]}|| |j j j j qWdn1s9wY|jj|dWddS1sPwYdS)zw Reload the database with data from a string generated by the serialize_db_to_string() method. )rUr^N) table_names)rsetr rr6constraint_checks_disabledr deserializesaveaddobject __class__rQdb_tablecheck_constraints)rdatarcobjrrrdeserialize_db_from_strings "z/BaseDatabaseCreation.deserialize_db_from_stringcCs$d|jj|dkrd|fSdfS)zR Return display string for a database for use in various actions. z'%s'%sz ('%s'))rr6)rr) database_namerrrr2s z.BaseDatabaseCreation._get_database_display_strcCs0|jjddr|jjddSt|jjdS)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!)rr7TEST_DATABASE_PREFIXrrrrr1sz&BaseDatabaseCreation._get_test_db_namecCs|d|dS)Nz%CREATE DATABASE %(dbname)s %(suffix)s)execute)rcursor parametersrCrrr_execute_create_test_dbsz,BaseDatabaseCreation._execute_create_test_dbc Cst|}|jj||d}|}z ||||Wnty}zu|r7|WYd}~WdS|d||sFt d|}|sL|dkrz |dkr]|d| ||f| d|||||Wn(ty}z|d |t d WYd}~nd}~ww|d t dWYd}~n d}~wwWd|SWd|S1swY|S) zF Internal implementation - create the test db tables. )dbnamesuffixNz+Got an error creating the test database: %szXType 'yes' if you would like to try deleting the test database '%s', or 'no' to cancel: yesrz,Destroying old test database for alias %s...zDROP DATABASE %(dbname)sz-Got an error recreating the test database: %srpzTests cancelled.)r1rops quote_namesql_table_creation_suffixrrw Exceptionrinputr2rtrexit) rr)rArCrDtest_db_paramsrueconfirmrrrr3sV         z$BaseDatabaseCreation._create_test_dbcCsL|jjd}|dkrd}|rd}|d||||f||||dS)z( Clone a test database. r!rzCloning test databasezUsing existing clonez%s for alias %s...N)rr7rr2_clone_test_db)rryr)rArCsource_database_namerErrr clone_test_dbs  z"BaseDatabaseCreation.clone_test_dbcCs$|jj}i|dd|d|iS)zX Return a modified connection settings dict for the n-th clone of a DB. r!z{}_{})rr7format)rryorig_settings_dictrrrget_test_db_clone_settingssz/BaseDatabaseCreation.get_test_db_clone_settingscCstd)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)rryr)rCrrrrsz#BaseDatabaseCreation._clone_test_dbNcCs|j|dur|jjd}n||d}|dkr.d}|r!d}|d||||f|s6||||durK|tj|jj d<||jjd<dSdS)zv Destroy a test database, prompting the user for confirmation if the database already exists. Nr!r Destroying Preservingr ) rr4r7rrr2_destroy_test_dbrr5r6)rold_database_namer)rCryrDrErrrdestroy_test_dbs$   z$BaseDatabaseCreation.destroy_test_dbcCsF|}|d|jj|WddS1swYdS)zF Internal implementation - remove the test db tables. zDROP DATABASE %sN)rrtrr{r|)rrDr)rurrrr!s  "z%BaseDatabaseCreation._destroy_test_dbc Cs|jjjD]'}|d\}}}|dd}|tjvr,t|}t||}t ||t |q|jjj D]0\}} | D])}|d\}}}|dd}|tjvrct|}t||}t ||t ||q:q4dS)z Mark tests in Django's test suite which are expected failures on this database and test which should be skipped on this database. .rN)rfeaturesdjango_test_expected_failures rpartitionsplitrINSTALLED_APPSr getattrsetattrrdjango_test_skipsitemsr) r test_nametest_case_name_test_method_nametest_app test_case test_methodreasontestsrrrr@-s&    z5BaseDatabaseCreation.mark_expected_failures_and_skipscCsdS)zQ SQL to append to the end of the test table creation statements. rqrrrrrr}Dsz.BaseDatabaseCreation.sql_table_creation_suffixcCs$|jj}|d|d|d|fS)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. HOSTPORTENGINE)rr7r1)rr7rrrtest_db_signatureJs z&BaseDatabaseCreation.test_db_signature)rFTF)F)rFF)NrFN)__name__ __module__ __qualname____doc__rrrrGrIr;ror2r1rwr3rrrrrr@r}rrrrrr s* F  )   r )rriorunittestrr django.appsr django.confr django.corer django.dbrdjango.db.transactionr django.utils.module_loadingr rsr rrrrs