diff --git a/.github/workflows/python-package.yml b/.github/workflows/python-package.yml index 8fd3a91e6..4bca9dab3 100644 --- a/.github/workflows/python-package.yml +++ b/.github/workflows/python-package.yml @@ -96,7 +96,9 @@ jobs: - name: Run S3 tracking server tests run: | - python -m pytest tests/tracking/test_bip0042_s3_buffering.py -v + python -m pytest \ + tests/tracking/test_bip0042_s3_buffering.py \ + tests/tracking/test_s3_initialize_db.py -v test-bedrock: runs-on: ubuntu-latest diff --git a/burr/tracking/server/s3/initialize_db.py b/burr/tracking/server/s3/initialize_db.py index e06528bd4..cfe31f5f9 100644 --- a/burr/tracking/server/s3/initialize_db.py +++ b/burr/tracking/server/s3/initialize_db.py @@ -15,19 +15,16 @@ # specific language governing permissions and limitations # under the License. -import os from pathlib import Path from tortoise import Tortoise, run_async from burr.tracking.server.s3 import settings -DB_PATH = Path("~/.burr_server/db.sqlite3").expanduser() - async def connect(): - if not os.path.exists(DB_PATH): - os.makedirs(os.path.dirname(DB_PATH), exist_ok=True) + db_path = Path(settings.DB_PATH).expanduser() + db_path.parent.mkdir(parents=True, exist_ok=True) await Tortoise.init( config=settings.TORTOISE_ORM, ) diff --git a/tests/tracking/test_s3_initialize_db.py b/tests/tracking/test_s3_initialize_db.py new file mode 100644 index 000000000..197587c72 --- /dev/null +++ b/tests/tracking/test_s3_initialize_db.py @@ -0,0 +1,38 @@ +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. + +import asyncio +from unittest.mock import AsyncMock + +import pytest + +pytest.importorskip("tortoise") +from burr.tracking.server.s3 import initialize_db, settings + + +def test_connect_uses_configured_database_path(tmp_path, monkeypatch): + db_path = tmp_path / "nested" / "custom.sqlite3" + tortoise_config = {"connections": {"default": f"sqlite:///{db_path}"}, "apps": {}} + tortoise_init = AsyncMock() + monkeypatch.setattr(settings, "DB_PATH", str(db_path)) + monkeypatch.setattr(settings, "TORTOISE_ORM", tortoise_config) + monkeypatch.setattr(initialize_db.Tortoise, "init", tortoise_init) + + asyncio.run(initialize_db.connect()) + + assert db_path.parent.is_dir() + tortoise_init.assert_awaited_once_with(config=tortoise_config)