# Path: applicationform/models.py
# class FullApplicationChecklistLink(models.Model):
#     '''
#         This model encapsulates the link between a checklist question
#         and one of more QuestionGroups that will be added to the
#         ethics application_form if that question is answered yes.    
#     '''
#     
#     checklist_question = models.ForeignKey(Question, related_name='checklist_question')
#     included_group = models.ForeignKey(QuestionGroup, related_name='included_group')
#     order = models.IntegerField()
#     objects = FullApplicationChecklistLinkManager()
# 
# class FullApplicationChecklistLinkManager(models.Manager):
#     
#     def get_ordered_groups_for_question(self, the_question):
#         '''
#             Returns all of the Groups that are linked to a particular 
#             checklist_question, in order.
#         '''
#         
#         return [link.included_group for link in super(FullApplicationChecklistLinkManager, self).
#                 get_query_set().filter(checklist_question=the_question).order_by('order')]


from django.test import TestCase
from applicationform.models import FullApplicationChecklistLink,\
    FullApplicationChecklistLinkManager
from questionnaire.models import QuestionGroup, Question
from django.db.utils import IntegrityError
        self.assertRaises(IntegrityError, FullApplicationChecklistLink.objects.create, checklist_question=test_question, included_group=test_question_group)
        self.assertTrue( FullApplicationChecklistLink.objects.create(checklist_question=test_question, included_group=test_question_group, order=1) )
        
    def test_default_manager(self):
        '''
            This model should have an instance of  FullApplicationChecklistLinkManager
            as the default manager available through the ''objects'' field.
        '''
        self.assertIsInstance(FullApplicationChecklistLink.objects, FullApplicationChecklistLinkManager)
        
    
class FullApplicationChecklistLinkManagerTestCase(TestCase):
    def setUp(self):
        self.test_question = Question.objects.create(label='test', field_type='charfield')
        self.test_first_group = QuestionGroup.objects.create(name='group1')
        self.test_second_group = QuestionGroup.objects.create(name='group2')
        
    def test_get_ordered_groups_for_question_no_links(self):
        '''
            If there are no links avaialble questions matching
            the query then this function should return an empty list
        '''
        self.assertEqual(FullApplicationChecklistLink.objects.get_ordered_groups_for_question(self.test_question), [])
        
    def test_get_ordered_groups_for_question_links(self):
        '''
            If there are links avaialble then this function should return
            all of them in a list, which is ordered by the order field on the 
            FullApplicationChecklistLink objects.
        '''

# Path: applicationform/models.py
# class FullApplicationChecklistLink(models.Model):
#     '''
#         This model encapsulates the link between a checklist question
#         and one of more QuestionGroups that will be added to the
#         ethics application_form if that question is answered yes.    
#     '''
#     
#     checklist_question = models.ForeignKey(Question, related_name='checklist_question')
#     included_group = models.ForeignKey(QuestionGroup, related_name='included_group')
#     order = models.IntegerField()
#     objects = FullApplicationChecklistLinkManager()
# 
# class FullApplicationChecklistLinkManager(models.Manager):
#     
#     def get_ordered_groups_for_question(self, the_question):
#         '''
#             Returns all of the Groups that are linked to a particular 
#             checklist_question, in order.
#         '''
#         
#         return [link.included_group for link in super(FullApplicationChecklistLinkManager, self).
#                 get_query_set().filter(checklist_question=the_question).order_by('order')]


# Path: applicationform/tests/models.py
from django.test import TestCase
from applicationform.models import FullApplicationChecklistLink,\
    FullApplicationChecklistLinkManager
from questionnaire.models import QuestionGroup, Question
from django.db.utils import IntegrityError

        self.assertRaises(IntegrityError, FullApplicationChecklistLink.objects.create, checklist_question=test_question, included_group=test_question_group)
        self.assertTrue( FullApplicationChecklistLink.objects.create(checklist_question=test_question, included_group=test_question_group, order=1) )
        
    def test_default_manager(self):
        '''
            This model should have an instance of  FullApplicationChecklistLinkManager
            as the default manager available through the ''objects'' field.
        '''
        self.assertIsInstance(FullApplicationChecklistLink.objects, FullApplicationChecklistLinkManager)
        
    
class FullApplicationChecklistLinkManagerTestCase(TestCase):
    def setUp(self):
        self.test_question = Question.objects.create(label='test', field_type='charfield')
        self.test_first_group = QuestionGroup.objects.create(name='group1')
        self.test_second_group = QuestionGroup.objects.create(name='group2')
        
    def test_get_ordered_groups_for_question_no_links(self):
        '''
            If there are no links avaialble questions matching
            the query then this function should return an empty list
        '''
        self.assertEqual(FullApplicationChecklistLink.objects.get_ordered_groups_for_question(self.test_question), [])
        
    def test_get_ordered_groups_for_question_links(self):
        '''
            If there are links avaialble then this function should return
            all of them in a list, which is ordered by the order field on the 
            FullApplicationChecklistLink objects.
        '''

        FullApplicationChecklistLink.objects.create(checklist_question=self.test_question, included_group=self.test_first_group, order=1)