
    8 f5P                    *   d Z ddlmZ ddlZddlmZ ddlmZ ddlm	Z	m
Z
mZmZmZmZmZmZmZmZmZmZmZ ddlZddlmZ dd	lmZ dd
lmZ e	rddlZddlZddlmZ eeed         ee          f                  Z!eee                   Z"ee#e#e!e#e"f         Z$g dZ% ej&        d          d^d            Z' ej&        d          d_d            Z( ej&        d          d`d            Z) ej&        d          dad            Z* ej&        d          dbd            Z+ ej&        d          dbd            Z, ej&        d          dbd            Z- ej&        d          dcd'            Z. ej&                    ddd)            Z/ded+Z0d^d,Z1dfd/Z2 ej&                    dgd1            Z3 ej&                    dgd2            Z4 ej&                    dhd4            Z5 ej&                    did6            Z6 ej&                    djd8            Z7 ej&                    dkd:            Z8 ej&                    dld;            Z9 ej&                    dmd<            Z: ej&                    dnd>            Z; ej&                    dod?            Z< ej&                    dpdA            Z= ej&                    dqdC            Z> G dD dE          Z? ej&                    dF             Z@ ej&        d          drdG            ZA ej&        dHI          dsdJ            ZB G dK dLe          ZCe	 	 	 dtdudW            ZD ej&                    dvdY            ZE ej&                    dvdZ            ZF G d[ d\e          ZG ej&                    dwd]            ZHdS )xzAll pytest-django fixtures    )annotationsN)contextmanager)partial)TYPE_CHECKINGAnyCallableClassVarContextManager	GeneratorIterableListLiteralOptionalProtocolTupleUnion   )live_server_helper)is_django_unittestskip_if_no_django)DjangoDbBlocker__all__)django_db_setupdbtransactional_dbdjango_db_reset_sequencesdjango_db_serialized_rollback
admin_userdjango_user_modeldjango_username_fieldclientasync_clientadmin_clientrfasync_rfsettingslive_server_live_server_helperdjango_assert_num_queriesdjango_assert_max_num_queries"django_capture_on_commit_callbackssession)scopereturnNonec                 r    t                       t          j        d          } | rt          |            d S d S )NTOX_PARALLEL_ENVsuffix)r   osgetenv_set_suffix_to_test_databases)tox_environments    L/var/www/equiseq/venv/lib/python3.11/site-packages/pytest_django/fixtures.py'django_db_modify_db_settings_tox_suffixr:   B   sG    i 233O >%_======> >    requestpytest.FixtureRequestc                    t                       t          | j        di                               d          }|rt	          |           d S d S )Nworkerinputworkeridr3   )r   getattrconfiggetr7   )r<   xdist_suffixs     r9   )django_db_modify_db_settings_xdist_suffixrE   L   sX    7>="==AA*MML ;%\::::::; ;r;   r:   rE   c                "    t                       d S Nr   )r:   rE   s     r9   ,django_db_modify_db_settings_parallel_suffixrH   V   s    
 r;   rH   c                "    t                       d S rG   r   )rH   s    r9   django_db_modify_db_settingsrJ   ^   s     r;   boolc                8    | j                             d           S )NnomigrationsrB   getvalue)r<   s    r9   django_db_use_migrationsrP   e   s    ~&&~6666r;   c                :    | j                             d          }|S )Nreuse_dbrN   )r<   rR   s     r9   django_db_keepdbrS   j   s    ^,,Z88HOr;   c                :    | j                             d          }|S )N	create_dbrN   )r<   rU   s     r9   django_db_createdbrV   p   s    n--k::Ir;   django_test_environmentdjango_db_blockerr   rP   rS   rV   rJ   Generator[None, None, None]c              #    K   ddl m}m} i }	|st                       |r|sd|	d<   |                                5   |d
| j        j        j        dd|	}
ddd           n# 1 swxY w Y   dV  |s|                                5  	  ||
| j        j        j                   nF# t          $ r9}| j	        
                    t          j        d	|                     Y d}~nd}~ww xY wddd           dS # 1 swxY w Y   dS dS )z8Top level fixture to ensure test databases are availabler   )setup_databasesteardown_databasesTkeepdbF)	verbosityinteractiveN)r^   z.Error when trying to teardown test databases:  )django.test.utilsr[   r\   _disable_migrationsunblockrB   optionverbose	ExceptionnodewarnpytestPytestWarning)r<   rW   rX   rP   rS   rV   rJ   r[   r\   setup_databases_argsdb_cfgexcs               r9   r   r   v   s      FEEEEEEE#  . 2 .)-X&		"	"	$	$ 
 
  
n+3
 
 #
 

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
EEE &&(( 	 	""6W^5J5RSSSSS   !!()aZ])a)abb       	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 sM   A!!A%(A%C8B%$C8%
C(//C#C8#C((C88C<?C<r   c           	   #    K   ddl m t          |           rd V  d S | j                            d          }|rt          |          \  }}}}nd\  }}}}p|pd| j        v pd| j        v |pd| j        v }|pd| j        v }|                                 dd ldd l	rj
        j        }nj
        j        }|||| G fd	d
|          }	|	                                  |	d          }
|
                                 d V  |
                                 |	                                 dk    r|	                                 |                                 d S )Nr   )VERSION	django_dbFFNFNr   r(   r   r   c                  z     e Zd ZZZZZs$ed fd            Zed fd            Z	 xZ
S  xZ
S )/_django_db_helper.<locals>.PytestDjangoTestCaseNr/   r0   c                    t          j        j        |                                            dk     rdj        j        j        _        d S d S )N   r   F)supertestTestCase
setUpClassr   transactionAtomic_ensure_durabilityclsro   	__class__djangos    r9   rz   z:_django_db_helper.<locals>.PytestDjangoTestCase.setUpClass   sM    fk*C00;;===V##FKFI)0CCC $#r;   c                    dk     rdj         j        j        _        t	          j        j        |                                            d S )Nru   T)r   r{   r|   r}   rw   rx   ry   tearDownClassr~   s    r9   r   z=_django_db_helper.<locals>.PytestDjangoTestCase.tearDownClass   sE    V##FJFI)0Cfk*C00>>@@@@@r;   r/   r0   )__name__
__module____qualname__reset_sequencesserialized_rollback	databasesavailable_appsclassmethodrz   r   __classcell__)r   ro   _available_apps
_databases_reset_sequences_serialized_rollbackr   transactionals   @r9   PytestDjangoTestCasers      s        *2!"I&,N  	AL L L L L L L [L
 A A A A A A A [A A A A A	A 	A 	A 	Ar;   r   __init__)
methodName)rv   r   )r   ro   r   rg   get_closest_markervalidate_django_dbfixturenamesrc   	django.dbdjango.testrx   TransactionTestCasery   rz   
_pre_setup_post_teardownr   doClassCleanupsrestore)r<   r   rX   markerr   r   r   r   test_case_classr   	test_casero   r   r   r   r   r   r   s              @@@@@@@r9   _django_db_helperr      sT      '"" \,,[99F , v&&	
N ,	
 	 	a	a'"66_-7K_:_ 
 &^*EI]*]O- '7+??   / +9 +.&.J$O!A !A !A !A !A !A !A !A !A !A !A !A !A !A !A !AF ##%%%$$
;;;I	EEE&&(((&,,...r;   	_DjangoDbc                :    	 	 	 	 	 ddd} || j         i | j        S )ao  Validate the django_db marker.

    It checks the signature and creates the ``transaction``,
    ``reset_sequences``, ``databases``, ``serialized_rollback`` and
    ``available_apps`` attributes on the marker which will have the correct
    values.

    Sequence reset, serialized_rollback, and available_apps are only allowed
    when combined with transaction.
    FNr{   rK   r   r   _DjangoDbDatabasesr   r   _DjangoDbAvailableAppsr/   r   c                    | ||||fS rG   r`   )r{   r   r   r   r   s        r9   apifunz"validate_django_db.<locals>.apifun  s     OY8K^[[r;   rq   )r{   rK   r   rK   r   r   r   rK   r   r   r/   r   )argskwargs)r   r   s     r9   r   r   	  sL     " %(,$)15\ \ \ \ \ 66;0&-000r;   c                     ddl m}  ddlm}  G d d          } |            | _         G d d|j                  }||_        d S )Nr   r'   )migratec                      e Zd Zd	dZd
dZdS )._disable_migrations.<locals>.DisableMigrationsitemstrr/   rK   c                    dS )NTr`   selfr   s     r9   __contains__z;_disable_migrations.<locals>.DisableMigrations.__contains__&      4r;   r0   c                    d S rG   r`   r   s     r9   __getitem__z:_disable_migrations.<locals>.DisableMigrations.__getitem__)  r   r;   N)r   r   r/   rK   )r   r   r/   r0   )r   r   r   r   r   r`   r;   r9   DisableMigrationsr   %  s<        	 	 	 		 	 	 	 	 	r;   r   c                       e Zd Z fdZ xZS )1_disable_migrations.<locals>.MigrateSilentCommandc                @    d|d<    t                      j        |i |S )Nr   r^   )rw   handle)r   r   r   r   s      r9   r   z8_disable_migrations.<locals>.MigrateSilentCommand.handle/  s(    "#F;!577>426222r;   )r   r   r   r   r   )r   s   @r9   MigrateSilentCommandr   .  s8        	3 	3 	3 	3 	3 	3 	3 	3 	3r;   r   )django.confr'   django.core.management.commandsr   MIGRATION_MODULESCommand)r'   r   r   r   s       r9   rb   rb   !  s    $$$$$$777777        "3!2!4!4H3 3 3 3 3w 3 3 3
 +GOOOr;   r4   r   c                *   ddl m} |j                                        D ]r}|                    di                               d          }|s|d         dk    r:d|d          }|dk    rL|                    di            | d	|  |d         d<   sd S )
Nr   r   TESTNAMEENGINEzdjango.db.backends.sqlite3test_z:memory:_)r   r'   	DATABASESvaluesrC   
setdefault)r4   r'   db_settings	test_names       r9   r7   r7   6  s    $$$$$$)0022 > >OOFB//33F;;	 	68$(DDD5F 355I
""vr***)2&=&=V&=&=FF##> >r;   r   c                    dS )a  Require a django test database.

    This database will be setup with the default fixtures and will have
    the transaction management disabled. At the end of the test the outer
    transaction that wraps the test itself will be rolled back to undo any
    changes to the database (in case the backend supports transactions).
    This is more limited than the ``transactional_db`` fixture but
    faster.

    If both ``db`` and ``transactional_db`` are requested,
    ``transactional_db`` takes precedence.
    Nr`   r   s    r9   r   r   K        r;   c                    dS )au  Require a django test database with transaction support.

    This will re-initialise the django database for each test and is
    thus slower than the normal ``db`` fixture.

    If you want to use the database with transactions you must request
    this resource.

    If both ``db`` and ``transactional_db`` are requested,
    ``transactional_db`` takes precedence.
    Nr`   r   s    r9   r   r   \  r   r;   r   c                    dS )aa  Require a transactional test database with sequence reset support.

    This requests the ``transactional_db`` fixture, and additionally
    enforces a reset of all auto increment sequences.  If the enquiring
    test relies on such values (e.g. ids as primary keys), you should
    request this resource to ensure they are consistent across tests.
    Nr`   )r   r   s     r9   r   r   l  r   r;   r   c                    dS )a  Require a test database with serialized rollbacks.

    This requests the ``db`` fixture, and additionally performs rollback
    emulation - serializes the database contents during setup and restores
    it during teardown.

    This fixture may be useful for transactional tests, so is usually combined
    with ``transactional_db``, but can also be useful on databases which do not
    support transactions.

    Note that this will slow down that test suite by approximately 3x.
    Nr`   )r   r   s     r9   r   r   |  r   r;   django.test.Clientc                 >    t                       ddlm}   |             S )zA Django test client instance.r   Client)r   r   r   r   s    r9   r"   r"     s-     """"""688Or;   django.test.AsyncClientc                 >    t                       ddlm}   |             S )z$A Django test async client instance.r   AsyncClient)r   r   r   r   s    r9   r#   r#     s.     '''''';==r;   c                "    ddl m}  |            S )z!The class of Django's user model.r   )get_user_model)django.contrib.authr   )r   r   s     r9   r    r      s$     322222>r;   c                    | j         }|S )z=The fieldname for the username used with Django's user model.)USERNAME_FIELD)r    fields     r9   r!   r!     s     #1ELr;   r!   c                    |}|}|dk    rdnd}	 |j                             |          }n<# |j        $ r/ i }d|j        v rd|d<   d|d<   |||<    |j         j        di |}Y nw xY w|S )zA Django admin user.

    This uses an existing user with username "admin", or creates a new one with
    password "password".
    emailzadmin@example.comadminpasswordr`   )_default_managerget_by_natural_keyDoesNotExistREQUIRED_FIELDScreate_superuser)r   r    r!   	UserModelusername_fieldusernameuser	user_datas           r9   r   r     s     "I*N&4&?&?""WHH
 )<<XFF! H H H	i///!4Ig *	*$,	.!:y):GGYGGH Ks   + 6A$#A$c                P    ddl m}  |            }|                    |           |S )z0A Django test client logged in as an admin user.r   r   )r   r   force_login)r   r   r   r"   s       r9   r$   r$     s:     #"""""VXXF
z"""Mr;   django.test.RequestFactoryc                 >    t                       ddlm}   |             S )zRequestFactory instancer   RequestFactory)r   r   r   r   s    r9   r%   r%     s0     ******>r;   django.test.AsyncRequestFactoryc                 >    t                       ddlm}   |             S )zAsyncRequestFactory instancer   AsyncRequestFactory)r   r   r   r   s    r9   r&   r&     s1     //////   r;   c                  >    e Zd ZU g Zded<   ddZddZdd	Zdd
ZdS )SettingsWrapperzClassVar[list[Any]]_to_restoreattrr   r/   r0   c                    ddl m}  |            }|                                 ddlm} t          ||           | j                            |           d S )Nr   override_settingsr   )r   r  enabler   r'   delattrr   append)r   r   r  overrider'   s        r9   __delattr__zSettingsWrapper.__delattr__  sq    111111$$&&(((((($)))))r;   c                    ddl m}  |di ||i}|                                 | j                            |           d S )Nr   r   r`   )r   r  r  r   r  )r   r   valuer  r  s        r9   __setattr__zSettingsWrapper.__setattr__  s[    111111$$55e}55)))))r;   c                .    ddl m} t          ||          S )Nr   r   )r   r'   rA   )r   r   r'   s      r9   __getattr__zSettingsWrapper.__getattr__	  s$    ((((((x&&&r;   c                p    t          | j                  D ]}|                                 | j        d d = d S rG   )reversedr   disable)r   r  s     r9   finalizezSettingsWrapper.finalize  sF     !122 	 	HQQQr;   N)r   r   r/   r0   )r   r   r   )	r   r   r   r   __annotations__r  r	  r  r  r`   r;   r9   r   r     sw         ')K))))	* 	* 	* 	** * * *' ' ' '
           r;   r   c               #  r   K   t                       t                      } | V  |                                  dS )zAA Django settings object which restores changes after the testrunN)r   r   r  )wrappers    r9   r'   r'     s@       G
MMMr;   c              #     K   t                       | j                            d          pt          j        d          pd}t          j        |          }|V  |                                 dS )at  Run a live Django server in the background during tests

    The address the server is started from is taken from the
    --liveserver command line option or if this is not provided from
    the DJANGO_LIVE_TEST_SERVER_ADDRESS environment variable.  If
    neither is provided ``localhost`` is used.  See the Django
    documentation for its full syntax.

    NOTE: If the live server needs database access to handle a request
          your test will have to request database access.  Furthermore
          when the tests want to see data added by the live-server (or
          the other way around) transactional database access will be
          needed as data inside a transaction is not shared between
          the live server and test code.

          Static assets will be automatically served when
          ``django.contrib.staticfiles`` is available in INSTALLED_APPS.
    
liveserverDJANGO_LIVE_TEST_SERVER_ADDRESS	localhostN)r   rB   rO   r5   r6   r   
LiveServerstop)r<   addrservers      r9   r(   r(     su      (  	-- 	9677	 	  *400F
LLL
KKMMMMMr;   T)autousec              #     K   d| j         vrdV  dS |                     d           |                     d          }|j                                         dV  |j                                         dS )aZ  Helper to make live_server work, internal to pytest-django.

    This helper will dynamically request the transactional_db fixture
    for a test which uses the live_server fixture.  This allows the
    server and test to access the database without having to mark
    this explicitly which is handy since it is usually required and
    matches the Django behaviour.

    The separate helper is required since live_server can not request
    transactional_db directly since it is session scoped instead of
    function-scoped.

    It will also override settings only for the duration of the test.
    r(   Nr   )r   getfixturevalue_live_server_modified_settingsr  r  )r<   r(   s     r9   r)   r)   @  s        G000.///))-88K.55777	EEE.6688888r;   c                       e Zd ZdZ	 	 dddZdS )DjangoAssertNumQueriesz]The type of the `django_assert_num_queries` and
    `django_assert_max_num_queries` fixtures..numint
connection
Any | Noneinfo
str | Noner/   'django.test.utils.CaptureQueriesContextc                    d S rG   r`   )r   r!  r#  r%  s       r9   __call__zDjangoAssertNumQueries.__call__`  	     	r;   N)..)r!  r"  r#  r$  r%  r&  r/   r'  r   r   r   __doc__r)  r`   r;   r9   r   r   \  s?        1 1 "%	      r;   r   rB   pytest.Configr!  r"  exactr#  r$  r%  r&  >Generator[django.test.utils.CaptureQueriesContext, None, None]c              #    K   ddl m} |ddlm} n|}|                     d          dk    } ||          5 }|V  t          |          }	|r||	k    }
n|	|k    }
|
rud| d}|s|dz  }|	dk    rd	nd
}|d|	 d| dz  }|r|d| z  }|r-d |j        D             }|dd                    |          z   z  }n|dz  }t          j	        |           d d d            d S # 1 swxY w Y   d S )Nr   )CaptureQueriesContext)r#  re   zExpected to perform z	 queries zor less r   waswerezbut  z done
c              3  &   K   | ]}|d          V  dS )sqlNr`   ).0qs     r9   	<genexpr>z&_assert_num_queries.<locals>.<genexpr>  s&      CCQ%CCCCCCr;   z

Queries:
========

z

z  (add -v option to show queries))
ra   r1  r   r#  	getoptionlencaptured_queriesjoinri   fail)rB   r!  r.  r#  r%  r1  connre   contextnum_performedfailedmsgverbsqlss                 r9   _assert_num_queriesrG  i  s      8777770000000y))A-G		t	$	$ G 	)M)FF"S(F 	7777C "z!)Q..55FD5-55$5555C #{D{{" :CC'*BCCC3fkk$6G6GGG99K)                 s   BCC#&C#pytestconfigc                ,    t          t          |           S rG   r   rG  rH  s    r9   r*   r*     s    &555r;   c                0    t          t          | d          S )NF)r.  rJ  rK  s    r9   r+   r+     s    &EBBBBr;   c                  "    e Zd ZdZddddd
ZdS )DjangoCaptureOnCommitCallbacksz=The type of the `django_capture_on_commit_callbacks` fixture..)usingexecuterO  r   rP  rK   r/   'ContextManager[list[Callable[[], Any]]]c                   d S rG   r`   )r   rO  rP  s      r9   r)  z'DjangoCaptureOnCommitCallbacks.__call__  r*  r;   N)rO  r   rP  rK   r/   rQ  r+  r`   r;   r9   rN  rN    s@        GG
 	       r;   rN  c                     ddl m}  | j        S )Nr   ry   )r   ry   captureOnCommitCallbacksrT  s    r9   r,   r,     s    $$$$$$,,r;   r   )r<   r=   r/   r0   )r:   r0   rE   r0   r/   r0   )rH   r0   r/   r0   )r<   r=   r/   rK   )r<   r=   rW   r0   rX   r   rP   rK   rS   rK   rV   rK   rJ   r0   r/   rY   )r<   r=   r   r0   rX   r   r/   rY   )r/   r   )r4   r   r/   r0   )r   r0   r/   r0   )r   r0   r   r0   r/   r0   )r   r0   r   r0   r/   r0   )r/   r   )r/   r   )r   r0   )r/   r   )r   r0   r!   r   )r   r0   r/   r   )r/   r   )r/   r   )r<   r=   )r<   r=   r/   rY   )TNN)rB   r-  r!  r"  r.  rK   r#  r$  r%  r&  r/   r/  )rH  r-  r/   r   )r/   rN  )Ir,  
__future__r   r5   
contextlibr   	functoolsr   typingr   r   r   r	   r
   r   r   r   r   r   r   r   r   ri    r   django_compatr   lazy_djangor   r   r   r   r   r   r   rK   r   r   fixturer:   rE   rH   rJ   rP   rS   rV   r   r   r   rb   r7   r   r   r   r   r"   r#   r    r!   r   r$   r%   r&   r   r'   r(   r)   r   rG  r*   r+   rN  r,   r`   r;   r9   <module>r^     s^       " " " " " " 				 % % % % % %                                                  - - - - - - * * * * * *  "MMM!!!!!! eGI$6$EFG !$s), $0$8NNO	  . i   > > > ! > i   ; ; ; ! ; i      !  i      !  i   7 7 7 ! 7 i      ! 
 i      ! 
 i   $ $ $ ! $N g  g  g  g T1 1 1 10+ + + +*> > > >*          
 
 
 
    (                    < 	 	 	 	     ! ! ! !               B    i      ! @ 9 9 9 96
 
 
 
 
X 
 
 
  !# # # # #L 6 6 6 6 C C C C	 	 	 	 	X 	 	 	 - - - - - -r;   