# Path: cms_saq/models.py
# class Submission(models.Model):
#     question = models.SlugField()
#     answer = models.TextField(blank=True)
#     score = models.IntegerField()
#     user = models.ForeignKey('auth.User', related_name='saq_submissions')
# 
#     submission_set = models.ForeignKey(
#         SubmissionSet, related_name='submissions', null=True)
# 
#     class Meta:
#         ordering = ('submission_set', 'user', 'question')
#         unique_together = ('question', 'user', 'submission_set')
# 
#     def answer_list(self):
#         return self.answer.split(",")
# 
#     def __unicode__(self):
#         return u"%s answer to %s (%s)" % (
#             self.user, self.question, self.submission_set.slug
#             if self.submission_set else "default")
# 
# class Question(CMSPlugin):
#     QUESTION_TYPES = [
#         ('S', 'Single-choice question'),
#         ('M', 'Multi-choice question'),
#         ('F', 'Free-text question'),
#     ]
# 
#     slug = models.SlugField(
#         help_text="A slug for identifying answers to this specific question "
#         "(allows multiple only for multiple languages)")
#     tags = TaggableManager(blank=True)
#     label = models.CharField(max_length=512, blank=True)
#     help_text = models.CharField(max_length=512, blank=True)
#     question_type = models.CharField(max_length=1, choices=QUESTION_TYPES)
#     optional = models.BooleanField(
#         default=False,
#         help_text="Only applies to free text questions",
#     )
# 
#     depends_on_answer = models.ForeignKey(
#         Answer, null=True, blank=True, related_name='trigger_questions')
# 
#     def copy_relations(self, oldinstance):
#         for answer in oldinstance.answers.all():
#             answer.pk = None
#             answer.question = self
#             answer.save()
# 
#         self.depends_on_answer = oldinstance.depends_on_answer
# 
#     @staticmethod
#     def all_in_tree(page):
#         root = page.get_root()
#         # Remember that there might be questions on the root page as well!
#         tree = root.get_descendants() | Page.objects.filter(id=root.id)
#         placeholders = Placeholder.objects.filter(page__in=tree)
#         return Question.objects.filter(placeholder__in=placeholders)
# 
#     @staticmethod
#     def all_in_page(page):
#         placeholders = Placeholder.objects.filter(page=page)
#         return Question.objects.filter(placeholder__in=placeholders)
# 
#     def score(self, answers):
#         if self.question_type == 'F':
#             return 0
#         elif self.question_type == 'S':
#             return self.answers.get(slug=answers).score
#         elif self.question_type == 'M':
#             answers_list = answers.split(',')
#             return sum([self.answers.get(slug=a).score for a in answers_list])
# 
#     @property
#     def max_score(self):
#         if not hasattr(self, '_max_score'):
#             if self.question_type == "S":
#                 self._max_score = self.answers.aggregate(
#                     Max('score'))['score__max']
#             elif self.question_type == "M":
#                 self._max_score = self.answers.aggregate(
#                     Sum('score'))['score__sum']
#             else:
#                 self._max_score = None  # don't score free-text answers
#         return self._max_score
# 
#     def percent_score_for_user(self, user):
#         if self.max_score:
#             try:
#                 score = Submission.objects.get(
#                     question=self.slug,
#                     user=user,
#                 ).score
#             except Submission.DoesNotExist:
#                 return 0
#             return 100.0 * score / self.max_score
#         else:
#             return None
# 
#     def __unicode__(self):
#         return self.slug


import json
from django.test import TestCase
from django.contrib.auth.models import User
from django.core.urlresolvers import reverse
from django.test.client import Client, RequestFactory
from django.template import Template, RequestContext
from cms_saq.models import Submission, Question
"""
This file demonstrates writing tests using the unittest module. These will pass
when you run "manage.py test".

Replace this with more appropriate tests for your application.
"""





class SubmissionTest(TestCase):
    fixtures = ['submission_test']

    def setUp(self):
        self.client = Client()

    def test_submit_one_singlechoiceanswer(self):
        """Send one SingleChoiceAnswer submission to the submit view"""
        r = self.client.post(reverse('cms_saq_submit'), {'favourite-colour': 'red'})
        self.assertEqual(r.status_code, 200)
        self.assertEqual(1, Submission.objects.all().count())
        Submission.objects.get(question='favourite-colour', answer='red', score=10)

    def test_submit_two_singlechoiceanswers(self):
        """Send two SingleChoiceAnswer submissions to the submit view"""
        r = self.client.post(reverse('cms_saq_submit'), {'favourite-colour': 'red', 'favourite-sport': 'football'})
        self.assertEqual(r.status_code, 200)
        self.assertEqual(2, Submission.objects.all().count())
        s1 = Submission.objects.get(question='favourite-colour', answer='red', score=10)

# Path: cms_saq/models.py
# class Submission(models.Model):
#     question = models.SlugField()
#     answer = models.TextField(blank=True)
#     score = models.IntegerField()
#     user = models.ForeignKey('auth.User', related_name='saq_submissions')
# 
#     submission_set = models.ForeignKey(
#         SubmissionSet, related_name='submissions', null=True)
# 
#     class Meta:
#         ordering = ('submission_set', 'user', 'question')
#         unique_together = ('question', 'user', 'submission_set')
# 
#     def answer_list(self):
#         return self.answer.split(",")
# 
#     def __unicode__(self):
#         return u"%s answer to %s (%s)" % (
#             self.user, self.question, self.submission_set.slug
#             if self.submission_set else "default")
# 
# class Question(CMSPlugin):
#     QUESTION_TYPES = [
#         ('S', 'Single-choice question'),
#         ('M', 'Multi-choice question'),
#         ('F', 'Free-text question'),
#     ]
# 
#     slug = models.SlugField(
#         help_text="A slug for identifying answers to this specific question "
#         "(allows multiple only for multiple languages)")
#     tags = TaggableManager(blank=True)
#     label = models.CharField(max_length=512, blank=True)
#     help_text = models.CharField(max_length=512, blank=True)
#     question_type = models.CharField(max_length=1, choices=QUESTION_TYPES)
#     optional = models.BooleanField(
#         default=False,
#         help_text="Only applies to free text questions",
#     )
# 
#     depends_on_answer = models.ForeignKey(
#         Answer, null=True, blank=True, related_name='trigger_questions')
# 
#     def copy_relations(self, oldinstance):
#         for answer in oldinstance.answers.all():
#             answer.pk = None
#             answer.question = self
#             answer.save()
# 
#         self.depends_on_answer = oldinstance.depends_on_answer
# 
#     @staticmethod
#     def all_in_tree(page):
#         root = page.get_root()
#         # Remember that there might be questions on the root page as well!
#         tree = root.get_descendants() | Page.objects.filter(id=root.id)
#         placeholders = Placeholder.objects.filter(page__in=tree)
#         return Question.objects.filter(placeholder__in=placeholders)
# 
#     @staticmethod
#     def all_in_page(page):
#         placeholders = Placeholder.objects.filter(page=page)
#         return Question.objects.filter(placeholder__in=placeholders)
# 
#     def score(self, answers):
#         if self.question_type == 'F':
#             return 0
#         elif self.question_type == 'S':
#             return self.answers.get(slug=answers).score
#         elif self.question_type == 'M':
#             answers_list = answers.split(',')
#             return sum([self.answers.get(slug=a).score for a in answers_list])
# 
#     @property
#     def max_score(self):
#         if not hasattr(self, '_max_score'):
#             if self.question_type == "S":
#                 self._max_score = self.answers.aggregate(
#                     Max('score'))['score__max']
#             elif self.question_type == "M":
#                 self._max_score = self.answers.aggregate(
#                     Sum('score'))['score__sum']
#             else:
#                 self._max_score = None  # don't score free-text answers
#         return self._max_score
# 
#     def percent_score_for_user(self, user):
#         if self.max_score:
#             try:
#                 score = Submission.objects.get(
#                     question=self.slug,
#                     user=user,
#                 ).score
#             except Submission.DoesNotExist:
#                 return 0
#             return 100.0 * score / self.max_score
#         else:
#             return None
# 
#     def __unicode__(self):
#         return self.slug


# Path: cms_saq/tests.py
import json
from django.test import TestCase
from django.contrib.auth.models import User
from django.core.urlresolvers import reverse
from django.test.client import Client, RequestFactory
from django.template import Template, RequestContext
from cms_saq.models import Submission, Question

"""
This file demonstrates writing tests using the unittest module. These will pass
when you run "manage.py test".

Replace this with more appropriate tests for your application.
"""





class SubmissionTest(TestCase):
    fixtures = ['submission_test']

    def setUp(self):
        self.client = Client()

    def test_submit_one_singlechoiceanswer(self):
        """Send one SingleChoiceAnswer submission to the submit view"""
        r = self.client.post(reverse('cms_saq_submit'), {'favourite-colour': 'red'})
        self.assertEqual(r.status_code, 200)
        self.assertEqual(1, Submission.objects.all().count())
        Submission.objects.get(question='favourite-colour', answer='red', score=10)

    def test_submit_two_singlechoiceanswers(self):
        """Send two SingleChoiceAnswer submissions to the submit view"""
        r = self.client.post(reverse('cms_saq_submit'), {'favourite-colour': 'red', 'favourite-sport': 'football'})
        self.assertEqual(r.status_code, 200)
        self.assertEqual(2, Submission.objects.all().count())
        s1 = Submission.objects.get(question='favourite-colour', answer='red', score=10)

        s2 = Submission.objects.get(question='favourite-sport', answer='football', score=40)