Skip to content

Commit

Permalink
tests: Add tests for FilesStore.spider_closed()
Browse files Browse the repository at this point in the history
  • Loading branch information
jpmckinney committed Aug 18, 2022
1 parent 81e9572 commit 594b782
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 1 deletion.
3 changes: 2 additions & 1 deletion kingfisher_scrapy/extensions/files_store.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import math
import os

from scrapy import signals
Expand Down Expand Up @@ -48,7 +49,7 @@ def spider_closed(self, spider):
path = os.path.join(self.directory, self.relative_crawl_directory(spider))

message = f'The data is available at: {path}'
message_length = len(message) + 1
message_length = math.ceil(len(message) / 2) * 2
title_length = message_length // 2 - 8

spider.logger.info(f"+-{'-' * title_length } DATA DIRECTORY {'-' * title_length }-+")
Expand Down
33 changes: 33 additions & 0 deletions tests/extensions/test_files_store.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import logging
import os
from tempfile import TemporaryDirectory
from unittest.mock import Mock
Expand Down Expand Up @@ -37,6 +38,38 @@ def test_spider_opened(job, tmpdir):
assert not os.path.exists(path)


def test_spider_closed_odd(caplog):
spider = spider_with_files_store('1')

extension = FilesStore.from_crawler(spider.crawler)
with caplog.at_level(logging.INFO):
extension.spider_closed(spider)

assert [record.message for record in caplog.records] == [
'+----------------- DATA DIRECTORY -----------------+',
'| |',
'| The data is available at: 1/test/20010203_040506 |',
'| |',
'+--------------------------------------------------+',
]


def test_spider_closed_even(caplog):
spider = spider_with_files_store('22')

extension = FilesStore.from_crawler(spider.crawler)
with caplog.at_level(logging.INFO):
extension.spider_closed(spider)

assert [record.message for record in caplog.records] == [
'+------------------ DATA DIRECTORY ------------------+',
'| |',
'| The data is available at: 22/test/20010203_040506 |',
'| |',
'+----------------------------------------------------+',
]


@pytest.mark.parametrize('sample,path', [
(None, os.path.join('test', '20010203_040506', 'file.json')),
('true', os.path.join('test_sample', '20010203_040506', 'file.json')),
Expand Down

0 comments on commit 594b782

Please sign in to comment.