1
- import time
1
+ import os
2
2
from typing import Any
3
3
4
4
import pytest
5
5
import pytest_asyncio
6
- from neo4j import AsyncGraphDatabase , Driver , GraphDatabase
7
- from neo4j . exceptions import DatabaseError
6
+ from neo4j import AsyncGraphDatabase
7
+ from testcontainers . neo4j import Neo4jContainer
8
8
9
9
from mcp_neo4j_cypher .server import create_mcp_server
10
10
11
+ neo4j = (
12
+ Neo4jContainer ("neo4j:latest" )
13
+ .with_env ("NEO4J_apoc_export_file_enabled" , "true" )
14
+ .with_env ("NEO4J_apoc_import_file_enabled" , "true" )
15
+ .with_env ("NEO4J_apoc_import_file_use__neo4j__config" , "true" )
16
+ .with_env ("NEO4J_PLUGINS" , '["apoc"]' )
17
+ )
11
18
12
- @pytest_asyncio .fixture (scope = "session" )
13
- async def async_neo4j_driver ():
19
+
20
+ @pytest .fixture (scope = "module" , autouse = True )
21
+ def setup (request ):
22
+ neo4j .start ()
23
+
24
+ def remove_container ():
25
+ neo4j .get_driver ().close ()
26
+ neo4j .stop ()
27
+
28
+ request .addfinalizer (remove_container )
29
+ os .environ ["NEO4J_URI" ] = neo4j .get_connection_url ()
30
+ os .environ ["NEO4J_HOST" ] = neo4j .get_container_host_ip ()
31
+ os .environ ["NEO4J_PORT" ] = neo4j .get_exposed_port (7687 )
32
+
33
+ yield neo4j
34
+
35
+
36
+ @pytest_asyncio .fixture (scope = "function" )
37
+ async def async_neo4j_driver (setup : Neo4jContainer ):
14
38
driver = AsyncGraphDatabase .driver (
15
- "neo4j://localhost:7687" , auth = ("neo4j" , "testingpassword" )
39
+ setup . get_connection_url () , auth = (setup . username , setup . password )
16
40
)
17
41
try :
18
42
yield driver
19
43
finally :
20
44
await driver .close ()
21
45
22
46
23
- @pytest .fixture (scope = "session" )
24
- def sync_neo4j_driver ():
25
- uri = "neo4j://localhost:7687"
26
- auth = ("neo4j" , "testingpassword" )
27
- driver = GraphDatabase .driver (uri , auth = auth )
28
- yield driver
29
- driver .close ()
30
-
31
-
32
- @pytest .fixture (scope = "session" )
33
- def healthcheck (sync_neo4j_driver : Driver ):
34
- """Confirm that Neo4j is running before running IT."""
35
-
36
- print ("Confirming Neo4j is running..." )
37
- attempts = 0
38
- success = False
39
- print ("\n Waiting for Neo4j to Start...\n " )
40
- time .sleep (3 )
41
- while not success and attempts < 3 :
42
- try :
43
- print (f"Attempt { attempts + 1 } to connect to Neo4j..." )
44
- with sync_neo4j_driver .session (database = "neo4j" ) as session :
45
- session .run ("RETURN 1" )
46
- success = True
47
- print ("Neo4j is running!" )
48
- except Exception as e :
49
- attempts += 1
50
- print (
51
- f"failed connection { attempts } | waiting { (1 + attempts ) * 2 } seconds..."
52
- )
53
- print (f"Error: { e } " )
54
- time .sleep ((1 + attempts ) * 2 )
55
- if not success :
56
- raise DatabaseError ()
57
- yield
58
-
59
-
60
- @pytest_asyncio .fixture (scope = "session" )
47
+ @pytest_asyncio .fixture (scope = "function" )
61
48
async def mcp_server (async_neo4j_driver ):
62
49
mcp = create_mcp_server (async_neo4j_driver , "neo4j" )
63
50
64
51
return mcp
65
52
66
53
67
- @pytest .fixture (scope = "session " )
68
- def init_data (sync_neo4j_driver : Driver , clear_data : Any ):
69
- with sync_neo4j_driver .session (database = "neo4j" ) as session :
54
+ @pytest .fixture (scope = "function " )
55
+ def init_data (setup : Neo4jContainer , clear_data : Any ):
56
+ with setup . get_driver () .session (database = "neo4j" ) as session :
70
57
session .run ("CREATE (a:Person {name: 'Alice', age: 30})" )
71
58
session .run ("CREATE (b:Person {name: 'Bob', age: 25})" )
72
59
session .run ("CREATE (c:Person {name: 'Charlie', age: 35})" )
@@ -78,7 +65,7 @@ def init_data(sync_neo4j_driver: Driver, clear_data: Any):
78
65
)
79
66
80
67
81
- @pytest .fixture (scope = "session " )
82
- def clear_data (sync_neo4j_driver : Driver ):
83
- with sync_neo4j_driver .session (database = "neo4j" ) as session :
68
+ @pytest .fixture (scope = "function " )
69
+ def clear_data (setup : Neo4jContainer ):
70
+ with setup . get_driver () .session (database = "neo4j" ) as session :
84
71
session .run ("MATCH (n) DETACH DELETE n" )
0 commit comments