o
    #.h                     @   s  d dl Z d dlZd dlZd dlZd dlZd dlZd dlZd dlZd dlZd dl	Z	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mZ d dlmZ d dlZd dl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! 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- zd dl.Z/W n e0y   d dl/Z/Y nw zd dl1Z2W n e0y   dZ2Y nw G dd dej3Z4G dd dej3Z5G dd dZ6G dd dej7Z8G dd dZ9dd Z:dd Z;d a<					d>dd Z=d!d" Z>d#d$ Z?G d%d& d&ej@ZAG d'd( d(ZBG d)d* d*ZCd+d, ZDd-d. ZEd/d0 ZFd1d2 ZGd?d4d5ZHd@d6d7ZId8d9 ZJd:d; ZKd<d= ZLdS )A    N)defaultdict)contextmanager)import_module)StringIO)call_command)connections)SimpleTestCaseTestCase)NullTimeKeeper
TimeKeeperiter_test_cases)setup_databases)setup_test_environment)teardown_databases)teardown_test_environment)new_hash)
OrderedSet)RemovedInDjango50Warningc                       s`   e Zd Z fddZ fddZ fddZ fddZ fd	d
Z fddZdd Z	  Z
S )DebugSQLTextTestResultc                    s4   t d| _| jt j d | _t ||| d S )Nzdjango.db.backends)logging	getLoggerloggersetLevelDEBUGdebug_sql_streamsuper__init__)selfstreamdescriptions	verbosity	__class__ Q/var/www/supernova/superenvnew/lib/python3.10/site-packages/django/test/runner.pyr   /   s   zDebugSQLTextTestResult.__init__c                    s4   t  | _t| j| _| j| j t | d S N)	r   r   r   StreamHandlerhandlerr   
addHandlerr   	startTestr   testr!   r#   r$   r)   5   s   z DebugSQLTextTestResult.startTestc                    sT   t  | | j| j | jr(| jd | j	| j
  | j| j d S d S )Nr   )r   stopTestr   removeHandlerr'   showAllr   seekr   writereadwriteln
separator2r*   r!   r#   r$   r,   ;   s   zDebugSQLTextTestResult.stopTestc                    sN   t  || | jd u rd}n| jd | j }| jd |f | jd< d S )N r   )r   addErrorr   r/   r1   errors)r   r+   errsqlr!   r#   r$   r6   C   s   

zDebugSQLTextTestResult.addErrorc                    s:   t  || | jd | jd | j f | jd< d S Nr   r5   )r   
addFailurer   r/   failuresr1   r   r+   r8   r!   r#   r$   r;   M   s    z!DebugSQLTextTestResult.addFailurec                    s`   t  ||| |d ur.| jd t|d |jr| jn| j}|d | j f |d< d S d S r:   )	r   
addSubTestr   r/   
issubclassfailureExceptionr<   r7   r1   )r   r+   subtestr8   r7   r!   r#   r$   r>   R   s   z!DebugSQLTextTestResult.addSubTestc                 C   s|   |D ]9\}}}| j | j | j d|| |f  | j | j | j | | j | j | j tj|ddd qd S )Nz%s: %sTupper)reindentkeyword_case)r   r2   
separator1getDescriptionr3   sqlparseformat)r   flavourr7   r+   r8   	sql_debugr#   r#   r$   printErrorList]   s   z%DebugSQLTextTestResult.printErrorList)__name__
__module____qualname__r   r)   r,   r6   r;   r>   rK   __classcell__r#   r#   r!   r$   r   .   s    
r   c                       s@   e Zd ZdZ fddZ fddZ fddZdd	 Z  ZS )
PDBDebugResultz^
    Custom result class that triggers a PDB session when an error or failure
    occurs.
    c                       t  || | | d S r%   )r   r6   debugr=   r!   r#   r$   r6   o      zPDBDebugResult.addErrorc                    rQ   r%   )r   r;   rR   r=   r!   r#   r$   r;   s   rS   zPDBDebugResult.addFailurec                    s&   |d ur	|  | t ||| d S r%   )rR   r   r>   r   r+   rA   r8   r!   r#   r$   r>   w   s   
zPDBDebugResult.addSubTestc                 C   s2   |    d| _|\}}}td|  t| d S )NFz
Opening PDB: %r)_restoreStdoutbufferprintpdbpost_mortem)r   errorexc_type	exc_value	tracebackr#   r#   r$   rR   |   s
   
zPDBDebugResult.debug)	rL   rM   rN   __doc__r6   r;   r>   rR   rO   r#   r#   r!   r$   rP   i   s    rP   c                   @   s   e Zd ZdZdZdd ZdS )	DummyListzP
    Dummy list class for faking storage of results in unittest.TestResult.
    r#   c                 C   s   d S r%   r#   )r   itemr#   r#   r$   append   s   zDummyList.appendN)rL   rM   rN   r^   	__slots__ra   r#   r#   r#   r$   r_      s    r_   c                       s   e Zd ZdZ fddZdd Zedd Zdd	 Zd
d Z	dd Z
dd Z fddZ fddZ fddZ fddZ fddZ fddZ fddZ fddZ fd d!Z fd"d#Z fd$d%Zd&d' Zd(d) Z  ZS )*RemoteTestResultz
    Extend unittest.TestResult to record events in the child processes so they
    can be replayed in the parent process. Events include things like which
    tests succeeded or failed.
    c                    sR   t  j|i | t }|| _|| _|| _|| _|| _td ur$tj	
  g | _d S r%   )r   r   r_   r<   r7   skippedexpectedFailuresunexpectedSuccessestblibpickling_supportinstallevents)r   argskwargs
dummy_listr!   r#   r$   r      s   

zRemoteTestResult.__init__c                 C   s>   | j  }|dd  |dd  |dd  |dd  |S )N_stdout_buffer_stderr_buffer_original_stdout_original_stderr)__dict__copypop)r   stater#   r#   r$   __getstate__   s   
zRemoteTestResult.__getstate__c                 C   s
   | j d S )N   )testsRunr   r#   r#   r$   
test_index   s   
zRemoteTestResult.test_indexc                 C   s   t t | dS )z
        Confirm that obj can be pickled and unpickled as multiprocessing will
        need to pickle the exception in the child process and unpickle it in
        the parent process. Let the exception rise, if not.
        N)pickleloadsdumps)r   objr#   r#   r$   _confirm_picklable   s   z#RemoteTestResult._confirm_picklablec                 C   s   t d||| d S )Na'  
Subtest failed:

    test: {}
 subtest: {}

Unfortunately, the subtest that failed cannot be pickled, so the parallel
test runner cannot handle it cleanly. Here is the pickling error:

> {}

You should re-run this test with --parallel=1 to reproduce the failure
with a cleaner failure message.
)rW   rH   )r   r+   rA   
pickle_excr#   r#   r$   _print_unpicklable_subtest   s   z+RemoteTestResult._print_unpicklable_subtestc              
   C   s   z|  | W d S  tyG } z3t|d }tj|dddd}t|}tj|dddd}td u r9td||  td|||  d }~ww )Nrw   K   z    )initial_indentsubsequent_indentz

{} failed:

{}

Unfortunately, tracebacks cannot be pickled, making it impossible for the
parallel test runner to handle this exception cleanly.

In order to see the traceback, you should install tblib:

    python -m pip install tblib
aL  

{} failed:

{}

Unfortunately, the exception it raised cannot be pickled, making it impossible
for the parallel test runner to handle it cleanly.

Here's the error encountered while trying to pickle the exception:

{}

You should re-run this test with the --parallel=1 option to reproduce the
failure and get a correct traceback.
)r   	Exceptionreprtextwrapfillrg   rW   rH   )r   r+   r8   excoriginal_exc_txtpickle_exc_txtr#   r#   r$   check_picklable   s8   &z RemoteTestResult.check_picklablec              
   C   s:   z|  | W d S  ty } z| |||  d }~ww r%   )r   r   r   )r   r+   rA   r   r#   r#   r$   check_subtest_picklable  s   z(RemoteTestResult.check_subtest_picklablec                       t    | jd d S )N)startTestRun)r   r   rj   ra   ry   r!   r#   r$   r        
zRemoteTestResult.startTestRunc                    r   )N)stopTestRun)r   r   rj   ra   ry   r!   r#   r$   r     r   zRemoteTestResult.stopTestRunc                    "   t  | | jd| jf d S )Nr)   )r   r)   rj   ra   rz   r*   r!   r#   r$   r)        zRemoteTestResult.startTestc                    r   )Nr,   )r   r,   rj   ra   rz   r*   r!   r#   r$   r,     r   zRemoteTestResult.stopTestc                    2   |  || | jd| j|f t || d S )Nr6   )r   rj   ra   rz   r   r6   r=   r!   r#   r$   r6   "     zRemoteTestResult.addErrorc                    r   )Nr;   )r   rj   ra   rz   r   r;   r=   r!   r#   r$   r;   '  r   zRemoteTestResult.addFailurec                    sJ   |d ur|  || | || | jd| j||f t ||| d S )Nr>   )r   r   rj   ra   rz   r   r>   rT   r!   r#   r$   r>   ,  s
   zRemoteTestResult.addSubTestc                    "   | j d| jf t | d S )N
addSuccess)rj   ra   rz   r   r   r*   r!   r#   r$   r   7     zRemoteTestResult.addSuccessc                    s&   | j d| j|f t || d S )NaddSkip)rj   ra   rz   r   r   )r   r+   reasonr!   r#   r$   r   ;  s   zRemoteTestResult.addSkipc                    sL   t d u r|d |d d f}| || | jd| j|f t || d S )Nr   rw   addExpectedFailure)rg   r   rj   ra   rz   r   r   r=   r!   r#   r$   r   ?  s
   z#RemoteTestResult.addExpectedFailurec                    r   )NaddUnexpectedSuccess)rj   ra   rz   r   r   r*   r!   r#   r$   r   J  r   z%RemoteTestResult.addUnexpectedSuccessc                    s    h d t  fdd| jD S )z/Tells whether or not this result was a success.>   r6   r;   r>   r   c                 3   s    | ]	}|d   vV  qdS )r   Nr#   ).0efailure_typesr#   r$   	<genexpr>Q  s    z1RemoteTestResult.wasSuccessful.<locals>.<genexpr>)allrj   ry   r#   r   r$   wasSuccessfulN  s   zRemoteTestResult.wasSuccessfulc                 C      dS )Nr4   r#   )r   r8   r+   r#   r#   r$   _exc_info_to_stringS  s   z$RemoteTestResult._exc_info_to_string)rL   rM   rN   r^   r   rv   propertyrz   r   r   r   r   r   r   r)   r,   r6   r;   r>   r   r   r   r   r   r   rO   r#   r#   r!   r$   rc      s.    
;rc   c                   @   s&   e Zd ZdZeZdddZdd ZdS )	RemoteTestRunnerz
    Run tests and record everything but don't display anything.

    The implementation matches the unpythonic coding style of unittest2.
    FNc                 C   s"   || _ || _|d ur|| _d S d S r%   )failfastrV   resultclass)r   r   r   rV   r#   r#   r$   r   c  s
   
zRemoteTestRunner.__init__c                 C   s.   |   }t| | j|_| j|_|| |S r%   )r   unittestregisterResultr   rV   )r   r+   resultr#   r#   r$   runi  s   
zRemoteTestRunner.run)FNF)rL   rM   rN   r^   rc   r   r   r   r#   r#   r#   r$   r   Z  s
    
r   c                   C   s<   t  dvrdS zttjd W S  ty   t   Y S w )zP
    The maximum number of test processes when using the --parallel option.
    >   forkspawnrw   DJANGO_TEST_PROCESSES)multiprocessingget_start_methodintosenvironKeyError	cpu_countr#   r#   r#   r$   get_max_test_processesr  s   r   c                 C   s6   | dkr| S zt | W S  ty   t| dw )z,Parse value passed to the --parallel option.autoz' is not an integer or the string 'auto')r   
ValueErrorargparseArgumentTypeError)valuer#   r#   r$   parallel_type  s   
r   c           
      C   s   |    |  jd7  _| jaW d   n1 sw   Y  t }|dkr?|r6t|r6|du r2d}||  t  t|d t	D ]"}t	| }|dkr]|j
||  || }	r]|	|_|jt qAdS )z
    Switch to databases dedicated to this worker.

    This helper lives at module-level because of the multiprocessing module's
    requirements.
    rw   Nr   r#   rR   )get_lockr   
_worker_idr   r   callabledjangosetupr   r   settings_dictupdateget_test_serialized_contentscreationsetup_worker_connection)
counterinitial_settingsserialized_contentsprocess_setupprocess_setup_args
debug_modestart_methodalias
connectionr   r#   r#   r$   _init_worker  s(   

r   c                 C   s.   | \}}}}}|||d}| |}||jfS )z
    Run a suite of tests with a RemoteTestRunner and return a RemoteTestResult.

    This helper lives at module-level and its arguments are wrapped in a tuple
    because of the multiprocessing module's requirements.
    )r   rV   )r   rj   )rk   runner_classsubsuite_indexsubsuiter   rV   runnerr   r#   r#   r$   _run_subsuite  s   

r   c                  G   r   )z-Stub method to simplify run() implementation.Nr#   )rk   r#   r#   r$   _process_setup_stub  s   r   c                       sP   e Zd ZdZeZeZdZe	Z
eZ	d fdd	Zdd Zdd	 Zd
d Z  ZS )ParallelTestSuitea  
    Run a series of tests in parallel in several processes.

    While the unittest module's documentation implies that orchestrating the
    execution of tests is the responsibility of the test runner, in practice,
    it appears that TestRunner classes are more concerned with formatting and
    displaying test results.

    Since there are fewer use cases for customizing TestSuite than TestRunner,
    implementing parallelization at the level of the TestSuite improves
    interoperability with existing custom test runners. A single instance of a
    test runner can still collect results from all tests without being aware
    that they have been run in parallel.
    r#   Fc                    s8   || _ || _|| _|| _|| _d | _d | _t   d S r%   )		subsuites	processesr   r   rV   r   r   r   r   )r   r   r   r   r   rV   r!   r#   r$   r     s   zParallelTestSuite.__init__c              	      s,      ttjd}tj j jj| j	 j
 jj j jgd} fddt jD }| jj|}	 |jr?|  nQz
|jdd\}}W n tjyS   Y q6 ty_   |  Y n1w t j| }|D ]%}	|	d }
t||
d}|du rzqi||	d	  }|	d
d }||g|R   qiq7|  |S )a  
        Distribute test cases across workers.

        Return an identifier of each test case with its result in order to use
        imap_unordered to show results as soon as they're available.

        To minimize pickling errors when getting results from workers:

        - pass back numeric indexes in self.subsuites instead of tests
        - make tracebacks picklable with tblib, if available

        Even with tblib, errors may still occur for dynamically created
        exception classes which cannot be unpickled.
        r   )r   initializerinitargsc                    s$   g | ]\}} j || j jfqS r#   )r   r   rV   )r   indexr   ry   r#   r$   
<listcomp>
  s    z)ParallelTestSuite.run.<locals>.<listcomp>Tg?)timeoutNrw      )initialize_suiter   Valuectypesc_intPoolr   init_worker__func__r   r   r   r   r   	enumerater   imap_unorderedrun_subsuite
shouldStop	terminatenextTimeoutErrorStopIterationcloselistgetattrjoin)r   r   r   poolrk   test_resultsr   rj   testsevent
event_namer'   r+   r#   ry   r$   r     sR   
zParallelTestSuite.runc                 C   s
   t | jS r%   )iterr   ry   r#   r#   r$   __iter__+  s   
zParallelTestSuite.__iter__c                    s8   t  dkrdd tD  _ fddtD  _d S d S )Nr   c                 S   s   i | ]}|t | jqS r#   )r   r   r   r   r#   r#   r$   
<dictcomp>0  s    z6ParallelTestSuite.initialize_suite.<locals>.<dictcomp>c                    s"   i | ]}| j v r|t| jqS r#   )serialized_aliasesr   r   r   ry   r#   r$   r   3  s
    

)r   r   r   r   r   ry   r#   ry   r$   r   .  s   
z"ParallelTestSuite.initialize_suite)FFF)rL   rM   rN   r^   r   r   r   r   r   r   r   r   r   r   r   r   r   rO   r#   r#   r!   r$   r     s    >r   c                   @   sF   e Zd ZdZdZedd ZdddZedd	 Z	d
d Z
dd ZdS )Shufflera  
    This class implements shuffling with a special consistency property.
    Consistency means that, for a given seed and key function, if two sets of
    items are shuffled, the resulting order will agree on the intersection of
    the two sets. For example, if items are removed from an original set, the
    shuffled order for the new set will be the shuffled order of the original
    set restricted to the smaller set.
    md5c                 C   s&   t | jdd}||d | S )NF)usedforsecurityzutf-8)r   hash_algorithmr   encode	hexdigest)clstexthr#   r#   r$   
_hash_textG  s   zShuffler._hash_textNc                 C   s.   |d u rt dd}d}nd}|| _|| _d S )Nr   l   c(	 	generatedgiven)randomrandintseedseed_source)r   r  r  r#   r#   r$   r   M  s   
zShuffler.__init__c                 C   s   | j d| j dS )Nz ())r  r  ry   r#   r#   r$   seed_displayW     zShuffler.seed_displayc                 C   s   d | j||}| |S )Nz{}{})rH   r  r
  )r   r`   keyr  r#   r#   r$   
_hash_item[  s   
zShuffler._hash_itemc                    sZ   i  |D ]}|  ||}| v rd|| | }t|| |< q fddt D S )ab  
        Return a new list of the items in a shuffled order.

        The `key` is a function that accepts an item in `items` and returns
        a string unique for that item that can be viewed as a string id. The
        order of the return value is deterministic. It depends on the seed
        and key function but not on the original order.
        z)item {!r} has same hash {!r} as item {!r}c                    s   g | ]} | qS r#   r#   )r   hashedhashesr#   r$   r   s      z$Shuffler.shuffle.<locals>.<listcomp>)r  rH   RuntimeErrorsorted)r   itemsr  r`   r  msgr#   r  r$   shuffle_  s   	
zShuffler.shuffler%   )rL   rM   rN   r^   r  classmethodr
  r   r   r  r  r  r#   r#   r#   r$   r  :  s    




r  c                   @   s  e Zd ZdZejZeZej	Z
ejZeefZ																			d/ddZed	d
 Zedd Zd0ddZdd Zdd Zedd Zdd Zd1d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%d0d-d.Z&dS )2DiscoverRunnerz8A Django test runner that uses unittest2 test discovery.Nrw   TFr   c              	   K   s  || _ || _|| _|| _|| _|| _|| _|| _|	| _|
| _	t
|p"g | _t
|p)g | _t sS|rSztjtj d W n ttjfyR   tjtj d Y nw || _| jrb| j	dkrbtd|| _d | _|rmt nt | _|r{dd |D | _|| _d | _|| _ d S )N)filerw   zFYou cannot use --pdb with parallel tests; pass --parallel=1 to use it.c                 S   s    h | ]}d |v r
|nd| qS )*z*%s*r#   )r   patternr#   r#   r$   	<setcomp>  s    z*DiscoverRunner.__init__.<locals>.<setcomp>)!r#  	top_levelr    interactiver   keepdbreverser   	debug_sqlparallelsettagsexclude_tagsfaulthandler
is_enabledenablesysstderrfilenoAttributeErrorioUnsupportedOperation
__stderr__rX   r   rV   test_name_patternsr   r
   time_keeperr  	_shufflerr   )r   r#  r%  r    r&  r   r'  r(  r   r)  r*  r,  r-  r8  rX   rV   enable_faulthandlertimingr  r   rl   r#   r#   r$   r     sB   
zDiscoverRunner.__init__c              	   C   s  |j ddddd |j dddd	d
 |j dddd |j dddtddd |j ddddd |j dddd |j ddddd |j ddddtd d!d" |j d#d$d%d&d' |j d(d$d)d*d' |j d+dd,d |j d-d.dd/d |j d0d1d2d3d' |j d4dd5d |j d6d$d7d8d' d S )9Nz-tz--top-level-directoryr%  z,Top level of project for unittest discovery.)desthelpz-pz	--patternztest*.pyz0The test matching pattern. Defaults to test*.py.)defaultr>  z--keepdb
store_truez#Preserves the test DB between runs.)actionr>  z	--shuffle?FSEEDzShuffles test case order.)nargsr?  typemetavarr>  z-rz	--reversezReverses test case order.z--debug-modezSets settings.DEBUG to True.z-dz--debug-sqlz%Prints logged SQL queries on failure.z
--parallelr   r   NzqRun tests using up to N parallel processes. Use the value "auto" to run one test process for each processor core.)rD  constr?  rE  rF  r>  z--tagra   r,  zBRun only tests with the specified tag. Can be used multiple times.)rA  r=  r>  z--exclude-tagr-  zDDo not run tests with the specified tag. Can be used multiple times.z--pdbz@Runs a debugger (pdb, or ipdb if installed) on error or failure.z-bz--bufferz"Discard output from passing tests.z--no-faulthandlerstore_falser;  z5Disables the Python faulthandler module during tests.z--timingz=Output timings, including database set up and total run time.z-kr8  z~Only run test methods and classes that match the pattern or substring. Can be used multiple times. Same as unittest -k option.)add_argumentr   r   )r  parserr#   r#   r$   add_arguments  s   
zDiscoverRunner.add_argumentsc                 C   s   | j d u rd S | j jS r%   )r:  r  ry   r#   r#   r$   shuffle_seed  s   
zDiscoverRunner.shuffle_seedc                 C   sX   |du rt j}| jdu r#| jdks| jdkr|t jk rdS t| dS | j|| dS )a\  
        Log the message at the given logging level (the default is INFO).

        If a logger isn't set, the message is instead printed to the console,
        respecting the configured verbosity. A verbosity of 0 prints no output,
        a verbosity of 1 prints INFO and above, and a verbosity of 2 or higher
        prints all levels.
        Nr   rw   )r   INFOr   r    rW   log)r   r  levelr#   r#   r$   rO  #  s   	
zDiscoverRunner.logc                 K   s   t | jd t  d S )Nr   )r   r   r   installHandlerr   rl   r#   r#   r$   r   5  s   z%DiscoverRunner.setup_test_environmentc                 C   s6   | j du rd S t| j d}| d|j  || _d S )NF)r  zUsing shuffle seed: )r  r  rO  r  r:  )r   shufflerr#   r#   r$   setup_shuffler9  s
   

zDiscoverRunner.setup_shufflerc                 c   s4    | j j}| j| j _z
d V  W || j _d S || j _w r%   )test_loadertestNamePatternsr8  )r   original_test_name_patternsr#   r#   r$   load_with_patterns@  s   
z!DiscoverRunner.load_with_patternsc                 C   s  t j|}d }t j|s.|   | j|}W d    n1 s#w   Y  | r.|S t|\}}|r;|s:|S nt j	|sWt j|rU|d u sMJ t
d|d|S | }t j	|rj| jsjt||d< |   | jjdd|i|}W d    n1 sw   Y  d | j_|S )Nz,One of the test labels is a path to a file: zR, which is not supported. Use a dotted module name or path to a directory instead.top_level_dir	start_dirr#   )r   pathabspathexistsrX  rU  loadTestsFromNamecountTestCasestry_importingisdirr  rs   r%  find_top_leveldiscover_top_level_dir)r   labeldiscover_kwargslabel_as_pathr   is_importable
is_packagerl   r#   r#   r$   load_tests_for_labelJ  s8   


z#DiscoverRunner.load_tests_for_labelc                 K   s  |d urt jdtdd |pdg}|pg }i }| jd ur!| j|d< | jd ur+| j|d< |   g }|D ]}| ||}|t| q3|t| | j	sP| j
r~| j	rc| jddt| j	 tjd	 | j
rv| jd
dt| j
 tjd	 t|| j	| j
}tjjg| jR }tt||| j| jd}| dt|  | |}	| jdkrt|	}
t| jt|
}|| _|dkr| |
|| j| j | j!}	|	S )N'The extra_tests argument is deprecated.r   
stacklevel.r#  rY  zIncluding test tag(s): %s., rP  zExcluding test tag(s): %s.rS  r(  zFound %d test(s).rw   )"warningswarnr   r#  r%  rT  rj  extendr   r,  r-  rO  r   r  r   r   filter_tests_by_tagsr   loader_FailedTest
reorder_byr   reorder_testsr:  r(  len
test_suiter*  partition_suite_by_caseminparallel_test_suiter   r   rV   )r   test_labelsextra_testsrl   rf  	all_testsre  r   
test_typessuiter   r   r#   r#   r$   build_suiteo  sn   






zDiscoverRunner.build_suitec                 K   s(   t | j| jf| j| j| j| jd|S )N)r9  r'  r)  r*  )_setup_databasesr    r&  r9  r'  r)  r*  rR  r#   r#   r$   r     s   zDiscoverRunner.setup_databasesc                 C   s   | j rtS | jr
tS d S r%   )r)  r   rX   rP   ry   r#   r#   r$   get_resultclass  s
   zDiscoverRunner.get_resultclassc                 C   s   | j |  | j| jdS )N)r   r   r    rV   )r   r  r    rV   ry   r#   r#   r$   get_test_runner_kwargs  s
   z%DiscoverRunner.get_test_runner_kwargsc                 C   s   t d| j|d d S )Ncheck)r    	databases)r   r    )r   r  r#   r#   r$   
run_checks  s   zDiscoverRunner.run_checksc              
   K   sp   |   }| jdi |}z||W | jd ur$| jj}| d|  S S | jd ur7| jj}| d|  w w )NzUsed shuffle seed: r#   )r  test_runnerr   r:  r  rO  )r   r  rl   r   r  r#   r#   r$   	run_suite  s   

zDiscoverRunner.run_suitec                 K   s   t || j| j| jd dS )z%Destroy all the non-mirror databases.)r    r*  r'  N)_teardown_databasesr    r*  r'  )r   
old_configrl   r#   r#   r$   r     s   
z!DiscoverRunner.teardown_databasesc                 K   s   t   t  d S r%   )r   r-   r   rR  r#   r#   r$   r     s   
z(DiscoverRunner.teardown_test_environmentc                 K   s   t |jt |j t |j S r%   )rz  r<   r7   rf   )r   r  r   rl   r#   r#   r$   suite_result  s   zDiscoverRunner.suite_resultc                    sX   i  t |D ]#}t|dd }|dkrt}|r)t|dd  fdd|D  q S )Nr  __all__serialized_rollbackFc                 3   s$    | ]}|p  |d fV  qdS FN)r   r   r  r  r#   r$   r     s
    
z0DiscoverRunner._get_databases.<locals>.<genexpr>)r   r   r   r   )r   r  r+   test_databasesr#   r  r$   _get_databases  s   zDiscoverRunner._get_databasesc                    sB   |  |  fddtD }|r| jddt| tjd  S )Nc                    s   g | ]}| vr|qS r#   r#   r   r  r#   r$   r         z0DiscoverRunner.get_databases.<locals>.<listcomp>z)Skipping setup of unused database(s): %s.ro  rp  )r  r   rO  r   r  r   r   )r   r  unused_databasesr#   r  r$   get_databases  s   
zDiscoverRunner.get_databasesc           	      K   s  |durt jdtdd |   | ||}| |}tdd | D |_| j	
d | j||jd}W d   n1 s@w   Y  d	}zFz| | | |}W n
 ty^   d
} w W z!| j	
d | | W d   n1 sww   Y  |   W n: ty   |s Y n/w z!| j	
d | | W d   n1 sw   Y  |   W w  ty   |s Y w w | j	  | ||S )z
        Run the unit tests for all the test labels in the provided list.

        Test labels should be dotted Python paths to test modules, test
        classes, or test methods.

        Return the number of tests that failed.
        Nrk  r   rl  c                 s   s    | ]	\}}|r|V  qd S r%   r#   )r   r   	serializer#   r#   r$   r     s    
z+DiscoverRunner.run_tests.<locals>.<genexpr>zTotal database setup)aliasesr   FTzTotal database teardown)rr  rs  r   r   r  r  r+  r  r   r9  timedr   r  r  r   r   r   print_resultsr  )	r   r  r  rl   r  r  r  
run_failedr   r#   r#   r$   	run_tests  sb   	



zDiscoverRunner.run_tests)NNrw   TFFFFFr   NNNFFTFFNr%   )NN)'rL   rM   rN   r^   r   	TestSuiter{  r   r~  TextTestRunnerr  defaultTestLoaderrU  r	   r   rx  r   r  rL  r   rM  rO  r   rT  r   rX  rj  r  r   r  r  r  r  r   r   r  r  r  r  r#   r#   r#   r$   r   v  sb    
;
b


	
%F
	r   c              	   C   s4   zt | }W n ttfy   Y dS w dt|dfS )z
    Try importing a test label, and return (is_importable, is_package).

    Relative labels like "." and ".." are seen as directories.
    )FFT__path__)r   ImportError	TypeErrorhasattr)re  modr#   r#   r$   r`  7  s   r`  c                 C   sB   	 t j| d}t j|s	 | S t j| }|| kr	 | S |} q)NTz__init__.py)r   r[  r   r]  dirname)r%  init_pytry_nextr#   r#   r$   rb  E  s   rb  c                 C   s   | j  d| j S )Nrn  )rM   rN   )r  r#   r#   r$   _class_shuffle_key]  s   r  c                    sn   i  t | tD ]\}}t|}t|d }|j|dd d}| |< q|j td}t j fdd|D  S )z
    Return an iterator over the given tests in a shuffled order, keeping tests
    next to other tests of their class.

    `tests` should be an iterable of tests.
    r   c                 S   s   |   S r%   )id)r+   r#   r#   r$   <lambda>l  s    zshuffle_tests.<locals>.<lambda>)r  c                 3   s    | ]} | V  qd S r%   r#   )r   r  tests_by_typer#   r$   r   q  s    z shuffle_tests.<locals>.<genexpr>)	itertoolsgroupbyrE  r   r  r  chain)r   rS  _class_tests	test_typeclassesr#   r  r$   shuffle_testsa  s   
r  Fc                 C   s:   |du r|r
t | S t| S t| |} |s| S t t| S )z
    Return an iterator that reorders the given tests, keeping tests next to
    other tests of their class.

    `tests` should be an iterable of tests that supports reversed().
    N)reversedr   r  r   )r   rS  r(  r#   r#   r$   reorder_test_bint  s   
r  c           
      c   s    dd t t|d D }|^ }}| D ]}t||D ]\}}	t||	r' nq|}|t| | q|D ]}ttj	|
 } t| ||dE dH  q6dS )a~  
    Reorder an iterable of tests, grouping by the given TestCase classes.

    This function also removes any duplicates and reorders so that tests of the
    same type are consecutive.

    The result is returned as an iterator. `classes` is a sequence of types.
    Tests that are instances of `classes[0]` are grouped first, followed by
    instances of `classes[1]`, etc. Tests that are not instances of any of the
    classes are grouped last.

    If `reverse` is True, the tests within each `classes` group are reversed,
    but without reversing the order of `classes` itself.

    The `shuffler` argument is an optional instance of this module's `Shuffler`
    class. If provided, tests will be shuffled within each `classes` group, but
    keeping tests with other tests of their TestCase class. Reversing is
    applied after shuffling to allow reversing the same random order.
    c                 S   s   g | ]}t tqS r#   )r   r   )r   ir#   r#   r$   r     r  z!reorder_tests.<locals>.<listcomp>rw   rq  N)rangerz  zip
isinstancerE  addr   r  r  from_iterablevaluesr  )
r   r  r(  rS  bins
class_binslast_binr+   test_bin
test_classr#   r#   r$   ry    s   

ry  c                    s*   t |  t| } fddt|t D S )zCPartition a test suite by test case, preserving the order of tests.c                    s   g | ]\}} |qS r#   r#   )r   r  r   suite_classr#   r$   r     r  z+partition_suite_by_case.<locals>.<listcomp>)rE  r   r  r  )r  r  r#   r  r$   r|    s   r|  c                 C   s|   t | tjjr	dS tt| dg }t| dt| }t| |r0t| |}tt|dg }|	|}|r9|
|r9dS |
|S )NTr,  _testMethodNameF)r  r   rv  rw  r+  r   strr  r   union
isdisjoint)r+   r,  r-  	test_tagstest_fn_nametest_fntest_fn_tagsr#   r#   r$   test_match_tags  s   



r  c                    s    fdd| D S )z)Return the matching tests as an iterator.c                 3   s     | ]}t | r|V  qd S r%   )r  )r   r+   r-  r,  r#   r$   r     s    z'filter_tests_by_tags.<locals>.<genexpr>r#   )r   r,  r-  r#   r  r$   ru    r  ru  )NNNNN)NFr  )Mr   r   r.  r5  r  r   r   r   r{   r  r1  r   r   rr  collectionsr   
contextlibr   	importlibr   r   rG   r   django.core.managementr   	django.dbr   django.testr   r	   django.test.utilsr
   r   r   r   r  r   r   r  r   django.utils.cryptor   django.utils.datastructuresr   django.utils.deprecationr   ipdbrX   r  tblib.pickling_supportrg   TextTestResultr   rP   r_   
TestResultrc   r   r   r   r   r   r   r   r  r   r  r   r`  rb  r  r  r  ry  r|  r  ru  r#   r#   r#   r$   <module>   s    ; L
)p<   D

'