Skip to content

Scrapy, a fast high-level web crawling & scraping framework for Python.

Add custom contracts tests

Stas Glubokiyebbde57eca31
diff --git a/tests/test_contracts.py b/tests/test_contracts.py
index 1cea2afb7..078ef6e0d 100644
--- a/tests/test_contracts.py
+++ b/tests/test_contracts.py
@@ -5,7 +5,7 @@ from twisted.trial import unittest
from scrapy.spiders import Spider
from scrapy.http import Request
from scrapy.item import Item, Field
-from scrapy.contracts import ContractsManager
+from scrapy.contracts import ContractsManager, Contract
from scrapy.contracts.default import (
UrlContract,
ReturnsContract,
@@ -22,6 +22,21 @@ class ResponseMock(object):
url = 'http://scrapy.org'
+class CustomSuccessContract(Contract):
+ name = 'custom_success_contract'
+
+ def adjust_request_args(self, args):
+ args['url'] = 'http://scrapy.org'
+ return args
+
+
+class CustomFailContract(Contract):
+ name = 'custom_fail_contract'
+
+ def adjust_request_args(self, args):
+ raise TypeError('Error in adjust_request_args')
+
+
class TestSpider(Spider):
name = 'demo_spider'
@@ -99,8 +114,34 @@ class TestSpider(Spider):
pass
+class CustomContractSuccessSpider(Spider):
+ name = 'custom_contract_success_spider'
+
+ def parse(self, response):
+ """
+ @custom_success_contract
+ """
+ pass
+
+
+class CustomContractFailSpider(Spider):
+ name = 'custom_contract_fail_spider'
+
+ def parse(self, response):
+ """
+ @custom_fail_contract
+ """
+ pass
+
+
class ContractsManagerTest(unittest.TestCase):
- contracts = [UrlContract, ReturnsContract, ScrapesContract]
+ contracts = [
+ UrlContract,
+ ReturnsContract,
+ ScrapesContract,
+ CustomSuccessContract,
+ CustomFailContract
+ ]
def setUp(self):
self.conman = ContractsManager(self.contracts)
@@ -114,6 +155,9 @@ class ContractsManagerTest(unittest.TestCase):
self.assertTrue(self.results.failures)
self.assertFalse(self.results.errors)
+ def should_error(self):
+ self.assertTrue(self.results.errors)
+
def test_contracts(self):
spider = TestSpider()
@@ -185,3 +229,10 @@ class ContractsManagerTest(unittest.TestCase):
self.results)
request.callback(response)
self.should_fail()
+
+ def test_custom_contracts(self):
+ self.conman.from_spider(CustomContractSuccessSpider(), self.results)
+ self.should_succeed()
+
+ self.conman.from_spider(CustomContractFailSpider(), self.results)
+ self.should_error()