| | 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 | |
|---|