forked from scrapy-plugins/scrapy-monkeylearn
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_monkeylearn.py
59 lines (45 loc) · 1.82 KB
/
test_monkeylearn.py
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
# -*- coding: utf-8 -*-
import json
from mock import Mock
from scrapy import Item, Field, Spider
from scrapy.http import Response
from scrapy.utils.test import get_crawler
from twisted.internet.defer import Deferred
from twisted.trial import unittest
from scrapy_monkeylearn.pipelines import MonkeyLearnPipeline
class TestSpider(Spider):
name = 'example_spider'
def parse(self, response):
pass
class TestItem(Item):
title = Field()
desc = Field()
category = Field()
class MonkeyLearnPipelineTest(unittest.TestCase):
def setUp(self):
settings = {
'MONKEYLEARN_CLASSIFIER': 'dummyclassifier',
'MONKEYLEARN_AUTH_TOKEN': 'notsosecret',
'MONKEYLEARN_CLASSIFIER_FIELDS': ['title', 'desc'],
'MONKEYLEARN_CATEGORIES_FIELD': 'category'}
self.crawler = get_crawler(settings_dict=settings)
self.crawler.engine = Mock()
self.crawler.engine.download = Mock(return_value=Deferred())
# workaround to set crawler during instantiation
self.spider = TestSpider().set_crawler(self.crawler)
self.item = TestItem({
'title': u'Foo bar',
'desc': u'For Guido\'s sake'})
def test_check_response(self):
pipe = MonkeyLearnPipeline(self.crawler)
def get_response(item, status=200):
return Response(
'http://api.monkeylearn.com/',
status=status,
body=json.dumps({'result': 'python'}))
response = get_response(self.item)
updated_item = pipe.check_response(response, self.item.copy())
self.assertEqual(updated_item['category'], 'python')
response = get_response(self.item, status=404)
updated_item = pipe.check_response(response, self.item.copy())
self.assertEqual(updated_item, self.item)