Changeset 120

Show
Ignore:
Timestamp:
2002-03-30 16:50:08 (7 years ago)
Author:
schwa
Message:
Moved class FailOnLoginSession from _mock_ftplib.py to _test_ftputil.py .
Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/_mock_ftplib.py

    r119 r120  
    3030# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 
    3131 
    32 # $Id: _mock_ftplib.py,v 1.8 2002/03/30 16:48:07 schwa Exp $ 
     32# $Id: _mock_ftplib.py,v 1.9 2002/03/30 16:50:08 schwa Exp $ 
    3333 
    3434""" 
     
    410410 
    411411 
    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  
    3030# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 
    3131 
    32 # $Id: _test_ftputil.py,v 1.33 2002/03/30 15:36:25 schwa Exp $ 
     32# $Id: _test_ftputil.py,v 1.34 2002/03/30 16:50:08 schwa Exp $ 
    3333 
    3434import unittest 
     
    4242 
    4343 
    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) 
     44class FailOnLoginSession(_mock_ftplib.MockSession): 
     45    def __init__(self, host='', user='', password=''): 
     46        raise ftplib.error_perm 
     47 
     48class 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 
     69def 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) 
    4872 
    4973 
     
    5377    def test_invalid_login(self): 
    5478        """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) 
    5781 
    5882 
     
    6589    def test_failing_lstat(self): 
    6690        """Test whether lstat fails for a nonexistent path.""" 
    67         host = FTPHostWrapper(_mock_ftplib.MockSession) 
     91        host = ftp_host_factory(_mock_ftplib.MockSession) 
    6892        self.assertRaises(ftputil.PermanentError, host.lstat, 
    6993                          '/home/sschw/notthere') 
     
    7397    def test_lstat_one_file(self): 
    7498        """Test FTPHost.lstat with a file.""" 
    75         host = FTPHostWrapper(_mock_ftplib.MockSession) 
     99        host = ftp_host_factory(_mock_ftplib.MockSession) 
    76100        stat_result = host.lstat('/home/sschwarzer/index.html') 
    77101        self.assertEqual( oct(stat_result.st_mode), '0100644' ) 
     
    81105        """Test FTPHost.lstat with a directory.""" 
    82106        # some directory 
    83         host = FTPHostWrapper(_mock_ftplib.MockSession) 
     107        host = ftp_host_factory(_mock_ftplib.MockSession) 
    84108        stat_result = host.lstat('/home/sschwarzer/scios2') 
    85109        self.assertEqual( oct(stat_result.st_mode), '042755' ) 
     
    97121    def test_lstat_via_stat_module(self): 
    98122        """Test FTPHost.lstat indirectly via stat module.""" 
    99         host = FTPHostWrapper(_mock_ftplib.MockSession) 
     123        host = ftp_host_factory(_mock_ftplib.MockSession) 
    100124        stat_result = host.lstat('/home/sschwarzer/') 
    101125        self.failUnless( stat.S_ISDIR(stat_result.st_mode) ) 
     
    107131    def test_failing_listdir(self): 
    108132        """Test failing FTPHost.listdir.""" 
    109         host = FTPHostWrapper(_mock_ftplib.MockSession) 
     133        host = ftp_host_factory(_mock_ftplib.MockSession) 
    110134        self.assertRaises(ftputil.PermanentError, 
    111135                          host.listdir, 'notthere') 
     
    114138        """Test succeeding FTPHost.listdir.""" 
    115139        # do we have all expected "files"? 
    116         host = FTPHostWrapper(_mock_ftplib.MockSession) 
     140        host = ftp_host_factory(_mock_ftplib.MockSession) 
    117141        self.assertEqual( len(host.listdir(host.curdir)), 9 ) 
    118142        # have they the expected names? 
    119         host = FTPHostWrapper(_mock_ftplib.MockSession) 
     143        host = ftp_host_factory(_mock_ftplib.MockSession) 
    120144        expected = ('chemeng download image index.html os2 ' 
    121145                    'osup publications python scios2').split() 
     
    131155        """Test FTPHost._Path.isdir/isfile/islink.""" 
    132156        testdir = '/home/sschwarzer' 
    133         host = FTPHostWrapper(_mock_ftplib.MockSession) 
     157        host = ftp_host_factory(_mock_ftplib.MockSession) 
    134158        host.chdir(testdir) 
    135159        # test a path which isn't there 
     
    148172        # test a link 
    149173        testlink = '/home/sschwarzer/osup' 
    150         # uncomment these two when following links is implemented 
     174        #XXX uncomment these two when following links is implemented 
    151175        #self.failIf( host.path.isdir(testlink) ) 
    152176        #self.failIf( host.path.isfile(testlink) ) 
     
    195219#         host.remove(path2) 
    196220# 
    197 #     def binary_write(self): 
    198 #         """Write binary data to the host and read it back.""" 
    199 #         host = self.host 
    200 #         local_data = '\000a\001b\r\n\002c\003\n\004\r\005' 
    201 #         # write data in binary mode 
    202 #         self.write_test_data(local_data, 'wb') 
    203 #         # check the file length on the remote host 
    204 #         remote_size = host.path.getsize(self.remote_name) 
    205 #         self.assertEqual( remote_size, len(local_data) ) 
    206 #         # read the data back and compare 
    207 #         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) 
    211235# 
    212236#     def ascii_write(self): 
     
    248272#                           self.testdir, 'w') 
    249273#         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() 
    255276# 
    256277#     def ascii_read(self):