Changeset 120
- Timestamp:
- 2002-03-30 16:50:08 (7 years ago)
- Files:
-
- trunk/_mock_ftplib.py (modified) (2 diffs)
- trunk/_test_ftputil.py (modified) (13 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
trunk/_mock_ftplib.py
r119 r120 30 30 # SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 31 31 32 # $Id: _mock_ftplib.py,v 1. 8 2002/03/30 16:48:07schwa Exp $32 # $Id: _mock_ftplib.py,v 1.9 2002/03/30 16:50:08 schwa Exp $ 33 33 34 34 """ … … 410 410 411 411 412 class FailOnLoginSession(MockSession): 413 def __init__(self, host='', user='', password=''): 414 raise ftplib.error_perm 415 416 412 trunk/_test_ftputil.py
r118 r120 30 30 # SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 31 31 32 # $Id: _test_ftputil.py,v 1.3 3 2002/03/30 15:36:25schwa Exp $32 # $Id: _test_ftputil.py,v 1.34 2002/03/30 16:50:08 schwa Exp $ 33 33 34 34 import unittest … … 42 42 43 43 44 class FTPHostWrapper(ftputil.FTPHost): 45 def __init__(self, session_factory): 46 ftputil.FTPHost.__init__(self, 'dummy_host', 'dummy_user', 47 'dummy_password', session_factory=session_factory) 44 class FailOnLoginSession(_mock_ftplib.MockSession): 45 def __init__(self, host='', user='', password=''): 46 raise ftplib.error_perm 47 48 class InspectableFTPHost(ftputil.FTPHost): 49 def file_content(self, child_index): 50 """ 51 Return content for the associated file object of a child 52 FTPHost object. 53 54 Usage of the getvalue() method implies that the file object 55 really is a StringIO.StringIO object. In fact, this kind of 56 file object is established by the MockSocket class which in 57 turn is used by the MockSession class. 58 59 Unfortunately, this method requires deep knowledge of the 60 internal implementation of the FTPHost and _FTPFile classes. 61 """ 62 child = self._children[child_index] 63 ftp_file = child._file 64 file_object = ftp_file._fo 65 file_content = file_object.getvalue() 66 return file_content 67 68 69 def ftp_host_factory(session_factory, ftp_host_class=ftputil.FTPHost): 70 return ftp_host_class('dummy_host', 'dummy_user', 'dummy_password', 71 session_factory=session_factory) 48 72 49 73 … … 53 77 def test_invalid_login(self): 54 78 """Login to invalid host must fail.""" 55 self.assertRaises(ftputil.FTPOSError, FTPHostWrapper,56 _mock_ftplib.FailOnLoginSession)79 self.assertRaises(ftputil.FTPOSError, ftp_host_factory, 80 FailOnLoginSession) 57 81 58 82 … … 65 89 def test_failing_lstat(self): 66 90 """Test whether lstat fails for a nonexistent path.""" 67 host = FTPHostWrapper(_mock_ftplib.MockSession)91 host = ftp_host_factory(_mock_ftplib.MockSession) 68 92 self.assertRaises(ftputil.PermanentError, host.lstat, 69 93 '/home/sschw/notthere') … … 73 97 def test_lstat_one_file(self): 74 98 """Test FTPHost.lstat with a file.""" 75 host = FTPHostWrapper(_mock_ftplib.MockSession)99 host = ftp_host_factory(_mock_ftplib.MockSession) 76 100 stat_result = host.lstat('/home/sschwarzer/index.html') 77 101 self.assertEqual( oct(stat_result.st_mode), '0100644' ) … … 81 105 """Test FTPHost.lstat with a directory.""" 82 106 # some directory 83 host = FTPHostWrapper(_mock_ftplib.MockSession)107 host = ftp_host_factory(_mock_ftplib.MockSession) 84 108 stat_result = host.lstat('/home/sschwarzer/scios2') 85 109 self.assertEqual( oct(stat_result.st_mode), '042755' ) … … 97 121 def test_lstat_via_stat_module(self): 98 122 """Test FTPHost.lstat indirectly via stat module.""" 99 host = FTPHostWrapper(_mock_ftplib.MockSession)123 host = ftp_host_factory(_mock_ftplib.MockSession) 100 124 stat_result = host.lstat('/home/sschwarzer/') 101 125 self.failUnless( stat.S_ISDIR(stat_result.st_mode) ) … … 107 131 def test_failing_listdir(self): 108 132 """Test failing FTPHost.listdir.""" 109 host = FTPHostWrapper(_mock_ftplib.MockSession)133 host = ftp_host_factory(_mock_ftplib.MockSession) 110 134 self.assertRaises(ftputil.PermanentError, 111 135 host.listdir, 'notthere') … … 114 138 """Test succeeding FTPHost.listdir.""" 115 139 # do we have all expected "files"? 116 host = FTPHostWrapper(_mock_ftplib.MockSession)140 host = ftp_host_factory(_mock_ftplib.MockSession) 117 141 self.assertEqual( len(host.listdir(host.curdir)), 9 ) 118 142 # have they the expected names? 119 host = FTPHostWrapper(_mock_ftplib.MockSession)143 host = ftp_host_factory(_mock_ftplib.MockSession) 120 144 expected = ('chemeng download image index.html os2 ' 121 145 'osup publications python scios2').split() … … 131 155 """Test FTPHost._Path.isdir/isfile/islink.""" 132 156 testdir = '/home/sschwarzer' 133 host = FTPHostWrapper(_mock_ftplib.MockSession)157 host = ftp_host_factory(_mock_ftplib.MockSession) 134 158 host.chdir(testdir) 135 159 # test a path which isn't there … … 148 172 # test a link 149 173 testlink = '/home/sschwarzer/osup' 150 # uncomment these two when following links is implemented174 #XXX uncomment these two when following links is implemented 151 175 #self.failIf( host.path.isdir(testlink) ) 152 176 #self.failIf( host.path.isfile(testlink) ) … … 195 219 # host.remove(path2) 196 220 # 197 #def binary_write(self):198 #"""Write binary data to the host and read it back."""199 #host = self.host200 #local_data = '\000a\001b\r\n\002c\003\n\004\r\005'201 ## write data in binary mode202 #self.write_test_data(local_data, 'wb')203 ## check the file length on the remote host204 #remote_size = host.path.getsize(self.remote_name)205 #self.assertEqual( remote_size, len(local_data) )206 ## read the data back and compare207 #input_ = host.file(self.remote_name, 'rb')208 #remote_data = input_.read()209 #input_.close()210 #self.assertEqual(local_data, remote_data)221 def binary_write(self): 222 """Write binary data to the host and read it back.""" 223 host = self.host 224 local_data = '\000a\001b\r\n\002c\003\n\004\r\005' 225 # write data in binary mode 226 self.write_test_data(local_data, 'wb') 227 # check the file length on the remote host 228 remote_size = host.path.getsize(self.remote_name) 229 self.assertEqual( remote_size, len(local_data) ) 230 # read the data back and compare 231 input_ = host.file(self.remote_name, 'rb') 232 remote_data = input_.read() 233 input_.close() 234 self.assertEqual(local_data, remote_data) 211 235 # 212 236 # def ascii_write(self): … … 248 272 # self.testdir, 'w') 249 273 # self.binary_write() 250 # self.ascii_write() 251 # self.ascii_writelines() 252 # # clean up 253 # host.remove(self.remote_name) 254 # host.chdir(self.rootdir) 274 # #self.ascii_write() 275 # #self.ascii_writelines() 255 276 # 256 277 # def ascii_read(self):
