From cf06cd034d1a7e00b5ef5e1239eaf7ad53d31544 Mon Sep 17 00:00:00 2001 From: Lacey Williams Henschel Date: Thu, 15 Dec 2022 15:32:20 -0800 Subject: [PATCH] :pencil: Retrieve open issues from DB and not from GH API --- libraries/tests/test_views.py | 27 ++++++++++++++++++++++----- libraries/views.py | 11 ++--------- 2 files changed, 24 insertions(+), 14 deletions(-) diff --git a/libraries/tests/test_views.py b/libraries/tests/test_views.py index 19b0a966..735327f0 100644 --- a/libraries/tests/test_views.py +++ b/libraries/tests/test_views.py @@ -1,15 +1,32 @@ -from unittest.mock import patch +from model_bakery import baker def test_library_list(library, tp): + """GET /libraries/""" res = tp.get("libraries") tp.response_200(res) def test_library_detail(library, tp): + """GET /libraries/{repo}/""" url = tp.reverse("library-detail", library.slug) + response = tp.get(url) + tp.response_200(response) - with patch("libraries.views.LibraryDetail.get_open_issues_count") as count_mock: - count_mock.return_value = 21 - res = tp.get(url) - tp.response_200(res) + +def test_library_detail_issues_context(tp, library): + """ + GET /libraries/{repo}/ + Test that the custom context vars appear as expected + """ + # Create open and closed issues for this library, and another random issue + lib2 = baker.make("libraries.Library", slug="sample") + baker.make("libraries.Issue", library=library, is_open=True) + baker.make("libraries.Issue", library=library, is_open=False) + baker.make("libraries.Issue", library=lib2, is_open=True) + url = tp.reverse("library-detail", library.slug) + response = tp.get(url) + tp.response_200(response) + assert "open_issues_count" in response.context + # Verify that the count only includes the one open issue for this library + assert response.context["open_issues_count"] == 1 diff --git a/libraries/views.py b/libraries/views.py index bfa82fea..d4c867eb 100644 --- a/libraries/views.py +++ b/libraries/views.py @@ -1,7 +1,6 @@ from django.views.generic import DetailView, ListView -from .github import repo_issues -from .models import Category, Library +from .models import Category, Issue, Library class CategoryMixin: @@ -64,10 +63,4 @@ class LibraryDetail(CategoryMixin, DetailView): return self.render_to_response(context) def get_open_issues_count(self, obj): - try: - issues = repo_issues( - obj.github_owner, obj.github_repo, state="open", issues_only=True - ) - return len(issues) - except Exception: - return 0 + return Issue.objects.filter(library=obj, is_open=True).count()