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

108 lines
3.3 KiB
Python

import datetime
import os
import pathlib
import docx
import openpyxl
import pytest
import unstructured.file_utils.metadata as meta
DIRECTORY = pathlib.Path(__file__).parent.resolve()
EXAMPLE_JPG_FILENAME = os.path.join(DIRECTORY, "..", "..", "example-docs", "example.jpg")
def test_get_docx_metadata_from_filename(tmpdir):
filename = os.path.join(tmpdir, "test-doc.docx")
document = docx.Document()
document.add_paragraph("Lorem ipsum dolor sit amet.")
document.core_properties.author = "Mr. Miagi"
document.save(filename)
metadata = meta.get_docx_metadata(filename=filename)
assert metadata.author == "Mr. Miagi"
assert metadata.to_dict()["author"] == "Mr. Miagi"
def test_get_docx_metadata_from_file(tmpdir):
filename = os.path.join(tmpdir, "test-doc.docx")
document = docx.Document()
document.add_paragraph("Lorem ipsum dolor sit amet.")
document.core_properties.author = "Mr. Miagi"
document.save(filename)
with open(filename, "rb") as f:
metadata = meta.get_docx_metadata(file=f)
assert metadata.author == "Mr. Miagi"
def test_get_docx_metadata_raises_without_file_or_filename():
with pytest.raises(FileNotFoundError):
meta.get_docx_metadata()
def test_get_xlsx_metadata_from_filename(tmpdir):
filename = os.path.join(tmpdir, "test-excel.xlsx")
workbook = openpyxl.Workbook()
workbook.properties.creator = "Mr. Miagi"
workbook.save(filename)
metadata = meta.get_xlsx_metadata(filename=filename)
metadata.author = "Mr. Miagi"
def test_get_xlsx_metadata_from_file(tmpdir):
filename = os.path.join(tmpdir, "test-excel.xlsx")
workbook = openpyxl.Workbook()
workbook.properties.creator = "Mr. Miagi"
workbook.save(filename)
with open(filename, "rb") as f:
metadata = meta.get_xlsx_metadata(file=f)
metadata.author = "Mr. Miagi"
def test_get_xlsx_metadata_raises_without_file_or_filename():
with pytest.raises(FileNotFoundError):
meta.get_xlsx_metadata()
def test_get_jpg_metadata_from_filename():
metadata = meta.get_jpg_metadata(filename=EXAMPLE_JPG_FILENAME)
assert metadata.modified == datetime.datetime(2003, 12, 14, 12, 1, 44)
assert metadata.exif_data["Make"] == "Canon"
def test_get_jpg_metadata_from_file():
with open(EXAMPLE_JPG_FILENAME, "rb") as f:
metadata = meta.get_jpg_metadata(file=f)
assert metadata.modified == datetime.datetime(2003, 12, 14, 12, 1, 44)
assert metadata.exif_data["Make"] == "Canon"
def test_get_jpg_metadata_raises_without_file_or_filename():
with pytest.raises(FileNotFoundError):
meta.get_jpg_metadata()
def test_get_exif_datetime():
exif_data = {"DateTime": "2022:12:23 15:49:00", "DateTimeOriginal": "2020:12:14 12:00:00"}
date = meta._get_exif_datetime(exif_data, "DateTime")
assert date == datetime.datetime(2022, 12, 23, 15, 49, 0)
def test_get_exif_datetime_ignores_bad_formats():
exif_data = {"DateTime": "2022-12-23TZ15:49:00", "DateTimeOriginal": "2020:12:14 12:00:00"}
date = meta._get_exif_datetime(exif_data, "DateTime")
assert date is None
def test_get_exif_datetime_ignores_missing_key():
exif_data = {"Datetime": "2022-12-23TZ15:49:00", "DateTimeOriginal": "2020:12:14 12:00:00"}
date = meta._get_exif_datetime(exif_data, "DateTimeDigitized")
assert date is None