Repository: gratipay/gratipay.com

Issue:
Members count incorrect for team someone is member of
Hi,

I've created a "Wolf CMS" team and when I go to the team's page & click on members, it correctly displays 2 members.

However, if I go to my own profile page (Martijn) and look at the member count for my team there, it says "with 76 others".

Seems the count is incorrectly generated.



Discussion:
I'd assume that this is due to this SQL statement (specifically the highlighted condition) https://github.com/gittip/www.gittip.com/blob/master/gittip/models/participant.py#L315

I'm having trouble confirming since the fake_data script does not generate Teams and I can't see how to coerce it to do so within the constraints and magic of that script. Anybody who knows how to do this want to pair up?

@wilkie does its inability to generate Teams warrant a new Issue?


Resolving patch:
diff --git a/gittip/fake_data.py b/gittip/fake_data.py
--- a/gittip/fake_data.py
+++ b/gittip/fake_data.py
@@ -1,9 +1,10 @@
 from faker import Factory
-from gittip import orm, MAX_TIP, MIN_TIP
+from gittip import orm, wireup, MAX_TIP, MIN_TIP
 from gittip.models.tip import Tip
 from gittip.models.participant import Participant
 from gittip.models.elsewhere import Elsewhere
 
+import gittip
 import decimal
 import random
 import string
@@ -116,7 +117,7 @@ def fake_elsewhere(participant, platform=None):
     )
 
 
-def populate_db(session, num_participants=100, num_tips=50):
+def populate_db(session, num_participants=100, num_tips=50, num_teams=5):
     """
     Populate DB with fake data
     """
@@ -135,6 +136,19 @@ def populate_db(session, num_participants=100, num_tips=50):
             e = fake_elsewhere(p, platform_name)
             session.add(e)
 
+    #Make teams
+    teams = []
+    for i in xrange(num_teams):
+        t = fake_participant()
+        t.number = "plural"
+        session.add(t)
+        session.commit()
+        #Add 1 to 3 members to the team
+        members = random.sample(participants, random.randint(1, 3))
+        for p in members:
+            t.add_member(p)
+        teams.append(t)
+
     #Make the tips
     tips = []
     for i in xrange(num_tips):
@@ -148,6 +162,7 @@ def populate_db(session, num_participants=100, num_tips=50):
 def main():
     db = orm.db
     dbsession = db.session
+    gittip.db = wireup.db()
     populate_db(dbsession)
 
 if __name__ == '__main__':
diff --git a/gittip/models/participant.py b/gittip/models/participant.py
--- a/gittip/models/participant.py
+++ b/gittip/models/participant.py
@@ -312,9 +312,9 @@ def get_teams(self):
             SELECT team AS name
                  , ( SELECT count(*)
                        FROM current_memberships
-                      WHERE team=team
+                      WHERE team=x.team
                     ) AS nmembers
-              FROM current_memberships
+              FROM current_memberships x
              WHERE member=%s;
 
         """, (self.username,)))


Test patch:
diff --git a/gittip/testing/__init__.py b/gittip/testing/__init__.py
--- a/gittip/testing/__init__.py
+++ b/gittip/testing/__init__.py
@@ -53,10 +53,10 @@ def setUp(self):
     def tearDown(self):
         self.db.empty_tables()
 
-    def make_participant(self, username, **kw):
+    def make_participant(self, username, number='singular', **kw):
         participant = Participant( username=username
                                  , username_lower=username.lower()
-                                 , number='singular'
+                                 , number=number
                                  , **kw
                                   )
         self.session.add(participant)
diff --git a/tests/test_fake_data.py b/tests/test_fake_data.py
--- a/tests/test_fake_data.py
+++ b/tests/test_fake_data.py
@@ -15,9 +15,10 @@ def setUp(self):
     def test_fake_data(self):
         num_participants = 5
         num_tips = 5
-        fake_data.populate_db(self.session, num_participants, num_tips)
+        num_teams = 1
+        fake_data.populate_db(self.session, num_participants, num_tips, num_teams)
         tips = Tip.query.all()
         participants = Participant.query.all()
         assert len(tips) == num_tips
-        assert len(participants) == num_participants
+        assert len(participants) == num_participants + num_teams
 
diff --git a/tests/test_teams.py b/tests/test_teams.py
new file mode 100644
--- /dev/null
+++ b/tests/test_teams.py
@@ -0,0 +1,45 @@
+from __future__ import unicode_literals
+import random
+import datetime
+from decimal import Decimal
+
+import psycopg2
+import pytz
+from nose.tools import assert_raises
+
+from gittip.testing import Harness
+from gittip.models import Participant, Tip
+
+
+class Tests(Harness):
+
+    def setUp(self):
+        super(Harness, self).setUp()
+        self.team = self.make_participant('team1', 'plural')  # Our team
+
+    def test_is_team(self):
+        expeted = True
+        actual = self.team.IS_PLURAL
+        assert actual == expeted, actual
+
+    # def test_show_as_team(self):
+    #     expeted = True
+    #     actual = self.team.show_as_team(self.team)
+    #     assert actual == expected, actual
+
+    def test_can_add_members(self):
+        user = self.make_participant('user1')
+        expected = True
+        self.team.add_member(user)
+        actual = user.member_of(self.team)
+        assert actual == expected, actual
+
+    def test_get_teams_for_member(self):
+        user = self.make_participant('user1')
+        user2 = self.make_participant('user2')
+        team = self.make_participant('team2', 'plural')
+        self.team.add_member(user)
+        team.add_member(user2)
+        expected = 1
+        actual = user.get_teams().pop()['nmembers']
+        assert actual == expected, actual