@@ -34,22 +34,9 @@ def clear_db(driver: Driver) -> Any:
34
34
yield
35
35
36
36
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 """{
53
40
"nodes": [
54
41
{
55
42
"id": "0",
@@ -86,8 +73,26 @@ async def test_pipeline_builder_happy_path(
86
73
}
87
74
]
88
75
}"""
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
+ )
91
96
]
92
97
93
98
# Instantiate Entity and Relation objects
@@ -121,6 +126,83 @@ async def test_pipeline_builder_happy_path(
121
126
# Run the knowledge graph building process with text input
122
127
await kg_builder_text .run_async (text = harry_potter_text )
123
128
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
+
124
206
125
207
@pytest .mark .asyncio
126
208
@pytest .mark .usefixtures ("setup_neo4j_for_kg_construction" )
0 commit comments