File: test_input_validation.py

package info (click to toggle)
python-web-poet 0.23.2-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 908 kB
  • sloc: python: 6,112; makefile: 19
file content (312 lines) | stat: -rw-r--r-- 6,135 bytes parent folder | download | duplicates (3)
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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
"""Test page object input validation scenarios."""

from __future__ import annotations

import attrs
import pytest

from web_poet import ItemPage, Returns, field, validates_input
from web_poet.exceptions import Retry, UseFallback


@attrs.define
class Item:
    a: str
    is_valid: bool = True


EXPECTED_ITEM = Item(a="a", is_valid=True)


class BasePage(ItemPage[Item]):
    @field
    def a(self):
        return "a"


# Valid input


class BaseValidInputPage(BasePage):
    def validate_input(self):
        pass


def test_valid_input_sync_to_item():
    class Page(BaseValidInputPage):
        def to_item(self):
            return Item(a=self.a)

    assert Page().to_item() == EXPECTED_ITEM


@pytest.mark.asyncio
async def test_valid_input_async_to_item():
    assert await BaseValidInputPage().to_item() == EXPECTED_ITEM


def test_valid_input_sync_field():
    assert BaseValidInputPage().a == "a"


@pytest.mark.asyncio
async def test_valid_input_async_field():
    class Page(BaseValidInputPage):
        @field
        async def a(self):
            return "a"

    assert await Page().a == "a"


# Retry


class BaseRetryPage(BasePage):
    def validate_input(self):
        raise Retry


def test_retry_sync_to_item():
    class Page(BaseRetryPage):
        def to_item(self):
            return Item(a=self.a)

    page = Page()
    with pytest.raises(Retry):
        page.to_item()


@pytest.mark.asyncio
async def test_retry_async_to_item():
    page = BaseRetryPage()
    with pytest.raises(Retry):
        await page.to_item()


def test_retry_sync_field():
    page = BaseRetryPage()
    with pytest.raises(Retry):
        page.a


@pytest.mark.asyncio
async def test_retry_async_field():
    class Page(BaseRetryPage):
        @field
        async def a(self):
            return "a"

    page = Page()
    with pytest.raises(Retry):
        await page.a


# Use fallback


class BaseUseFallbackPage(BasePage):
    def validate_input(self):
        if self.a is None:
            raise UseFallback

    @field
    def a(self):
        return None


def test_use_fallback_sync_to_item():
    class Page(BaseUseFallbackPage):
        def to_item(self):
            return Item(a=self.a)

    page = Page()
    with pytest.raises(UseFallback):
        page.to_item()


@pytest.mark.asyncio
async def test_use_fallback_async_to_item():
    page = BaseUseFallbackPage()
    with pytest.raises(UseFallback):
        await page.to_item()


def test_use_fallback_sync_field():
    page = BaseUseFallbackPage()
    with pytest.raises(UseFallback):
        page.a


@pytest.mark.asyncio
async def test_use_fallback_async_field():
    class Page(BaseUseFallbackPage):
        def validate_input(self):
            # Cannot use async self.a
            raise UseFallback

        @field
        async def a(self):
            return "a"

    page = Page()
    with pytest.raises(UseFallback):
        await page.a


# Invalid input


INVALID_ITEM = Item(a="invalid", is_valid=False)


class BaseInvalidInputPage(ItemPage[Item]):
    def validate_input(self):
        return INVALID_ITEM

    @field
    def a(self):
        raise RuntimeError("This exception should never be raised")


def test_invalid_input_sync_to_item():
    class Page(BaseInvalidInputPage):
        @validates_input
        def to_item(self):
            return Item(a=self.a)

    assert Page().to_item() == INVALID_ITEM


@pytest.mark.asyncio
async def test_invalid_input_async_to_item():
    assert await BaseInvalidInputPage().to_item() == INVALID_ITEM


def test_invalid_input_sync_field():
    assert BaseInvalidInputPage().a == "invalid"


@pytest.mark.asyncio
async def test_invalid_input_async_field():
    class Page(BaseInvalidInputPage):
        @field
        async def a(self):
            raise RuntimeError("This exception should never be raised")

    assert await Page().a == "invalid"


# Unvalidated input


def test_unvalidated_input_sync_to_item():
    class Page(BasePage):
        def to_item(self):
            return Item(a=self.a)

    assert Page().to_item() == EXPECTED_ITEM


@pytest.mark.asyncio
async def test_unvalidated_input_async_to_item():
    assert await BasePage().to_item() == EXPECTED_ITEM


def test_unvalidated_input_sync_field():
    assert BasePage().a == "a"


@pytest.mark.asyncio
async def test_unvalidated_input_async_field():
    class Page(BasePage):
        @field
        async def a(self):
            return "a"

    assert await Page().a == "a"


# Caching


class BaseCachingPage(BasePage):
    _raise = False

    def validate_input(self):
        if self._raise:
            raise UseFallback
        self._raise = True


def test_invalid_input_sync_to_item_caching():
    class Page(BaseCachingPage):
        def to_item(self):
            return Item(a=self.a)

    page = Page()
    page.to_item()
    page.to_item()


@pytest.mark.asyncio
async def test_invalid_input_async_to_item_caching():
    page = BaseCachingPage()
    await page.to_item()
    await page.to_item()


def test_invalid_input_sync_field_caching():
    page = BaseCachingPage()
    page.a
    page.a


@pytest.mark.asyncio
async def test_invalid_input_async_field_caching():
    class Page(BaseCachingPage):
        @field
        async def a(self):
            return "a"

    page = Page()
    await page.a
    await page.a


@pytest.mark.asyncio
async def test_invalid_input_cross_api_caching():
    @attrs.define
    class _Item(Item):
        b: str | None = None

    class Page(BaseCachingPage, Returns[_Item]):
        @field
        async def b(self):
            return "b"

    page = Page()
    page.a
    await page.b
    await page.to_item()


# Recursion


@pytest.mark.asyncio
async def test_recursion():
    """Make sure that using fields within the validate_input method does not
    result in a recursive call to the validate_input method."""

    class Page(BasePage):
        _raise = False

        def validate_input(self):
            if self._raise:
                raise UseFallback
            self._raise = True
            assert self.a == "a"

    page = Page()
    assert page.a == "a"