-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathtest_db_config.py
153 lines (129 loc) · 4.55 KB
/
test_db_config.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
from typing import Dict, Optional, Union
from pydantic import Field, PostgresDsn
from pydantic_settings import BaseSettings, SettingsConfigDict
from tempenv import TemporaryEnvironment
from pydjantic import BaseDBConfig
def test_empty():
class DatabaseConfig(BaseDBConfig):
default: Union[Optional[PostgresDsn], Dict] = None
replica: Optional[Dict] = None
db_settings = DatabaseConfig()
assert db_settings.model_dump() == {"default": {}, "replica": {}}
def test_dsn():
class DatabaseConfig(BaseDBConfig):
default: str = Field(default="postgres://user:password@hostname:5432/dbname")
db_settings = DatabaseConfig()
assert db_settings.default == {
"CONN_MAX_AGE": 0,
"CONN_HEALTH_CHECKS": False,
"ENGINE": "django.db.backends.postgresql",
"HOST": "hostname",
"NAME": "dbname",
"PASSWORD": "password",
"PORT": 5432,
"USER": "user",
}
def test_dsn_extra_params():
class DatabaseConfig(BaseDBConfig):
default: str = Field(
default="postgres://user:password@hostname:5432/dbname?connect_timeout=10",
json_schema_extra={
"test_options": {"NAME": "mytestdb"},
"engine": "my.custom.backend",
"conn_health_checks": True,
"ssl_require": "True",
"conn_max_age": 60,
},
)
db_settings = DatabaseConfig()
assert db_settings.default == {
"CONN_MAX_AGE": 60,
"CONN_HEALTH_CHECKS": True,
"ENGINE": "my.custom.backend",
"HOST": "hostname",
"NAME": "dbname",
"OPTIONS": {"connect_timeout": 10, "sslmode": "require"},
"PASSWORD": "password",
"PORT": 5432,
"USER": "user",
"TEST": {"NAME": "mytestdb"},
}
class PostgresDB(BaseSettings):
model_config = SettingsConfigDict(env_prefix="DATABASE_")
ENGINE: str = "django.db.backends.postgresql"
HOST: str = Field(default="replicahost")
NAME: str = Field(default="replicadbname")
PASSWORD: str = Field(default="replicapwd")
PORT: int = Field(default=5432)
USER: str = Field(default="replicauser")
OPTIONS: dict = Field(default={})
CONN_MAX_AGE: int = Field(default=0)
CONN_HEALTH_CHECKS: bool = Field(default=False)
def test_exact():
class DatabaseConfig(BaseDBConfig):
default: PostgresDB = PostgresDB()
db_settings = DatabaseConfig()
assert db_settings.model_dump() == {
"default": {
"ENGINE": "django.db.backends.postgresql",
"HOST": "replicahost",
"NAME": "replicadbname",
"USER": "replicauser",
"PASSWORD": "replicapwd",
"PORT": 5432,
"OPTIONS": {},
"CONN_MAX_AGE": 0,
"CONN_HEALTH_CHECKS": False,
}
}
def test_dsn_and_exact_config():
class DatabaseConfig(BaseDBConfig):
default: Union[str, Dict] = Field(default="postgres://user:password@hostname:5432/dbname")
replica: PostgresDB = PostgresDB()
db_settings = DatabaseConfig()
assert db_settings.model_dump() == {
"default": {
"CONN_MAX_AGE": 0,
"CONN_HEALTH_CHECKS": False,
"ENGINE": "django.db.backends.postgresql",
"HOST": "hostname",
"NAME": "dbname",
"PASSWORD": "password",
"PORT": 5432,
"USER": "user",
},
"replica": {
"CONN_MAX_AGE": 0,
"CONN_HEALTH_CHECKS": False,
"ENGINE": "django.db.backends.postgresql",
"HOST": "replicahost",
"NAME": "replicadbname",
"USER": "replicauser",
"PASSWORD": "replicapwd",
"OPTIONS": {},
"PORT": 5432,
},
}
def test_env_vars_for_exact_config():
with TemporaryEnvironment(
{
"DATABASE_CONN_MAX_AGE": "60",
"DATABASE_OPTIONS": '{"sslmode": "require"}',
}
):
class DatabaseConfig(BaseSettings):
default: PostgresDB = PostgresDB()
db_settings = DatabaseConfig()
assert db_settings.model_dump() == {
"default": {
"ENGINE": "django.db.backends.postgresql",
"HOST": "replicahost",
"NAME": "replicadbname",
"USER": "replicauser",
"PASSWORD": "replicapwd",
"PORT": 5432,
"OPTIONS": {"sslmode": "require"},
"CONN_MAX_AGE": 60,
"CONN_HEALTH_CHECKS": False,
},
}