diff --git a/libraries/tests/test_views.py b/libraries/tests/test_views.py index 735327f0..396f5ba1 100644 --- a/libraries/tests/test_views.py +++ b/libraries/tests/test_views.py @@ -14,10 +14,28 @@ def test_library_detail(library, tp): tp.response_200(response) -def test_library_detail_issues_context(tp, library): +def test_library_detail_context_get_closed_prs_count(tp, library): """ GET /libraries/{repo}/ - Test that the custom context vars appear as expected + Test that the custom closed_prs_count var appears as expected + """ + # Create open and closed PRs for this library, and another random PR + lib2 = baker.make("libraries.Library", slug="sample") + baker.make("libraries.PullRequest", library=library, is_open=True) + baker.make("libraries.PullRequest", library=library, is_open=False) + baker.make("libraries.PullRequest", library=lib2, is_open=True) + url = tp.reverse("library-detail", library.slug) + response = tp.get(url) + tp.response_200(response) + assert "closed_prs_count" in response.context + # Verify that the count only includes the one open PR for this library + assert response.context["closed_prs_count"] == 1 + + +def test_library_detail_context_get_open_issues_count(tp, library): + """ + GET /libraries/{repo}/ + Test that the custom open_issues_count var appears as expected """ # Create open and closed issues for this library, and another random issue lib2 = baker.make("libraries.Library", slug="sample") diff --git a/libraries/views.py b/libraries/views.py index d4c867eb..330eed67 100644 --- a/libraries/views.py +++ b/libraries/views.py @@ -1,6 +1,6 @@ from django.views.generic import DetailView, ListView -from .models import Category, Issue, Library +from .models import Category, Issue, Library, PullRequest class CategoryMixin: @@ -59,8 +59,12 @@ class LibraryDetail(CategoryMixin, DetailView): def get(self, request, *args, **kwargs): self.object = self.get_object() context = self.get_context_data(object=self.object) + context["closed_prs_count"] = self.get_closed_prs_count(self.object) context["open_issues_count"] = self.get_open_issues_count(self.object) return self.render_to_response(context) + def get_closed_prs_count(self, obj): + return PullRequest.objects.filter(library=obj, is_open=True).count() + def get_open_issues_count(self, obj): return Issue.objects.filter(library=obj, is_open=True).count() diff --git a/templates/libraries/detail.html b/templates/libraries/detail.html index 6a0bcc9e..a586eaa7 100644 --- a/templates/libraries/detail.html +++ b/templates/libraries/detail.html @@ -63,7 +63,7 @@

Closed Pull Requests

- X per Month + {{ closed_prs_count }}