2
2
3
3
import wrds
4
4
import unittest
5
+
5
6
try :
6
7
import unittest .mock as mock
7
8
except ImportError :
10
11
11
12
12
13
class TestInitMethod (unittest .TestCase ):
13
- """ Test the wrds.Connection.__init__() method,
14
- with both default and custom parameters.
14
+ """Test the wrds.Connection.__init__() method,
15
+ with both default and custom parameters.
15
16
"""
16
- @mock .patch ('wrds.sql.sa' )
17
+
18
+ @mock .patch ("wrds.sql.sa" )
17
19
def test_init_calls_sqlalchemy_create_engine_defaults (self , mock_sa ):
18
20
wrds .Connection ()
19
- connstring = ' postgresql://{host}:{port}/{dbname}'
21
+ connstring = " postgresql://{host}:{port}/{dbname}"
20
22
connstring = connstring .format (
21
23
host = wrds .sql .WRDS_POSTGRES_HOST ,
22
24
port = wrds .sql .WRDS_POSTGRES_PORT ,
23
- dbname = wrds .sql .WRDS_POSTGRES_DB )
25
+ dbname = wrds .sql .WRDS_POSTGRES_DB ,
26
+ )
24
27
mock_sa .create_engine .assert_called_with (
25
28
connstring ,
26
- connect_args = {' sslmode' : ' require' ,
27
- 'application_name' : wrds . sql . appname } ,
28
- isolation_level = 'AUTOCOMMIT' )
29
+ connect_args = {" sslmode" : " require" , "application_name" : wrds . sql . appname } ,
30
+ isolation_level = "AUTOCOMMIT" ,
31
+ )
29
32
30
- @mock .patch (' wrds.sql.sa' )
33
+ @mock .patch (" wrds.sql.sa" )
31
34
def test_init_calls_sqlalchemy_create_engine_custom (self , mock_sa ):
32
- username = ' faketestusername'
33
- connstring = ' postgresql://{usr}@{host}:{port}/{dbname}'
35
+ username = " faketestusername"
36
+ connstring = " postgresql://{usr}@{host}:{port}/{dbname}"
34
37
connstring = connstring .format (
35
38
usr = username ,
36
39
host = wrds .sql .WRDS_POSTGRES_HOST ,
37
40
port = wrds .sql .WRDS_POSTGRES_PORT ,
38
- dbname = wrds .sql .WRDS_POSTGRES_DB )
41
+ dbname = wrds .sql .WRDS_POSTGRES_DB ,
42
+ )
39
43
wrds .Connection (wrds_username = username )
40
44
mock_sa .create_engine .assert_called_with (
41
45
connstring ,
42
- connect_args = {' sslmode' : ' require' ,
43
- 'application_name' : wrds . sql . appname } ,
44
- isolation_level = 'AUTOCOMMIT' )
46
+ connect_args = {" sslmode" : " require" , "application_name" : wrds . sql . appname } ,
47
+ isolation_level = "AUTOCOMMIT" ,
48
+ )
45
49
46
- @mock .patch (' wrds.sql.Connection.load_library_list' )
47
- @mock .patch (' wrds.sql.Connection.connect' )
50
+ @mock .patch (" wrds.sql.Connection.load_library_list" )
51
+ @mock .patch (" wrds.sql.Connection.connect" )
48
52
def test_init_default_connect (self , mock_connect , mock_lll ):
49
53
wrds .Connection ()
50
54
mock_connect .assert_called_once ()
51
55
52
- @mock .patch (' wrds.sql.Connection.connect' )
56
+ @mock .patch (" wrds.sql.Connection.connect" )
53
57
def test_init_autoconnect_false_no_connect (self , mock_connect ):
54
58
wrds .Connection (autoconnect = False )
55
59
mock_connect .assert_not_called ()
56
60
57
- @mock .patch (' wrds.sql.Connection.connect' )
58
- @mock .patch (' wrds.sql.Connection.load_library_list' )
61
+ @mock .patch (" wrds.sql.Connection.connect" )
62
+ @mock .patch (" wrds.sql.Connection.load_library_list" )
59
63
def test_init_default_load_library_list (self , mock_lll , mock_connect ):
60
64
wrds .Connection ()
61
65
mock_lll .assert_called_once ()
62
66
63
- @mock .patch ('wrds.sql.Connection.connect' )
64
- @mock .patch ('wrds.sql.Connection.load_library_list' )
65
- def test_init_autoconnect_false_no_connect_second_function (self , mock_lll , mock_connect ):
67
+ @mock .patch ("wrds.sql.Connection.connect" )
68
+ @mock .patch ("wrds.sql.Connection.load_library_list" )
69
+ def test_init_autoconnect_false_no_connect_second_function (
70
+ self , mock_lll , mock_connect
71
+ ):
66
72
wrds .Connection (autoconnect = False )
67
73
mock_lll .assert_not_called ()
68
74
69
75
70
76
class TestConnectMethod (unittest .TestCase ):
71
- """ Test the wrds.Connection.connect method.
77
+ """
78
+ Test the wrds.Connection.connect method.
72
79
73
- Since all exceptions are caught immediately,
74
- I'm just not smart enough to simulate bad passwords with
75
- the code as written.
80
+ Since all exceptions are caught immediately,
81
+ I'm just not smart enough to simulate bad passwords with
82
+ the code as written.
76
83
"""
77
84
78
85
def setUp (self ):
79
86
self .t = wrds .Connection (autoconnect = False )
80
- self .t ._hostname = ' wrds.test.private'
87
+ self .t ._hostname = " wrds.test.private"
81
88
self .t ._port = 12345
82
- self .t ._username = ' faketestusername'
83
- self .t ._password = ' faketestuserpass'
84
- self .t ._dbname = ' testdbname'
89
+ self .t ._username = " faketestusername"
90
+ self .t ._password = " faketestuserpass"
91
+ self .t ._dbname = " testdbname"
85
92
self .t ._Connection__get_user_credentials = mock .Mock ()
86
- self .t ._Connection__get_user_credentials .return_value = (self .t ._username , self .t ._password )
93
+ self .t ._Connection__get_user_credentials .return_value = (
94
+ self .t ._username ,
95
+ self .t ._password ,
96
+ )
87
97
88
98
def test_connect_calls_sqlalchemy_engine_connect (self ):
89
99
self .t .engine = mock .Mock ()
90
100
self .t .connect ()
91
101
self .t .engine .connect .assert_called_once ()
92
102
93
- @mock .patch (' wrds.sql.sa' )
103
+ @mock .patch (" wrds.sql.sa" )
94
104
def test_connect_calls_get_user_credentials_on_exception (self , mock_sa ):
95
105
self .t .engine = mock .Mock ()
96
- self .t .engine .connect .side_effect = Exception (' Fake exception for testing' )
106
+ self .t .engine .connect .side_effect = Exception (" Fake exception for testing" )
97
107
self .t .connect ()
98
108
self .t ._Connection__get_user_credentials .assert_called_once ()
99
109
100
- @mock .patch (' wrds.sql.sa' )
110
+ @mock .patch (" wrds.sql.sa" )
101
111
def test_connect_calls_sqlalchemy_create_engine_on_exception (self , mock_sa ):
102
112
self .t .engine = mock .Mock ()
103
- self .t .engine .connect .side_effect = Exception (' Fake exception for testing' )
104
- connstring = ' postgresql://{usr}:{pwd}@{host}:{port}/{dbname}'
113
+ self .t .engine .connect .side_effect = Exception (" Fake exception for testing" )
114
+ connstring = " postgresql://{usr}:{pwd}@{host}:{port}/{dbname}"
105
115
connstring = connstring .format (
106
116
usr = self .t ._username ,
107
117
pwd = self .t ._password ,
108
118
host = self .t ._hostname ,
109
119
port = self .t ._port ,
110
- dbname = self .t ._dbname )
120
+ dbname = self .t ._dbname ,
121
+ )
111
122
self .t .connect ()
112
123
mock_sa .create_engine .assert_called_with (
113
124
connstring ,
114
- connect_args = {' sslmode' : ' require' ,
115
- 'application_name' : wrds . sql . appname } ,
116
- isolation_level = 'AUTOCOMMIT' )
125
+ connect_args = {" sslmode" : " require" , "application_name" : wrds . sql . appname } ,
126
+ isolation_level = "AUTOCOMMIT" ,
127
+ )
117
128
118
129
119
130
class TestRawSqlMethod (unittest .TestCase ):
120
- """ Test the wrds.Connection.raw_sql method.
131
+ """Test the wrds.Connection.raw_sql method.
121
132
122
- wrds.Connection.raw_sql() should be able to take
123
- 'normal' and parameterized SQL,
124
- and throw an error if not all parameters are supplied.
133
+ wrds.Connection.raw_sql() should be able to take
134
+ 'normal' and parameterized SQL,
135
+ and throw an error if not all parameters are supplied.
125
136
"""
126
137
127
138
def setUp (self ):
128
139
self .t = wrds .Connection (autoconnect = False )
129
- self .t ._hostname = ' wrds.test.private'
140
+ self .t ._hostname = " wrds.test.private"
130
141
self .t ._port = 12345
131
- self .t ._username = ' faketestusername'
132
- self .t ._password = ' faketestuserpass'
133
- self .t ._dbname = ' testdbname'
142
+ self .t ._username = " faketestusername"
143
+ self .t ._password = " faketestuserpass"
144
+ self .t ._dbname = " testdbname"
134
145
self .t ._Connection__get_user_credentials = mock .Mock ()
135
- self .t ._Connection__get_user_credentials .return_value = (self .t ._username , self .t ._password )
146
+ self .t ._Connection__get_user_credentials .return_value = (
147
+ self .t ._username ,
148
+ self .t ._password ,
149
+ )
136
150
self .t .connection = mock .Mock ()
137
151
self .t .engine = mock .Mock ()
138
152
139
- @mock .patch (' wrds.sql.sa' )
140
- @mock .patch (' wrds.sql.pd' )
153
+ @mock .patch (" wrds.sql.sa" )
154
+ @mock .patch (" wrds.sql.pd" )
141
155
def test_rawsql_takes_unparameterized_sql (self , mock_pd , mock_sa ):
142
156
sql = "SELECT * FROM information_schema.tables LIMIT 1"
143
157
self .t .raw_sql (sql )
@@ -150,10 +164,13 @@ def test_rawsql_takes_unparameterized_sql(self, mock_pd, mock_sa):
150
164
params = None ,
151
165
)
152
166
153
- @mock .patch (' wrds.sql.sa' )
154
- @mock .patch (' wrds.sql.pd' )
167
+ @mock .patch (" wrds.sql.sa" )
168
+ @mock .patch (" wrds.sql.pd" )
155
169
def test_rawsql_takes_parameterized_sql (self , mock_pd , mock_sa ):
156
- sql = "SELECT * FROM information_schema.tables where table_name = %(tablename)s LIMIT 1"
170
+ sql = (
171
+ "SELECT * FROM information_schema.tables "
172
+ "WHERE table_name = %(tablename)s LIMIT 1"
173
+ )
157
174
tablename = "pg_stat_activity"
158
175
self .t .engine = mock .Mock ()
159
176
self .t .raw_sql (sql , params = tablename )
@@ -171,7 +188,10 @@ class TestCreatePgpassFile(unittest.TestCase):
171
188
def setUp (self ):
172
189
self .t = wrds .Connection (autoconnect = False )
173
190
self .t ._Connection__get_user_credentials = mock .Mock ()
174
- self .t ._Connection__get_user_credentials .return_value = ('faketestusername' , 'faketestpassword' )
191
+ self .t ._Connection__get_user_credentials .return_value = (
192
+ "faketestusername" ,
193
+ "faketestpassword" ,
194
+ )
175
195
self .t ._Connection__create_pgpass_file_win32 = mock .Mock ()
176
196
self .t ._Connection__create_pgpass_file_unix = mock .Mock ()
177
197
@@ -185,16 +205,16 @@ def test_create_pgpass_calls_get_user_credentials_if_not_password(self):
185
205
self .t .create_pgpass_file ()
186
206
self .t ._Connection__get_user_credentials .assert_called_once ()
187
207
188
- @unittest .skipIf (sys .platform != ' win32' , ' Windows-only test' )
208
+ @unittest .skipIf (sys .platform != " win32" , " Windows-only test" )
189
209
def test_create_pgpass_calls_win32_version_if_windows (self ):
190
210
self .t .create_pgpass_file ()
191
211
self .t ._Connection__create_pgpass_file_win32 .assert_called_once ()
192
212
193
- @unittest .skipIf (sys .platform == ' win32' , ' Unix-only test' )
213
+ @unittest .skipIf (sys .platform == " win32" , " Unix-only test" )
194
214
def test_create_pgpass_calls_unix_version_if_unix (self ):
195
215
self .t .create_pgpass_file ()
196
216
self .t ._Connection__create_pgpass_file_unix .assert_called_once ()
197
217
198
218
199
- if ( __name__ == ' __main__' ) :
219
+ if __name__ == " __main__" :
200
220
unittest .main ()
0 commit comments