Skip to content

Commit 1252add

Browse files
committed
add database.port to settings.py, and update conftest
1 parent 718e219 commit 1252add

File tree

2 files changed

+39
-4
lines changed

2 files changed

+39
-4
lines changed

src/datajoint/settings.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -278,6 +278,7 @@ def __setitem__(self, key, value):
278278
"database.host",
279279
"database.user",
280280
"database.password",
281+
"database.port",
281282
"external.aws_access_key_id",
282283
"external.aws_secret_access_key",
283284
"loglevel",
@@ -288,6 +289,7 @@ def __setitem__(self, key, value):
288289
"DJ_HOST",
289290
"DJ_USER",
290291
"DJ_PASS",
292+
"DJ_PORT",
291293
"DJ_AWS_ACCESS_KEY_ID",
292294
"DJ_AWS_SECRET_ACCESS_KEY",
293295
"DJ_LOG_LEVEL",
@@ -296,6 +298,14 @@ def __setitem__(self, key, value):
296298
)
297299
if v is not None
298300
}
301+
302+
# Convert DJ_PORT from string to int if present
303+
if "database.port" in mapping and mapping["database.port"] is not None:
304+
try:
305+
mapping["database.port"] = int(mapping["database.port"])
306+
except ValueError:
307+
logger.warning(f"Invalid DJ_PORT value: {mapping['database.port']}, using default port 3306")
308+
del mapping["database.port"]
299309
if mapping:
300310
logger.info(f"Overloaded settings {tuple(mapping)} from environment variables.")
301311
config.update(mapping)

tests/conftest.py

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,18 @@
3434
logger = logging.getLogger(__name__)
3535

3636

37+
def pytest_sessionstart(session):
38+
"""Called after the Session object has been created and configured."""
39+
# This runs very early, before most fixtures, but we don't have container info yet
40+
pass
41+
42+
43+
def pytest_configure(config):
44+
"""Called after command line options have been parsed."""
45+
# This runs before pytest_sessionstart but still too early for containers
46+
pass
47+
48+
3749

3850

3951
# Global container registry for cleanup
@@ -313,17 +325,30 @@ def db_creds_test(mysql_container) -> Dict:
313325
)
314326

315327

316-
@pytest.fixture(scope="session")
317-
def db_creds_root(mysql_container) -> Dict:
328+
@pytest.fixture(scope="session", autouse=True)
329+
def configure_datajoint_for_containers(mysql_container):
330+
"""Configure DataJoint to use pytest-managed containers. Runs automatically for all tests."""
318331
_, host, port = mysql_container
319-
# Set environment variables for DataJoint at module level
332+
333+
# Set environment variables FIRST - these will be inherited by subprocesses
334+
logger.info(f"🔧 Setting environment: DJ_HOST={host}, DJ_PORT={port}")
320335
os.environ["DJ_HOST"] = host
321336
os.environ["DJ_PORT"] = str(port)
322337

323-
# Also update DataJoint's configuration directly
338+
# Verify the environment variables were set
339+
logger.info(f"🔧 Environment after setting: DJ_HOST={os.environ.get('DJ_HOST')}, DJ_PORT={os.environ.get('DJ_PORT')}")
340+
341+
# Also update DataJoint's configuration directly for in-process connections
324342
dj.config["database.host"] = host
325343
dj.config["database.port"] = port
326344

345+
logger.info(f"🔧 Configured DataJoint to use MySQL container at {host}:{port}")
346+
return host, port # Return values so other fixtures can use them
347+
348+
349+
@pytest.fixture(scope="session")
350+
def db_creds_root(mysql_container) -> Dict:
351+
_, host, port = mysql_container
327352
return dict(
328353
host=f"{host}:{port}",
329354
user=os.getenv("DJ_USER", "root"),

0 commit comments

Comments
 (0)