# Path: review/models.py
# class Committee(models.Model):
#     '''
#         This model represents the review committee.
#         It consists of a foreign key to users for the members
#         of the committee, and a count which maintains the number of
#         applications each committee member has reviewed.
#     '''
#     member = models.ForeignKey(User)
#     count = models.IntegerField(max_length=3, default=0)
# 
#     objects = CommitteeManager()
#     
#     def __unicode__(self):
#         return '%s count: %s' % (self.member, self.count)
# 
# class CommitteeManager(models.Manager):
#     '''
#         The deafult manager for the Committee model
#     '''
#     
#     def get_next_free_reviewer(self):
#         '''
#             This function should return the committe member who has the smallest
#             application count. If there are two members with the same count then the
#             user with the highest pk is returned. If there are no committee members
#             then this function will return None
#         
#         '''
#         query_set = super(CommitteeManager, self).get_query_set().order_by('count', '-id')
# 
#         if len(query_set) > 0:
#             member = query_set[0].member
#             return member
#         else:
#             return None


from django.test import TestCase
from django.db.utils import IntegrityError
from review.models import Committee, CommitteeManager
from django.contrib.auth.models import User
        self.assertEquals(committee.count, 0)
        
    def test_default_manager(self):
        '''
            The committee model should have an instance of CommitteeManager as its deafult manager
        ''' 
        self.assertIsInstance(Committee.objects, CommitteeManager)

class CommitteeManagerTestCase(TestCase):
    
    
        
    def test_get_next_free_reviewer(self):
        '''
            This function should return the reviewer that has the smallest count.
            If two reviewers have the same count then then the reviewer with the largest primary
            key is returned.
        '''
        
        reviewer1 = User.objects.create_user('user1', 'test@test.com', 'password')
        reviewer2 = User.objects.create_user('user2', 'test@test.com', 'password')
        reviewer3 = User.objects.create_user('user3', 'test@test.com', 'password')
        
        reviewers = [(reviewer1, 5), (reviewer2, 0), (reviewer3, 2)]
        
        commiteeMap = {}
        for reviewer in reviewers:
            commiteeMap[reviewer[0]] = Committee.objects.create(member = reviewer[0] , count = reviewer[1])
            
        #currently the reviewer 2 should be returned

# Path: review/models.py
# class Committee(models.Model):
#     '''
#         This model represents the review committee.
#         It consists of a foreign key to users for the members
#         of the committee, and a count which maintains the number of
#         applications each committee member has reviewed.
#     '''
#     member = models.ForeignKey(User)
#     count = models.IntegerField(max_length=3, default=0)
# 
#     objects = CommitteeManager()
#     
#     def __unicode__(self):
#         return '%s count: %s' % (self.member, self.count)
# 
# class CommitteeManager(models.Manager):
#     '''
#         The deafult manager for the Committee model
#     '''
#     
#     def get_next_free_reviewer(self):
#         '''
#             This function should return the committe member who has the smallest
#             application count. If there are two members with the same count then the
#             user with the highest pk is returned. If there are no committee members
#             then this function will return None
#         
#         '''
#         query_set = super(CommitteeManager, self).get_query_set().order_by('count', '-id')
# 
#         if len(query_set) > 0:
#             member = query_set[0].member
#             return member
#         else:
#             return None


# Path: review/tests/models.py
from django.test import TestCase
from django.db.utils import IntegrityError
from review.models import Committee, CommitteeManager
from django.contrib.auth.models import User

        self.assertEquals(committee.count, 0)
        
    def test_default_manager(self):
        '''
            The committee model should have an instance of CommitteeManager as its deafult manager
        ''' 
        self.assertIsInstance(Committee.objects, CommitteeManager)

class CommitteeManagerTestCase(TestCase):
    
    
        
    def test_get_next_free_reviewer(self):
        '''
            This function should return the reviewer that has the smallest count.
            If two reviewers have the same count then then the reviewer with the largest primary
            key is returned.
        '''
        
        reviewer1 = User.objects.create_user('user1', 'test@test.com', 'password')
        reviewer2 = User.objects.create_user('user2', 'test@test.com', 'password')
        reviewer3 = User.objects.create_user('user3', 'test@test.com', 'password')
        
        reviewers = [(reviewer1, 5), (reviewer2, 0), (reviewer3, 2)]
        
        commiteeMap = {}
        for reviewer in reviewers:
            commiteeMap[reviewer[0]] = Committee.objects.create(member = reviewer[0] , count = reviewer[1])
            
        #currently the reviewer 2 should be returned

        self.assertEqual(Committee.objects.get_next_free_reviewer(), reviewer2)