File: test_exceptions.py

package info (click to toggle)
praw 7.7.1%2Bds-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 149,360 kB
  • sloc: python: 16,669; makefile: 131
file content (200 lines) | stat: -rw-r--r-- 6,272 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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
# coding: utf-8
import pytest

from praw.exceptions import (
    APIException,
    ClientException,
    DuplicateReplaceException,
    InvalidFlairTemplateID,
    InvalidImplicitAuth,
    InvalidURL,
    MediaPostFailed,
    MissingRequiredAttributeException,
    PRAWException,
    RedditAPIException,
    RedditErrorItem,
    WebSocketException,
)


class TestAPIException:
    def test_catch(self):
        exc = RedditAPIException([["test", "testing", "test"]])
        with pytest.raises(APIException):
            raise exc


class TestClientException:
    def test_inheritance(self):
        assert issubclass(ClientException, PRAWException)

    def test_str(self):
        assert str(ClientException()) == ""
        assert str(ClientException("error message")) == "error message"


class TestDuplicateReplaceException:
    def test_inheritance(self):
        assert issubclass(DuplicateReplaceException, ClientException)

    def test_message(self):
        assert (
            str(DuplicateReplaceException())
            == "A duplicate comment has been detected. Are you attempting to call 'replace_more_comments' more than once?"
        )


class TestInvalidFlairTemplateID:
    def test_inheritance(self):
        assert issubclass(InvalidFlairTemplateID, ClientException)

    def test_str(self):
        assert (
            str(InvalidFlairTemplateID("123"))
            == "The flair template ID '123' is invalid. If you are trying to create a flair, please use the 'add' method."
        )


class TestInvalidImplicitAuth:
    def test_inheritance(self):
        assert issubclass(InvalidImplicitAuth, ClientException)

    def test_message(self):
        assert (
            str(InvalidImplicitAuth())
            == "Implicit authorization can only be used with installed apps."
        )


class TestInvalidURL:
    def test_custom_message(self):
        assert (
            str(InvalidURL("https://www.google.com", message="Test custom {}"))
            == "Test custom https://www.google.com"
        )

    def test_inheritance(self):
        assert issubclass(InvalidURL, ClientException)

    def test_message(self):
        assert (
            str(InvalidURL("https://www.google.com"))
            == "Invalid URL: https://www.google.com"
        )


class TestMediaPostFailed:
    def test_inheritance(self):
        assert issubclass(MediaPostFailed, WebSocketException)

    def test_message(self):
        assert (
            str(MediaPostFailed())
            == "The attempted media upload action has failed. Possible causes include the corruption of media files. Check that the media file can be opened on your local machine."
        )


class TestMissingRequiredAttributeException:
    def test_inheritance(self):
        assert issubclass(MissingRequiredAttributeException, ClientException)

    def test_str(self):
        assert str(MissingRequiredAttributeException()) == ""
        assert (
            str(MissingRequiredAttributeException("error message")) == "error message"
        )


class TestPRAWException:
    def test_inheritance(self):
        assert issubclass(PRAWException, Exception)

    def test_str(self):
        assert str(PRAWException()) == ""
        assert str(PRAWException("foo")) == "foo"


class TestRedditAPIException:
    @pytest.mark.filterwarnings("ignore", category=DeprecationWarning)
    def test_apiexception_value(self):
        exc = RedditAPIException("test", "testing", "test")
        assert exc.error_type == "test"
        exc2 = RedditAPIException(["test", "testing", "test"])
        assert exc2.message == "testing"
        exc3 = RedditAPIException([["test", "testing", "test"]])
        assert exc3.field == "test"

    def test_inheritance(self):
        assert issubclass(RedditAPIException, PRAWException)

    def test_items(self):
        container = RedditAPIException(
            [
                ["BAD_SOMETHING", "invalid something", "some_field"],
                RedditErrorItem(
                    "BAD_SOMETHING", field="some_field", message="invalid something"
                ),
            ]
        )
        for exception in container.items:
            assert isinstance(exception, RedditErrorItem)


class TestRedditErrorItem:
    def test_equality(self):
        resp = {
            "error_type": "BAD_SOMETHING",
            "field": "some_field",
            "message": "invalid something",
        }
        error = RedditErrorItem(**resp)
        error2 = RedditErrorItem(**resp)
        assert error == error2
        assert error != 0

    def test_property(self):
        error = RedditErrorItem(
            "BAD_SOMETHING", field="some_field", message="invalid something"
        )
        assert (
            error.error_message
            == "BAD_SOMETHING: 'invalid something' on field 'some_field'"
        )

    def test_repr(self):
        error = RedditErrorItem(
            "BAD_SOMETHING", field="some_field", message="invalid something"
        )
        assert (
            repr(error)
            == "RedditErrorItem(error_type='BAD_SOMETHING', message='invalid something', field='some_field')"
        )

    def test_str(self):
        error = RedditErrorItem(
            "BAD_SOMETHING", field="some_field", message="invalid something"
        )
        assert str(error) == "BAD_SOMETHING: 'invalid something' on field 'some_field'"


class TestWebSocketException:
    @pytest.mark.filterwarnings("ignore", category=DeprecationWarning)
    def test_exception_attr(self):
        exc = WebSocketException(None, None)
        assert exc.original_exception is None
        assert isinstance(WebSocketException(None, Exception()), Exception)
        assert (
            str(WebSocketException(None, Exception("test")).original_exception)
            == "test"
        )
        exc.original_exception = Exception()
        assert isinstance(exc.original_exception, Exception)
        del exc.original_exception
        assert "_original_exception" not in vars(exc)

    def test_inheritance(self):
        assert issubclass(WebSocketException, ClientException)

    def test_str(self):
        assert str(WebSocketException("", None)) == ""
        assert str(WebSocketException("error message", None)) == "error message"