a
    ¡˜ëjG  ã                   @  sz  d Z ddlmZ ddlmZmZ ddlZddlmZ ddl	m
Z
mZmZmZmZ ddlmZ ddlmZmZmZmZmZ dd	lmZ dd
lmZ ddlmZ dd„ Zdd„ Zdd„ Zdd„ Z dd„ Z!dd„ Z"dd„ Z#ej$ %di ddig¡ddœdd „ƒZ&d!d"„ Z'd#d$œd%d&„Z(d#d$œd'd(„Z)d)d*„ Z*d+d,„ Z+ej$ %d-i d.fd/did.fd/d0id0fg¡d1dd2d3œd4d5„ƒZ,d6d7„ Z-d8d9„ Z.dS ):z3Test suite for `unstructured.partition.odt` module.é    )Úannotations)ÚAnyÚIteratorN)ÚMockFixture)ÚANYÚFixtureRequestÚassert_round_trips_through_JSONÚexample_doc_pathÚmethod_mock)Úchunk_elements)ÚCompositeElementÚElementÚTableÚ
TableChunkÚText)Úpartition_docx)Úpartition_odt)Ú#UNSTRUCTURED_INCLUDE_DEBUG_METADATAc                  C  s(   t dƒ} t dƒ}t| ƒt|ƒks$J ‚d S )Nú
simple.odtzsimple.docx)r	   r   r   )Zodt_file_pathZdocx_file_path© r   úc/mnt/bigstor/www/usecases/.venv/lib/python3.9/site-packages/test_unstructured/partition/test_odt.pyÚ)test_partition_odt_matches_partition_docx    s    r   c                  C  sZ   t tdƒƒ} | tdƒtdƒgks$J ‚tdd„ | D ƒƒs:J ‚trVdd„ | D ƒdhksVJ ‚d S )	Núfake.odtúLorem ipsum dolor sit amet.úSHeader row Mon Wed Fri Color Blue Red Green Time 1pm 2pm 3pm Leader Sarah Mark Ryanc                 s  s   | ]}|j jd kV  qdS )r   N©ÚmetadataÚfilename©Ú.0Úer   r   r   Ú	<genexpr>6   ó    z3test_partition_odt_from_filename.<locals>.<genexpr>c                 S  s   h | ]}|j j’qS r   )r   Zdetection_originr   r   r   r   Ú	<setcomp>9   r"   z3test_partition_odt_from_filename.<locals>.<setcomp>Zdocx)r   r	   r   r   Úallr   ©Úelementsr   r   r   Ú test_partition_odt_from_filename*   s    ÿþ
	r'   c                  C  sT   t tdƒdƒ�} t| d�}W d   ƒ n1 s.0    Y  |tdƒtdƒgksPJ ‚d S )Nr   Úrb)Úfiler   r   )Úopenr	   r   r   r   ©Úfr&   r   r   r   Útest_partition_odt_from_file<   s    (ÿþr-   c                  C  s<   t tdƒƒ} tdd„ | D ƒƒs8J dt| d jjƒ› �ƒ‚d S )Nr   c                 s  s   | ]}|j jd kV  qdS )r   Nr   r   r   r   r   r!   P   r"   zktest_partition_odt_from_filename_gets_the_ODT_filename_in_metadata_not_the_DOCX_filename.<locals>.<genexpr>zGExpected all elements to have 'simple.odt' as their filename, but got: r   )r   r	   r$   Úreprr   r   r%   r   r   r   ÚXtest_partition_odt_from_filename_gets_the_ODT_filename_in_metadata_not_the_DOCX_filenameN   s    ÿÿr/   c                  C  s*   t tdƒdd�} tdd„ | D ƒƒs&J ‚d S )Nr   Útest)Úmetadata_filenamec                 s  s   | ]}|j jd kV  qdS ©r0   Nr   r   r   r   r   r!   X   r"   zJtest_partition_odt_from_filename_with_metadata_filename.<locals>.<genexpr>©r   r	   r$   r%   r   r   r   Ú7test_partition_odt_from_filename_with_metadata_filenameV   s    r4   c                  C  sT   t tdƒdƒ�} t| dd�}W d   ƒ n1 s00    Y  tdd„ |D ƒƒsPJ ‚d S )Nr   r(   r0   )r)   r1   c                 s  s   | ]}|j jd kV  qdS r2   r   r   r   r   r   r!   ^   r"   zFtest_partition_odt_from_file_with_metadata_filename.<locals>.<genexpr>)r*   r	   r   r$   r+   r   r   r   Ú3test_partition_odt_from_file_with_metadata_filename[   s    *r5   c                    sJ   d‰ t tdƒƒ} t‡ fdd„| D ƒƒsFJ dˆ › dt| d jjƒ› �ƒ‚d S )Nz'application/vnd.oasis.opendocument.textr   c                 3  s   | ]}|j jˆ kV  qd S ©N)r   Úfiletyper   ©ZODT_MIME_TYPEr   r   r!   g   r"   zQtest_partition_odt_gets_the_ODT_MIME_type_in_metadata_filetype.<locals>.<genexpr>zExpected all elements to have 'z' as their filetype, but got: r   )r   r	   r$   r.   r   r7   r%   r   r8   r   Ú>test_partition_odt_gets_the_ODT_MIME_type_in_metadata_filetyped   s    ÿÿr9   ÚkwargsÚinfer_table_structureTzdict[str, Any])r:   c                 C  s|   t tdƒdƒ�"}tf d|i| ¤Ž}W d   ƒ n1 s60    Y  |d }t|tƒsVJ ‚|jjd usfJ ‚|jj d¡sxJ ‚d S )Nr   r(   r)   é   z<table>)r*   r	   r   Ú
isinstancer   r   Útext_as_htmlÚ
startswith)r:   r,   r&   Útabler   r   r   ÚRtest_partition_odt_adds_text_as_html_when_infer_table_structure_is_omitted_or_Truep   s    0rA   c                  C  sd   t tdƒdƒ�} t| dd�}W d   ƒ n1 s00    Y  |d }t|tƒsPJ ‚|jjd u s`J ‚d S )Nr   r(   F)r)   r;   r<   )r*   r	   r   r=   r   r   r>   )r,   r&   r@   r   r   r   ÚNtest_partition_odt_suppresses_text_as_html_when_infer_table_structure_is_False}   s
    *rB   r   )Úmockerc                   s<   d‰ | j dˆ d� ttdƒƒ}t‡ fdd„|D ƒƒs8J ‚d S )Nú2029-07-05T09:24:28ú1unstructured.partition.odt.get_last_modified_date©Zreturn_valuer   c                 3  s   | ]}|j jˆ kV  qd S r6   ©r   Zlast_modifiedr   ©Úfilesystem_last_modifiedr   r   r!   ‘   r"   zItest_partition_odt_pulls_last_modified_from_filesystem.<locals>.<genexpr>©Úpatchr   r	   r$   )rC   r&   r   rH   r   Ú6test_partition_odt_pulls_last_modified_from_filesystem‰   s    ÿrL   c                   sD   d}d‰ | j d|d� ttdƒˆ d�}t‡ fdd„|D ƒƒs@J ‚d S )	NrD   z2020-07-05T09:24:28rE   rF   r   ©Zmetadata_last_modifiedc                 3  s   | ]}|j jˆ kV  qd S r6   rG   r   rM   r   r   r!   Ÿ   r"   zRtest_partition_odt_prefers_metadata_last_modified_when_provided.<locals>.<genexpr>rJ   )rC   rI   r&   r   rM   r   Ú?test_partition_odt_prefers_metadata_last_modified_when_provided”   s    ÿÿrN   c                  C  s&   t tdƒƒ} tdd„ | D ƒƒs"J ‚d S )Nr   c                 s  s   | ]}|j jd gkV  qdS )ÚengN©r   Ú	languagesr   r   r   r   r!   §   r"   z=test_partition_odt_adds_languages_metadata.<locals>.<genexpr>r3   r%   r   r   r   Ú*test_partition_odt_adds_languages_metadata¥   s    rR   c                  C  s@   t tdƒdd�} dd„ | D ƒdgddgdgdgdggks<J ‚d S )Nzlanguage-docs/eng_spa_mult.odtT)Zdetect_language_per_elementc                 S  s   g | ]}|j j‘qS r   rP   r   r   r   r   Ú
<listcomp>®   r"   zOtest_partition_odt_respects_detect_language_per_element_arg.<locals>.<listcomp>rO   Zspa)r   r	   r%   r   r   r   Ú;test_partition_odt_respects_detect_language_per_element_argª   s    ÿûrT   )r:   Úexpected_valueZhi_resÚstrategyÚautor   z
str | None)Úrequestr:   rU   c                 C  sd   ddl m} dddœdd„}t| |d|d	�}ttd
ƒfi |¤Ž\}| t¡ |jd|› �ks`J ‚d S )Nr   )Ú_DocxPartitionerrY   zIterator[Element])ÚselfÚreturnc                 s  s   t d| jj› �ƒV  d S )Nústrategy == )r   Z_optsrV   )rZ   r   r   r   Úfake_iter_document_elementsÃ   s    z_test_partition_odt_forwards_strategy_arg_to_partition_docx.<locals>.fake_iter_document_elementsZ_iter_document_elements)Zside_effectr   r\   )Úunstructured.partition.docxrY   r
   r   r	   Zassert_called_once_withr   Útext)rX   r:   rU   rY   r]   Z_iter_elements_Úelementr   r   r   Ú:test_partition_odt_forwards_strategy_arg_to_partition_docxº   s    ü
ra   c                   C  s   t ttdƒƒƒ dS )zCElements produced can be serialized then deserialized without loss.r   N)r   r   r	   r   r   r   r   Ú+test_partition_odt_round_trips_through_jsonÓ   s    rb   c                  C  sF   t dƒ} t| ƒ}t| dd�}tdd„ |D ƒƒs2J ‚|t|ƒksBJ ‚d S )Nr   Úbasic)Zchunking_strategyc                 s  s   | ]}t |tttfƒV  qd S r6   )r=   r   r   r   )r   Úcr   r   r   r!   Þ   r"   zYtest_partition_odt_chunks_elements_when_chunking_strategy_is_specified.<locals>.<genexpr>)r	   r   r$   r   )Zdocument_pathr&   Úchunksr   r   r   ÚFtest_partition_odt_chunks_elements_when_chunking_strategy_is_specifiedØ   s
    rf   )/Ú__doc__Ú
__future__r   Útypingr   r   ZpytestZpytest_mockr   Ztest_unstructured.unit_utilsr   r   r   r	   r
   Zunstructured.chunking.basicr   Zunstructured.documents.elementsr   r   r   r   r   r^   r   Zunstructured.partition.odtr   Z&unstructured.partition.utils.constantsr   r   r'   r-   r/   r4   r5   r9   ÚmarkZparametrizerA   rB   rL   rN   rR   rT   ra   rb   rf   r   r   r   r   Ú<module>   s>   
	þ