mirror of
https://github.com/scrapy/scrapy.git
synced 2025-02-25 14:03:41 +00:00
merge with scrapy-stable
This commit is contained in:
commit
ea9f2360eb
@ -31,9 +31,8 @@ class ItemLoader(object):
|
|||||||
self._values[field_name] += arg_to_iter(processed_value)
|
self._values[field_name] += arg_to_iter(processed_value)
|
||||||
|
|
||||||
def replace_value(self, field_name, value):
|
def replace_value(self, field_name, value):
|
||||||
value = arg_to_iter(value)
|
self._values.pop(field_name, None)
|
||||||
processed_value = self._process_input_value(field_name, value)
|
self.add_value(field_name, value)
|
||||||
self._values[field_name] = arg_to_iter(processed_value)
|
|
||||||
|
|
||||||
def load_item(self):
|
def load_item(self):
|
||||||
item = self.item
|
item = self.item
|
||||||
|
@ -311,6 +311,7 @@ class TestXPathItemLoader(XPathItemLoader):
|
|||||||
name_in = MapCompose(lambda v: v.title())
|
name_in = MapCompose(lambda v: v.title())
|
||||||
|
|
||||||
class XPathItemLoaderTest(unittest.TestCase):
|
class XPathItemLoaderTest(unittest.TestCase):
|
||||||
|
response = HtmlResponse(url="", body='<html><body><div id="id">marta</div><p>paragraph</p></body></html>')
|
||||||
|
|
||||||
def test_constructor_errors(self):
|
def test_constructor_errors(self):
|
||||||
self.assertRaises(RuntimeError, XPathItemLoader)
|
self.assertRaises(RuntimeError, XPathItemLoader)
|
||||||
@ -323,18 +324,32 @@ class XPathItemLoaderTest(unittest.TestCase):
|
|||||||
self.assertEqual(l.get_output_value('name'), [u'Marta'])
|
self.assertEqual(l.get_output_value('name'), [u'Marta'])
|
||||||
|
|
||||||
def test_constructor_with_response(self):
|
def test_constructor_with_response(self):
|
||||||
response = HtmlResponse(url="", body="<html><body><div>marta</div></body></html>")
|
l = TestXPathItemLoader(response=self.response)
|
||||||
l = TestXPathItemLoader(response=response)
|
|
||||||
self.assert_(l.selector)
|
self.assert_(l.selector)
|
||||||
l.add_xpath('name', '//div/text()')
|
l.add_xpath('name', '//div/text()')
|
||||||
self.assertEqual(l.get_output_value('name'), [u'Marta'])
|
self.assertEqual(l.get_output_value('name'), [u'Marta'])
|
||||||
|
|
||||||
def test_add_xpath_re(self):
|
def test_add_xpath_re(self):
|
||||||
response = HtmlResponse(url="", body="<html><body><div>marta</div></body></html>")
|
l = TestXPathItemLoader(response=self.response)
|
||||||
l = TestXPathItemLoader(response=response)
|
|
||||||
l.add_xpath('name', '//div/text()', re='ma')
|
l.add_xpath('name', '//div/text()', re='ma')
|
||||||
self.assertEqual(l.get_output_value('name'), [u'Ma'])
|
self.assertEqual(l.get_output_value('name'), [u'Ma'])
|
||||||
|
|
||||||
|
def test_replace_xpath(self):
|
||||||
|
l = TestXPathItemLoader(response=self.response)
|
||||||
|
self.assert_(l.selector)
|
||||||
|
l.add_xpath('name', '//div/text()')
|
||||||
|
self.assertEqual(l.get_output_value('name'), [u'Marta'])
|
||||||
|
l.replace_xpath('name', '//p/text()')
|
||||||
|
self.assertEqual(l.get_output_value('name'), [u'Paragraph'])
|
||||||
|
|
||||||
|
def test_replace_xpath_re(self):
|
||||||
|
l = TestXPathItemLoader(response=self.response)
|
||||||
|
self.assert_(l.selector)
|
||||||
|
l.add_xpath('name', '//div/text()')
|
||||||
|
self.assertEqual(l.get_output_value('name'), [u'Marta'])
|
||||||
|
l.replace_xpath('name', '//div/text()', re='ma')
|
||||||
|
self.assertEqual(l.get_output_value('name'), [u'Ma'])
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
unittest.main()
|
unittest.main()
|
||||||
|
Loading…
x
Reference in New Issue
Block a user