2022-10-04 18:51:11 +05:00
|
|
|
import json
|
2023-02-27 17:30:54 +01:00
|
|
|
import os
|
|
|
|
|
2022-10-04 18:51:11 +05:00
|
|
|
import pytest
|
|
|
|
|
2023-02-27 17:30:54 +01:00
|
|
|
from unstructured import utils
|
2022-10-04 18:51:11 +05:00
|
|
|
|
|
|
|
|
2023-02-27 17:30:54 +01:00
|
|
|
@pytest.fixture()
|
2022-10-04 18:51:11 +05:00
|
|
|
def input_data():
|
|
|
|
return [
|
|
|
|
{"text": "This is a sentence."},
|
|
|
|
{"text": "This is another sentence.", "meta": {"score": 0.1}},
|
|
|
|
]
|
|
|
|
|
|
|
|
|
2023-02-27 17:30:54 +01:00
|
|
|
@pytest.fixture()
|
2022-10-04 18:51:11 +05:00
|
|
|
def output_jsonl_file(tmp_path):
|
|
|
|
return os.path.join(tmp_path, "output.jsonl")
|
|
|
|
|
|
|
|
|
2023-02-27 17:30:54 +01:00
|
|
|
@pytest.fixture()
|
2022-10-04 18:51:11 +05:00
|
|
|
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)
|
2023-02-27 17:30:54 +01:00
|
|
|
with open(output_jsonl_file) as output_file:
|
2022-10-04 18:51:11 +05:00
|
|
|
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
|