| 1 | |
|---|
| 2 | |
|---|
| 3 | |
|---|
| 4 | |
|---|
| 5 | from __future__ import with_statement |
|---|
| 6 | |
|---|
| 7 | import unittest |
|---|
| 8 | |
|---|
| 9 | import _test_base |
|---|
| 10 | import ftp_error |
|---|
| 11 | |
|---|
| 12 | from _test_ftputil import FailOnLoginSession |
|---|
| 13 | from _test_ftp_file import InaccessibleDirSession, ReadMockSession |
|---|
| 14 | |
|---|
| 15 | |
|---|
| 16 | |
|---|
| 17 | class ClientCodeException(Exception): |
|---|
| 18 | pass |
|---|
| 19 | |
|---|
| 20 | |
|---|
| 21 | |
|---|
| 22 | |
|---|
| 23 | |
|---|
| 24 | class TestHostContextManager(unittest.TestCase): |
|---|
| 25 | |
|---|
| 26 | def test_normal_operation(self): |
|---|
| 27 | with _test_base.ftp_host_factory() as host: |
|---|
| 28 | self.assertEqual(host.closed, False) |
|---|
| 29 | self.assertEqual(host.closed, True) |
|---|
| 30 | |
|---|
| 31 | def test_ftputil_exception(self): |
|---|
| 32 | try: |
|---|
| 33 | with _test_base.ftp_host_factory(FailOnLoginSession) as host: |
|---|
| 34 | pass |
|---|
| 35 | except ftp_error.FTPOSError: |
|---|
| 36 | |
|---|
| 37 | |
|---|
| 38 | |
|---|
| 39 | self.failIf('host' in locals()) |
|---|
| 40 | else: |
|---|
| 41 | raise self.failureException("ftp_error.FTPOSError not raised") |
|---|
| 42 | |
|---|
| 43 | def test_client_code_exception(self): |
|---|
| 44 | try: |
|---|
| 45 | with _test_base.ftp_host_factory() as host: |
|---|
| 46 | self.assertEqual(host.closed, False) |
|---|
| 47 | raise ClientCodeException() |
|---|
| 48 | except ClientCodeException: |
|---|
| 49 | self.assertEqual(host.closed, True) |
|---|
| 50 | else: |
|---|
| 51 | raise self.failureException("ClientCodeException not raised") |
|---|
| 52 | |
|---|
| 53 | |
|---|
| 54 | class TestFileContextManager(unittest.TestCase): |
|---|
| 55 | |
|---|
| 56 | def test_normal_operation(self): |
|---|
| 57 | with _test_base.ftp_host_factory(session_factory=ReadMockSession) \ |
|---|
| 58 | as host: |
|---|
| 59 | with host.file('dummy', 'r') as f: |
|---|
| 60 | self.assertEqual(f.closed, False) |
|---|
| 61 | data = f.readline() |
|---|
| 62 | self.assertEqual(data, 'line 1\n') |
|---|
| 63 | self.assertEqual(f.closed, False) |
|---|
| 64 | self.assertEqual(f.closed, True) |
|---|
| 65 | |
|---|
| 66 | def test_ftputil_exception(self): |
|---|
| 67 | with _test_base.ftp_host_factory( |
|---|
| 68 | session_factory=InaccessibleDirSession) as host: |
|---|
| 69 | try: |
|---|
| 70 | |
|---|
| 71 | |
|---|
| 72 | with host.file('/inaccessible/new_file', 'w') as f: |
|---|
| 73 | pass |
|---|
| 74 | except ftp_error.FTPIOError: |
|---|
| 75 | |
|---|
| 76 | |
|---|
| 77 | self.failIf('f' in locals()) |
|---|
| 78 | else: |
|---|
| 79 | raise self.failureException("ftp_error.FTPIOError not raised") |
|---|
| 80 | |
|---|
| 81 | def test_client_code_exception(self): |
|---|
| 82 | with _test_base.ftp_host_factory(session_factory=ReadMockSession) \ |
|---|
| 83 | as host: |
|---|
| 84 | try: |
|---|
| 85 | with host.file('dummy', 'r') as f: |
|---|
| 86 | self.assertEqual(f.closed, False) |
|---|
| 87 | raise ClientCodeException() |
|---|
| 88 | except ClientCodeException: |
|---|
| 89 | self.assertEqual(f.closed, True) |
|---|
| 90 | else: |
|---|
| 91 | raise self.failureException("ClientCodeException not raised") |
|---|
| 92 | |
|---|
| 93 | |
|---|
| 94 | if __name__ == '__main__': |
|---|
| 95 | unittest.main() |
|---|