diff --git a/spidermon/contrib/scrapy/monitors/monitors.py b/spidermon/contrib/scrapy/monitors/monitors.py index b677803c..e530633e 100644 --- a/spidermon/contrib/scrapy/monitors/monitors.py +++ b/spidermon/contrib/scrapy/monitors/monitors.py @@ -632,7 +632,7 @@ def run(self, result): 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) + item_scraped_count = self.stats.get(self.stat_name, 0) crawler.stats.set_value("prev_item_scraped_count", item_scraped_count) threshold_increase = crawler.settings.get(self.threshold_setting) if isinstance(threshold_increase, int): diff --git a/tests/contrib/scrapy/monitors/test_periodic_execution_time_monitor.py b/tests/contrib/scrapy/monitors/test_periodic_execution_time_monitor.py index e6004917..d6461867 100644 --- a/tests/contrib/scrapy/monitors/test_periodic_execution_time_monitor.py +++ b/tests/contrib/scrapy/monitors/test_periodic_execution_time_monitor.py @@ -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