source: test/test_error.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# encoding: utf-8
2# Copyright (C) 2002-2016, Stefan Schwarzer <sschwarzer@sschwarzer.net>
3# and ftputil contributors (see `doc/contributors.txt`)
4# See the file LICENSE for licensing terms.
5
6from __future__ import unicode_literals
7
8import ftplib
9
10import pytest
11
12import ftputil.error
13
14
15class TestFTPErrorArguments(object):
16    """
17    The `*Error` constructors should accept either a byte string or a
18    unicode string.
19    """
20
21    def test_bytestring_argument(self):
22        # An umlaut as latin-1 character
23        io_error = ftputil.error.FTPIOError(b"\xe4")
24        os_error = ftputil.error.FTPOSError(b"\xe4")
25
26    def test_unicode_argument(self):
27        # An umlaut as unicode character
28        io_error = ftputil.error.FTPIOError("\xe4")
29        os_error = ftputil.error.FTPOSError("\xe4")
30
31
32class TestErrorConversion(object):
33
34    def callee(self):
35        raise ftplib.error_perm()
36
37    def test_ftplib_error_to_ftp_os_error(self):
38        """
39        Ensure the `ftplib` exception isn't used as `FTPOSError`
40        argument.
41        """
42        with pytest.raises(ftputil.error.FTPOSError) as exc_info:
43            with ftputil.error.ftplib_error_to_ftp_os_error:
44                self.callee()
45        exc = exc_info.value
46        assert not (exc.args and 
47                    isinstance(exc.args[0], ftplib.error_perm))
48        del exc_info
49
50    def test_ftplib_error_to_ftp_os_error_non_ascii_server_message(self):
51        """
52        Test that we don't get a `UnicodeDecodeError` if the server
53        sends a message containing non-ASCII characters.
54        """
55        # See ticket #77.
56        message = \
57          ftputil.tool.as_bytes(
58            "Não é possível criar um arquivo já existente.")
59        with pytest.raises(ftputil.error.PermanentError):
60            with ftputil.error.ftplib_error_to_ftp_os_error:
61                raise ftplib.error_perm(message)
62
63    def test_ftplib_error_to_ftp_io_error(self):
64        """
65        Ensure the `ftplib` exception isn't used as `FTPIOError`
66        argument.
67        """
68        with pytest.raises(ftputil.error.FTPIOError) as exc_info:
69            with ftputil.error.ftplib_error_to_ftp_io_error:
70                self.callee()
71        exc = exc_info.value
72        assert not (exc.args and
73                    isinstance(exc.args[0], ftplib.error_perm))
74        del exc_info
75
76    def test_error_message_reuse(self):
77        """
78        Test if the error message string is retained if the caught
79        exception has more than one element in `args`.
80        """
81        # See ticket #76.
82        with pytest.raises(ftputil.error.FTPOSError) as exc_info:
83            # Format "host:port" doesn't work.
84            host = ftputil.FTPHost("localhost:21", "", "")
85        exc = exc_info.value
86        # The error message may be different for different Python
87        # versions.
88        assert (
89          "No address associated with hostname" in str(exc) or
90          "Name or service not known" in str(exc))
91        del exc_info
Note: See TracBrowser for help on using the repository browser.