File: test_testing.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 (739 lines) | stat: -rw-r--r-- 25,330 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
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
from __future__ import annotations

import datetime
import json
from collections import deque
from typing import TYPE_CHECKING, Annotated, Any
from zoneinfo import ZoneInfo

import attrs
import dateutil.tz
import pytest
import time_machine
from itemadapter import ItemAdapter
from itemadapter.adapter import DictAdapter
from parsel import Selector
from zyte_common_items import Item, Metadata, Product

from web_poet import HttpClient, HttpRequest, HttpResponse, WebPage, field
from web_poet.annotated import AnnotatedInstance
from web_poet.exceptions import HttpRequestError, HttpResponseError, Retry, UseFallback
from web_poet.page_inputs.client import _SavedResponseData
from web_poet.page_inputs.url import RequestUrl
from web_poet.testing import Fixture
from web_poet.testing.__main__ import main as cli_main
from web_poet.testing.fixture import INPUT_DIR_NAME, META_FILE_NAME, OUTPUT_FILE_NAME
from web_poet.utils import get_fq_class_name

if TYPE_CHECKING:
    from pathlib import Path

N_TESTS = len(attrs.fields(Product)) + 2


def test_save_fixture(book_list_html_response, tmp_path) -> None:
    base_dir = tmp_path / "fixtures" / "some.po"
    item = {"foo": "bar"}
    meta = {"foo": "bar", "frozen_time": "2022-01-01"}

    def _assert_fixture_files(
        directory: Path, expected_meta: dict | None = None
    ) -> None:
        input_dir = directory / INPUT_DIR_NAME
        assert (input_dir / "HttpResponse-body.html").exists()
        assert (input_dir / "HttpResponse-body.html").read_bytes() == bytes(
            book_list_html_response.body
        )
        assert (input_dir / "HttpResponse-info.json").exists()
        assert (directory / OUTPUT_FILE_NAME).exists()
        assert json.loads((directory / OUTPUT_FILE_NAME).read_bytes()) == item
        if expected_meta:
            assert (
                json.loads((directory / META_FILE_NAME).read_bytes()) == expected_meta
            )
        else:
            assert not (directory / META_FILE_NAME).exists()

    Fixture.save(base_dir, inputs=[book_list_html_response], item=item)
    _assert_fixture_files(base_dir / "test-1")
    Fixture.save(
        base_dir, inputs=[book_list_html_response], item=item, fixture_name="custom"
    )
    _assert_fixture_files(base_dir / "custom")
    Fixture.save(base_dir, inputs=[book_list_html_response], item=item, meta=meta)
    _assert_fixture_files(base_dir / "test-2", expected_meta=meta)


def test_save_fixture_unicode_item(book_list_html_response, tmp_path) -> None:
    base_dir = tmp_path / "fixtures" / "some.po"
    item = {"foo": "✓bar£"}

    Fixture.save(base_dir, inputs=[book_list_html_response], item=item)
    fixture = Fixture(base_dir / "test-1")
    assert json.loads(fixture.output_path.read_bytes()) == item
    assert fixture.get_expected_output() == item


def test_save_fixture_unicode_exception(book_list_html_response, tmp_path) -> None:
    base_dir = tmp_path / "fixtures" / "some.po"
    exc = ValueError("✓bar£")

    Fixture.save(base_dir, inputs=[book_list_html_response], exception=exc)
    fixture = Fixture(base_dir / "test-1")
    exc_data = json.loads(fixture.exception_path.read_bytes())
    assert exc_data == {"import_path": "builtins.ValueError", "msg": "✓bar£"}
    expected_exc = fixture.get_expected_exception()
    assert type(expected_exc) is ValueError
    assert expected_exc.args == ("✓bar£",)


def test_save_fixture_unicode_meta(book_list_html_response, tmp_path) -> None:
    base_dir = tmp_path / "fixtures" / "some.po"
    item = {"foo": "bar"}
    meta = {"foo": "✓bar£", "frozen_time": "2022-01-01"}

    Fixture.save(base_dir, inputs=[book_list_html_response], item=item, meta=meta)
    fixture = Fixture(base_dir / "test-1")
    meta_data = json.loads(fixture.meta_path.read_bytes())
    assert meta_data == meta
    assert fixture.get_meta() == meta


class MyItemPage(WebPage):
    async def to_item(self) -> dict:
        return {"foo": "bar"}


class MyItemPage2(WebPage):
    async def to_item(self) -> dict:
        return {"foo": None}


class CapitalizingDictAdapter(DictAdapter):
    def __getitem__(self, field_name: str) -> Any:
        item = super().__getitem__(field_name)
        if isinstance(item, str):
            return item.capitalize()
        return item


class CustomItemAdapter(ItemAdapter):
    ADAPTER_CLASSES = deque([CapitalizingDictAdapter])


def test_fixture_adapter(book_list_html_response, tmp_path) -> None:
    item = {"foo": "bar"}
    meta = {"adapter": CustomItemAdapter}
    base_dir = tmp_path / "fixtures" / get_fq_class_name(MyItemPage)

    fixture = Fixture.save(
        base_dir, inputs=[book_list_html_response], item=item, meta=meta
    )
    saved_output = json.loads(fixture.output_path.read_bytes())
    assert saved_output["foo"] == "Bar"

    loaded_fixture = Fixture(base_dir / "test-1")
    loaded_output = loaded_fixture.get_output()
    assert loaded_output["foo"] == "Bar"
    actual_output = loaded_fixture.get_expected_output()
    assert actual_output["foo"] == "Bar"


def _save_fixture(
    pytester, page_cls, page_inputs, *, expected_output=None, expected_exception=None
):
    base_dir = pytester.path / "fixtures" / get_fq_class_name(page_cls)
    return Fixture.save(
        base_dir, inputs=page_inputs, item=expected_output, exception=expected_exception
    )


def test_pytest_plugin_pass(pytester, book_list_html_response) -> None:
    _save_fixture(
        pytester,
        page_cls=MyItemPage,
        page_inputs=[book_list_html_response],
        expected_output={"foo": "bar"},
    )
    result = pytester.runpytest()
    result.assert_outcomes(passed=3)


def test_pytest_plugin_bad_field_value(pytester, book_list_html_response) -> None:
    _save_fixture(
        pytester,
        page_cls=MyItemPage,
        page_inputs=[book_list_html_response],
        expected_output={"foo": "not bar"},
    )
    result = pytester.runpytest()
    result.assert_outcomes(failed=1, passed=2)
    result.stdout.fnmatch_lines("item.foo is not correct*")


def test_pytest_plugin_bad_field_value_None(pytester, book_list_html_response) -> None:
    _save_fixture(
        pytester,
        page_cls=MyItemPage2,
        page_inputs=[book_list_html_response],
        expected_output={"foo": "bar"},
    )
    result = pytester.runpytest()
    result.assert_outcomes(failed=1, passed=2)
    result.stdout.fnmatch_lines("item.foo is not correct*")
    result.stdout.fnmatch_lines("Expected: 'bar', got: None*")


def test_pytest_plugin_missing_field(pytester, book_list_html_response) -> None:
    _save_fixture(
        pytester,
        page_cls=MyItemPage,
        page_inputs=[book_list_html_response],
        expected_output={"foo": "bar", "foo2": "bar2"},
    )
    result = pytester.runpytest()
    result.assert_outcomes(failed=1, passed=3)
    result.stdout.fnmatch_lines("item.foo2 is missing*")


def test_pytest_plugin_extra_field(pytester, book_list_html_response) -> None:
    _save_fixture(
        pytester,
        page_cls=MyItemPage,
        page_inputs=[book_list_html_response],
        expected_output={"foo2": "bar2"},
    )
    result = pytester.runpytest()
    result.assert_outcomes(failed=2, passed=1)
    result.stdout.fnmatch_lines("item.foo2 is missing*")
    result.stdout.fnmatch_lines("*unexpected fields*")
    result.stdout.fnmatch_lines("*foo = 'bar'*")


class FieldExceptionPage(WebPage):
    @field
    def foo(self):
        return "foo"

    @field
    def bar(self):
        raise Exception


def test_pytest_plugin_field_exception(pytester, book_list_html_response) -> None:
    _save_fixture(
        pytester,
        page_cls=FieldExceptionPage,
        page_inputs=[book_list_html_response],
        expected_output={"foo": "foo", "bar": "bar"},
    )
    result = pytester.runpytest()
    result.assert_outcomes(failed=1, skipped=3)
    result.stdout.fnmatch_lines("*FAILED*TO_ITEM_DOESNT_RAISE*")


def test_pytest_plugin_compare_item(pytester, book_list_html_response) -> None:
    _save_fixture(
        pytester,
        page_cls=MyItemPage,
        page_inputs=[book_list_html_response],
        expected_output={"foo": "bar"},
    )
    result = pytester.runpytest("--web-poet-test-per-item")
    result.assert_outcomes(passed=1)


def test_pytest_plugin_compare_item_unformatted_output(
    pytester, book_list_html_response
) -> None:
    _save_fixture(
        pytester,
        page_cls=MyItemPage,
        page_inputs=[book_list_html_response],
        expected_output={"foo": "bar"},
    )
    base_dir = pytester.path / "fixtures" / get_fq_class_name(MyItemPage)
    fixture = Fixture(base_dir / "test-1")
    fixture.output_path.write_text('{"foo":"bar"}')
    result = pytester.runpytest("--web-poet-test-per-item")
    result.assert_outcomes(passed=1)


def test_pytest_plugin_compare_item_fail(pytester, book_list_html_response) -> None:
    _save_fixture(
        pytester,
        page_cls=MyItemPage,
        page_inputs=[book_list_html_response],
        expected_output={"foo": "not bar"},
    )
    result = pytester.runpytest("--web-poet-test-per-item", "-vv")
    result.assert_outcomes(passed=0, failed=1)

    result.stdout.fnmatch_lines(
        "*The output doesn't match.\n"
        '\'{\\n  "foo": "bar"\\n}\' == \'{\\n  "foo": "not bar"\\n}\'\n'
        "*"
        "  {\n"
        '-   "foo": "not bar"\n'
        "?           ----\n"
        '+   "foo": "bar"\n'
        "  }*"
    )


@attrs.define(kw_only=True)
class MetadataLocalTime(Metadata):
    dateDownloadedLocal: str | None = None


@attrs.define(kw_only=True)
class ProductLocalTime(Product):
    # in newer zyte-common-items this should inherit from ProductMetadata
    metadata: MetadataLocalTime | None  # type: ignore[assignment]


def _get_product_item(date: datetime.datetime) -> ProductLocalTime:
    if date.tzinfo is None:
        # convert to the aware object so that date_local_str always includes the offset
        date = date.astimezone()
    date_str = date.astimezone(dateutil.tz.UTC).strftime("%Y-%M-%dT%H:%M:%SZ")
    date_local_str = date.strftime("%Y-%M-%dT%H:%M:%S%z")
    return ProductLocalTime(
        url="http://example.com",
        name="foo",
        metadata=MetadataLocalTime(
            dateDownloaded=date_str,
            dateDownloadedLocal=date_local_str,  # type: ignore[call-arg]
        ),
    )


class DateItemPage(WebPage):
    async def to_item(self) -> Item:
        date = datetime.datetime.now().astimezone()
        return _get_product_item(date)


def _assert_frozen_item(
    frozen_time: datetime.datetime,
    pytester: pytest.Pytester,
    response: HttpResponse,
    *,
    outcomes: dict[str, int] | None = None,
) -> None:
    # this makes an item with datetime fields corresponding to frozen_time
    item = ItemAdapter(_get_product_item(frozen_time)).asdict()
    # this marks the fixture to be run under frozen_time
    meta = {"frozen_time": frozen_time.strftime("%Y-%m-%d %H:%M:%S %z")}
    base_dir = pytester.path / "fixtures" / get_fq_class_name(DateItemPage)
    Fixture.save(base_dir, inputs=[response], item=item, meta=meta)
    # this runs the test, faking the time and the timezone from frozen_time,
    # the result should contain frozen_time in the datetime fields
    result = pytester.runpytest()
    if outcomes is None:
        outcomes = {"passed": N_TESTS}
    result.assert_outcomes(**outcomes)


@pytest.mark.xfail(not time_machine.HAVE_TZSET, reason="Works on Windows only in UTC")
def test_pytest_frozen_time_utc(pytester, book_list_html_response) -> None:
    frozen_time = datetime.datetime(2022, 3, 4, 20, 21, 22, tzinfo=dateutil.tz.UTC)
    _assert_frozen_item(frozen_time, pytester, book_list_html_response)


def test_pytest_frozen_time_naive(pytester, book_list_html_response) -> None:
    frozen_time = datetime.datetime(2022, 3, 4, 20, 21, 22)
    _assert_frozen_item(frozen_time, pytester, book_list_html_response)


@pytest.mark.skipif(not time_machine.HAVE_TZSET, reason="Not supported on Windows")
@pytest.mark.parametrize("offset", [-5, 0, 8])
def test_pytest_frozen_time_tz(pytester, book_list_html_response, offset) -> None:
    tzinfo = ZoneInfo(f"Etc/GMT{-offset:+d}")
    frozen_time = datetime.datetime(2022, 3, 4, 20, 21, 22, tzinfo=tzinfo)
    _assert_frozen_item(frozen_time, pytester, book_list_html_response)


@pytest.mark.skipif(time_machine.HAVE_TZSET, reason="Tests Windows-specific code")
def test_pytest_frozen_time_tz_windows_fail(pytester, book_list_html_response) -> None:
    frozen_time = datetime.datetime(
        2022, 3, 4, 20, 21, 22, tzinfo=dateutil.tz.tzoffset(None, -7.5 * 3600)
    )
    _assert_frozen_item(
        frozen_time,
        pytester,
        book_list_html_response,
        outcomes={"failed": 1, "passed": N_TESTS - 1},
    )


@pytest.mark.skipif(time_machine.HAVE_TZSET, reason="Tests Windows-specific code")
def test_pytest_frozen_time_tz_windows_pass(pytester, book_list_html_response) -> None:
    frozen_time = datetime.datetime(
        2022, 3, 4, 20, 21, 22, tzinfo=dateutil.tz.tzlocal()
    )
    _assert_frozen_item(frozen_time, pytester, book_list_html_response)


@attrs.define
class ClientPage(WebPage):
    client: HttpClient

    async def to_item(self) -> dict:
        resp1 = await self.client.get("http://books.toscrape.com/1.html")
        resp2 = await self.client.post("http://books.toscrape.com/2.html", body=b"post")
        return {"foo": "bar", "additional": [resp1.body.decode(), resp2.body.decode()]}


def test_httpclient(pytester, book_list_html_response) -> None:
    url1 = "http://books.toscrape.com/1.html"
    request1 = HttpRequest(url1)
    response1 = HttpResponse(url=url1, body=b"body1", encoding="utf-8")
    url2 = "http://books.toscrape.com/2.html"
    request2 = HttpRequest(url2, method="POST", body=b"post")
    response2 = HttpResponse(url=url2, body=b"body2", encoding="utf-8")
    responses = [
        _SavedResponseData(request1, response1),
        _SavedResponseData(request2, response2),
    ]
    client = HttpClient(responses=responses)

    base_dir = pytester.path / "fixtures" / get_fq_class_name(ClientPage)
    item = {
        "foo": "bar",
        "additional": ["body1", "body2"],
    }
    Fixture.save(base_dir, inputs=[book_list_html_response, client], item=item)
    input_dir = base_dir / "test-1" / INPUT_DIR_NAME
    assert (input_dir / "HttpResponse-body.html").read_bytes() == bytes(
        book_list_html_response.body
    )
    assert (input_dir / "HttpClient-0-HttpRequest.info.json").exists()
    assert (input_dir / "HttpClient-0-HttpResponse.info.json").exists()
    assert (input_dir / "HttpClient-0-HttpResponse.body.html").read_bytes() == b"body1"
    assert (input_dir / "HttpClient-1-HttpResponse.body.html").read_bytes() == b"body2"
    result = pytester.runpytest()
    result.assert_outcomes(passed=4)


def test_httpclient_no_response(pytester, book_list_html_response) -> None:
    url = "http://books.toscrape.com/1.html"
    request = HttpRequest(url)
    response = HttpResponse(url=url, body=b"body1", encoding="utf-8")
    responses = [
        _SavedResponseData(request, response),
    ]
    client = HttpClient(responses=responses)

    item = {
        "foo": "bar",
        "additional": ["body1", "body2"],
    }
    _save_fixture(
        pytester,
        page_cls=ClientPage,
        page_inputs=[book_list_html_response, client],
        expected_output=item,
    )
    result = pytester.runpytest()
    result.assert_outcomes(failed=1, skipped=3)


@attrs.define
class ClientResponseErrorPage(WebPage):
    client: HttpClient

    async def to_item(self) -> dict:
        msg = ""
        try:
            await self.client.get("http://books.toscrape.com/1.html")
        except HttpResponseError as ex:
            msg = ex.args[0]
        return {"foo": "bar", "exception": msg}


def test_httpclient_response_exception(pytester, book_list_html_response) -> None:
    url = "http://books.toscrape.com/1.html"
    request = HttpRequest(url)
    response = HttpResponse(url=url, body=b"body1", status=404, encoding="utf-8")
    responses = [
        _SavedResponseData(request, response),
    ]
    client = HttpClient(responses=responses)

    item = {
        "foo": "bar",
        "exception": "404 NOT_FOUND response for http://books.toscrape.com/1.html",
    }
    _save_fixture(
        pytester,
        page_cls=ClientResponseErrorPage,
        page_inputs=[book_list_html_response, client],
        expected_output=item,
    )
    result = pytester.runpytest()
    result.assert_outcomes(passed=4)


@attrs.define
class ClientRequestErrorPage(WebPage):
    client: HttpClient

    async def to_item(self) -> dict:
        msg = ""
        try:
            await self.client.get("http://books.toscrape.com/1.html")
        except HttpRequestError as ex:
            msg = ex.args[0]
        return {"foo": "bar", "exception": msg}


def test_httpclient_request_exception(pytester, book_list_html_response) -> None:
    url = "http://books.toscrape.com/1.html"
    request = HttpRequest(url)
    exception = HttpRequestError("Bad Request", request)
    responses = [
        _SavedResponseData(request, None, exception),
    ]
    client = HttpClient(responses=responses)

    item = {
        "foo": "bar",
        "exception": "Bad Request",
    }
    fixture = _save_fixture(
        pytester,
        page_cls=ClientRequestErrorPage,
        page_inputs=[book_list_html_response, client],
        expected_output=item,
    )
    assert (fixture.input_path / "HttpClient-0-exception.json").exists()
    result = pytester.runpytest()
    result.assert_outcomes(passed=4)


class MyItemPage3(WebPage):
    async def to_item(self) -> dict:
        return {"foo": "bar", "egg": "spam", "hello": "world"}


def test_cli_rerun(pytester, book_list_html_response, capsys) -> None:
    fixture = _save_fixture(
        pytester,
        page_cls=MyItemPage3,
        page_inputs=[book_list_html_response],
        expected_output={"foo": "bar2", "egg": "spam", "hello": "world"},
    )
    cli_main(["rerun", str(fixture.path)])
    captured = capsys.readouterr()
    assert not captured.err
    assert json.loads(captured.out) == {"foo": "bar", "egg": "spam", "hello": "world"}


def test_cli_rerun_fields(pytester, book_list_html_response, capsys) -> None:
    fixture = _save_fixture(
        pytester,
        page_cls=MyItemPage3,
        page_inputs=[book_list_html_response],
        expected_output={"foo": "bar2", "egg": "spam", "hello": "world"},
    )
    cli_main(["rerun", str(fixture.path), "--fields=foo,egg"])
    captured = capsys.readouterr()
    assert not captured.err
    assert json.loads(captured.out) == {"foo": "bar", "egg": "spam"}


def test_cli_rerun_fields_unknown_names(
    pytester, book_list_html_response, capsys
) -> None:
    fixture = _save_fixture(
        pytester,
        page_cls=MyItemPage3,
        page_inputs=[book_list_html_response],
        expected_output={"foo": "bar2", "egg": "spam", "hello": "world"},
    )
    cli_main(["rerun", str(fixture.path), "--fields=foo,egg2"])
    captured = capsys.readouterr()
    assert (
        "Unknown field names: ['egg2']. Allowed names are: ['egg', 'foo', 'hello']"
        in captured.err
    )
    assert json.loads(captured.out) == {"foo": "bar"}


class RetryItemPage(WebPage):
    async def to_item(self):
        raise Retry


def test_page_object_exception_pass(pytester, book_list_html_response) -> None:
    fixture = _save_fixture(
        pytester,
        page_cls=RetryItemPage,
        page_inputs=[book_list_html_response],
        expected_exception=Retry(),
    )
    assert fixture.exception_path.exists()
    result = pytester.runpytest()
    result.assert_outcomes(passed=1)


def test_page_object_exception_wrong(pytester, book_list_html_response) -> None:
    fixture = _save_fixture(
        pytester,
        page_cls=RetryItemPage,
        page_inputs=[book_list_html_response],
        expected_exception=UseFallback(),
    )
    assert fixture.exception_path.exists()
    result = pytester.runpytest()
    result.assert_outcomes(failed=1)


def test_page_object_exception_none(pytester, book_list_html_response) -> None:
    fixture = _save_fixture(
        pytester,
        page_cls=WebPage,
        page_inputs=[book_list_html_response],
        expected_exception=Retry(),
    )
    assert fixture.exception_path.exists()
    result = pytester.runpytest()
    result.assert_outcomes(failed=1)


@attrs.define(kw_only=True)
class MyAnnotatedItemPage(MyItemPage):
    response: Annotated[HttpResponse, "foo", 42]

    async def to_item(self) -> dict:
        return {"foo": "bar"}


def test_annotated(pytester, book_list_html_response) -> None:
    _save_fixture(
        pytester,
        page_cls=MyAnnotatedItemPage,
        page_inputs=[AnnotatedInstance(book_list_html_response, ("foo", 42))],
        expected_output={"foo": "bar"},
    )
    result = pytester.runpytest()
    result.assert_outcomes(passed=3)


def test_request_url_output_serialization(book_list_html_response, tmp_path) -> None:
    base_dir = tmp_path / "fixtures" / "some.po"
    item = {"foo": RequestUrl("https://books.toscrape.com/")}
    item_json = {"foo": "https://books.toscrape.com/"}

    def _assert_fixture_files(
        directory: Path, expected_meta: dict | None = None
    ) -> None:
        input_dir = directory / INPUT_DIR_NAME
        assert (input_dir / "HttpResponse-body.html").exists()
        assert (input_dir / "HttpResponse-body.html").read_bytes() == bytes(
            book_list_html_response.body
        )
        assert (input_dir / "HttpResponse-info.json").exists()
        assert (directory / OUTPUT_FILE_NAME).exists()
        assert json.loads((directory / OUTPUT_FILE_NAME).read_bytes()) == item_json
        if expected_meta:
            assert (
                json.loads((directory / META_FILE_NAME).read_bytes()) == expected_meta
            )
        else:
            assert not (directory / META_FILE_NAME).exists()

    Fixture.save(base_dir, inputs=[book_list_html_response], item=item)
    _assert_fixture_files(base_dir / "test-1")


def test_unserializable(book_list_html_response, tmp_path) -> None:
    class Foo:
        pass

    base_dir = tmp_path / "fixtures" / "some.po"
    item = {"foo": Foo()}

    with pytest.raises(TypeError):
        Fixture.save(base_dir, inputs=[book_list_html_response], item=item)


@pytest.mark.parametrize("expected", ["bar", "not bar"])
def test_junitxml(
    pytester: pytest.Pytester, book_list_html_response: HttpResponse, expected: str
) -> None:
    _save_fixture(
        pytester,
        page_cls=MyItemPage,
        page_inputs=[book_list_html_response],
        expected_output={"foo": expected},
    )
    report_name = "junit.xml"
    result = pytester.runpytest(
        f"--junitxml={report_name}", "-o", "junit_family=legacy"
    )
    if expected == "bar":
        result.assert_outcomes(passed=3)
    else:
        result.assert_outcomes(failed=1, passed=2)
    report_path = pytester.path / report_name
    assert report_path.exists()
    sel = Selector(report_path.read_text(encoding="utf-8"))
    testcases = sel.xpath(
        "//testcase[@classname='fixtures.tests.test_testing.MyItemPage.test-1.output.json' and @name='foo']"
    )
    assert len(testcases) == 1
    testcase = testcases[0]
    expected_node = testcase.xpath(
        "properties/property[@name='web_poet_expected_value']"
    )[0]
    assert expected_node.xpath("@value").get() == f'"{expected}"'
    actual_node = testcase.xpath("properties/property[@name='web_poet_actual_value']")[
        0
    ]
    assert actual_node.xpath("@value").get() == '"bar"'


@pytest.mark.parametrize("expected", [Retry, UseFallback])
def test_junitxml_expected_exception(
    pytester: pytest.Pytester,
    book_list_html_response: HttpResponse,
    expected: type[Exception],
) -> None:
    _save_fixture(
        pytester,
        page_cls=RetryItemPage,
        page_inputs=[book_list_html_response],
        expected_exception=expected(),
    )
    report_name = "junit.xml"
    result = pytester.runpytest(
        f"--junitxml={report_name}", "-o", "junit_family=legacy"
    )
    if expected is Retry:
        result.assert_outcomes(passed=1)
    else:
        result.assert_outcomes(failed=1)
    report_path = pytester.path / report_name
    assert report_path.exists()
    sel = Selector(report_path.read_text(encoding="utf-8"))
    testcases = sel.xpath(
        "//testcase[@classname='fixtures.tests.test_testing.RetryItemPage.test-1.exception.json' and @name='TO_ITEM_RAISES']"
    )
    assert len(testcases) == 1
    testcase = testcases[0]
    expected_node = testcase.xpath(
        "properties/property[@name='web_poet_expected_exception']"
    )[0]
    expected_json = expected_node.xpath("@value").get()
    assert expected_json
    expected_data = json.loads(expected_json)
    assert expected_data == {"import_path": get_fq_class_name(expected), "msg": None}
    actual_node = testcase.xpath(
        "properties/property[@name='web_poet_actual_exception']"
    )[0]
    actual_json = actual_node.xpath("@value").get()
    assert actual_json
    actual_data = json.loads(actual_json)
    assert actual_data == {"import_path": "web_poet.exceptions.core.Retry", "msg": None}