Skip to content

Commit

Permalink
add integration tests for web-poet
Browse files Browse the repository at this point in the history
  • Loading branch information
BurnzZ committed Dec 23, 2021
1 parent c865c60 commit 54ea603
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 0 deletions.
27 changes: 27 additions & 0 deletions tests/po_lib/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
"""
This package is just for overrides testing purposes.
"""
import socket
from typing import Dict, Any, Callable

from url_matcher import Patterns
from url_matcher.util import get_domain
from web_poet import handle_urls, ItemWebPage

from tests.mockserver import get_ephemeral_port


# Need to define it here since it's always changing
DOMAIN = get_domain(socket.gethostbyname(socket.gethostname()))
PORT = get_ephemeral_port()


class POOverriden(ItemWebPage):
def to_item(self):
return {"msg": "PO that will be replace"}


@handle_urls(f"{DOMAIN}:{PORT}", POOverriden)
class POIntegration(ItemWebPage):
def to_item(self):
return {"msg": "PO replacement"}
24 changes: 24 additions & 0 deletions tests/test_middleware.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
from scrapy_poet.page_input_providers import (
PageObjectInputProvider
)
from web_poet import default_registry
from web_poet.page_inputs import ResponseData
from scrapy_poet import DummyResponse
from tests.utils import (HtmlResource,
Expand Down Expand Up @@ -350,3 +351,26 @@ def get_middleware(settings):
mock.call('/tmp/cache', compressed=True),
mock.call().close()
]


@inlineCallbacks
def test_web_poet_integration(settings):
"""This tests scrapy-poet's integration with web-poet most especially when
populating override settings via:
from web_poet import default_registry
SCRAPY_POET_OVERRIDES = default_registry.get_overrides()
"""

# Only import them in this test scope since they need to be synced with
# the URL of the Page Object annotated with @handle_urls.
from tests.po_lib import DOMAIN, PORT, POOverriden

# Override rules are defined in `tests/po_lib/__init__.py`.
settings["SCRAPY_POET_OVERRIDES"] = default_registry.get_overrides_from_module("tests.po_lib")

item, url, _ = yield crawl_single_item(
spider_for(POOverriden), ProductHtml, settings, port=PORT
)
assert item == {"msg": "PO replacement"}

0 comments on commit 54ea603

Please sign in to comment.