Skip to content
This repository has been archived by the owner on Jul 17, 2023. It is now read-only.

Commit

Permalink
Bugfix: KeyError on a second update in case of no issues were updated…
Browse files Browse the repository at this point in the history
… in repo from the beginning of the updation cycle.
  • Loading branch information
IlyaFaer committed Mar 6, 2020
1 parent 2712960 commit 598ffd2
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 5 deletions.
10 changes: 5 additions & 5 deletions scraper/sheet_builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,14 +52,14 @@ def retrieve_updated(self):
)
self.prs_index.index_closed_prs(repo)

# process issues of the repo
issues = repo.get_issues(**self._build_filter(repo_name))
logging.info("{repo}: processing issues".format(repo=repo.full_name))

if repo_name not in self._last_issue_updates.keys():
self._last_issue_updates[repo_name] = (datetime.datetime(1, 1, 1), "")
is_first_update = True

# process issues of the repo
issues = repo.get_issues(**self._build_filter(repo_name))
logging.info("{repo}: processing issues".format(repo=repo.full_name))

for index, issue in enumerate(issues):
# "since" filter returns the issue, which was
# the last updated in previous filling - skip it
Expand Down Expand Up @@ -162,7 +162,7 @@ def _build_filter(self, repo_name):
dict: Filter, ready to be passed into the method.
"""
args = {}
if repo_name in self._last_issue_updates:
if self._last_issue_updates[repo_name][1]:
# if it isn't the first get-issues request, than
# we're adding sorting and "since" filter
# to get only recently updated issues
Expand Down
9 changes: 9 additions & 0 deletions scraper/tests/test_sheet.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,3 +98,12 @@ def test_spot_issue_object_updated(self):
self.assertEqual(
sheet._spot_issue_object("123", {"1253": "Issue"}), "index_issue"
)

def test_update_no_updated_issues(self):
"""Check if update was breaked in case of no updated issues."""
sheet = SheetMock("sheet1", SPREADSHEET_ID)

with mock.patch.object(sheet._builder, "retrieve_updated", return_value={}):
with mock.patch.object(sheet, "_read") as read_mock:
sheet.update(None)
read_mock.assert_not_called()
58 changes: 58 additions & 0 deletions scraper/tests/test_sheetbuilder.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import datetime
import unittest
from mocks import SheetBuilderMock


class TestSheetBuilder(unittest.TestCase):
def test_build_filter_not_updated(self):
"""
Check if filter built correctly in case of no updates
were made for specified repository.
"""
builder = SheetBuilderMock()
builder._last_issue_updates = {"repo1": (datetime.datetime(1, 1, 1), "")}

self.assertEqual(builder._build_filter("repo1"), {})

def test_build_filter(self):
"""Check if filter build correctly."""
DATE_ = datetime.datetime(2020, 6, 3)

builder = SheetBuilderMock()
builder._last_issue_updates = {"repo1": (DATE_, "issue_url")}

self.assertEqual(
builder._build_filter("repo1"),
{"sort": "updated", "direction": "desc", "since": DATE_, "state": "all"},
)

def test_reload_config(self):
"""Check if configurations reloading working fine."""
REPOS = ("repo1", "repo2")

builder = SheetBuilderMock()
builder.reload_config({"repo_names": REPOS})

self.assertEqual(builder._repo_names, REPOS)

def test_get_from_index(self):
"""Test getting issues from index."""
ISSUE1 = {"Title": "Issue title", "Number": 1}
URL = "url1"

builder = SheetBuilderMock()
builder._issues_index = {URL: ISSUE1}

self.assertEqual(builder.get_from_index("url"), None)
self.assertEqual(builder.get_from_index(URL), ISSUE1)

def test_delete_from_index(self):
"""Check indexed issue deletion."""
ISSUE1 = {"Title": "Issue title", "Number": 1}
URL = "url1"

builder = SheetBuilderMock()
builder._issues_index = {URL: ISSUE1}

builder.delete_from_index(URL)
self.assertNotIn(URL, builder._issues_index.keys())

0 comments on commit 598ffd2

Please sign in to comment.