Skip to content

Commit

Permalink
test: Update test for recent changes
Browse files Browse the repository at this point in the history
  • Loading branch information
jpmckinney committed Apr 10, 2024
1 parent af221ca commit 66540c1
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 20 deletions.
2 changes: 1 addition & 1 deletion kingfisher_scrapy/pipelines.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ def __init__(self):
def process_item(self, item, spider):
if isinstance(item, (File, FileItem)):
if item.invalid_json:
raise DropItem(f'Invalid JSON data')
raise DropItem('Invalid JSON data')

validator = self.validators.get(item.__class__.__name__)
if validator:
Expand Down
14 changes: 7 additions & 7 deletions tests/pipelines/test_validate.py
Original file line number Diff line number Diff line change
Expand Up @@ -159,23 +159,23 @@ def test_process_item_with_duplicate_file_item(caplog):
assert str(excinfo.value) == "Duplicate FileItem: ('test1', 1)"


@pytest.mark.parametrize('klass, message', [(File, "'test.json'"), (FileItem, "('test.json', 1)")])
def test_process_item_with_invalid_json(klass, message):
spider = spider_with_crawler()
@pytest.mark.parametrize('klass', [File, FileItem])
def test_process_item_with_invalid_json(klass):
pipeline = Validate()
spider = spider_with_crawler()

kwargs = {'number': 1} if klass is FileItem else {}

item = klass(
file_name='test.json',
url='http://test.com',
file_name='test',
url='http://example.com',
data_type='release_package',
data='{"key": "value"}',
data='{"broken": }',
invalid_json=True,
**kwargs
)

with pytest.raises(DropItem) as excinfo:
pipeline.process_item(item, spider)

assert str(excinfo.value) == f"Invalid {klass.__name__} data: {message}"
assert str(excinfo.value) == 'Invalid JSON data'
21 changes: 9 additions & 12 deletions tests/test_spidermiddlewares.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@
from kingfisher_scrapy.items import File, FileError, FileItem
from kingfisher_scrapy.spidermiddlewares import (
AddPackageMiddleware,
ValidateJSONMiddleware,
ConcatenatedJSONMiddleware,
LineDelimitedMiddleware,
ReadDataMiddleware,
ResizePackageMiddleware,
RetryDataErrorMiddleware,
RootPathMiddleware,
ValidateJSONMiddleware,
)
from tests import response_fixture, spider_with_crawler

Expand Down Expand Up @@ -45,13 +45,13 @@ async def alist(iterable):
file_name='test.json',
url='http://test.com',
data_type='release_package',
data={},
data='{}',
),
FileItem(
file_name='test.json',
url='http://test.com',
data_type='release_package',
data={},
data='{}',
number=1,
),
FileError(
Expand Down Expand Up @@ -114,8 +114,8 @@ async def test_bytes_or_file(middleware_class, attribute, value, override, tmpdi
'file_name': 'test.json',
'url': 'http://test.com',
'data_type': 'release',
'path': '',
'invalid_json': False,
'path': '',
}
expected.update(override)

Expand Down Expand Up @@ -161,8 +161,8 @@ async def test_encoding(middleware_class, attribute, value, override, tmpdir):
'file_name': 'test.json',
'url': 'http://test.com',
'data_type': 'release',
'path': '',
'invalid_json': False,
'path': '',
}
expected.update(override)

Expand Down Expand Up @@ -203,8 +203,8 @@ async def test_add_package_middleware(data_type, data, root_path):
expected = {
'file_name': 'test.json',
'url': 'http://test.com',
'path': '',
'invalid_json': False,
'path': '',
}
if 'item' in root_path:
expected['number'] = 1
Expand Down Expand Up @@ -295,8 +295,8 @@ async def test_json_streaming_middleware(middleware_class, attribute, separator,
'data_type': 'release_package',
'data': data,
'number': i,
'path': '',
'invalid_json': False,
'path': '',
}


Expand Down Expand Up @@ -379,8 +379,8 @@ async def test_json_streaming_middleware_with_compressed_file_spider(middleware_
'data_type': 'release_package',
'data': data,
'number': i,
'path': '',
'invalid_json': False,
'path': '',
}


Expand Down Expand Up @@ -543,13 +543,10 @@ async def test_validate_json_middleware(invalid, klass):
file_name='test.json',
url='http://test.com',
data_type='release_package',
data='{"key": "value"}',
data='{"broken": }' if invalid else '{"key": "value"}',
**kwargs
)

if invalid:
item.data = '{"broken": }'

generator = middleware.process_spider_output(None, _aiter([item]), spider)
transformed_items = await alist(generator)

Expand Down

0 comments on commit 66540c1

Please sign in to comment.