Skip to content

Commit 2a3d76b

Browse files
Fixing build
1 parent 708ae81 commit 2a3d76b

File tree

5 files changed

+27
-3
lines changed

5 files changed

+27
-3
lines changed

app/config.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ class Settings(BaseSettings):
88
# db settings start
99
app_db_migration_enabled: bool = True
1010
app_db_migration_mode: str = "apply"
11+
app_db_enabled: bool = True
1112
app_db_host: str = "localhost"
1213
app_db_port: str = "5432"
1314
app_db_database: str = "myuser"

app/db/database_config.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
from databases import Database
22
from app.config import settings
33

4-
DATABASE_URL = f"postgresql://{settings.app_db_user}:{settings.app_db_password}@{settings.app_db_host}/{settings.app_db_database}"
4+
DATABASE_URL = f"postgresql://{settings.app_db_user}:{settings.app_db_password}@{settings.app_db_host}:{settings.app_db_port}/{settings.app_db_database}"
55
database = Database(
66
DATABASE_URL, min_size=settings.app_db_min_con, max_size=settings.app_db_max_con
77
)

app/main.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,11 @@
2626
async def lifespan(_app: FastAPI):
2727
if settings.app_db_migration_enabled:
2828
apply_db_migrations()
29-
await database.connect()
29+
if settings.app_db_enabled:
30+
await database.connect()
3031
yield
31-
await database.disconnect()
32+
if settings.app_db_enabled:
33+
await database.disconnect()
3234

3335

3436
app = FastAPI(openapi_url=settings.app_open_api_url, lifespan=lifespan)

tests_integration/conftest.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
import app.config as config
1313
import app.db_schema_migrations.yoyo_migration as yoyo_migration
14+
import app.db.database_config as database_config
1415
import app.main as main
1516
import time
1617

@@ -46,6 +47,7 @@ def test_client(
4647
app = reload(main).app
4748
with TestClient(app) as client:
4849
yield client
50+
reload(main)
4951

5052

5153
@pytest.fixture
@@ -58,9 +60,12 @@ def change_postgres_db_host(
5860
# set new value, reload module and yield setting
5961
new_settings = patch_env_var(monkeypatch, name, new_value)
6062
reload(yoyo_migration)
63+
reload(database_config)
6164
yield new_settings
6265
# reset to original value and reload module
6366
patch_env_var(monkeypatch, name, original_value)
67+
reload(yoyo_migration)
68+
reload(database_config)
6469

6570

6671
@pytest.fixture
@@ -73,9 +78,12 @@ def change_postgres_db_port(
7378
# set new value, reload module and yield setting
7479
new_settings = patch_env_var(monkeypatch, name, new_value)
7580
reload(yoyo_migration)
81+
reload(database_config)
7682
yield new_settings
7783
# reset to original value and reload module
7884
patch_env_var(monkeypatch, name, original_value)
85+
reload(yoyo_migration)
86+
reload(database_config)
7987

8088

8189
@pytest.fixture

tests_unit/conftest.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,12 @@
1313
@pytest.fixture
1414
def test_client(
1515
disable_db_migrations: config.Settings,
16+
disable_db_connection: config.Settings,
1617
) -> Generator[TestClient, None, None]:
1718
app = reload(main).app
1819
with TestClient(app) as client:
1920
yield client
21+
reload(main)
2022

2123

2224
@pytest.fixture
@@ -30,6 +32,17 @@ def disable_db_migrations(monkeypatch) -> Generator[config.Settings, None, None]
3032
patch_env_var(monkeypatch, name, original_value)
3133

3234

35+
@pytest.fixture
36+
def disable_db_connection(monkeypatch) -> Generator[config.Settings, None, None]:
37+
name = "APP_DB_ENABLED"
38+
original_value = os.getenv(name)
39+
new_value = "False"
40+
# set new value, reload module and yield setting
41+
yield patch_env_var(monkeypatch, name, new_value)
42+
# reset to original value and reload module
43+
patch_env_var(monkeypatch, name, original_value)
44+
45+
3346
@pytest.fixture
3447
def disable_open_api(monkeypatch) -> Generator[config.Settings, None, None]:
3548
name = "APP_OPEN_API_URL"

0 commit comments

Comments
 (0)