1
0
mirror of https://github.com/scrapy/scrapy.git synced 2025-02-25 10:43:49 +00:00

merge with scrapy-stable

This commit is contained in:
Pablo Hoffman 2009-09-29 16:16:35 -03:00
commit ea9f2360eb
2 changed files with 21 additions and 7 deletions

View File

@ -31,9 +31,8 @@ class ItemLoader(object):
self._values[field_name] += arg_to_iter(processed_value)
def replace_value(self, field_name, value):
value = arg_to_iter(value)
processed_value = self._process_input_value(field_name, value)
self._values[field_name] = arg_to_iter(processed_value)
self._values.pop(field_name, None)
self.add_value(field_name, value)
def load_item(self):
item = self.item

View File

@ -311,6 +311,7 @@ class TestXPathItemLoader(XPathItemLoader):
name_in = MapCompose(lambda v: v.title())
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):
self.assertRaises(RuntimeError, XPathItemLoader)
@ -323,18 +324,32 @@ class XPathItemLoaderTest(unittest.TestCase):
self.assertEqual(l.get_output_value('name'), [u'Marta'])
def test_constructor_with_response(self):
response = HtmlResponse(url="", body="<html><body><div>marta</div></body></html>")
l = TestXPathItemLoader(response=response)
l = TestXPathItemLoader(response=self.response)
self.assert_(l.selector)
l.add_xpath('name', '//div/text()')
self.assertEqual(l.get_output_value('name'), [u'Marta'])
def test_add_xpath_re(self):
response = HtmlResponse(url="", body="<html><body><div>marta</div></body></html>")
l = TestXPathItemLoader(response=response)
l = TestXPathItemLoader(response=self.response)
l.add_xpath('name', '//div/text()', re='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__":
unittest.main()