1- import time
1+ import os
22from typing import Any
33
44import pytest
55import 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
88
99from mcp_neo4j_cypher .server import create_mcp_server
1010
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+ )
1118
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 ):
1438 driver = AsyncGraphDatabase .driver (
15- "neo4j://localhost:7687" , auth = ("neo4j" , "testingpassword" )
39+ setup . get_connection_url () , auth = (setup . username , setup . password )
1640 )
1741 try :
1842 yield driver
1943 finally :
2044 await driver .close ()
2145
2246
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" )
6148async def mcp_server (async_neo4j_driver ):
6249 mcp = create_mcp_server (async_neo4j_driver , "neo4j" )
6350
6451 return mcp
6552
6653
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 :
7057 session .run ("CREATE (a:Person {name: 'Alice', age: 30})" )
7158 session .run ("CREATE (b:Person {name: 'Bob', age: 25})" )
7259 session .run ("CREATE (c:Person {name: 'Charlie', age: 35})" )
@@ -78,7 +65,7 @@ def init_data(sync_neo4j_driver: Driver, clear_data: Any):
7865 )
7966
8067
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 :
8471 session .run ("MATCH (n) DETACH DELETE n" )
0 commit comments