source: test/test_with_statement.py @ 1713:f146a1ea66aa

Last change on this file since 1713:f146a1ea66aa was 1713:f146a1ea66aa, checked in by Stefan Schwarzer <sschwarzer@…>, 8 months ago
Remove `__future__` imports With the switch to Python 3.x-only, the `__future__` imports are no longer needed. Update copyright years along with the `__future__` import removal.
File size: 2.9 KB
Line 
1# Copyright (C) 2008-2018, Stefan Schwarzer <sschwarzer@sschwarzer.net>
2# and ftputil contributors (see `doc/contributors.txt`)
3# See the file LICENSE for licensing terms.
4
5import pytest
6
7import ftputil.error
8
9from test import test_base
10from test.test_file import InaccessibleDirSession, ReadMockSession
11from test.test_host import FailOnLoginSession
12
13
14# Exception raised by client code, i. e. code using ftputil. Used to
15# test the behavior in case of client exceptions.
16class ClientCodeException(Exception):
17    pass
18
19
20#
21# Test cases
22#
23class TestHostContextManager(object):
24
25    def test_normal_operation(self):
26        with test_base.ftp_host_factory() as host:
27            assert host.closed is False
28        assert host.closed is True
29
30    def test_ftputil_exception(self):
31        with pytest.raises(ftputil.error.FTPOSError):
32            with test_base.ftp_host_factory(FailOnLoginSession) as host:
33                pass
34        # We arrived here, that's fine. Because the `FTPHost` object
35        # wasn't successfully constructed, the assignment to `host`
36        # shouldn't have happened.
37        assert "host" not in locals()
38
39    def test_client_code_exception(self):
40        try:
41            with test_base.ftp_host_factory() as host:
42                assert host.closed is False
43                raise ClientCodeException()
44        except ClientCodeException:
45            assert host.closed is True
46        else:
47            pytest.fail("`ClientCodeException` not raised")
48
49
50class TestFileContextManager(object):
51
52    def test_normal_operation(self):
53        with test_base.ftp_host_factory(
54               session_factory=ReadMockSession) as host:
55            with host.open("dummy", "r") as fobj:
56                assert fobj.closed is False
57                data = fobj.readline()
58                assert data == "line 1\n"
59                assert fobj.closed is False
60            assert fobj.closed is True
61
62    def test_ftputil_exception(self):
63        with test_base.ftp_host_factory(
64               session_factory=InaccessibleDirSession) as host:
65            with pytest.raises(ftputil.error.FTPIOError):
66                # This should fail since the directory isn't
67                # accessible by definition.
68                with host.open("/inaccessible/new_file", "w") as fobj:
69                    pass
70            # The file construction shouldn't have succeeded, so `fobj`
71            # should be absent from the local namespace.
72            assert "fobj" not in locals()
73
74    def test_client_code_exception(self):
75        with test_base.ftp_host_factory(
76               session_factory=ReadMockSession) as host:
77            try:
78                with host.open("dummy", "r") as fobj:
79                    assert fobj.closed is False
80                    raise ClientCodeException()
81            except ClientCodeException:
82                assert fobj.closed is True
83            else:
84                pytest.fail("`ClientCodeException` not raised")
Note: See TracBrowser for help on using the repository browser.