source: test/test_with_statement.py @ 1663:40280d912474

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