forked from ffeast/flask-dbshell
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tests.py
133 lines (104 loc) · 4.26 KB
/
tests.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
import unittest
from mock import patch
from flask.ext.dbshell import DbUrl, DbShell
from flask.ext.dbshell.backends import BaseBackend, BackendOptionError
class DbUrlTest(unittest.TestCase):
def test_valid_url(self):
url = DbUrl('mysql://test:123@dbserver:6666/superdb')
self.assertEquals(url.backend, 'mysql')
self.assertEquals(url.user, 'test')
self.assertEquals(url.password, '123')
self.assertEquals(url.host, 'dbserver')
self.assertEquals(url.port, 6666)
self.assertEquals(url.database, 'superdb')
def test_url_with_missing_args(self):
url = DbUrl('mysql://localhost/superdb')
self.assertEquals(url.backend, 'mysql')
self.assertEquals(url.user, None)
self.assertEquals(url.password, None)
self.assertEquals(url.host, 'localhost')
self.assertEquals(url.port, None)
self.assertEquals(url.database, 'superdb')
def test_explicit_args(self):
url = DbUrl(backend='mysql', user='test',
password='123', host='127.0.0.1',
port=8000)
self.assertEquals(url.backend, 'mysql')
self.assertEquals(url.user, 'test')
self.assertEquals(url.password, '123')
self.assertEquals(url.host, '127.0.0.1')
self.assertEquals(url.port, 8000)
self.assertEquals(url.database, None)
def test_override_args(self):
url = DbUrl('mysql://test:123@dbserver:6666/superdb',
database='anotherdb',
password='345')
self.assertEquals(url.database, 'anotherdb')
self.assertEquals(url.password, '345')
def test_unknown_arg(self):
#with self.assertRaises(AttributeError):
#DbUrl(unknown_arg=123)
self.assertRaises(AttributeError,
DbUrl,
unknown_arg=123)
def test_backend_with_dialect(self):
url = DbUrl('mysql+somedialect://user@server/superdb')
self.assertEquals(url.backend, 'mysql')
class BaseBackendTest(unittest.TestCase):
@patch.object(BaseBackend, 'execute_command')
def assert_command(self, dbshell, expected, mock_method):
dbshell.run_shell()
mock_method.assert_called_with(expected)
class MysqlBackendTest(BaseBackendTest):
def test_basic(self):
self.assert_command(
DbShell(url='mysql://user:[email protected]/test'),
['mysql',
'--host=127.0.0.1',
'--password=password',
'--user=user',
'test'])
def test_simple(self):
self.assert_command(DbShell(url='mysql:///mydb'),
['mysql', 'mydb'])
class SqliteBackendTest(BaseBackendTest):
def test_basic(self):
self.assert_command(
DbShell(url='sqlite:///dir1/file1'),
['sqlite3', 'dir1/file1'])
def test_unsupported_args(self):
#with self.assertRaises(BackendOptionError):
#self.assert_command(DbShell(url='sqlite://127.0.0.1/file'), None)
self.assertRaises(BackendOptionError,
self.assert_command,
DbShell(url='sqlite://127.0.0.1/file'),
None)
class PostgresqlBackendTest(BaseBackendTest):
def test_basic(self):
self.assert_command(
DbShell(url='postgresql://user:[email protected]:666/test'),
['psql',
'--host=127.0.0.1',
'--port=666',
'--password=password',
'--username=user',
'test'])
class RedisBackendTest(BaseBackendTest):
def test_basic(self):
self.assert_command(
DbShell(url='redis://127.0.0.1:3334/redis1', password='pwd'),
['redis-cli',
'-h 127.0.0.1',
'-p 3334',
'-a pwd',
'-n redis1'])
def test_unsupported_args(self):
#with self.assertRaises(BackendOptionError):
#self.assert_command(
#DbShell(url='redis://[email protected]:3334/redis1'), None)
self.assertRaises(BackendOptionError,
self.assert_command,
DbShell(url='redis://[email protected]:3334/redis1'),
None)
if __name__ == '__main__':
unittest.main()