Changeset 150

Show
Ignore:
Timestamp:
2002-03-31 23:18:06 (7 years ago)
Author:
schwa
Message:
TestUploadAndDownload: added tests for condiitional upload and download.
Files:

Legend:

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

    r148 r150  
    3030# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 
    3131 
    32 # $Id: _test_ftputil.py,v 1.54 2002/03/31 20:36:28 schwa Exp $ 
     32# $Id: _test_ftputil.py,v 1.55 2002/03/31 23:18:06 schwa Exp $ 
    3333 
    3434import unittest 
     
    8989 
    9090# 
     91# customized FTPHost class for conditional upload/download tests 
     92# 
     93class FailingUploadAndDownloadFTPHost(ftputil.FTPHost): 
     94    def upload(self, source, target, mode=''): 
     95        assert 0, "FTPHost.upload should not have been called" 
     96     
     97    def download(self, source, target, mode=''): 
     98        assert 0, "FTPHost.download should not have been called" 
     99 
     100 
     101# 
    91102# factory to produce FTPHost-like classes from a given FTPHost 
    92103#  class and a given MockSession class 
     
    135146 
    136147    def test_lstat_one_file(self): 
    137         """Test FTPHost.lstat with a file.""" 
     148        """Test FTPHost.lstat for a file.""" 
    138149        host = ftp_host_factory() 
    139150        stat_result = host.lstat('/home/sschwarzer/index.html') 
     
    142153 
    143154    def test_lstat_one_dir(self): 
    144         """Test FTPHost.lstat with a directory.""" 
     155        """Test FTPHost.lstat for a directory.""" 
    145156        # some directory 
    146157        host = ftp_host_factory() 
     
    220231 
    221232    def test_caching(self): 
    222         """Test if _FTPFile cache of FTPHost object works.""" 
     233        """Test whether _FTPFile cache of FTPHost object works.""" 
    223234        host = ftp_host_factory() 
    224235        self.assertEqual( len(host._children), 0 ) 
     
    392403    """Test ascii upload and binary download as examples.""" 
    393404 
     405    def generate_ascii_file(self, data, filename): 
     406        """Generate an ASCII data file.""" 
     407        source_file = open(filename, 'w') 
     408        source_file.write(data) 
     409        source_file.close() 
     410         
    394411    def test_ascii_upload(self): 
    395412        """Test ASCII mode upload.""" 
    396413        local_source = '__test_source' 
    397         # generate file 
    398414        data = ascii_data() 
    399         source_file = open(local_source, 'w') 
    400         source_file.write(data) 
    401         source_file.close() 
     415        self.generate_ascii_file(data, local_source) 
    402416        # upload 
    403417        host = ftp_host_factory() 
     
    425439        os.unlink(local_target) 
    426440 
     441    def test_conditional_upload(self): 
     442        """Test conditional ASCII mode upload.""" 
     443        local_source = '__test_source' 
     444        data = ascii_data() 
     445        self.generate_ascii_file(data, local_source) 
     446        # target is newer, so don't upload 
     447        host = ftp_host_factory( 
     448               ftp_host_class=FailingUploadAndDownloadFTPHost) 
     449        host.upload_if_newer(local_source, '/home/newer') 
     450        # target is older, so upload 
     451        host = ftp_host_factory() 
     452        host.upload_if_newer(local_source, '/home/older') 
     453        # check uploaded content 
     454        # the data which was uploaded has its line endings converted 
     455        #  so the conversion must also be applied to 'data' 
     456        data = data.replace('\n', '\r\n') 
     457        remote_file_content = _mock_ftplib.content_of('/home/older') 
     458        self.assertEqual(data, remote_file_content) 
     459        # target doesn't exist, so upload 
     460        host = ftp_host_factory() 
     461        host.upload_if_newer(local_source, '/home/notthere') 
     462        remote_file_content = _mock_ftplib.content_of('/home/notthere') 
     463        self.assertEqual(data, remote_file_content) 
     464        # clean up 
     465        os.unlink(local_source) 
     466         
     467    def compare_and_delete_downloaded_data(self, filename): 
     468        """Compare content of downloaded file with its source, then 
     469        delete the local target file.""" 
     470        data = open(filename, 'rb').read() 
     471        remote_file_content = _mock_ftplib.content_of('/home/newer') 
     472        self.assertEqual(data, remote_file_content) 
     473        # clean up 
     474        os.unlink(filename) 
     475 
     476    def test_conditional_download_without_target(self): 
     477        "Test conditional binary mode download when no target file exists." 
     478        local_target = '__test_target' 
     479        # target does not exist, so download 
     480        host = ftp_host_factory(session_factory=BinaryDownloadMockSession) 
     481        host.download_if_newer('/home/newer', local_target, 'b') 
     482        self.compare_and_delete_downloaded_data(local_target) 
     483 
     484    def test_conditional_download_with_older_target(self): 
     485        """Test conditional binary mode download with newer source file.""" 
     486        local_target = '__test_target' 
     487        # make target file 
     488        open(local_target, 'w').close() 
     489        # source is newer, so download 
     490        host = ftp_host_factory(session_factory=BinaryDownloadMockSession) 
     491        host.download_if_newer('/home/newer', local_target, 'b') 
     492        self.compare_and_delete_downloaded_data(local_target) 
     493 
     494    def test_conditional_download_with_newer_target(self): 
     495        """Test conditional binary mode download with older source file.""" 
     496        local_target = '__test_target' 
     497        # make target file 
     498        open(local_target, 'w').close() 
     499        # source is older, so don't download 
     500        host = ftp_host_factory(session_factory=BinaryDownloadMockSession) 
     501        host = ftp_host_factory( 
     502               ftp_host_class=FailingUploadAndDownloadFTPHost, 
     503               session_factory=BinaryDownloadMockSession) 
     504        host.download_if_newer('/home/older', local_target, 'b') 
     505        # remove target file 
     506        os.unlink(local_target) 
     507 
    427508 
    428509if __name__ == '__main__':