Esempio n. 1
0
def test_invalid_subclass():
    """Classes should inherit from ItemPage."""
    class MyClass(object):
        pass

    with pytest.raises(TypeError) as exc:
        callback_for(MyClass)

    msg = 'MyClass should be a sub-class of ItemPage.'
    assert str(exc.value) == msg
Esempio n. 2
0
def test_not_implemented_method():
    """Classes should implement to_item method."""
    class MyClass(ItemPage):
        pass

    with pytest.raises(NotImplementedError) as exc:
        callback_for(MyClass)

    msg = 'MyClass should implement to_item method.'
    assert str(exc.value) == msg
Esempio n. 3
0
def test_callback_for():
    """Simple test case to ensure it works as expected."""
    cb = callback_for(FakeItemPage)
    assert callable(cb)

    fake_page = FakeItemPage()
    response = DummyResponse('http://example.com/')
    result = cb(response=response, page=fake_page)
    assert list(result) == ['fake item page']
Esempio n. 4
0
def test_inline_callback():
    """Sample request with inline callback."""
    spider = MySpider()
    cb = callback_for(FakeItemPage)
    request = scrapy.Request('http://example.com/', callback=cb)
    with pytest.raises(ValueError) as exc:
        request_to_dict(request, spider)

    msg = f'Function {cb} is not a method of: {spider}'
    assert str(exc.value) == msg
Esempio n. 5
0
def test_callback_for_inline():
    callback = callback_for(FakeItemPage)
    response = DummyResponse('http://example.com/')
    fake_page = FakeItemPage()
    result = callback(response, page=fake_page)
    assert list(result) == ['fake item page']
Esempio n. 6
0
class MySpider(scrapy.Spider):

    name = 'my_spider'
    parse_item = callback_for(FakeItemPage)
    parse_web = callback_for(FakeItemWebPage)