a
    ¡˜ëj,!  ã                   @  s.  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dœdd„Z dddœdd„Z!ddddœdd„Z"dddœdd„Z#dddœdd„Z$dddœdd„Z%ddd œd!d"„Z&dOd$dd%d&d'd(œd)d*„Z'dPd$d+dd,d&d'd-œd.d/„Z(dQd$dd%d&d'd0œd1d2„Z)dRd$d+d%d&d'd3œd4d5„Z*dSd$d+d,d%d&d'd6œd7d8„Z+dTd$d,d&d'd9œd:d;„Z,dUd$d+dd%d&d'd<œd=d>„Z-dVd$d?dd%d&d'd@œdAdB„Z.d$dd&d'dCœdDdE„Z/d$d+dd&d'dFœdGdH„Z0d$dd&d'dIœdJdK„Z1dd
dLœdMdN„Z2dS )Wz!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_mockzList[Element]ÚNone)ÚelementsÚreturnc                 C  sN   t | ƒ}|dusJ ‚t|d�}t |ƒ}|dus2J ‚||ksJJ 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   Zoriginal_jsonZround_tripped_elementsZround_tripped_json© r!   ú[/mnt/bigstor/www/usecases/.venv/lib/python3.9/site-packages/test_unstructured/unit_utils.pyÚassert_round_trips_through_JSON,   s    
ÿr#   zlist[Element]c                 C  s    t | ƒD ]\}}| |¡ q| S )z5Updates the `id` attribute of each element to a hash.)Ú	enumerateZ
id_to_hash)r   ÚidxÚelementr!   r!   r"   Úassign_hash_idsD   s    r'   Ústr)ÚheadingÚactualÚexpectedc                 C  s6   |j dd�}|j dd�}d} | d t ¡  ||¡¡ 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ÚdifflibZDifferÚcompare)r)   r*   r+   Zexpected_linesZactual_linesr!   r!   r"   r    K   s    r    )Ú	file_namer   c                 C  s&   t  t¡jjd }||  }t| ¡ ƒS )zGResolve the absolute-path to `file_name` in the example-docs directory.zexample-docs©ÚpathlibÚPathÚ__file__Úparentr(   Úresolve)r2   Zexample_docs_dirÚ	file_pathr!   r!   r"   Úexample_doc_pathV   s    r:   c                 C  s8   t t| ƒƒ�}| ¡ W  d  ƒ S 1 s*0    Y  dS )z?Contents of example-doc `file_name` as text (decoded as utf-8).N)Úopenr:   Úread)r2   Úfr!   r!   r"   Úexample_doc_text]   s    r>   zOptional[str]zOptional[dt.datetime])Údatetime_strr   c                 C  s   | rt j | ¡S dS )zWParse `datetime_str` to a datetime.datetime instance or None if `datetime_str` is None.N)ÚdtÚdatetimeÚfromisoformat)r?   r!   r!   r"   Úparse_optional_datetimec   s    rC   )Úrel_pathr   c                 C  s$   t  t¡jd }||  }t| ¡ ƒS )zCResolve the absolute-path to `rel_path` in the testfiles directory.Z	testfilesr3   )rD   Ztestfiles_dirr9   r!   r!   r"   Ú
input_pathh   s    rE   Tr   Úboolr   r   )ÚrequestÚq_class_nameÚautospecÚkwargsr   c                 K  s(   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.
    rI   ©r   ÚaddfinalizerÚstopÚstart)rG   rH   rI   rJ   Ú_patchr!   r!   r"   r   w   s    	r   Útypez
str | None)rG   ÚclsÚ	attr_nameÚnamerJ   r   c                 K  s>   |du r| 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.
    NrS   )Úfixturenamer   ÚobjectrL   rM   rN   )rG   rQ   rR   rS   rJ   rO   r!   r!   r"   Úcls_attr_mock…   s    rV   )rG   Úq_function_namerI   rJ   r   c                 K  s(   t |fd|i|¤Ž}|  |j¡ | ¡ S )z|Return mock patching function with qualified name `q_function_name`.

    Patch is reversed after calling test returns.
    rI   rK   )rG   rW   rI   rJ   rO   r!   r!   r"   r   –   s    r   )rG   rQ   rI   rJ   r   c                 K  s.   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)rI   Zreturn_value©r   rU   rL   rM   rN   )rG   rQ   rI   rJ   rO   r!   r!   r"   r   ¢   s    r   )rG   rQ   rS   Úspec_setrJ   r   c                 K  s*   |dur|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)Ú_namerZ   Úinstance)rT   r   )rG   rQ   rS   rZ   rJ   r!   r!   r"   r   ®   s    r   )rG   rS   rJ   r   c                 K  s    |du r| j }tf d|i|¤ŽS )zÒReturn 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.
    NrS   )rT   r   )rG   rS   rJ   r!   r!   r"   Ú
loose_mock¿   s    r]   )rG   rQ   Úmethod_namerI   rJ   r   c                 K  s,   t j||fd|i|¤Ž}|  |j¡ | ¡ S )zdReturn mock for method `method_name` on `cls`.

    The patch is reversed after pytest uses it.
    rI   rY   )rG   rQ   r^   rI   rJ   rO   r!   r!   r"   r   Ê   s    r   ztypes.ModuleType)rG   Ústd_modÚfn_namerI   rJ   r   c                 K  s,   t j||fd|i|¤Ž}|  |j¡ | ¡ S )zfReturn mock for function `fn_name` on `std_mod`.

    The patch is reversed after pytest uses it.
    rI   rY   )rG   r_   r`   rI   rJ   rO   r!   r!   r"   Ústdlib_fn_mockÚ   s    ra   )rG   Úmodule_namerJ   r   c                 K  s4   d| }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   rL   rM   rN   )rG   rb   rJ   ÚtargetrO   r!   r!   r"   Ú	open_mockê   s    re   )rG   rQ   Ú	prop_namerJ   r   c                 K  s,   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.
    Znew_callable)r   rU   r	   rL   rM   rN   )rG   rQ   rf   rJ   rO   r!   r!   r"   r   ò   s    r   )rG   Ú
q_var_namerJ   r   c                 K  s$   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.
    rK   )rG   rg   rJ   rO   r!   r!   r"   Úvar_mockü   s    rh   ©r   r   c                   s   t ‡ fdd„|D ƒƒS )Nc                 3  s   | ]}|j  ˆ ¡d kV  qdS )éÿÿÿÿN)r   Úfind)Ú.0Úelr   r!   r"   Ú	<genexpr>  ó    z(find_text_in_elements.<locals>.<genexpr>)Úanyri   r!   r   r"   Úfind_text_in_elements  s    rq   )T)N)T)T)NT)N)T)T)3Ú__doc__Ú
__future__r   rA   r@   r0   r4   ÚtypesÚtypingr   r   r   Zunittest.mockr   r   r   r	   r
   r   r   r   Zpytestr   r   r   r   Zunstructured.documents.elementsr   Zunstructured.staging.baser   r   Ú__all__r#   r'   r    r:   r>   rC   rE   r   rV   r   r   r   r]   r   ra   re   r   rh   rq   r!   r!   r!   r"   Ú<module>   sL   ( ÿ ü ÿ ÿ  ü ü ü

