Skip to content

Commit 049cb00

Browse files
authored
Avoid TypeError when running at PeriodicItemCountMonitor (#436)
* Avoid TypeError when running when PeriodicItemCountMonitor runs first time with undefined item_scraped_count stat * Add success case * Move None to 0 conversion place
1 parent 8493465 commit 049cb00

File tree

2 files changed

+15
-1
lines changed

2 files changed

+15
-1
lines changed

spidermon/contrib/scrapy/monitors/monitors.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -632,7 +632,7 @@ def run(self, result):
632632
def get_threshold(self):
633633
crawler = self.data.crawler
634634
prev_item_scraped_count = self.stats.get("prev_item_scraped_count", 0)
635-
item_scraped_count = self.stats.get(self.stat_name)
635+
item_scraped_count = self.stats.get(self.stat_name, 0)
636636
crawler.stats.set_value("prev_item_scraped_count", item_scraped_count)
637637
threshold_increase = crawler.settings.get(self.threshold_setting)
638638
if isinstance(threshold_increase, int):

tests/contrib/scrapy/monitors/test_periodic_execution_time_monitor.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,3 +136,17 @@ def test_item_count_monitor_validation(
136136
runner.run(item_count_suite, **data)
137137
assert len(runner.result.monitor_results) == 1
138138
assert runner.result.monitor_results[0].status == expected_status
139+
140+
141+
def test_item_count_monitor_undefined_stats(make_data, item_count_suite):
142+
data = make_data({SPIDERMON_ITEM_COUNT_INCREASE: 0})
143+
data["stats"]["enable_stats"] = 1 # otherwise monitor wont run
144+
runner = data.pop("runner")
145+
runner.run(MonitorSuite(monitors=[PeriodicItemCountMonitor]), **data)
146+
assert runner.result.monitor_results[0].status == settings.MONITOR.STATUS.FAILURE
147+
runner.run(MonitorSuite(monitors=[PeriodicItemCountMonitor]), **data)
148+
assert runner.result.monitor_results[0].status == settings.MONITOR.STATUS.FAILURE
149+
150+
data["stats"]["item_scraped_count"] = 1
151+
runner.run(MonitorSuite(monitors=[PeriodicItemCountMonitor]), **data)
152+
assert runner.result.monitor_results[0].status == settings.MONITOR.STATUS.SUCCESS

0 commit comments

Comments
 (0)