2025-03-10 14:04:07 -07:00
|
|
|
# Copyright (c) 2024 Microsoft Corporation.
|
|
|
|
# Licensed under the MIT License
|
|
|
|
|
2025-06-12 16:14:39 -07:00
|
|
|
from graphrag.config.enums import InputFileType
|
2025-03-10 14:04:07 -07:00
|
|
|
from graphrag.config.models.input_config import InputConfig
|
2025-06-12 16:14:39 -07:00
|
|
|
from graphrag.config.models.storage_config import StorageConfig
|
2025-03-10 14:04:07 -07:00
|
|
|
from graphrag.index.input.factory import create_input
|
2025-06-12 16:14:39 -07:00
|
|
|
from graphrag.utils.api import create_storage_from_config
|
2025-03-10 14:04:07 -07:00
|
|
|
|
|
|
|
|
|
|
|
async def test_json_loader_one_file_one_object():
|
|
|
|
config = InputConfig(
|
2025-06-12 16:14:39 -07:00
|
|
|
storage=StorageConfig(
|
|
|
|
base_dir="tests/unit/indexing/input/data/one-json-one-object",
|
|
|
|
),
|
2025-03-10 14:04:07 -07:00
|
|
|
file_type=InputFileType.json,
|
|
|
|
file_pattern=".*\\.json$",
|
|
|
|
)
|
2025-06-12 16:14:39 -07:00
|
|
|
storage = create_storage_from_config(config.storage)
|
|
|
|
documents = await create_input(config=config, storage=storage)
|
2025-03-10 14:04:07 -07:00
|
|
|
assert documents.shape == (1, 4)
|
|
|
|
assert documents["title"].iloc[0] == "input.json"
|
|
|
|
|
|
|
|
|
|
|
|
async def test_json_loader_one_file_multiple_objects():
|
|
|
|
config = InputConfig(
|
2025-06-12 16:14:39 -07:00
|
|
|
storage=StorageConfig(
|
|
|
|
base_dir="tests/unit/indexing/input/data/one-json-multiple-objects",
|
|
|
|
),
|
2025-03-10 14:04:07 -07:00
|
|
|
file_type=InputFileType.json,
|
|
|
|
file_pattern=".*\\.json$",
|
|
|
|
)
|
2025-06-12 16:14:39 -07:00
|
|
|
storage = create_storage_from_config(config.storage)
|
|
|
|
documents = await create_input(config=config, storage=storage)
|
2025-03-10 14:04:07 -07:00
|
|
|
print(documents)
|
|
|
|
assert documents.shape == (3, 4)
|
|
|
|
assert documents["title"].iloc[0] == "input.json"
|
|
|
|
|
|
|
|
|
|
|
|
async def test_json_loader_one_file_with_title():
|
|
|
|
config = InputConfig(
|
2025-06-12 16:14:39 -07:00
|
|
|
storage=StorageConfig(
|
|
|
|
base_dir="tests/unit/indexing/input/data/one-json-one-object",
|
|
|
|
),
|
2025-03-10 14:04:07 -07:00
|
|
|
file_type=InputFileType.json,
|
|
|
|
file_pattern=".*\\.json$",
|
|
|
|
title_column="title",
|
|
|
|
)
|
2025-06-12 16:14:39 -07:00
|
|
|
storage = create_storage_from_config(config.storage)
|
|
|
|
documents = await create_input(config=config, storage=storage)
|
2025-03-10 14:04:07 -07:00
|
|
|
assert documents.shape == (1, 4)
|
|
|
|
assert documents["title"].iloc[0] == "Hello"
|
|
|
|
|
|
|
|
|
|
|
|
async def test_json_loader_one_file_with_metadata():
|
|
|
|
config = InputConfig(
|
2025-06-12 16:14:39 -07:00
|
|
|
storage=StorageConfig(
|
|
|
|
base_dir="tests/unit/indexing/input/data/one-json-one-object",
|
|
|
|
),
|
2025-03-10 14:04:07 -07:00
|
|
|
file_type=InputFileType.json,
|
|
|
|
file_pattern=".*\\.json$",
|
|
|
|
title_column="title",
|
|
|
|
metadata=["title"],
|
|
|
|
)
|
2025-06-12 16:14:39 -07:00
|
|
|
storage = create_storage_from_config(config.storage)
|
|
|
|
documents = await create_input(config=config, storage=storage)
|
2025-03-10 14:04:07 -07:00
|
|
|
assert documents.shape == (1, 5)
|
|
|
|
assert documents["metadata"][0] == {"title": "Hello"}
|
|
|
|
|
|
|
|
|
|
|
|
async def test_json_loader_multiple_files():
|
|
|
|
config = InputConfig(
|
2025-06-12 16:14:39 -07:00
|
|
|
storage=StorageConfig(
|
|
|
|
base_dir="tests/unit/indexing/input/data/multiple-jsons",
|
|
|
|
),
|
2025-03-10 14:04:07 -07:00
|
|
|
file_type=InputFileType.json,
|
|
|
|
file_pattern=".*\\.json$",
|
|
|
|
)
|
2025-06-12 16:14:39 -07:00
|
|
|
storage = create_storage_from_config(config.storage)
|
|
|
|
documents = await create_input(config=config, storage=storage)
|
2025-03-10 14:04:07 -07:00
|
|
|
assert documents.shape == (4, 4)
|