Skip to content

Commit

Permalink
Avoid TypeError when running at PeriodicItemCountMonitor (#436)
Browse files Browse the repository at this point in the history
* Avoid TypeError when running when PeriodicItemCountMonitor runs first time with undefined item_scraped_count stat

* Add success case

* Move None to 0 conversion place
  • Loading branch information
VMRuiz authored Mar 8, 2024
1 parent 8493465 commit 049cb00
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
2 changes: 1 addition & 1 deletion spidermon/contrib/scrapy/monitors/monitors.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
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

0 comments on commit 049cb00

Please sign in to comment.