
    Ng                    R   d Z ddlmZ ddlZddlZddlZddlZddlm	Z	m
Z
mZ ddlmZmZmZmZmZmZmZmZ ddlmZmZmZmZ ddlmZ ddlmZmZ d	Zd?dZ d@dZ!dAdZ"dBdZ#dBdZ$dCdZ%	 dDdEd&Z&	 dFdGd,Z'	 dDdHd.Z(	 dDdId/Z)	 	 dJdKd1Z*dFdLd2Z+	 dDdMd4Z,	 dDdNd8Z-dOd:Z.dPd<Z/dQd>Z0dS )Rz!Utilities that ease unit-testing.    )annotationsN)AnyListOptional)ANY	MagicMockMockPropertyMockcallcreate_autospec	mock_openpatch)CaptureFixtureFixtureRequestLogCaptureFixtureMonkeyPatch)Element)elements_from_jsonelements_to_json)r   r   r   r   r   r	   r   r   
class_mockfunction_mockinitializer_mockinstance_mockmethod_mockproperty_mockelementsList[Element]returnNonec                    t          |           }|J t          |          }t          |          }|J ||k    sJ t          d||                      dS )ab  Raises AssertionError if `elements -> JSON -> List[Element] -> JSON` are not equal.

    The procedure is:

        1. Serialize `elements` to (original) JSON.
        2. Deserialize that JSON to `List[Element]`.
        3. Serialize that `List[Element]` to JSON.
        3. Compare the original and round-tripped JSON, raise if they are different.

    N)textzJSON differs:)r   r   _diff)r   original_jsonround_tripped_elementsround_tripped_jsons       X/var/www/html/ai-engine/env/lib/python3.11/site-packages/test_unstructured/unit_utils.pyassert_round_trips_through_JSONr'   ,   sz     %X..M$$$/]CCC)*@AA)))...+]1 1.....    list[Element]c                Z    t          |           D ]\  }}|                    |           | S )z5Updates the `id` attribute of each element to a hash.)	enumerate
id_to_hash)r   idxelements      r&   assign_hash_idsr/   D   s9    !(++    W3Or(   headingstractualexpectedc                    |                     d          }|                     d          }d} | d                    t          j                                        ||                    z   S )z~Diff of actual compared to expected.

    "+" indicates unexpected lines actual, "-" indicates lines missing from actual.
    T)keependszFdiff: '+': unexpected lines in actual, '-': lines missing from actual
 )
splitlinesjoindifflibDiffercompare)r0   r2   r3   expected_linesactual_liness        r&   r"   r"   K   sd    
 (($(77N$$d$33LWGRWWW^--55lNSSTTTTr(   	file_namec                    t          j        t                    j        j        dz  }|| z  }t	          |                                          S )zGResolve the absolute-path to `file_name` in the example-docs directory.zexample-docs)pathlibPath__file__parentr1   resolve)r>   example_docs_dir	file_paths      r&   example_doc_pathrG   V   sA    |H--4;nL 9,Iy  ""###r(   c                    t          t          |                     5 }|                                cddd           S # 1 swxY w Y   dS )z?Contents of example-doc `file_name` as text (decoded as utf-8).N)openrG   read)r>   fs     r&   example_doc_textrL   ]   s    	y))	*	* avvxx                 s   >AAdatetime_strOptional[str]Optional[dt.datetime]c                H    | rt           j                            |           ndS )zWParse `datetime_str` to a datetime.datetime instance or None if `datetime_str` is None.N)dtdatetimefromisoformat)rM   s    r&   parse_optional_datetimerT   c   s"    6BL2;$$\222Lr(   Trequestr   q_class_nameautospecboolkwargsr   r	   c                |    t          |fd|i|}|                     |j                   |                                S )a$  Return mock patching class with qualified name `q_class_name`.

    The mock is autospec'ed based on the patched class unless the optional argument `autospec` is
    set to False. Any other keyword arguments are passed through to Mock(). Patch is reversed after
    calling test returns.
    rW   r   addfinalizerstopstart)rU   rV   rW   rY   _patchs        r&   r   r   p   sB     <==(=f==F%%%<<>>r(   clstype	attr_namename
str | Nonec                    || j         n|}t          j        ||fd|i|}|                     |j                   |                                S )zcReturn a mock for attribute `attr_name` on `cls`.

    Patch is reversed after pytest uses it.
    Nrc   )fixturenamer   objectr\   r]   r^   )rU   r`   rb   rc   rY   r_   s         r&   cls_attr_mockrh   ~   sW     #',7DD\#y>>t>v>>F%%%<<>>r(   q_function_namec                |    t          |fd|i|}|                     |j                   |                                S )z|Return mock patching function with qualified name `q_function_name`.

    Patch is reversed after calling test returns.
    rW   r[   )rU   ri   rW   rY   r_   s        r&   r   r      sB     ?@@X@@@F%%%<<>>r(   c                    t          j        |df|dd|}|                     |j                   |                                S )zaReturn mock for __init__() method on `cls`.

    The patch is reversed after pytest uses it.
    __init__N)rW   return_valuer   rg   r\   r]   r^   )rU   r`   rW   rY   r_   s        r&   r   r      sI     \#zZH4ZZSYZZF%%%<<>>r(   spec_setc                :    ||n| j         }t          |f||dd|S )ab  Return a mock for an instance of `cls` that draws its spec from the class.

    The mock does not allow new attributes to be set on the instance. If `name` is missing or
    |None|, the name of the returned |Mock| instance is set to *request.fixturename*. Additional
    keyword arguments are passed through to the Mock() call that creates the mock.
    NT)_namero   instance)rf   r   )rU   r`   rc   ro   rY   s        r&   r   r      s5     #44)<D3WdXWWPVWWWr(   c                0    || j         }t          dd|i|S )zReturn a "loose" mock, meaning it has no spec to constrain calls on it.

    Additional keyword arguments are passed through to Mock(). If called without a name, it is
    assigned the name of the fixture.
    Nrc    )rf   r	   )rU   rc   rY   s      r&   
loose_mockru      s*     |"$$T$V$$$r(   method_namec                    t          j        ||fd|i|}|                     |j                   |                                S )zdReturn mock for method `method_name` on `cls`.

    The patch is reversed after pytest uses it.
    rW   rn   )rU   r`   rv   rW   rY   r_   s         r&   r   r      sF     \#{HHXHHHF%%%<<>>r(   std_modtypes.ModuleTypefn_namec                    t          j        ||fd|i|}|                     |j                   |                                S )zfReturn mock for function `fn_name` on `std_mod`.

    The patch is reversed after pytest uses it.
    rW   rn   )rU   rx   rz   rW   rY   r_   s         r&   stdlib_fn_mockr|      sF     \'7HHXHHHF%%%<<>>r(   module_namec                    d|z  }t          |t                      fddi|}|                     |j                   |                                S )z?Return a mock for the builtin `open()` method in `module_name`.z%s.opencreateT)r   r   r\   r]   r^   )rU   r}   rY   targetr_   s        r&   	open_mockr      sP    $F69;;>>t>v>>F%%%<<>>r(   	prop_namec                    t          j        ||fdt          i|}|                     |j                   |                                S )zgA mock for property `prop_name` on class `cls`.

    Patch is reversed at the end of the test run.
    new_callable)r   rg   r
   r\   r]   r^   )rU   r`   r   rY   r_   s        r&   r   r      sF    
 \#yNN|NvNNF%%%<<>>r(   
q_var_namec                x    t          |fi |}|                     |j                   |                                S )z}Return a mock patching the variable with qualified name `q_var_name`.

    Patch is reversed after calling test returns.
    r[   )rU   r   rY   r_   s       r&   var_mockr      s=    
 :((((F%%%<<>>r(   )r   r   r   r   )r   r)   r   r)   )r0   r1   r2   r1   r3   r1   )r>   r1   r   r1   )rM   rN   r   rO   )T)
rU   r   rV   r1   rW   rX   rY   r   r   r	   )N)rU   r   r`   ra   rb   r1   rc   rd   rY   r   r   r	   )
rU   r   ri   r1   rW   rX   rY   r   r   r	   )
rU   r   r`   ra   rW   rX   rY   r   r   r	   )NT)rU   r   r`   ra   rc   rd   ro   rX   rY   r   r   r	   )rU   r   rc   rd   rY   r   r   r	   )rU   r   r`   ra   rv   r1   rW   rX   rY   r   r   r	   )rU   r   rx   ry   rz   r1   rW   rX   rY   r   r   r	   )rU   r   r}   r1   rY   r   r   r	   )
rU   r   r`   ra   r   r1   rY   r   r   r	   )rU   r   r   r1   rY   r   r   r	   )1__doc__
__future__r   rR   rQ   r9   r@   typestypingr   r   r   unittest.mockr   r   r	   r
   r   r   r   r   pytestr   r   r   r   unstructured.documents.elementsr   unstructured.staging.baser   r   __all__r'   r/   r"   rG   rL   rT   r   rh   r   r   r   ru   r   r|   r   r   r   rt   r(   r&   <module>r      s   ' ' " " " " " "        & & & & & & & & & &	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 R Q Q Q Q Q Q Q Q Q Q Q 3 3 3 3 3 3 J J J J J J J J$   0   U U U U$ $ $ $   M M M M BF    $ 	    $ EI	 	 	 	 	 :>	 	 	 	 	 	X X X X X"% % % % % 	    ( 	                r(   