|
| 1 | +from unittest import mock |
| 2 | + |
| 3 | +import os |
| 4 | +import pytest |
| 5 | +from swarmauri_parser_slate.SlateParser import SlateParser as Parser |
| 6 | +from swarmauri_core.documents.IDocument import IDocument |
| 7 | + |
| 8 | + |
| 9 | +@pytest.mark.unit |
| 10 | +def test_parser_resource(): |
| 11 | + """ |
| 12 | + Test to ensure the parser's resource attribute is correctly set. |
| 13 | + """ |
| 14 | + parser = Parser() |
| 15 | + assert parser.resource == "Parser", "The resource attribute should be 'Parser'." |
| 16 | + |
| 17 | + |
| 18 | +@pytest.mark.unit |
| 19 | +def test_parser_type(): |
| 20 | + """ |
| 21 | + Test to ensure the parser's type attribute is correctly set. |
| 22 | + """ |
| 23 | + parser = Parser() |
| 24 | + assert parser.type == "SlateParser", "The type attribute should be 'SlateParser'." |
| 25 | + |
| 26 | + |
| 27 | +@pytest.mark.unit |
| 28 | +def test_parser_serialization(): |
| 29 | + """ |
| 30 | + Test to ensure the parser can be serialized and deserialized correctly. |
| 31 | + """ |
| 32 | + parser = Parser() |
| 33 | + serialized = parser.model_dump_json() |
| 34 | + deserialized = Parser.model_validate_json(serialized) |
| 35 | + assert parser.id == deserialized.id, ( |
| 36 | + "Serialization and deserialization should preserve the parser's ID." |
| 37 | + ) |
| 38 | + |
| 39 | + |
| 40 | +@pytest.mark.unit |
| 41 | +def test_parser_success_mock_file_path(): |
| 42 | + """ |
| 43 | + Test the parser's ability to successfully parse a PDF file and extract text. |
| 44 | + """ |
| 45 | + os.chdir(os.path.dirname(__file__)) |
| 46 | + |
| 47 | + parser = Parser() |
| 48 | + file_path = "resources/demo.pdf" |
| 49 | + |
| 50 | + with mock.patch("slate3k.PDF") as mock_pdf_reader: |
| 51 | + |
| 52 | + mock_pdf_reader.return_value = ['Sample text from page 1.'] |
| 53 | + |
| 54 | + # Call the parser's parse method |
| 55 | + documents = parser.parse(file_path) |
| 56 | + |
| 57 | + # Assertions |
| 58 | + mock_pdf_reader.assert_called_once() |
| 59 | + |
| 60 | + assert len(documents) == 1, "Parser should return a list with one document." |
| 61 | + assert isinstance(documents[0], IDocument), ( |
| 62 | + "Returned object should be an instance of IDocument." |
| 63 | + ) |
| 64 | + assert documents[0].content == 'Sample text from page 1.', ( |
| 65 | + "Extracted content does not match expected." |
| 66 | + ) |
| 67 | + assert documents[0].metadata["page_number"] == 1, ( |
| 68 | + "Metadata 'page_number' should be 1." |
| 69 | + ) |
| 70 | + assert documents[0].metadata["source"] == file_path, ( |
| 71 | + "Metadata 'source' should match the file path." |
| 72 | + ) |
| 73 | + |
| 74 | +@pytest.mark.unit |
| 75 | +def test_parser_success_file_path(): |
| 76 | + """ |
| 77 | + Test the parser's ability to successfully read and parse a PDF file and extract text. |
| 78 | + """ |
| 79 | + os.chdir(os.path.dirname(__file__)) |
| 80 | + |
| 81 | + parser = Parser() |
| 82 | + file_path = "resources/demo.pdf" |
| 83 | + |
| 84 | + # Call the parser's parse method |
| 85 | + documents = parser.parse(file_path) |
| 86 | + |
| 87 | + assert len(documents) == 1, "Parser should return a list with one document." |
| 88 | + assert isinstance(documents[0], IDocument), ( |
| 89 | + "Returned object should be an instance of IDocument." |
| 90 | + ) |
| 91 | + assert documents[0].content == "This is a demo pdf", ( |
| 92 | + "Extracted content does not match expected." |
| 93 | + ) |
| 94 | + assert documents[0].metadata["page_number"] == 1, ( |
| 95 | + "Metadata 'page_number' should be 1." |
| 96 | + ) |
| 97 | + assert documents[0].metadata["source"] == file_path, ( |
| 98 | + "Metadata 'source' should match the file path." |
| 99 | + ) |
| 100 | + |
| 101 | + |
| 102 | +@pytest.mark.unit |
| 103 | +def test_parser_invalid_source(): |
| 104 | + """ |
| 105 | + Test that the parser raises a TypeError when given an invalid source type. |
| 106 | + """ |
| 107 | + parser = Parser() |
| 108 | + invalid_source = 12345 # Not a str or bytes |
| 109 | + |
| 110 | + with pytest.raises(TypeError) as exc_info: |
| 111 | + parser.parse(invalid_source) |
| 112 | + |
| 113 | + assert "Source must be of type str (file path) or bytes." in str(exc_info.value), ( |
| 114 | + "TypeError not raised as expected." |
| 115 | + ) |
| 116 | + |
| 117 | + |
| 118 | +@pytest.mark.unit |
| 119 | +def test_parser_exception_handling(): |
| 120 | + """ |
| 121 | + Test the parser's exception handling when an error occurs during parsing. |
| 122 | + """ |
| 123 | + parser = Parser() |
| 124 | + file_path = "non_existent_file.pdf" |
| 125 | + |
| 126 | + # Call the parser's parse method with a non-existent file |
| 127 | + documents = parser.parse(file_path) |
| 128 | + |
| 129 | + # Assertions |
| 130 | + assert len(documents) == 0, ( |
| 131 | + "Parser should return an empty list when an error occurs." |
| 132 | + ) |
0 commit comments