Changeset 137

Show
Ignore:
Timestamp:
2002-03-30 22:08:45 (7 years ago)
Author:
schwa
Message:
Module: moved code to generate random strings from class TestFileOperations
    to the module level. The binary_data function is then used in the
    new class BinaryDownloadMockSession.
TestFileOperations.test_ascii_upload: small changes.
TestFileOperations: added method test_binary_download.
Files:

Legend:

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

    r136 r137  
    3030# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 
    3131 
    32 # $Id: _test_ftputil.py,v 1.46 2002/03/30 21:54:38 schwa Exp $ 
     32# $Id: _test_ftputil.py,v 1.47 2002/03/30 22:08:45 schwa Exp $ 
    3333 
    3434import unittest 
     
    5454    mock_file_content = '\r\n'.join( map( str, range(20) ) ) 
    5555 
    56  
     56def random_data(pool, size=10000): 
     57    """ 
     58    Return a sequence of characters consisting of those from 
     59    the pool of integer numbers. 
     60    """ 
     61    character_list = [] 
     62    for i in range(size): 
     63        ordinal = random.choice(pool) 
     64        character_list.append( chr(ordinal) ) 
     65    result = ''.join(character_list) 
     66    return result 
     67     
     68def ascii_data(): 
     69    """Return an ASCII character string.""" 
     70    pool = range(32, 128) 
     71    pool.append( ord('\n') ) 
     72    return random_data(pool) 
     73     
     74def binary_data(): 
     75    """Return an binary character string.""" 
     76    pool = range(0, 256) 
     77    return random_data(pool) 
     78 
     79class BinaryDownloadMockSession(_mock_ftplib.MockSession): 
     80    mock_file_content = binary_data() 
     81 
     82     
    5783def ftp_host_factory(session_factory=_mock_ftplib.MockSession, 
    5884                     ftp_host_class=ftputil.FTPHost): 
     
    350376                          'notthere', 'r') 
    351377 
    352     def random_data(self, pool, size=10000): 
    353         """ 
    354         Return a sequence of characters consisting of those from 
    355         the pool of integer numbers. 
    356         """ 
    357         character_list = [] 
    358         for i in range(size): 
    359             ordinal = random.choice(pool) 
    360             character_list.append( chr(ordinal) ) 
    361         result = ''.join(character_list) 
    362         return result 
    363          
    364     def ascii_data(self): 
    365         """Return an ASCII character string.""" 
    366         pool = range(32, 128) 
    367         pool.append( ord('\n') ) 
    368         return self.random_data(pool) 
    369          
    370     def binary_data(self): 
    371         """Return an binary character string.""" 
    372         pool = range(0, 256) 
    373         return self.random_data(pool) 
    374  
    375378    def test_ascii_upload(self): 
    376379        """Test ASCII mode upload.""" 
    377380        local_source = '__test_source' 
    378381        # generate file 
    379         data = self.ascii_data() 
     382        data = ascii_data() 
    380383        source_file = open(local_source, 'w') 
    381384        source_file.write(data) 
     
    385388        host.upload(local_source, 'dummy') 
    386389        # check uploaded content 
     390        # the data which was uploaded has its line endings converted 
     391        #  so the conversion must also be applied to 'data' 
    387392        data = data.replace('\n', '\r\n') 
    388         file_content = _mock_ftplib.content_of('dummy') 
    389         self.assertEqual(data, file_content) 
    390  
    391 #         host.download(remote_path, local_test_path) 
    392 #         # compare local data 
    393 #         input_ = file(local_source) 
    394 #         original = input_.read() 
    395 #         input_.close() 
    396 #         input_ = file(local_test_path) 
    397 #         copy = input_.read() 
    398 #         input_.close() 
    399 #         self.assertEqual(original, copy) 
    400 #         # test binary up/download 
    401 #         host.upload(local_source, remote_path, 'b') 
    402 #         host.download(remote_path, local_test_path, 'b') 
    403 #         # compare local data 
    404 #         input_ = file(local_source, 'rb') 
    405 #         original = input_.read() 
    406 #         input_.close() 
    407 #         input_ = file(local_test_path, 'rb') 
    408 #         copy = input_.read() 
    409 #         input_.close() 
    410 #         self.assertEqual(original, copy) 
    411 #         # clean up 
    412 #         host.remove(remote_path) 
    413 #         os.remove(local_test_path) 
     393        remote_file_content = _mock_ftplib.content_of('dummy') 
     394        self.assertEqual(data, remote_file_content) 
     395        # clean up 
     396        os.unlink(local_source) 
     397 
     398    def test_binary_download(self): 
     399        """Test binary mode download.""" 
     400        local_target = '__test_target' 
     401        host = ftp_host_factory(session_factory=BinaryDownloadMockSession) 
     402        # download 
     403        host.download('dummy', local_target, 'b') 
     404        # read file and compare 
     405        data = open(local_target, 'rb').read() 
     406        remote_file_content = _mock_ftplib.content_of('dummy') 
     407        self.assertEqual(data, remote_file_content) 
     408        # clean up 
     409        os.unlink(local_target) 
    414410 
    415411