def test_conditional_upload(self): """Test conditional ASCII mode upload.""" local_source = '__test_source' data = ascii_data() self.generate_ascii_file(data, local_source) # target is newer, so don't upload host = _test_base.ftp_host_factory( ftp_host_class=FailingUploadAndDownloadFTPHost) flag = host.upload_if_newer(local_source, '/home/newer') self.assertEqual(flag, False) # target is older, so upload host = _test_base.ftp_host_factory() flag = host.upload_if_newer(local_source, '/home/older') self.assertEqual(flag, True) # check uploaded content # the data which was uploaded has its line endings converted # so the conversion must also be applied to 'data' data = data.replace('\n', '\r\n') remote_file_content = _mock_ftplib.content_of('older') self.assertEqual(data, remote_file_content) # target doesn't exist, so upload host = _test_base.ftp_host_factory() flag = host.upload_if_newer(local_source, '/home/notthere') self.assertEqual(flag, True) remote_file_content = _mock_ftplib.content_of('notthere') self.assertEqual(data, remote_file_content) # clean up os.unlink(local_source)
def compare_and_delete_downloaded_data(self, filename): """Compare content of downloaded file with its source, then delete the local target file.""" data = open(filename, 'rb').read() remote_file_content = _mock_ftplib.content_of('newer') self.assertEqual(data, remote_file_content) # clean up os.unlink(filename)
def test_ascii_write(self): """Write ASCII text with `write`.""" host = _test_base.ftp_host_factory() data = ' \nline 2\nline 3' output = host.file('dummy', 'w') output.write(data) output.close() child_data = _mock_ftplib.content_of('dummy') expected_data = ' \r\nline 2\r\nline 3' self.assertEqual(child_data, expected_data)
def test_binary_write(self): """Write binary data with `write`.""" host = _test_base.ftp_host_factory() data = '\000a\001b\r\n\002c\003\n\004\r\005' output = host.file('dummy', 'wb') output.write(data) output.close() child_data = _mock_ftplib.content_of('dummy') expected_data = data self.assertEqual(child_data, expected_data)
def test_binary_download(self): """Test binary mode download.""" local_target = '__test_target' host = _test_base.ftp_host_factory( session_factory=BinaryDownloadMockSession) # download host.download('dummy', local_target, 'b') # read file and compare data = open(local_target, 'rb').read() remote_file_content = _mock_ftplib.content_of('dummy') self.assertEqual(data, remote_file_content) # clean up os.unlink(local_target)
def test_ascii_writelines(self): """Write ASCII text with `writelines`.""" host = _test_base.ftp_host_factory() data = [' \n', 'line 2\n', 'line 3'] backup_data = data[:] output = host.file('dummy', 'w') output.writelines(data) output.close() child_data = _mock_ftplib.content_of('dummy') expected_data = ' \r\nline 2\r\nline 3' self.assertEqual(child_data, expected_data) # ensure that the original data was not modified self.assertEqual(data, backup_data)
def test_ascii_upload(self): """Test ASCII mode upload.""" local_source = '__test_source' data = ascii_data() self.generate_ascii_file(data, local_source) # upload host = _test_base.ftp_host_factory() host.upload(local_source, 'dummy') # check uploaded content # the data which was uploaded has its line endings converted # so the conversion must also be applied to `data` data = data.replace('\n', '\r\n') remote_file_content = _mock_ftplib.content_of('dummy') self.assertEqual(data, remote_file_content) # clean up os.unlink(local_source)