Scrapy, a fast high-level web crawling & scraping framework for Python.
Add tests.
sortafreela1bca6a8e722
diff --git a/scrapy/loader/__init__.py b/scrapy/loader/__init__.pyindex 7c7f66866..20f0f90c3 100644--- a/scrapy/loader/__init__.py+++ b/scrapy/loader/__init__.py@@ -36,6 +36,7 @@ class ItemLoader(object):self._local_item = context['item'] = itemself._local_values = defaultdict(list)# Preprocess values if item built from dict+ # Values need to be added to item._values if added them from dict (not with add_values)for field_name, value in item.items():self._values[field_name] = self._process_input_value(field_name, value)diff --git a/tests/test_loader.py b/tests/test_loader.pyindex 8b58e4dbd..eb4a01572 100644--- a/tests/test_loader.py+++ b/tests/test_loader.py@@ -419,6 +419,29 @@ class BasicItemLoaderTest(unittest.TestCase):self.assertEqual(item['url'], u'rabbit.hole')self.assertEqual(item['summary'], u'rabbithole')+ def test_create_item_from_dict(self):+ class TestItem(Item):+ title = Field()++ class TestItemLoader(ItemLoader):+ default_item_class = TestItem++ input_item = {'title': 'Test item title 1'}+ il = TestItemLoader(item=input_item)+ # Getting output value mustn't remove value from item+ self.assertEqual(il.load_item(), {+ 'title': 'Test item title 1',+ })+ self.assertEqual(il.get_output_value('title'), 'Test item title 1')+ self.assertEqual(il.load_item(), {+ 'title': 'Test item title 1',+ })++ input_item = {'title': 'Test item title 2'}+ il = TestItemLoader(item=input_item)+ # Values from dict must be added to item _values+ self.assertEqual(il._values.get('title'), 'Test item title 2')+class ProcessorsTest(unittest.TestCase):@@ -709,28 +732,28 @@ class SubselectorLoaderTest(unittest.TestCase):class SelectJmesTestCase(unittest.TestCase):- test_list_equals = {- 'simple': ('foo.bar', {"foo": {"bar": "baz"}}, "baz"),- 'invalid': ('foo.bar.baz', {"foo": {"bar": "baz"}}, None),- 'top_level': ('foo', {"foo": {"bar": "baz"}}, {"bar": "baz"}),- 'double_vs_single_quote_string': ('foo.bar', {"foo": {"bar": "baz"}}, "baz"),- 'dict': (- 'foo.bar[*].name',- {"foo": {"bar": [{"name": "one"}, {"name": "two"}]}},- ['one', 'two']- ),- 'list': ('[1]', [1, 2], 2)- }-- def test_output(self):- for l in self.test_list_equals:- expr, test_list, expected = self.test_list_equals[l]- test = SelectJmes(expr)(test_list)- self.assertEqual(- test,- expected,- msg='test "{}" got {} expected {}'.format(l, test, expected)- )+ test_list_equals = {+ 'simple': ('foo.bar', {"foo": {"bar": "baz"}}, "baz"),+ 'invalid': ('foo.bar.baz', {"foo": {"bar": "baz"}}, None),+ 'top_level': ('foo', {"foo": {"bar": "baz"}}, {"bar": "baz"}),+ 'double_vs_single_quote_string': ('foo.bar', {"foo": {"bar": "baz"}}, "baz"),+ 'dict': (+ 'foo.bar[*].name',+ {"foo": {"bar": [{"name": "one"}, {"name": "two"}]}},+ ['one', 'two']+ ),+ 'list': ('[1]', [1, 2], 2)+ }++ def test_output(self):+ for l in self.test_list_equals:+ expr, test_list, expected = self.test_list_equals[l]+ test = SelectJmes(expr)(test_list)+ self.assertEqual(+ test,+ expected,+ msg='test "{}" got {} expected {}'.format(l, test, expected)+ )if __name__ == "__main__":