Skip to content

Commit 1538ad8

Browse files
committed
Add e2e test
1 parent d244774 commit 1538ad8

File tree

1 file changed

+100
-18
lines changed

1 file changed

+100
-18
lines changed

tests/e2e/experimental/test_simplekgpipeline_e2e.py

Lines changed: 100 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -34,22 +34,9 @@ def clear_db(driver: Driver) -> Any:
3434
yield
3535

3636

37-
@pytest.mark.asyncio
38-
@pytest.mark.usefixtures("setup_neo4j_for_kg_construction")
39-
async def test_pipeline_builder_happy_path(
40-
harry_potter_text: str,
41-
llm: MagicMock,
42-
embedder: MagicMock,
43-
driver: neo4j.Driver,
44-
) -> None:
45-
"""When everything works as expected, extracted entities, relations and text
46-
chunks must be in the DB
47-
"""
48-
driver.execute_query("MATCH (n) DETACH DELETE n")
49-
embedder.embed_query.return_value = [1, 2, 3]
50-
llm.ainvoke.side_effect = [
51-
LLMResponse(
52-
content="""{
37+
@pytest.fixture(scope="module")
38+
def llm_json_response_3_nodes_2_relationships() -> str:
39+
return """{
5340
"nodes": [
5441
{
5542
"id": "0",
@@ -86,8 +73,26 @@ async def test_pipeline_builder_happy_path(
8673
}
8774
]
8875
}"""
89-
),
90-
LLMResponse(content='{"nodes": [], "relationships": []}'),
76+
77+
78+
@pytest.mark.asyncio
79+
@pytest.mark.usefixtures("setup_neo4j_for_kg_construction")
80+
async def test_pipeline_builder_happy_path_legacy_schema(
81+
harry_potter_text: str,
82+
llm: MagicMock,
83+
embedder: MagicMock,
84+
driver: neo4j.Driver,
85+
llm_json_response_3_nodes_2_relationships: str,
86+
) -> None:
87+
"""When everything works as expected, extracted entities, relations and text
88+
chunks must be in the DB
89+
"""
90+
driver.execute_query("MATCH (n) DETACH DELETE n")
91+
embedder.embed_query.return_value = [1, 2, 3]
92+
llm.ainvoke.side_effect = [
93+
LLMResponse(
94+
content=llm_json_response_3_nodes_2_relationships,
95+
)
9196
]
9297

9398
# Instantiate Entity and Relation objects
@@ -121,6 +126,83 @@ async def test_pipeline_builder_happy_path(
121126
# Run the knowledge graph building process with text input
122127
await kg_builder_text.run_async(text=harry_potter_text)
123128

129+
# check the content of the graph:
130+
# check lexical graph content
131+
records, _, _ = driver.execute_query(
132+
"MATCH (start:chunkNodeLabel) RETURN start"
133+
)
134+
assert len(records) == 1
135+
136+
# check entity -> chunk relationships
137+
records, _, _ = driver.execute_query(
138+
"MATCH (chunk:chunkNodeLabel)<-[rel:FROM_CHUNK]-(entity:__Entity__) RETURN chunk, rel, entity"
139+
)
140+
assert len(records) == 3 # three entities according to mocked LLMResponse
141+
142+
143+
@pytest.mark.asyncio
144+
@pytest.mark.usefixtures("setup_neo4j_for_kg_construction")
145+
async def test_pipeline_builder_happy_path(
146+
harry_potter_text: str,
147+
llm: MagicMock,
148+
embedder: MagicMock,
149+
driver: neo4j.Driver,
150+
llm_json_response_3_nodes_2_relationships: str,
151+
) -> None:
152+
"""When everything works as expected, extracted entities, relations and text
153+
chunks must be in the DB
154+
"""
155+
driver.execute_query("MATCH (n) DETACH DELETE n")
156+
embedder.embed_query.return_value = [1, 2, 3]
157+
llm.ainvoke.side_effect = [
158+
LLMResponse(
159+
content=llm_json_response_3_nodes_2_relationships,
160+
)
161+
]
162+
163+
# Instantiate schema
164+
entities = ["Person"]
165+
relations = []
166+
potential_schema = []
167+
schema = {
168+
"node_types": entities,
169+
"relationship_types": relations,
170+
"patterns": potential_schema,
171+
"additional_node_types": False,
172+
}
173+
174+
# Additional arguments
175+
lexical_graph_config = LexicalGraphConfig(chunk_node_label="chunkNodeLabel")
176+
from_pdf = False
177+
on_error = "RAISE"
178+
179+
# Create an instance of the SimpleKGPipeline
180+
kg_builder_text = SimpleKGPipeline(
181+
llm=llm,
182+
driver=driver,
183+
embedder=embedder,
184+
schema=schema,
185+
from_pdf=from_pdf,
186+
on_error=on_error,
187+
lexical_graph_config=lexical_graph_config,
188+
)
189+
190+
# Run the knowledge graph building process with text input
191+
await kg_builder_text.run_async(text=harry_potter_text)
192+
193+
# check the content of the graph:
194+
# check lexical graph content
195+
records, _, _ = driver.execute_query(
196+
"MATCH (start:chunkNodeLabel) RETURN start"
197+
)
198+
assert len(records) == 1
199+
200+
# check entity -> chunk relationships
201+
records, _, _ = driver.execute_query(
202+
"MATCH (chunk:chunkNodeLabel)<-[rel:FROM_CHUNK]-(entity:__Entity__) RETURN chunk, rel, entity"
203+
)
204+
assert len(records) == 2 # only two persons
205+
124206

125207
@pytest.mark.asyncio
126208
@pytest.mark.usefixtures("setup_neo4j_for_kg_construction")

0 commit comments

Comments
 (0)