mirror of
https://github.com/Unstructured-IO/unstructured.git
synced 2025-06-27 02:30:08 +00:00

* Add `requires_dependencies` decorator * Use `required_dependencies` on Reddit & S3 * Fix bug in `requires_dependencies` To used named args the decorator needs to be also wrapped * Add `requires_dependencies` integration tests * Add `requires_dependencies` in `Competition.md` * Update `CHANGELOG.md` * Bump version 0.4.16-dev5 * Ignore `F401` unused imports in `requires_dependencies` tests * Apply suggestions from code review * Add `functools.wrap` to keep docs, & annotations * Use `requires_dependencies` in `GitHubConnector`
85 lines
2.1 KiB
Python
85 lines
2.1 KiB
Python
import json
|
|
import os
|
|
|
|
import pytest
|
|
|
|
from unstructured import utils
|
|
|
|
|
|
@pytest.fixture()
|
|
def input_data():
|
|
return [
|
|
{"text": "This is a sentence."},
|
|
{"text": "This is another sentence.", "meta": {"score": 0.1}},
|
|
]
|
|
|
|
|
|
@pytest.fixture()
|
|
def output_jsonl_file(tmp_path):
|
|
return os.path.join(tmp_path, "output.jsonl")
|
|
|
|
|
|
@pytest.fixture()
|
|
def input_jsonl_file(tmp_path, input_data):
|
|
file_path = os.path.join(tmp_path, "input.jsonl")
|
|
with open(file_path, "w+") as input_file:
|
|
input_file.writelines([json.dumps(obj) + "\n" for obj in input_data])
|
|
return file_path
|
|
|
|
|
|
def test_save_as_jsonl(input_data, output_jsonl_file):
|
|
utils.save_as_jsonl(input_data, output_jsonl_file)
|
|
with open(output_jsonl_file) as output_file:
|
|
file_data = [json.loads(line) for line in output_file]
|
|
assert file_data == input_data
|
|
|
|
|
|
def test_read_as_jsonl(input_jsonl_file, input_data):
|
|
file_data = utils.read_from_jsonl(input_jsonl_file)
|
|
assert file_data == input_data
|
|
|
|
|
|
def test_requires_dependencies_decorator():
|
|
@utils.requires_dependencies(dependencies="numpy")
|
|
def test_func():
|
|
import numpy # noqa: F401
|
|
|
|
test_func()
|
|
|
|
|
|
def test_requires_dependencies_decorator_multiple():
|
|
@utils.requires_dependencies(dependencies=["numpy", "pandas"])
|
|
def test_func():
|
|
import numpy # noqa: F401
|
|
import pandas # noqa: F401
|
|
|
|
test_func()
|
|
|
|
|
|
def test_requires_dependencies_decorator_import_error():
|
|
@utils.requires_dependencies(dependencies="not_a_package")
|
|
def test_func():
|
|
import not_a_package # noqa: F401
|
|
|
|
with pytest.raises(ImportError):
|
|
test_func()
|
|
|
|
|
|
def test_requires_dependencies_decorator_import_error_multiple():
|
|
@utils.requires_dependencies(dependencies=["not_a_package", "numpy"])
|
|
def test_func():
|
|
import not_a_package # noqa: F401
|
|
import numpy # noqa: F401
|
|
|
|
with pytest.raises(ImportError):
|
|
test_func()
|
|
|
|
|
|
def test_requires_dependencies_decorator_in_class():
|
|
@utils.requires_dependencies(dependencies="numpy")
|
|
class TestClass:
|
|
def __init__(self):
|
|
import numpy # noqa: F401
|
|
|
|
TestClass()
|