2023-03-28 16:15:22 -04:00
|
|
|
import os
|
|
|
|
import pathlib
|
|
|
|
|
|
|
|
import msg_parser
|
|
|
|
import pytest
|
|
|
|
|
2023-04-04 14:23:41 -04:00
|
|
|
from unstructured.documents.elements import (
|
|
|
|
ElementMetadata,
|
|
|
|
ListItem,
|
|
|
|
NarrativeText,
|
|
|
|
Title,
|
|
|
|
)
|
2023-04-21 11:14:46 -05:00
|
|
|
from unstructured.partition.msg import extract_msg_attachment_info, partition_msg
|
2023-03-28 16:15:22 -04:00
|
|
|
|
|
|
|
DIRECTORY = pathlib.Path(__file__).parent.resolve()
|
|
|
|
EXAMPLE_DOCS_DIRECTORY = os.path.join(DIRECTORY, "..", "..", "example-docs")
|
|
|
|
|
|
|
|
EXPECTED_MSG_OUTPUT = [
|
|
|
|
NarrativeText(text="This is a test email to use for unit tests."),
|
|
|
|
Title(text="Important points:"),
|
|
|
|
ListItem(text="Roses are red"),
|
|
|
|
ListItem(text="Violets are blue"),
|
|
|
|
]
|
|
|
|
|
2023-04-21 11:14:46 -05:00
|
|
|
ATTACH_EXPECTED_OUTPUT = [
|
|
|
|
{
|
|
|
|
"filename": "fake-attachment.txt",
|
|
|
|
"extension": ".txt",
|
|
|
|
"file_size": "unknown",
|
|
|
|
"payload": b"Hey this is a fake attachment!",
|
|
|
|
},
|
|
|
|
]
|
|
|
|
|
2023-03-28 16:15:22 -04:00
|
|
|
|
|
|
|
def test_partition_msg_from_filename():
|
|
|
|
filename = os.path.join(EXAMPLE_DOCS_DIRECTORY, "fake-email.msg")
|
|
|
|
elements = partition_msg(filename=filename)
|
|
|
|
assert elements == EXPECTED_MSG_OUTPUT
|
2023-04-04 14:23:41 -04:00
|
|
|
assert elements[0].metadata == ElementMetadata(
|
2023-05-12 11:33:01 -04:00
|
|
|
filename="fake-email.msg",
|
2023-04-04 14:23:41 -04:00
|
|
|
date="2022-12-16T17:04:16-05:00",
|
2023-05-15 13:23:19 -05:00
|
|
|
page_number=1,
|
2023-04-04 14:23:41 -04:00
|
|
|
url=None,
|
|
|
|
sent_from=["Matthew Robinson <mrobinson@unstructured.io>"],
|
|
|
|
sent_to=["Matthew Robinson (None)"],
|
|
|
|
subject="Test Email",
|
2023-05-15 13:23:19 -05:00
|
|
|
filetype="application/vnd.ms-outlook",
|
2023-04-04 14:23:41 -04:00
|
|
|
)
|
2023-03-28 16:15:22 -04:00
|
|
|
|
|
|
|
|
|
|
|
class MockMsOxMessage:
|
|
|
|
def __init__(self, filename):
|
|
|
|
self.body = "Here is an email with plain text."
|
|
|
|
|
|
|
|
|
|
|
|
def test_partition_msg_from_filename_with_text_content(monkeypatch):
|
|
|
|
monkeypatch.setattr(msg_parser, "MsOxMessage", MockMsOxMessage)
|
|
|
|
filename = os.path.join(EXAMPLE_DOCS_DIRECTORY, "fake-email.msg")
|
|
|
|
elements = partition_msg(filename=filename)
|
|
|
|
assert str(elements[0]) == "Here is an email with plain text."
|
|
|
|
|
|
|
|
|
|
|
|
def test_partition_msg_raises_with_missing_file():
|
|
|
|
filename = os.path.join(EXAMPLE_DOCS_DIRECTORY, "doesnt-exist.msg")
|
|
|
|
with pytest.raises(FileNotFoundError):
|
|
|
|
partition_msg(filename=filename)
|
|
|
|
|
|
|
|
|
|
|
|
def test_partition_msg_from_file():
|
|
|
|
filename = os.path.join(EXAMPLE_DOCS_DIRECTORY, "fake-email.msg")
|
|
|
|
with open(filename, "rb") as f:
|
|
|
|
elements = partition_msg(file=f)
|
|
|
|
assert elements == EXPECTED_MSG_OUTPUT
|
|
|
|
|
|
|
|
|
2023-04-21 11:14:46 -05:00
|
|
|
def test_extract_attachment_info():
|
|
|
|
filename = os.path.join(DIRECTORY, "..", "..", "example-docs", "fake-email-attachment.msg")
|
|
|
|
attachment_info = extract_msg_attachment_info(filename)
|
|
|
|
assert len(attachment_info) > 0
|
|
|
|
assert attachment_info == ATTACH_EXPECTED_OUTPUT
|
|
|
|
|
|
|
|
|
2023-03-28 16:15:22 -04:00
|
|
|
def test_partition_msg_raises_with_both_specified():
|
|
|
|
filename = os.path.join(EXAMPLE_DOCS_DIRECTORY, "fake-email.msg")
|
|
|
|
with open(filename, "rb") as f, pytest.raises(ValueError):
|
|
|
|
partition_msg(filename=filename, file=f)
|
|
|
|
|
|
|
|
|
|
|
|
def test_partition_msg_raises_with_neither():
|
|
|
|
with pytest.raises(ValueError):
|
|
|
|
partition_msg()
|