참고소스 수정본

This commit is contained in:
LASTA_DEV01\lasta
2026-05-12 19:40:31 +09:00
parent 0f34a451fc
commit 2e9204243d
8708 changed files with 3259488 additions and 869 deletions

View File

@@ -0,0 +1,293 @@
# Copyright (c) "Neo4j"
# Neo4j Sweden AB [https://neo4j.com]
# #
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
# #
# https://www.apache.org/licenses/LICENSE-2.0
# #
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from pathlib import Path
from unittest.mock import patch, Mock, mock_open
import pytest
from neo4j_graphrag.utils.file_handler import FileHandler, FileFormat
def test_file_handler_guess_format() -> None:
handler = FileHandler()
assert handler._guess_file_format(Path("file.json")) == FileFormat.JSON
assert handler._guess_file_format(Path("file.JSON")) == FileFormat.JSON
assert handler._guess_file_format(Path("file.yaml")) == FileFormat.YAML
assert handler._guess_file_format(Path("file.YAML")) == FileFormat.YAML
assert handler._guess_file_format(Path("file.yml")) == FileFormat.YAML
assert handler._guess_file_format(Path("file.YML")) == FileFormat.YAML
assert handler._guess_file_format(Path("file.txt")) is None
@patch("neo4j_graphrag.utils.file_handler.FileHandler._read_json")
@patch("neo4j_graphrag.utils.file_handler.FileHandler._check_file_exists")
def test_file_handler_read_json_from_read_method_happy_path(
mock_file_exists: Mock,
mock_read_json: Mock,
) -> None:
handler = FileHandler()
mock_file_exists.return_value = Path("file.json")
mock_read_json.return_value = {}
data = handler.read("file.json")
mock_read_json.assert_called_with(Path("file.json"))
assert data == {}
mock_file_exists.return_value = Path("file.JSON")
mock_read_json.return_value = {}
data = handler.read("file.JSON")
mock_read_json.assert_called_with(Path("file.JSON"))
assert data == {}
@patch("neo4j_graphrag.utils.file_handler.FileHandler._read_yaml")
@patch("neo4j_graphrag.utils.file_handler.FileHandler._check_file_exists")
def test_file_handler_read_yaml_from_read_method_happy_path(
mock_file_exists: Mock,
mock_read_yaml: Mock,
) -> None:
mock_file_exists.return_value = Path("file.yaml")
handler = FileHandler()
mock_read_yaml.return_value = {}
data = handler.read("file.yaml")
mock_read_yaml.assert_called_with(Path("file.yaml"))
assert data == {}
mock_file_exists.return_value = Path("file.yml")
mock_read_yaml.return_value = {}
data = handler.read("file.yml")
mock_read_yaml.assert_called_with(Path("file.yml"))
assert data == {}
mock_file_exists.return_value = Path("file.YAML")
mock_read_yaml.return_value = {}
data = handler.read("file.YAML")
mock_read_yaml.assert_called_with(Path("file.YAML"))
assert data == {}
@patch("neo4j_graphrag.utils.file_handler.LocalFileSystem")
def test_file_handler_read_json_method_happy_path(
mock_fs: Mock,
) -> None:
mock_fs_open = mock_open(read_data='{"data": 1}')
mock_fs.return_value.open = mock_fs_open
handler = FileHandler()
data = handler._read_json(Path("file.json"))
mock_fs_open.assert_called_once_with("file.json", "r")
assert data == {"data": 1}
@patch("neo4j_graphrag.utils.file_handler.LocalFileSystem")
def test_file_handler_read_yaml_method_happy_path(
mock_fs: Mock,
) -> None:
mock_fs_open = mock_open(
read_data="""
data: 1
"""
)
mock_fs.return_value.open = mock_fs_open
handler = FileHandler()
data = handler._read_yaml(Path("file.yaml"))
mock_fs_open.assert_called_once_with("file.yaml", "r")
assert data == {"data": 1}
@patch("neo4j_graphrag.utils.file_handler.LocalFileSystem")
def test_file_handler_read_json_file_does_not_exist(
mock_fs: Mock,
) -> None:
mock_fs.return_value.open.side_effect = FileNotFoundError
handler = FileHandler()
with pytest.raises(FileNotFoundError):
handler._read_json(Path("file.json"))
@patch("neo4j_graphrag.utils.file_handler.LocalFileSystem")
def test_file_handler_read_json_invalid_json(
mock_fs: Mock,
) -> None:
mock_fs_open = mock_open(read_data="{")
mock_fs.return_value.open = mock_fs_open
handler = FileHandler()
with pytest.raises(ValueError, match="Invalid JSON"):
handler._read_json(Path("file.json"))
@patch("neo4j_graphrag.utils.file_handler.LocalFileSystem")
def test_file_handler_read_yaml_file_does_not_exist(
mock_fs: Mock,
) -> None:
mock_fs.return_value.open.side_effect = FileNotFoundError
handler = FileHandler()
with pytest.raises(FileNotFoundError):
handler._read_yaml(Path("file.yaml"))
@patch("neo4j_graphrag.utils.file_handler.LocalFileSystem")
def test_file_handler_read_yaml_invalid_yaml(
mock_fs: Mock,
) -> None:
mock_fs_open = mock_open(
read_data="""
data: [
"""
)
mock_fs.return_value.open = mock_fs_open
handler = FileHandler()
with pytest.raises(ValueError, match="Invalid YAML"):
handler._read_yaml(Path("file.yaml"))
@patch("neo4j_graphrag.utils.file_handler.FileHandler._check_file_exists")
def test_file_handler_file_can_be_written_file(mock_file_exists: Mock) -> None:
# file does not exist
mock_file_exists.side_effect = FileNotFoundError()
handler = FileHandler()
# nothing happens = all good
handler._check_file_can_be_written(Path("file.json"))
# file exist, overwrite is False
mock_file_exists.side_effect = None
handler = FileHandler()
with pytest.raises(ValueError):
handler._check_file_can_be_written(Path("file.json"), overwrite=False)
# file exists, overwrite is True
mock_file_exists.side_effect = None
handler = FileHandler()
# nothing happens = all good
handler._check_file_can_be_written(Path("file.json"), overwrite=True)
@patch("neo4j_graphrag.utils.file_handler.LocalFileSystem")
@patch("neo4j_graphrag.utils.file_handler.json")
def test_file_handler_write_json_happy_path(
mock_json_module: Mock,
mock_fs: Mock,
) -> None:
mock_fs_open = mock_open()
mock_fs.return_value.open = mock_fs_open
file_handler = FileHandler()
file_handler._write_json({"some": "data"}, Path("file.json"))
mock_fs_open.assert_called_once_with("file.json", "w")
mock_json_module.dump.assert_called_with(
{"some": "data"}, mock_fs_open.return_value, indent=2
)
@patch("neo4j_graphrag.utils.file_handler.LocalFileSystem")
@patch("neo4j_graphrag.utils.file_handler.json")
def test_file_handler_write_json_extra_kwargs_happy_path(
mock_json_module: Mock,
mock_fs: Mock,
) -> None:
mock_fs_open = mock_open()
mock_fs.return_value.open = mock_fs_open
file_handler = FileHandler()
file_handler._write_json({"some": "data"}, Path("file.json"), indent=4, default=str)
mock_fs_open.assert_called_once_with("file.json", "w")
mock_json_module.dump.assert_called_with(
{"some": "data"}, mock_fs_open.return_value, indent=4, default=str
)
@patch("neo4j_graphrag.utils.file_handler.LocalFileSystem")
@patch("neo4j_graphrag.utils.file_handler.yaml")
def test_file_handler_write_yaml_happy_path(
mock_yaml_module: Mock,
mock_fs: Mock,
) -> None:
mock_fs_open = mock_open()
mock_fs.return_value.open = mock_fs_open
file_handler = FileHandler()
file_handler._write_yaml({"some": "data"}, Path("file.yaml"))
mock_fs_open.assert_called_once_with("file.yaml", "w")
mock_yaml_module.safe_dump.assert_called_with(
{"some": "data"},
mock_fs_open.return_value,
default_flow_style=False,
sort_keys=True,
)
@patch("neo4j_graphrag.utils.file_handler.LocalFileSystem")
@patch("neo4j_graphrag.utils.file_handler.yaml")
def test_file_handler_write_yaml_extra_kwargs_happy_path(
mock_yaml_module: Mock,
mock_fs: Mock,
) -> None:
mock_fs_open = mock_open()
mock_fs.return_value.open = mock_fs_open
file_handler = FileHandler()
file_handler._write_yaml(
{"some": "data"}, Path("file.json"), default_flow_style="toto", other_keyword=42
)
mock_fs_open.assert_called_once_with("file.json", "w")
mock_yaml_module.safe_dump.assert_called_with(
{"some": "data"},
mock_fs_open.return_value,
default_flow_style="toto",
sort_keys=True,
other_keyword=42,
)
@patch("neo4j_graphrag.utils.file_handler.FileHandler._write_json")
@patch("neo4j_graphrag.utils.file_handler.FileHandler._check_file_can_be_written")
def test_file_handler_write_json_from_write_method_happy_path(
mock_file_can_be_written: Mock,
mock_write_json: Mock,
) -> None:
handler = FileHandler()
handler.write("data", "file.json")
mock_write_json.assert_called_with("data", Path("file.json"))
@patch("neo4j_graphrag.utils.file_handler.FileHandler._write_yaml")
@patch("neo4j_graphrag.utils.file_handler.FileHandler._check_file_can_be_written")
def test_file_handler_write_yaml_from_write_method_happy_path(
mock_file_can_be_written: Mock,
mock_write_yaml: Mock,
) -> None:
handler = FileHandler()
handler.write("data", "file.yaml")
mock_write_yaml.assert_called_with("data", Path("file.yaml"))
@patch("neo4j_graphrag.utils.file_handler.FileHandler._write_yaml")
@patch("neo4j_graphrag.utils.file_handler.FileHandler._check_file_can_be_written")
def test_file_handler_write_yaml_from_write_method_overwrite_format_happy_path(
mock_file_can_be_written: Mock,
mock_write_yaml: Mock,
) -> None:
handler = FileHandler()
handler.write("data", "file.txt", format=FileFormat.YAML)
mock_write_yaml.assert_called_with("data", Path("file.txt"))
@patch("neo4j_graphrag.utils.file_handler.FileHandler._write_json")
@patch("neo4j_graphrag.utils.file_handler.FileHandler._check_file_can_be_written")
def test_file_handler_write_json_from_write_method_overwrite_format_happy_path(
mock_file_can_be_written: Mock,
mock_write_json: Mock,
) -> None:
handler = FileHandler()
handler.write("data", "file.txt", format=FileFormat.JSON)
mock_write_json.assert_called_with("data", Path("file.txt"))

View File

@@ -0,0 +1,45 @@
# Neo4j Sweden AB [https://neo4j.com]
# #
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
# #
# https://www.apache.org/licenses/LICENSE-2.0
# #
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from __future__ import annotations
from pydantic import BaseModel, ConfigDict
from neo4j_graphrag.utils.json_schema_structured_output import (
make_strict_json_schema_for_structured_output,
)
class _M(BaseModel):
model_config = ConfigDict(extra="forbid")
a: str
b: int = 1
def test_make_strict_sets_additional_properties_and_required() -> None:
raw = _M.model_json_schema()
make_strict_json_schema_for_structured_output(raw)
assert raw.get("additionalProperties") is False
assert set(raw["required"]) == {"a", "b"}
def test_make_strict_const_to_enum() -> None:
class _Const(BaseModel):
model_config = ConfigDict(extra="forbid")
x: str
raw = _Const.model_json_schema()
# pydantic may not emit const in default schema; ensure function is safe
make_strict_json_schema_for_structured_output(raw)
assert "properties" in raw

View File

@@ -0,0 +1,118 @@
# Copyright (c) "Neo4j"
# Neo4j Sweden AB [https://neo4j.com]
# #
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
# #
# https://www.apache.org/licenses/LICENSE-2.0
# #
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from unittest.mock import Mock, call, patch
from neo4j_graphrag.utils.logging import Prettifier, prettify
def test_prettifier_short_str() -> None:
p = Prettifier()
value = "ab"
pretty_value = p._prettify_str(value)
assert pretty_value == "ab"
def test_prettifier_long_str() -> None:
p = Prettifier()
value = "ab" * 200
pretty_value = p._prettify_str(value)
assert pretty_value == "ab" * 100 + "... (200 chars)"
@patch("neo4j_graphrag.utils.logging.os.environ")
def test_prettifier_str_custom_max_length(mock_env: Mock) -> None:
mock_env.return_value = {"LOGGING__MAX_STRING_LENGTH": "1"}
p = Prettifier()
value = "abc" * 100
pretty_value = p._prettify_str(value)
assert pretty_value == "a" + "... (299 chars)"
def test_prettifier_short_list() -> None:
p = Prettifier()
value = list("abc")
pretty_value = p._prettify_list(value)
assert pretty_value == ["a", "b", "c"]
def test_prettifier_long_list() -> None:
p = Prettifier()
value = list("abc") * 10
pretty_value = p._prettify_list(value)
assert pretty_value == ["a", "b", "c", "a", "b", "... (25 items)"]
@patch("neo4j_graphrag.utils.logging.os.environ")
def test_prettifier_list_custom_max_length(mock_env: Mock) -> None:
mock_env.return_value = {"LOGGING__MAX_LIST_LENGTH": "1"}
p = Prettifier()
value = list("abc") * 10
pretty_value = p._prettify_list(value)
assert pretty_value == ["a", "... (29 items)"]
def test_prettifier_list_nested() -> None:
with patch.object(
Prettifier, "_prettify_str", return_value="mocked string"
) as mock:
p = Prettifier()
value = ["abc" * 200] * 6
pretty_value = p._prettify_list(value)
mock.assert_has_calls([call("abc" * 200)] * p.max_list_length)
assert pretty_value == ["mocked string"] * 5 + ["... (1 items)"]
def test_prettifier_dict_nested() -> None:
with patch.object(
Prettifier, "_prettify_str", return_value="mocked string"
) as mock_str:
with patch.object(
Prettifier, "_prettify_list", return_value=["mocked list"]
) as mock_list:
p = Prettifier()
value = {
"key1": "string",
"key2": ["a", "list"],
}
pretty_value = p._prettify_dict(value)
mock_str.assert_has_calls([call("string")])
mock_list.assert_has_calls(
[
call(["a", "list"]),
]
)
assert pretty_value == {
"key1": "mocked string",
"key2": ["mocked list"],
}
def test_prettify_function() -> None:
assert prettify(
{
"key": {
"key0.1": "ab" * 200,
"key0.2": ["a"] * 10,
"key0.3": {"key0.3.1": "a short string"},
}
}
) == {
"key": {
"key0.1": "ab" * 100 + "... (200 chars)",
"key0.2": ["a"] * 5 + ["... (5 items)"],
"key0.3": {"key0.3.1": "a short string"},
}
}

View File

@@ -0,0 +1,32 @@
# Copyright (c) "Neo4j"
# Neo4j Sweden AB [https://neo4j.com]
# #
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
# #
# https://www.apache.org/licenses/LICENSE-2.0
# #
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from typing import Tuple, Type, cast
from neo4j_graphrag.utils.validation import issubclass_safe
def test_issubclass_safe_direct_subclass() -> None:
assert issubclass_safe(bool, int) is True
def test_issubclass_safe_not_subclass_returns_false() -> None:
# Covers the `return False` branch (line 45)
assert issubclass_safe(str, int) is False
def test_issubclass_safe_with_tuple() -> None:
# Covers the `isinstance(class_or_tuple, tuple)` branch (line 32)
assert issubclass_safe(bool, cast(Tuple[Type[object]], (str, int))) is True
assert issubclass_safe(str, cast(Tuple[Type[object]], (int, float))) is False

View File

@@ -0,0 +1,205 @@
# Copyright (c) "Neo4j"
# Neo4j Sweden AB [https://neo4j.com]
# #
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
# #
# https://www.apache.org/licenses/LICENSE-2.0
# #
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from unittest.mock import MagicMock
import pytest
from neo4j_graphrag.utils.version_utils import (
clear_version_cache,
get_version,
get_version_cached,
has_vector_index_support,
has_metadata_filtering_support,
is_version_5_23_or_above,
supports_search_clause,
)
@pytest.mark.parametrize(
"db_version,expected_version",
[
(["5.18-aura"], ((5, 18, 0), True, True)),
(["5.3-aura"], ((5, 3, 0), True, True)),
(["5.19.0"], ((5, 19, 0), False, True)),
(["4.3.5"], ((4, 3, 5), False, True)),
(["5.23.0-6698"], ((5, 23, 0), False, True)),
(["2025.01.0"], ((2025, 1, 0), False, True)),
(["2025.01-aura"], ((2025, 1, 0), True, True)),
],
)
def test_get_version(
driver: MagicMock,
db_version: list[str],
expected_version: tuple[tuple[int, ...], bool, bool],
) -> None:
"""
Verifies that the get_version function correctly parses the database
version and identifies whether the database is hosted on the Aura platform.
"""
driver.execute_query.return_value = [
[{"versions": db_version, "edition": "enterprise"}],
None,
None,
]
assert get_version(driver) == expected_version, f"Failed test case: {db_version}"
@pytest.mark.parametrize(
"version_tuple,expected_result",
[
((5, 22, 0), False),
((5, 23, 0), True),
((2025, 1, 0), True),
],
)
def test_is_version_5_23_or_above(
version_tuple: tuple[int, ...], expected_result: bool
) -> None:
"""
Ensures that the is_version_5_23_or_above function accurately determines if
a given version is 5.23 or higher.
"""
assert (
is_version_5_23_or_above(version_tuple) == expected_result
), f"Failed test case: {version_tuple}"
@pytest.mark.parametrize(
"version_tuple,expected_result",
[
((5, 10, 0), False),
((5, 11, 0), True),
((2025, 1, 0), True),
],
)
def test_has_vector_index_support(
version_tuple: tuple[int, ...], expected_result: bool
) -> None:
"""
Tests the has_vector_index_support function to confirm it correctly
identifies if the given version and platform support vector indexing.
"""
assert (
has_vector_index_support(version_tuple) == expected_result
), f"Failed test case: {version_tuple}"
@pytest.mark.parametrize(
"version_tuple,is_aura,expected_result",
[
((5, 18, 0), True, True),
((5, 18, 0), False, False),
((5, 18, 1), True, True),
((5, 18, 1), False, True),
((2025, 1, 0), True, True),
((2025, 1, 0), False, True),
],
)
def test_has_metadata_filtering_support(
version_tuple: tuple[int, ...], is_aura: bool, expected_result: bool
) -> None:
"""
Tests the has_metadata_filtering_support function to confirm it correctly
identifies if the given version and platform support vector index metadata filtering.
"""
assert (
has_metadata_filtering_support(version_tuple, is_aura) == expected_result
), f"Failed test case: {version_tuple}, is_aura: {is_aura}"
class TestGetVersionCached:
def setup_method(self) -> None:
clear_version_cache()
def test_caches_per_driver(self, driver: MagicMock) -> None:
driver.execute_query.return_value = [
[{"versions": ["2026.01.0"], "edition": "enterprise"}],
None,
None,
]
result1 = get_version_cached(driver)
result2 = get_version_cached(driver)
assert result1 == result2
# Only one actual query should have been made
assert driver.execute_query.call_count == 1
def test_different_drivers_not_shared(self) -> None:
driver1 = MagicMock()
driver1.execute_query.return_value = [
[{"versions": ["2026.01.0"], "edition": "enterprise"}],
None,
None,
]
driver2 = MagicMock()
driver2.execute_query.return_value = [
[{"versions": ["5.23.0"], "edition": "community"}],
None,
None,
]
r1 = get_version_cached(driver1)
r2 = get_version_cached(driver2)
assert r1 == ((2026, 1, 0), False, True)
assert r2 == ((5, 23, 0), False, False)
def test_clear_cache(self, driver: MagicMock) -> None:
driver.execute_query.return_value = [
[{"versions": ["2026.01.0"], "edition": "enterprise"}],
None,
None,
]
get_version_cached(driver)
clear_version_cache()
get_version_cached(driver)
assert driver.execute_query.call_count == 2
class TestSupportsSearchClause:
def setup_method(self) -> None:
clear_version_cache()
@pytest.mark.parametrize(
"version_str,expected",
[
("2026.01.0", True),
("2026.02.0", True),
("2027.01.0", True),
("2025.12.0", False),
("5.23.0", False),
("5.26.0", False),
],
)
def test_version_check(
self, driver: MagicMock, version_str: str, expected: bool
) -> None:
driver.execute_query.return_value = [
[{"versions": [version_str], "edition": "enterprise"}],
None,
None,
]
clear_version_cache()
assert supports_search_clause(driver) is expected
def test_connection_error_returns_false(self, driver: MagicMock) -> None:
driver.execute_query.side_effect = Exception("connection refused")
assert supports_search_clause(driver) is False
def test_uses_cache(self, driver: MagicMock) -> None:
driver.execute_query.return_value = [
[{"versions": ["2026.01.0"], "edition": "enterprise"}],
None,
None,
]
supports_search_clause(driver)
supports_search_clause(driver)
assert driver.execute_query.call_count == 1