Skip to content

Commit 63029dc

Browse files
committed
add integration tests for web-poet
1 parent c865c60 commit 63029dc

File tree

2 files changed

+51
-0
lines changed

2 files changed

+51
-0
lines changed

tests/po_lib/__init__.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
"""
2+
This package is just for overrides testing purposes.
3+
"""
4+
import socket
5+
from typing import Dict, Any, Callable
6+
7+
from url_matcher import Patterns
8+
from url_matcher.util import get_domain
9+
from web_poet import handle_urls, ItemWebPage
10+
11+
from tests.mockserver import get_ephemeral_port
12+
13+
14+
# Need to define it here since it's always changing
15+
DOMAIN = get_domain(socket.gethostbyname(socket.gethostname()))
16+
PORT = get_ephemeral_port()
17+
18+
19+
class POOverriden(ItemWebPage):
20+
def to_item(self):
21+
return {"msg": "PO that will be replace"}
22+
23+
24+
@handle_urls(f"{DOMAIN}:{PORT}", POOverriden)
25+
class POIntegration(ItemWebPage):
26+
def to_item(self):
27+
return {"msg": "PO replacement"}

tests/test_middleware.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
from scrapy_poet.page_input_providers import (
2525
PageObjectInputProvider
2626
)
27+
from web_poet import default_registry
2728
from web_poet.page_inputs import ResponseData
2829
from scrapy_poet import DummyResponse
2930
from tests.utils import (HtmlResource,
@@ -350,3 +351,26 @@ def get_middleware(settings):
350351
mock.call('/tmp/cache', compressed=True),
351352
mock.call().close()
352353
]
354+
355+
356+
@inlineCallbacks
357+
def test_web_poet_integration(settings):
358+
"""This tests scrapy-poet's integration with web-poet most especially when
359+
populating override settings via:
360+
361+
from web_poet import default_registry
362+
363+
SCRAPY_POET_OVERRIDES = default_registry.get_overrides()
364+
"""
365+
366+
# Only import them in this test scope since they need to be synced with
367+
# the URL of the Page Object annotated with @handle_urls.
368+
from tests.po_lib import DOMAIN, PORT, POOverriden
369+
370+
# Override rules are defined in `tests/po_lib/__init__.py`.
371+
settings["SCRAPY_POET_OVERRIDES"] = default_registry.get_overrides()
372+
373+
item, url, _ = yield crawl_single_item(
374+
spider_for(POOverriden), ProductHtml, settings, port=PORT
375+
)
376+
assert item == {"msg": "PO replacement"}

0 commit comments

Comments
 (0)