def test_binary_write(self): """Write binary data with `write`.""" host = test_base.ftp_host_factory() data = b"\000a\001b\r\n\002c\003\n\004\r\005" with host.open("dummy", "wb") as output: output.write(data) child_data = mock_ftplib.content_of("dummy") expected_data = data self.assertEqual(child_data, expected_data)
def test_binary_write(self): """Write binary data with `write`.""" host = test_base.ftp_host_factory() data = b"\000a\001b\r\n\002c\003\n\004\r\005" with host.open("dummy", "wb") as output: output.write(data) child_data = mock_ftplib.content_of("dummy") expected_data = data assert child_data == expected_data
def test_ascii_write(self): """Write ASCII text with `write`.""" host = test_base.ftp_host_factory() data = " \nline 2\nline 3" with host.open("dummy", "w", newline="\r\n") as output: output.write(data) child_data = mock_ftplib.content_of("dummy") # This corresponds to the byte stream, so expect a `bytes` object. expected_data = b" \r\nline 2\r\nline 3" self.assertEqual(child_data, expected_data)
def test_ascii_write(self): """Write ASCII text with `write`.""" host = test_base.ftp_host_factory() data = " \nline 2\nline 3" with host.open("dummy", "w", newline="\r\n") as output: output.write(data) child_data = mock_ftplib.content_of("dummy") # This corresponds to the byte stream, so expect a `bytes` object. expected_data = b" \r\nline 2\r\nline 3" assert child_data == expected_data
def compare_and_delete_downloaded_data(self, file_name): """ Compare content of downloaded file with its source, then delete the local target file. """ with open(file_name, "rb") as fobj: data = fobj.read() # The name `newer` is used by all callers, so use it here, too. remote_file_content = mock_ftplib.content_of("newer") self.assertEqual(data, remote_file_content) # Clean up os.unlink(file_name)
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.open("dummy", "w", newline="\r\n") output.writelines(data) output.close() child_data = mock_ftplib.content_of("dummy") expected_data = b" \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_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.open("dummy", "w", newline="\r\n") output.writelines(data) output.close() child_data = mock_ftplib.content_of("dummy") expected_data = b" \r\nline 2\r\nline 3" assert child_data == expected_data # Ensure that the original data was not modified. assert data == backup_data
def test_download(self): """Test mode download.""" local_target = "_test_target_" host = test_base.ftp_host_factory( session_factory=BinaryDownloadMockSession) # Download host.download("dummy", local_target) # Read file and compare with open(local_target, "rb") as fobj: data = fobj.read() remote_file_content = mock_ftplib.content_of("dummy") self.assertEqual(data, remote_file_content) # Clean up os.unlink(local_target)
def test_conditional_upload(self): """Test conditional upload.""" local_source = "_test_source_" data = binary_data() self.generate_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) 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)