root/_test_with_statement.py

Revision 875:30884edad2ee, 3.2 kB (checked in by Stefan Schwarzer <sschwarzer@…>, 6 weeks ago)
Add empty lines according to PEP 8.
Line 
1# Copyright (C) 2008, Roger Demetrescu <roger.demetrescu@gmail.com>
2# Copyright (C) 2008, Stefan Schwarzer <sschwarzer@sschwarzer.net>
3# See the file LICENSE for licensing terms.
4
5from __future__ import with_statement
6
7import unittest
8
9import _test_base
10import ftp_error
11
12from _test_ftputil import FailOnLoginSession
13from _test_ftp_file import InaccessibleDirSession, ReadMockSession
14
15
16# exception raised by client code, i. e. code using ftputil
17class ClientCodeException(Exception):
18    pass
19
20
21#
22# test cases
23#
24class 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            # we arrived here, that's fine
37            # because the `FTPHost` object wasn't successfully constructed
38            #  the assignment to `host` shouldn't have happened
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
54class 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                # this should fail since the directory isn't accessible
71                #  by definition
72                with host.file('/inaccessible/new_file', 'w') as f:
73                    pass
74            except ftp_error.FTPIOError:
75                # the file construction didn't succeed, so `f` should
76                #  be absent from the namespace
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
94if __name__ == '__main__':
95    unittest.main()
Note: See TracBrowser for help on using the browser.