From b0fe849f5811722417ccf867c1361d2ae6b71e3d Mon Sep 17 00:00:00 2001
From: Todd Dembrey <todd.dembrey@torchbox.com>
Date: Fri, 24 May 2019 15:18:36 +0100
Subject: [PATCH] GH-1191: Improve counting of reviewers who disagree

---
 opentech/apply/funds/models/submissions.py |  7 ++++---
 opentech/apply/funds/tests/test_models.py  | 21 +++++++++++++++++++--
 2 files changed, 23 insertions(+), 5 deletions(-)

diff --git a/opentech/apply/funds/models/submissions.py b/opentech/apply/funds/models/submissions.py
index 8bb320685..2ca3bba01 100644
--- a/opentech/apply/funds/models/submissions.py
+++ b/opentech/apply/funds/models/submissions.py
@@ -180,7 +180,7 @@ class ApplicationSubmissionQueryset(JSONOrderable):
                 output_field=IntegerField(),
             ),
             review_submitted_count=Subquery(
-                reviewers.reviewed().exclude(opinions__opinion=DISAGREE).values('submission').annotate(
+                reviewers.reviewed().values('submission').annotate(
                     count=Count('pk', distinct=True)
                 ).values('count'),
                 output_field=IntegerField(),
@@ -820,13 +820,14 @@ class AssignedReviewersQuerySet(models.QuerySet):
 
     def reviewed(self):
         return self.filter(
-            Q(opinions__isnull=False) | Q(Q(review__isnull=False) & Q(review__is_draft=False))
+            Q(opinions__opinion=AGREE) |
+            Q(Q(review__isnull=False) & Q(review__is_draft=False))
         ).distinct()
 
     def not_reviewed(self):
         return self.filter(
             Q(review__isnull=True) | Q(review__is_draft=True),
-            opinions__isnull=True,
+            Q(opinions__isnull=True) | Q(opinions__opinion=DISAGREE),
         ).distinct()
 
     def never_tried_to_review(self):
diff --git a/opentech/apply/funds/tests/test_models.py b/opentech/apply/funds/tests/test_models.py
index 545ce1c57..eaa22338c 100644
--- a/opentech/apply/funds/tests/test_models.py
+++ b/opentech/apply/funds/tests/test_models.py
@@ -535,7 +535,23 @@ class TestForTableQueryset(TestCase):
         submission = qs[0]
         self.assertEqual(submission.opinion_disagree, 1)
         self.assertEqual(submission.review_count, 2)
-        self.assertEqual(submission.review_submitted_count, 2)
+        # Reviewers that disagree are not counted
+        self.assertEqual(submission.review_submitted_count, 1)
+        self.assertEqual(submission.review_recommendation, MAYBE)
+
+    def test_opinionated_slash_confused_reviewer(self):
+        staff = StaffFactory()
+        submission = ApplicationSubmissionFactory()
+        review_one = ReviewFactory(submission=submission)
+        review_two = ReviewFactory(submission=submission)
+        ReviewOpinionFactory(opinion_disagree=True, review=review_one, author__reviewer=staff)
+        ReviewOpinionFactory(opinion_agree=True, review=review_two, author__reviewer=staff)
+        qs = ApplicationSubmission.objects.for_table(user=staff)
+        submission = qs[0]
+        self.assertEqual(submission.opinion_disagree, 1)
+        self.assertEqual(submission.review_count, 3)
+        # Reviewers that disagree are not counted
+        self.assertEqual(submission.review_submitted_count, 3)
         self.assertEqual(submission.review_recommendation, MAYBE)
 
     def test_dont_double_count_review_and_opinion(self):
@@ -570,7 +586,8 @@ class TestForTableQueryset(TestCase):
         self.assertEqual(submission, submission_one)
         self.assertEqual(submission.opinion_disagree, 1)
         self.assertEqual(submission.review_count, 2)
-        self.assertEqual(submission.review_submitted_count, 2)
+        # Reviewers that disagree are not counted
+        self.assertEqual(submission.review_submitted_count, 1)
         self.assertEqual(submission.review_recommendation, MAYBE)
 
         submission = qs[1]
-- 
GitLab