Changeset 849:0997baf2fa68
- Timestamp:
- 2010-02-11 20:36:28 (6 months ago)
- Author:
- Stefan Schwarzer <sschwarzer@…>
- Branch:
- default
- Message:
-
Added unit test for download operations. Improved `make_file` method.
- Files:
-
Legend:
- Unmodified
- Added
- Removed
-
|
r843
|
r849
|
|
| 1 | | # Copyright (C) 2003-2009, Stefan Schwarzer |
| | 1 | # Copyright (C) 2003-2010, Stefan Schwarzer |
| 2 | 2 | # All rights reserved. |
| 3 | 3 | # |
| … |
… |
|
| 139 | 139 | self.cleaner.add_file(path) |
| 140 | 140 | file_ = self.host.file(path, 'wb') |
| | 141 | # write something; otherwise the FTP server might not update |
| | 142 | # the time of last modification if the file existed before |
| | 143 | file_.write("\n") |
| 141 | 144 | file_.close() |
| 142 | 145 | |
| 143 | 146 | def make_local_file(self): |
| 144 | | fobj = file('_localfile_', 'wb') |
| | 147 | fobj = file('_local_file_', 'wb') |
| 145 | 148 | fobj.write("abc\x12\x34def\t") |
| 146 | 149 | fobj.close() |
| … |
… |
|
| 459 | 462 | host = self.host |
| 460 | 463 | host.synchronize_times() |
| | 464 | local_file = '_local_file_' |
| | 465 | remote_file = '_remote_file_' |
| 461 | 466 | # make local file and upload it |
| 462 | 467 | self.make_local_file() |
| … |
… |
|
| 465 | 470 | time.sleep(65) |
| 466 | 471 | try: |
| 467 | | self.cleaner.add_file('_remotefile_') |
| 468 | | host.upload('_localfile_', '_remotefile_', 'b') |
| | 472 | self.cleaner.add_file(remote_file) |
| | 473 | host.upload(local_file, remote_file, 'b') |
| 469 | 474 | # retry; shouldn't be uploaded |
| 470 | | uploaded = host.upload_if_newer('_localfile_', '_remotefile_', 'b') |
| | 475 | uploaded = host.upload_if_newer(local_file, remote_file, 'b') |
| 471 | 476 | self.assertEqual(uploaded, False) |
| 472 | 477 | # rewrite the local file |
| 473 | 478 | self.make_local_file() |
| 474 | | time.sleep(65) |
| 475 | 479 | # retry; should be uploaded now |
| 476 | | uploaded = host.upload_if_newer('_localfile_', '_remotefile_', 'b') |
| | 480 | uploaded = host.upload_if_newer(local_file, remote_file, 'b') |
| 477 | 481 | self.assertEqual(uploaded, True) |
| 478 | 482 | finally: |
| 479 | 483 | # clean up |
| 480 | | os.unlink('_localfile_') |
| | 484 | os.unlink(local_file) |
| | 485 | |
| | 486 | def test_download(self): |
| | 487 | host = self.host |
| | 488 | host.synchronize_times() |
| | 489 | local_file = '_local_file_' |
| | 490 | remote_file = '_remote_file_' |
| | 491 | # make a remote file |
| | 492 | self.make_file(remote_file) |
| | 493 | # file should be downloaded as it's not present yet |
| | 494 | downloaded = host.download_if_newer(remote_file, local_file, 'b') |
| | 495 | self.assertEqual(downloaded, True) |
| | 496 | try: |
| | 497 | # local file is present and newer, so shouldn't download |
| | 498 | downloaded = host.download_if_newer(remote_file, local_file, 'b') |
| | 499 | self.assertEqual(downloaded, False) |
| | 500 | # wait; else small time differences between client and server |
| | 501 | # actually could trigger the update |
| | 502 | time.sleep(65) |
| | 503 | # re-make the remote file |
| | 504 | self.make_file(remote_file) |
| | 505 | # local file is present but older, so should download |
| | 506 | downloaded = host.download_if_newer(remote_file, local_file, 'b') |
| | 507 | self.assertEqual(downloaded, True) |
| | 508 | finally: |
| | 509 | # clean up |
| | 510 | os.unlink(local_file) |
| 481 | 511 | |
| 482 | 512 | # |
| … |
… |
|
| 486 | 516 | host = self.host |
| 487 | 517 | self.assertRaises(ftp_error.PermanentError, host.remove, "nonexistent") |
| 488 | | |
| | 518 | |
| 489 | 519 | def test_remove_existent_file(self): |
| 490 | 520 | self.cleaner.add_file('_testfile_') |