Skip to content

Commit da864d0

Browse files
committed
Tests for db_url
1 parent fb6c277 commit da864d0

File tree

2 files changed

+112
-14
lines changed

2 files changed

+112
-14
lines changed

Diff for: tortoise/backends/base/db_url.py

+15-14
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
from tortoise.backends.asyncpg.client import AsyncpgDBClient
55
from tortoise.backends.sqlite.client import SqliteClient
6+
from tortoise.exceptions import ConfigurationError
67

78
urlparse.uses_netloc.append('postgres')
89
urlparse.uses_netloc.append('sqlite')
@@ -16,9 +17,6 @@
1617
'username': 'user',
1718
'password': 'password',
1819
},
19-
'test_defaults': {
20-
'single_connection': True
21-
},
2220
},
2321
'sqlite': {
2422
'client': SqliteClient,
@@ -32,19 +30,20 @@
3230

3331
def expand_db_url(db_url: str, testing: bool = False) -> dict:
3432
url = urlparse.urlparse(db_url)
33+
if url.scheme not in DB_LOOKUP:
34+
raise ConfigurationError('Unknown DB scheme: {}'.format(url.scheme))
35+
3536
db = DB_LOOKUP[url.scheme]
3637
if db.get('skip_first_char', True):
3738
path = url.path[1:]
3839
else:
3940
path = url.path
40-
if '?' in path and not url.query:
41-
path, query = path.split('?', 2)
42-
else:
43-
path, query = path, url.query
41+
42+
if not path:
43+
raise ConfigurationError('No path specified for DB_URL')
4444

4545
params = {} # type: dict
46-
params.update(db.get('test_defaults', {})) # type: ignore
47-
for key, val in urlparse.parse_qs(query).items():
46+
for key, val in urlparse.parse_qs(url.query).items():
4847
params[key] = val[-1]
4948

5049
if testing:
@@ -53,13 +52,15 @@ def expand_db_url(db_url: str, testing: bool = False) -> dict:
5352
path = path.format(uuid.uuid4().hex)
5453

5554
vars = {} # type: dict
56-
vars.update(db.get('vars', {})) # type: ignore
57-
if vars.get('path'):
58-
params[vars['path']] = path
55+
vars.update(db['vars']) # type: ignore
56+
params[vars['path']] = path
5957
if vars.get('hostname'):
6058
params[vars['hostname']] = str(url.hostname or '')
61-
if vars.get('port'):
62-
params[vars['port']] = str(url.port or '')
59+
try:
60+
if vars.get('port'):
61+
params[vars['port']] = str(url.port or '')
62+
except ValueError:
63+
raise ConfigurationError('Port is not an integer')
6364
if vars.get('username'):
6465
params[vars['username']] = str(url.username or '')
6566
if vars.get('password'):

Diff for: tortoise/tests/test_db_url.py

+97
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
from tortoise.backends.asyncpg.client import AsyncpgDBClient
2+
from tortoise.backends.base.db_url import expand_db_url
3+
from tortoise.backends.sqlite.client import SqliteClient
4+
from tortoise.contrib import test
5+
from tortoise.exceptions import ConfigurationError
6+
7+
8+
class TestDBUrl(test.SimpleTestCase):
9+
10+
def test_unknown_scheme(self):
11+
with self.assertRaises(ConfigurationError):
12+
expand_db_url('moo://baa')
13+
14+
def test_sqlite_basic(self):
15+
res = expand_db_url('sqlite:///tmp/test.sqlite')
16+
self.assertEqual(res, {
17+
'client': SqliteClient,
18+
'params': {
19+
'filename': '/tmp/test.sqlite', # nosec
20+
}
21+
})
22+
23+
def test_sqlite_testing(self):
24+
res = expand_db_url('sqlite:///tmp/test-{}.sqlite', testing=True)
25+
self.assertIn('/tmp/test-', res['params']['filename']) # nosec
26+
self.assertIn('.sqlite', res['params']['filename'])
27+
self.assertNotEqual('sqlite:///tmp/test-{}.sqlite', res['params']['filename'])
28+
self.assertEqual(res, {
29+
'client': SqliteClient,
30+
'params': {
31+
'filename': res['params']['filename'],
32+
'single_connection': True,
33+
}
34+
})
35+
36+
def test_sqlite_params(self):
37+
res = expand_db_url('sqlite:///tmp/test.sqlite?AHA=5&moo=yes')
38+
self.assertEqual(res, {
39+
'client': SqliteClient,
40+
'params': {
41+
'filename': '/tmp/test.sqlite', # nosec
42+
'AHA': '5',
43+
'moo': 'yes',
44+
}
45+
})
46+
47+
def test_sqlite_invalid(self):
48+
with self.assertRaises(ConfigurationError):
49+
expand_db_url('sqlite://')
50+
51+
def test_postgres_basic(self):
52+
res = expand_db_url('postgres://postgres:@127.0.0.1:5432/test')
53+
self.assertEqual(res, {
54+
'client': AsyncpgDBClient,
55+
'params': {
56+
'database': 'test',
57+
'host': '127.0.0.1',
58+
'password': '',
59+
'port': '5432',
60+
'user': 'postgres',
61+
}
62+
})
63+
64+
def test_postgres_nonint_port(self):
65+
with self.assertRaises(ConfigurationError):
66+
expand_db_url('postgres://postgres:@127.0.0.1:moo/test')
67+
68+
def test_postgres_testing(self):
69+
res = expand_db_url('postgres://postgres:@127.0.0.1:5432/test_\{\}', testing=True)
70+
self.assertIn('test_', res['params']['database'])
71+
self.assertNotEqual('test_{}', res['params']['database'])
72+
self.assertEqual(res, {
73+
'client': AsyncpgDBClient,
74+
'params': {
75+
'database': res['params']['database'],
76+
'host': '127.0.0.1',
77+
'password': '',
78+
'port': '5432',
79+
'user': 'postgres',
80+
'single_connection': True,
81+
}
82+
})
83+
84+
def test_postgres_params(self):
85+
res = expand_db_url('postgres://postgres:@127.0.0.1:5432/test?AHA=5&moo=yes')
86+
self.assertEqual(res, {
87+
'client': AsyncpgDBClient,
88+
'params': {
89+
'database': 'test',
90+
'host': '127.0.0.1',
91+
'password': '',
92+
'port': '5432',
93+
'user': 'postgres',
94+
'AHA': '5',
95+
'moo': 'yes',
96+
}
97+
})

0 commit comments

Comments
 (0)