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
|
from __future__ import annotations
from typing import TYPE_CHECKING
import pytest
from web_poet.testing.exceptions import (
ExceptionNotRaised,
FieldMissing,
FieldsUnexpected,
FieldValueIncorrect,
ItemValueIncorrect,
WrongExceptionRaised,
)
from web_poet.testing.fixture import EXCEPTION_FILE_NAME, OUTPUT_FILE_NAME, Fixture
from web_poet.testing.utils import comparison_error_message
from web_poet.utils import get_fq_class_name
if TYPE_CHECKING:
from collections.abc import Iterable
from pathlib import Path
class TestCase(pytest.File):
"""Represents the ``output.json`` or ``exception.json`` file in a testcase
directory."""
def __init__(self, *args, **kwargs) -> None:
super().__init__(*args, **kwargs)
self.fixture = Fixture(self.path.parent)
def collect(self) -> Iterable[pytest.Item | pytest.Collector]:
if self.fixture.exception_path.exists():
return [
WebPoetExpectedException.from_parent(
parent=self, name="TO_ITEM_RAISES", fixture=self.fixture
)
]
if self.config.getoption("WEB_POET_TEST_PER_ITEM", default=False):
return [
WebPoetItem.from_parent(parent=self, name="item", fixture=self.fixture)
]
overall_tests: list[pytest.Item] = [
WebPoetNoToItemException.from_parent(
parent=self, name="TO_ITEM_DOESNT_RAISE", fixture=self.fixture
),
WebPoetNoExtraFieldsItem.from_parent(
parent=self, name="NO_EXTRA_FIELDS", fixture=self.fixture
),
]
field_tests: list[pytest.Item] = [
WebPoetFieldItem.from_parent(
parent=self, name=field, fixture=self.fixture, field_name=field
)
for field in self.fixture.get_expected_output_fields()
]
return overall_tests + field_tests
class _WebPoetItem(pytest.Item):
def __init__(self, *, fixture: Fixture, **kwargs) -> None:
super().__init__(**kwargs)
self.fixture = fixture
class WebPoetItem(_WebPoetItem):
def runtest(self) -> None:
self.fixture.assert_full_item_correct()
def reportinfo(self):
return self.path, 0, f"{self.fixture.short_name}"
def repr_failure(self, excinfo, style=None):
if isinstance(excinfo.value, ItemValueIncorrect):
got, expected = excinfo.value.args
return comparison_error_message(
config=self.config,
op="==",
expected=expected,
got=got,
prefix="The output doesn't match.",
)
return super().repr_failure(excinfo, style)
class WebPoetNoExtraFieldsItem(_WebPoetItem):
def runtest(self) -> None:
if self.fixture.to_item_raised():
raise pytest.skip(
"Skipping a test for unexpected item fields "
"because to_item raised an exception."
)
self.fixture.assert_no_extra_fields()
def reportinfo(self):
return self.path, 0, f"{self.fixture.short_name}: extra fields"
def repr_failure(self, excinfo, style=None):
if isinstance(excinfo.value, FieldsUnexpected):
fields = excinfo.value.args[0]
return f"The item contains unexpected fields: \n{self._format_extra_fields(fields)}"
return super().repr_failure(excinfo, style)
def _format_extra_fields(self, extra_fields):
lines = []
for field, value in extra_fields.items():
lines.append(f" * {field} = {value!r}")
return "\n".join(lines)
class WebPoetNoToItemException(_WebPoetItem):
def runtest(self) -> None:
self.fixture.assert_no_toitem_exceptions()
def reportinfo(self):
return (
self.path,
0,
f"{self.fixture.short_name}: to_item doesn't raise an error",
)
class WebPoetExpectedException(_WebPoetItem):
def runtest(self) -> None:
self.fixture.assert_toitem_exception(self.user_properties)
def reportinfo(self):
return (
self.path,
0,
f"{self.fixture.short_name}: to_item raises {self.fixture.get_expected_exception().__class__.__name__}",
)
def repr_failure(self, excinfo, style=None):
expected = self.fixture.get_expected_exception()
if isinstance(excinfo.value, ExceptionNotRaised):
return (
f"to_item() didn't raise an exception."
f" {get_fq_class_name(type(expected))} was expected."
)
if isinstance(excinfo.value, WrongExceptionRaised):
got = excinfo.value.__cause__
inner_excinfo = pytest.ExceptionInfo.from_exc_info(
(type(got), got, got.__traceback__)
)
return (
f"to_item() raised a wrong exception. Expected"
f" {get_fq_class_name(type(expected))}, got"
f" {get_fq_class_name(type(got))}.\n\n"
+ str(super().repr_failure(inner_excinfo, style))
)
return super().repr_failure(excinfo, style)
class WebPoetFieldItem(_WebPoetItem):
def __init__(self, *, field_name: str, **kwargs) -> None:
super().__init__(**kwargs)
self.field_name = field_name
def runtest(self) -> None:
if self.fixture.to_item_raised():
raise pytest.skip(
f"Skipping a test for item.{self.field_name} "
f"because to_item raised an exception"
)
self.fixture.assert_field_correct(self.field_name, self.user_properties)
def reportinfo(self):
return self.path, 0, f"{self.fixture.short_name} @ {self.field_name}"
def repr_failure(self, excinfo, style=None):
if isinstance(excinfo.value, FieldValueIncorrect):
got, expected = excinfo.value.args
return comparison_error_message(
config=self.config,
op="==",
expected=expected,
got=got,
prefix=f"item.{self.field_name} is not correct.",
)
if isinstance(excinfo.value, FieldMissing):
field_name = excinfo.value.args[0]
return f"item.{field_name} is missing."
return super().repr_failure(excinfo, style)
def pytest_addoption(parser: pytest.Parser, pluginmanager: pytest.PytestPluginManager):
parser.addoption(
"--web-poet-test-per-item",
dest="WEB_POET_TEST_PER_ITEM",
action="store_true",
help="web-poet: use a single test per item, not a test per field",
)
def pytest_collect_file(
file_path: Path, parent: pytest.Collector
) -> pytest.Collector | None:
if file_path.name in {OUTPUT_FILE_NAME, EXCEPTION_FILE_NAME}:
fixture = Fixture(file_path.parent)
if fixture.is_valid():
return TestCase.from_parent(parent, path=file_path)
return None
|