forked from jazzband/dj-database-url
-
Notifications
You must be signed in to change notification settings - Fork 7
/
test_dj_database_url.py
341 lines (273 loc) · 14.2 KB
/
test_dj_database_url.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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
# -*- coding: utf-8 -*-
#!/usr/bin/env python
import os
import unittest
from django import VERSION as DJANGO_VERSION
import dj_database_url
POSTGIS_URL = 'postgis://uf07k1i6d8ia0v:wegauwhgeuioweg@ec2-107-21-253-135.compute-1.amazonaws.com:5431/d8r82722r2kuvn'
# Django deprecated the `django.db.backends.postgresql_psycopg2` in 2.0.
# https://docs.djangoproject.com/en/2.0/releases/2.0/#id1
EXPECTED_POSTGRES_ENGINE = 'django.db.backends.postgresql'
if DJANGO_VERSION < (2, 0):
EXPECTED_POSTGRES_ENGINE = 'django.db.backends.postgresql_psycopg2'
class DatabaseTestSuite(unittest.TestCase):
def test_postgres_parsing(self):
url = 'postgres://uf07k1i6d8ia0v:wegauwhgeuioweg@ec2-107-21-253-135.compute-1.amazonaws.com:5431/d8r82722r2kuvn'
url = dj_database_url.parse(url)
assert url['ENGINE'] == EXPECTED_POSTGRES_ENGINE
assert url['NAME'] == 'd8r82722r2kuvn'
assert url['HOST'] == 'ec2-107-21-253-135.compute-1.amazonaws.com'
assert url['USER'] == 'uf07k1i6d8ia0v'
assert url['PASSWORD'] == 'wegauwhgeuioweg'
assert url['PORT'] == 5431
def test_postgres_unix_socket_parsing(self):
url = 'postgres://%2Fvar%2Frun%2Fpostgresql/d8r82722r2kuvn'
url = dj_database_url.parse(url)
assert url['ENGINE'] == EXPECTED_POSTGRES_ENGINE
assert url['NAME'] == 'd8r82722r2kuvn'
assert url['HOST'] == '/var/run/postgresql'
assert url['USER'] == ''
assert url['PASSWORD'] == ''
assert url['PORT'] == ''
url = 'postgres://%2FUsers%2Fpostgres%2FRuN/d8r82722r2kuvn'
url = dj_database_url.parse(url)
assert url['ENGINE'] == EXPECTED_POSTGRES_ENGINE
assert url['HOST'] == '/Users/postgres/RuN'
assert url['USER'] == ''
assert url['PASSWORD'] == ''
assert url['PORT'] == ''
def test_ipv6_parsing(self):
url = 'postgres://ieRaekei9wilaim7:wegauwhgeuioweg@[2001:db8:1234::1234:5678:90af]:5431/d8r82722r2kuvn'
url = dj_database_url.parse(url)
assert url['ENGINE'] == EXPECTED_POSTGRES_ENGINE
assert url['NAME'] == 'd8r82722r2kuvn'
assert url['HOST'] == '2001:db8:1234::1234:5678:90af'
assert url['USER'] == 'ieRaekei9wilaim7'
assert url['PASSWORD'] == 'wegauwhgeuioweg'
assert url['PORT'] == 5431
def test_postgres_search_path_parsing(self):
url = 'postgres://uf07k1i6d8ia0v:wegauwhgeuioweg@ec2-107-21-253-135.compute-1.amazonaws.com:5431/d8r82722r2kuvn?currentSchema=otherschema'
url = dj_database_url.parse(url)
assert url['ENGINE'] == EXPECTED_POSTGRES_ENGINE
assert url['NAME'] == 'd8r82722r2kuvn'
assert url['HOST'] == 'ec2-107-21-253-135.compute-1.amazonaws.com'
assert url['USER'] == 'uf07k1i6d8ia0v'
assert url['PASSWORD'] == 'wegauwhgeuioweg'
assert url['PORT'] == 5431
assert url['OPTIONS']['options'] == '-c search_path=otherschema'
assert 'currentSchema' not in url['OPTIONS']
def test_postgres_parsing_with_special_characters(self):
url = 'postgres://%23user:%23password@ec2-107-21-253-135.compute-1.amazonaws.com:5431/%23database'
url = dj_database_url.parse(url)
assert url['ENGINE'] == EXPECTED_POSTGRES_ENGINE
assert url['NAME'] == '#database'
assert url['HOST'] == 'ec2-107-21-253-135.compute-1.amazonaws.com'
assert url['USER'] == '#user'
assert url['PASSWORD'] == '#password'
assert url['PORT'] == 5431
def test_postgis_parsing(self):
url = 'postgis://uf07k1i6d8ia0v:wegauwhgeuioweg@ec2-107-21-253-135.compute-1.amazonaws.com:5431/d8r82722r2kuvn'
url = dj_database_url.parse(url)
assert url['ENGINE'] == 'django.contrib.gis.db.backends.postgis'
assert url['NAME'] == 'd8r82722r2kuvn'
assert url['HOST'] == 'ec2-107-21-253-135.compute-1.amazonaws.com'
assert url['USER'] == 'uf07k1i6d8ia0v'
assert url['PASSWORD'] == 'wegauwhgeuioweg'
assert url['PORT'] == 5431
def test_postgis_search_path_parsing(self):
url = 'postgis://uf07k1i6d8ia0v:wegauwhgeuioweg@ec2-107-21-253-135.compute-1.amazonaws.com:5431/d8r82722r2kuvn?currentSchema=otherschema'
url = dj_database_url.parse(url)
assert url['ENGINE'] == 'django.contrib.gis.db.backends.postgis'
assert url['NAME'] == 'd8r82722r2kuvn'
assert url['HOST'] == 'ec2-107-21-253-135.compute-1.amazonaws.com'
assert url['USER'] == 'uf07k1i6d8ia0v'
assert url['PASSWORD'] == 'wegauwhgeuioweg'
assert url['PORT'] == 5431
assert url['OPTIONS']['options'] == '-c search_path=otherschema'
assert 'currentSchema' not in url['OPTIONS']
def test_mysql_gis_parsing(self):
url = 'mysqlgis://uf07k1i6d8ia0v:wegauwhgeuioweg@ec2-107-21-253-135.compute-1.amazonaws.com:5431/d8r82722r2kuvn'
url = dj_database_url.parse(url)
assert url['ENGINE'] == 'django.contrib.gis.db.backends.mysql'
assert url['NAME'] == 'd8r82722r2kuvn'
assert url['HOST'] == 'ec2-107-21-253-135.compute-1.amazonaws.com'
assert url['USER'] == 'uf07k1i6d8ia0v'
assert url['PASSWORD'] == 'wegauwhgeuioweg'
assert url['PORT'] == 5431
def test_mysql_connector_parsing(self):
url = 'mysql-connector://uf07k1i6d8ia0v:wegauwhgeuioweg@ec2-107-21-253-135.compute-1.amazonaws.com:5431/d8r82722r2kuvn'
url = dj_database_url.parse(url)
assert url['ENGINE'] == 'mysql.connector.django'
assert url['NAME'] == 'd8r82722r2kuvn'
assert url['HOST'] == 'ec2-107-21-253-135.compute-1.amazonaws.com'
assert url['USER'] == 'uf07k1i6d8ia0v'
assert url['PASSWORD'] == 'wegauwhgeuioweg'
assert url['PORT'] == 5431
def test_cleardb_parsing(self):
url = 'mysql://bea6eb025ca0d8:69772142@us-cdbr-east.cleardb.com/heroku_97681db3eff7580?reconnect=true'
url = dj_database_url.parse(url)
assert url['ENGINE'] == 'django.db.backends.mysql'
assert url['NAME'] == 'heroku_97681db3eff7580'
assert url['HOST'] == 'us-cdbr-east.cleardb.com'
assert url['USER'] == 'bea6eb025ca0d8'
assert url['PASSWORD'] == '69772142'
assert url['PORT'] == ''
def test_database_url(self):
del os.environ['DATABASE_URL']
a = dj_database_url.config()
assert not a
os.environ['DATABASE_URL'] = 'postgres://uf07k1i6d8ia0v:wegauwhgeuioweg@ec2-107-21-253-135.compute-1.amazonaws.com:5431/d8r82722r2kuvn'
url = dj_database_url.config()
assert url['ENGINE'] == EXPECTED_POSTGRES_ENGINE
assert url['NAME'] == 'd8r82722r2kuvn'
assert url['HOST'] == 'ec2-107-21-253-135.compute-1.amazonaws.com'
assert url['USER'] == 'uf07k1i6d8ia0v'
assert url['PASSWORD'] == 'wegauwhgeuioweg'
assert url['PORT'] == 5431
def test_empty_sqlite_url(self):
url = 'sqlite://'
url = dj_database_url.parse(url)
assert url['ENGINE'] == 'django.db.backends.sqlite3'
assert url['NAME'] == ':memory:'
def test_memory_sqlite_url(self):
url = 'sqlite://:memory:'
url = dj_database_url.parse(url)
assert url['ENGINE'] == 'django.db.backends.sqlite3'
assert url['NAME'] == ':memory:'
def test_parse_engine_setting(self):
engine = 'django_mysqlpool.backends.mysqlpool'
url = 'mysql://bea6eb025ca0d8:69772142@us-cdbr-east.cleardb.com/heroku_97681db3eff7580?reconnect=true'
url = dj_database_url.parse(url, engine)
assert url['ENGINE'] == engine
def test_config_engine_setting(self):
engine = 'django_mysqlpool.backends.mysqlpool'
os.environ['DATABASE_URL'] = 'mysql://bea6eb025ca0d8:69772142@us-cdbr-east.cleardb.com/heroku_97681db3eff7580?reconnect=true'
url = dj_database_url.config(engine=engine)
assert url['ENGINE'] == engine
def test_parse_conn_max_age_setting(self):
conn_max_age = 600
url = 'mysql://bea6eb025ca0d8:69772142@us-cdbr-east.cleardb.com/heroku_97681db3eff7580?reconnect=true'
url = dj_database_url.parse(url, conn_max_age=conn_max_age)
assert url['CONN_MAX_AGE'] == conn_max_age
def test_config_conn_max_age_setting(self):
conn_max_age = 600
os.environ['DATABASE_URL'] = 'mysql://bea6eb025ca0d8:69772142@us-cdbr-east.cleardb.com/heroku_97681db3eff7580?reconnect=true'
url = dj_database_url.config(conn_max_age=conn_max_age)
assert url['CONN_MAX_AGE'] == conn_max_age
def test_database_url_with_options(self):
# Test full options
os.environ['DATABASE_URL'] = 'postgres://uf07k1i6d8ia0v:wegauwhgeuioweg@ec2-107-21-253-135.compute-1.amazonaws.com:5431/d8r82722r2kuvn?sslrootcert=rds-combined-ca-bundle.pem&sslmode=verify-full'
url = dj_database_url.config()
assert url['ENGINE'] == EXPECTED_POSTGRES_ENGINE
assert url['NAME'] == 'd8r82722r2kuvn'
assert url['HOST'] == 'ec2-107-21-253-135.compute-1.amazonaws.com'
assert url['USER'] == 'uf07k1i6d8ia0v'
assert url['PASSWORD'] == 'wegauwhgeuioweg'
assert url['PORT'] == 5431
assert url['OPTIONS'] == {
'sslrootcert': 'rds-combined-ca-bundle.pem',
'sslmode': 'verify-full'
}
# Test empty options
os.environ['DATABASE_URL'] = 'postgres://uf07k1i6d8ia0v:wegauwhgeuioweg@ec2-107-21-253-135.compute-1.amazonaws.com:5431/d8r82722r2kuvn?'
url = dj_database_url.config()
assert 'OPTIONS' not in url
def test_mysql_database_url_with_sslca_options(self):
os.environ['DATABASE_URL'] = 'mysql://uf07k1i6d8ia0v:wegauwhgeuioweg@ec2-107-21-253-135.compute-1.amazonaws.com:3306/d8r82722r2kuvn?ssl-ca=rds-combined-ca-bundle.pem'
url = dj_database_url.config()
assert url['ENGINE'] == 'django.db.backends.mysql'
assert url['NAME'] == 'd8r82722r2kuvn'
assert url['HOST'] == 'ec2-107-21-253-135.compute-1.amazonaws.com'
assert url['USER'] == 'uf07k1i6d8ia0v'
assert url['PASSWORD'] == 'wegauwhgeuioweg'
assert url['PORT'] == 3306
assert url['OPTIONS'] == {
'ssl': {
'ca': 'rds-combined-ca-bundle.pem'
}
}
# Test empty options
os.environ['DATABASE_URL'] = 'mysql://uf07k1i6d8ia0v:wegauwhgeuioweg@ec2-107-21-253-135.compute-1.amazonaws.com:3306/d8r82722r2kuvn?'
url = dj_database_url.config()
assert 'OPTIONS' not in url
def test_oracle_parsing(self):
url = 'oracle://scott:tiger@oraclehost:1521/hr'
url = dj_database_url.parse(url)
assert url['ENGINE'] == 'django.db.backends.oracle'
assert url['NAME'] == 'hr'
assert url['HOST'] == 'oraclehost'
assert url['USER'] == 'scott'
assert url['PASSWORD'] == 'tiger'
assert url['PORT'] == '1521'
def test_oracle_gis_parsing(self):
url = 'oraclegis://scott:tiger@oraclehost:1521/hr'
url = dj_database_url.parse(url)
assert url['ENGINE'] == 'django.contrib.gis.db.backends.oracle'
assert url['NAME'] == 'hr'
assert url['HOST'] == 'oraclehost'
assert url['USER'] == 'scott'
assert url['PASSWORD'] == 'tiger'
assert url['PORT'] == 1521
def test_oracle_dsn_parsing(self):
url = (
'oracle://scott:tiger@/'
'(DESCRIPTION=(ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCP)'
'(HOST=oraclehost)(PORT=1521)))'
'(CONNECT_DATA=(SID=hr)))'
)
url = dj_database_url.parse(url)
assert url['ENGINE'] == 'django.db.backends.oracle'
assert url['USER'] == 'scott'
assert url['PASSWORD'] == 'tiger'
assert url['HOST'] == ''
assert url['PORT'] == ''
dsn = (
'(DESCRIPTION=(ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCP)'
'(HOST=oraclehost)(PORT=1521)))'
'(CONNECT_DATA=(SID=hr)))'
)
assert url['NAME'] == dsn
def test_oracle_tns_parsing(self):
url = 'oracle://scott:tiger@/tnsname'
url = dj_database_url.parse(url)
assert url['ENGINE'] == 'django.db.backends.oracle'
assert url['USER'] == 'scott'
assert url['PASSWORD'] == 'tiger'
assert url['NAME'] == 'tnsname'
assert url['HOST'] == ''
assert url['PORT'] == ''
def test_redshift_parsing(self):
url = 'redshift://uf07k1i6d8ia0v:wegauwhgeuioweg@ec2-107-21-253-135.compute-1.amazonaws.com:5439/d8r82722r2kuvn?currentSchema=otherschema'
url = dj_database_url.parse(url)
assert url['ENGINE'] == 'django_redshift_backend'
assert url['NAME'] == 'd8r82722r2kuvn'
assert url['HOST'] == 'ec2-107-21-253-135.compute-1.amazonaws.com'
assert url['USER'] == 'uf07k1i6d8ia0v'
assert url['PASSWORD'] == 'wegauwhgeuioweg'
assert url['PORT'] == 5439
assert url['OPTIONS']['options'] == '-c search_path=otherschema'
assert 'currentSchema' not in url['OPTIONS']
def test_mssql_parsing(self):
url = 'mssql://uf07k1i6d8ia0v:wegauwhgeuioweg@ec2-107-21-253-135.compute-1.amazonaws.com/d8r82722r2kuvn?driver=ODBC Driver 13 for SQL Server'
url = dj_database_url.parse(url)
assert url['ENGINE'] == 'sql_server.pyodbc'
assert url['NAME'] == 'd8r82722r2kuvn'
assert url['HOST'] == 'ec2-107-21-253-135.compute-1.amazonaws.com'
assert url['USER'] == 'uf07k1i6d8ia0v'
assert url['PASSWORD'] == 'wegauwhgeuioweg'
assert url['PORT'] == ''
assert url['OPTIONS']['driver'] == 'ODBC Driver 13 for SQL Server'
assert 'currentSchema' not in url['OPTIONS']
def test_mssql_instance_port_parsing(self):
url = 'mssql://uf07k1i6d8ia0v:wegauwhgeuioweg@ec2-107-21-253-135.compute-1.amazonaws.com\\insnsnss:12345/d8r82722r2kuvn?driver=ODBC Driver 13 for SQL Server'
url = dj_database_url.parse(url)
assert url['ENGINE'] == 'sql_server.pyodbc'
assert url['NAME'] == 'd8r82722r2kuvn'
assert url['HOST'] == 'ec2-107-21-253-135.compute-1.amazonaws.com\\insnsnss'
assert url['USER'] == 'uf07k1i6d8ia0v'
assert url['PASSWORD'] == 'wegauwhgeuioweg'
assert url['PORT'] == '12345'
assert url['OPTIONS']['driver'] == 'ODBC Driver 13 for SQL Server'
assert 'currentSchema' not in url['OPTIONS']
if __name__ == '__main__':
unittest.main()