Tom Aarsen 5eb1466acc
Resolve various style issues to improve overall code quality (#282)
* Apply import sorting

ruff . --select I --fix

* Remove unnecessary open mode parameter

ruff . --select UP015 --fix

* Use f-string formatting rather than .format

* Remove extraneous parentheses

Also use "" instead of str()

* Resolve missing trailing commas

ruff . --select COM --fix

* Rewrite list() and dict() calls using literals

ruff . --select C4 --fix

* Add () to pytest.fixture, use tuples for parametrize, etc.

ruff . --select PT --fix

* Simplify code: merge conditionals, context managers

ruff . --select SIM --fix

* Import without unnecessary alias

ruff . --select PLR0402 --fix

* Apply formatting via black

* Rewrite ValueError somewhat

Slightly unrelated to the rest of the PR

* Apply formatting to tests via black

* Update expected exception message to match
0d81564

* Satisfy E501 line too long in test

* Update changelog & version

* Add ruff to make tidy and test deps

* Run 'make tidy'

* Update changelog & version

* Update changelog & version

* Add ruff to 'check' target

Doing so required me to also fix some non-auto-fixable issues. Two of them I fixed with a noqa: SIM115, but especially the one in __init__ may need some attention. That said, that refactor is out of scope of this PR.
2023-02-27 11:30:54 -05:00

65 lines
2.1 KiB
Python

import os
from pathlib import Path
import pytest
from lxml import etree
from unstructured.documents.xml import XMLDocument
FILEPATH = Path(__file__).absolute().parent
@pytest.fixture()
def sample_document():
return """"<SEC-DOCUMENT>
<TYPE>10-K
<COMPANY>Proctor & Gamble
</SEC-DOCUMENT>"""
def test_read_xml(sample_document, tmpdir):
filename = os.path.join(tmpdir.dirname, "sample-document.xml")
with open(filename, "w") as f:
f.write(sample_document)
xml_document = XMLDocument.from_file(filename=filename)
document_tree = xml_document.document_tree
type_tag = document_tree.find(".//type")
assert type_tag.text.strip() == "10-K"
# Test to make sure the & character is retained
company_tag = document_tree.find(".//company")
assert company_tag.text.strip() == "Proctor & Gamble"
def test_xml_read_raises():
xml_document = XMLDocument()
with pytest.raises(NotImplementedError):
xml_document._read()
def test_from_string(sample_document):
xml_document = XMLDocument.from_string(sample_document)
type_tag = xml_document.document_tree.find(".//type")
assert type_tag.text.strip() == "10-K"
def test_read_with_stylesheet():
filename = os.path.join(FILEPATH, "..", "..", "example-docs", "factbook.xml")
stylesheet = os.path.join(FILEPATH, "..", "..", "example-docs", "factbook.xsl")
xml_document = XMLDocument.from_file(filename=filename, stylesheet=stylesheet)
doc_tree = xml_document.document_tree
# NOTE(robinson) - The table heading row plus one row for each of the four data items
assert int(doc_tree.xpath("count(//tr)")) == 5
# NOTE(robinson) - Four data elements x four attributes for each
assert int(doc_tree.xpath("count(//td)")) == 16
def test_read_with_stylesheet_warns_with_html_parser(caplog):
filename = os.path.join(FILEPATH, "..", "..", "example-docs", "factbook.xml")
stylesheet = os.path.join(FILEPATH, "..", "..", "example-docs", "factbook.xsl")
XMLDocument.from_file(filename=filename, stylesheet=stylesheet, parser=etree.HTMLParser())
assert "WARNING" in caplog.text