Skip to content

Commit e8e4078

Browse files
committed
Remove static variables from neo4j.
1 parent 1d6e402 commit e8e4078

File tree

1 file changed

+10
-22
lines changed

1 file changed

+10
-22
lines changed

neo4j/testcontainers/neo4j/__init__.py

Lines changed: 10 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,10 @@
1515

1616
from neo4j import Driver, GraphDatabase
1717

18+
from testcontainers.core.config import TIMEOUT
1819
from testcontainers.core.generic import DbContainer
1920
from testcontainers.core.waiting_utils import wait_container_is_ready, wait_for_logs
21+
from typing import Optional
2022

2123

2224
class Neo4jContainer(DbContainer):
@@ -35,26 +37,17 @@ class Neo4jContainer(DbContainer):
3537
... result = session.run("MATCH (n) RETURN n LIMIT 1")
3638
... record = result.single()
3739
"""
38-
39-
# The official image requires a change of password on startup.
40-
NEO4J_ADMIN_PASSWORD = os.environ.get("NEO4J_ADMIN_PASSWORD", "password")
41-
# Default port for the binary Bolt protocol.
42-
DEFAULT_BOLT_PORT = 7687
43-
AUTH_FORMAT = "neo4j/{password}"
44-
NEO4J_STARTUP_TIMEOUT_SECONDS = 10
45-
NEO4J_USER = "neo4j"
46-
47-
def __init__(self, image: str = "neo4j:latest", **kwargs) -> None:
40+
def __init__(self, image: str = "neo4j:latest", *, bolt_port: int = 7687,
41+
password: Optional[str] = None, username: Optional[str] = None, **kwargs) -> None:
4842
super(Neo4jContainer, self).__init__(image, **kwargs)
49-
self.bolt_port = Neo4jContainer.DEFAULT_BOLT_PORT
43+
self.username = username or os.environ.get("NEO4J_USER", "password")
44+
self.password = password or os.environ.get("NEO4J_PASSWORD", "password")
45+
self.bolt_port = bolt_port
5046
self.with_exposed_ports(self.bolt_port)
5147
self._driver = None
5248

5349
def _configure(self) -> None:
54-
self.with_env(
55-
"NEO4J_AUTH",
56-
Neo4jContainer.AUTH_FORMAT.format(password=Neo4jContainer.NEO4J_ADMIN_PASSWORD)
57-
)
50+
self.with_env("NEO4J_AUTH", f"neo4j/{self.password}")
5851

5952
def get_connection_url(self) -> str:
6053
return "{dialect}://{host}:{port}".format(
@@ -65,12 +58,7 @@ def get_connection_url(self) -> str:
6558

6659
@wait_container_is_ready()
6760
def _connect(self) -> None:
68-
# First we wait for Neo4j to say it's listening
69-
wait_for_logs(
70-
self,
71-
"Remote interface available at",
72-
Neo4jContainer.NEO4J_STARTUP_TIMEOUT_SECONDS,
73-
)
61+
wait_for_logs(self, "Remote interface available at", TIMEOUT)
7462

7563
# Then we actually check that the container really is listening
7664
with self.get_driver() as driver:
@@ -81,6 +69,6 @@ def _connect(self) -> None:
8169
def get_driver(self, **kwargs) -> Driver:
8270
return GraphDatabase.driver(
8371
self.get_connection_url(),
84-
auth=(Neo4jContainer.NEO4J_USER, Neo4jContainer.NEO4J_ADMIN_PASSWORD),
72+
auth=(self.username, self.password),
8573
**kwargs
8674
)

0 commit comments

Comments
 (0)