File: test_with_statement.py

package info (click to toggle)
python-ftputil 3.4-3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, sid
  • size: 848 kB
  • sloc: python: 3,308; makefile: 3
file content (86 lines) | stat: -rw-r--r-- 3,006 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# Copyright (C) 2008-2016, Stefan Schwarzer <sschwarzer@sschwarzer.net>
# and ftputil contributors (see `doc/contributors.txt`)
# See the file LICENSE for licensing terms.

from __future__ import unicode_literals

import pytest

import ftputil.error

from test import test_base
from test.test_file import InaccessibleDirSession, ReadMockSession
from test.test_host import FailOnLoginSession


# Exception raised by client code, i. e. code using ftputil. Used to
# test the behavior in case of client exceptions.
class ClientCodeException(Exception):
    pass


#
# Test cases
#
class TestHostContextManager(object):

    def test_normal_operation(self):
        with test_base.ftp_host_factory() as host:
            assert host.closed is False
        assert host.closed is True

    def test_ftputil_exception(self):
        with pytest.raises(ftputil.error.FTPOSError):
            with test_base.ftp_host_factory(FailOnLoginSession) as host:
                pass
        # We arrived here, that's fine. Because the `FTPHost` object
        # wasn't successfully constructed, the assignment to `host`
        # shouldn't have happened.
        assert "host" not in locals()

    def test_client_code_exception(self):
        try:
            with test_base.ftp_host_factory() as host:
                assert host.closed is False
                raise ClientCodeException()
        except ClientCodeException:
            assert host.closed is True
        else:
            pytest.fail("`ClientCodeException` not raised")


class TestFileContextManager(object):

    def test_normal_operation(self):
        with test_base.ftp_host_factory(
               session_factory=ReadMockSession) as host:
            with host.open("dummy", "r") as fobj:
                assert fobj.closed is False
                data = fobj.readline()
                assert data == "line 1\n"
                assert fobj.closed is False
            assert fobj.closed is True

    def test_ftputil_exception(self):
        with test_base.ftp_host_factory(
               session_factory=InaccessibleDirSession) as host:
            with pytest.raises(ftputil.error.FTPIOError):
                # This should fail since the directory isn't
                # accessible by definition.
                with host.open("/inaccessible/new_file", "w") as fobj:
                    pass
            # The file construction shouldn't have succeeded, so `fobj`
            # should be absent from the local namespace.
            assert "fobj" not in locals()

    def test_client_code_exception(self):
        with test_base.ftp_host_factory(
               session_factory=ReadMockSession) as host:
            try:
                with host.open("dummy", "r") as fobj:
                    assert fobj.closed is False
                    raise ClientCodeException()
            except ClientCodeException:
                assert fobj.closed is True
            else:
                pytest.fail("`ClientCodeException` not raised")