Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Avoid TypeError when running at PeriodicItemCountMonitor #436

Merged
merged 5 commits into from
Mar 8, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion spidermon/contrib/scrapy/monitors/monitors.py
Original file line number Diff line number Diff line change
Expand Up @@ -633,7 +633,7 @@ def get_threshold(self):
crawler = self.data.crawler
prev_item_scraped_count = self.stats.get("prev_item_scraped_count", 0)
item_scraped_count = self.stats.get(self.stat_name)
crawler.stats.set_value("prev_item_scraped_count", item_scraped_count)
crawler.stats.set_value("prev_item_scraped_count", item_scraped_count or 0)
VMRuiz marked this conversation as resolved.
Show resolved Hide resolved
threshold_increase = crawler.settings.get(self.threshold_setting)
if isinstance(threshold_increase, int):
return prev_item_scraped_count + threshold_increase
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -136,3 +136,17 @@ def test_item_count_monitor_validation(
runner.run(item_count_suite, **data)
assert len(runner.result.monitor_results) == 1
assert runner.result.monitor_results[0].status == expected_status


def test_item_count_monitor_undefined_stats(make_data, item_count_suite):
data = make_data({SPIDERMON_ITEM_COUNT_INCREASE: 0})
data["stats"]["enable_stats"] = 1 # otherwise monitor wont run
runner = data.pop("runner")
runner.run(MonitorSuite(monitors=[PeriodicItemCountMonitor]), **data)
assert runner.result.monitor_results[0].status == settings.MONITOR.STATUS.FAILURE
runner.run(MonitorSuite(monitors=[PeriodicItemCountMonitor]), **data)
assert runner.result.monitor_results[0].status == settings.MONITOR.STATUS.FAILURE

data["stats"]["item_scraped_count"] = 1
runner.run(MonitorSuite(monitors=[PeriodicItemCountMonitor]), **data)
assert runner.result.monitor_results[0].status == settings.MONITOR.STATUS.SUCCESS
Loading