这是一个使用 Neo4j 图数据库的 Python 项目示例。
quanttide-example-of-knowledge-engineering/
├── src/
│ └── database/
├── tests/ # 单元测试目录
│ └── test_neo4j_client.py
├── integration_tests/ # 集成测试目录
│ └── test_neo4j_integration.py
├── pytest.ini
└── README.md
- Neo4j 数据库连接管理
- 节点的创建和查询
- 关系的创建和管理
- 完整的单元测试
- 环境变量配置支持
- 安装依赖
# 使用 pip
pip install -e .
# 开发环境额外依赖(用于运行测试)
pip install -e ".[dev]"- 配置环境变量
复制 .env.example 文件到 .env 并修改配置:
cp .env.example .env
# 编辑 .env 文件,设置你的 Neo4j 连接信息- 运行示例
python -m src项目包含两类测试,分别位于不同目录:
- 单元测试:
tests/目录 - 集成测试:
integration_tests/目录
运行测试的方式:
- 运行单元测试(不需要数据库):
pytest tests/- 运行集成测试(需要 Neo4j 数据库):
pytest integration_tests/- 运行所有测试:
pytest tests/ integration_tests/- 运行特定测试文件:
# 运行特定单元测试
pytest tests/test_neo4j_client.py -v
# 运行特定集成测试
pytest integration_tests/test_neo4j_integration.py -v注意:运行集成测试前请确保:
- Neo4j 数据库已启动且可访问
.env文件中的连接信息配置正确
from src.database import Neo4jClient
# 创建客户端实例
with Neo4jClient() as client:
# 创建节点
person = client.create_node("Person", {
"name": "张三",
"age": 30
})
# 获取节点
found = client.get_node("Person", {"name": "张三"})
# 创建关系
client.create_relationship(
from_label="Person",
from_props={"name": "张三"},
to_label="Person",
to_props={"name": "李四"},
rel_type="KNOWS",
rel_props={"since": 2023}
)