<commit_before>#!/usr/bin/env python2.5
#
# Copyright 2011 the Melange authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#   http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.


__authors__ = [
  '"<NAME>" <<EMAIL>>',
  ]


import httplib
import urllib

from tests.profile_utils import GSoCProfileHelper
from tests.test_utils import DjangoTestCase
from tests.test_utils import TaskQueueTestCase

from soc.modules.gsoc.models.grading_project_survey import GradingProjectSurvey
from soc.modules.gsoc.models.grading_project_survey_record import \
    GSoCGradingProjectSurveyRecord
from soc.modules.gsoc.models.grading_project_survey_record import \
    GradingProjectSurveyRecord
from soc.modules.gsoc.models.grading_record import GSoCGradingRecord
from soc.modules.gsoc.models.grading_record import GradingRecord
from soc.modules.gsoc.models.grading_survey_group import GradingSurveyGroup
from soc.modules.gsoc.models.grading_survey_group import GSoCGradingSurveyGroup
from soc.modules.gsoc.models.student_project import StudentProject
from soc.modules.gsoc.models.project import GSoCProject
from soc.modules.gsoc.models.project_survey import ProjectSurvey
from soc.modules.gsoc.models.project_survey_record import \
    GSoCProjectSurveyRecord
from soc.modules.gsoc.models.project_survey_record import ProjectSurveyRecord


class SurveyConversionTest(DjangoTestCase, TaskQueueTestCase):
  """Test the code that converts the old surveys.
  """

  PSR_URL = '/tasks/gsoc/convert_surveys/project_survey_record'
  GPSR_URL = '/tasks/gsoc/convert_surveys/grading_project_survey_record'
  GSG_URL = '/tasks/gsoc/convert_surveys/grading_survey_group'
  GR_URL = '/tasks/gsoc/convert_surveys/grading_record'


  def setUp(self):
    super(SurveyConversionTest, self).setUp()
    self.init()
    self.createMentor()
    self.createStudent()
    self.createSurveys()

  def createMentor(self):
    """Creates a new mentor.
    """
    profile_helper = GSoCProfileHelper(self.gsoc, self.dev_test)
    profile_helper.createOtherUser('<EMAIL>')
    self.mentor = profile_helper.createMentor(self.org)

  def createStudent(self):
    """Creates a Student with a project.
    """
    profile_helper = GSoCProfileHelper(self.gsoc, self.dev_test)
    profile_helper.createOtherUser('<EMAIL>')
    self.student = profile_helper.createStudentWithProject(self.org,
                                                            self.mentor)
    self.project = GSoCProject.all().ancestor(self.student).get()

    values = {
        'link_id': 'link_id',
        'scope': self.org,
        'scope_path': self.org.key().id_or_name(),
        'student': self.student}
    for prop in self.project.properties().keys():
      values[prop] = getattr(self.project, prop)
    self.old_project = StudentProject(key_name='keyname', **values)
    self.old_project.put()

  def createSurveys(self):
    """Creates the surveys and records required for the tests in the old
    format.
    """
    survey_values = {
        'author': self.founder,
        'title': 'Title',
        'modified_by': self.founder,
        'link_id': 'link_id',
        'scope': self.gsoc,
        'scope_path': self.gsoc.key().id_or_name()}

    self.project_survey = ProjectSurvey(**survey_values)
    self.project_survey.put()

    record_values = {
        'user': self.student.user,
        'org': self.org,
        'project': self.old_project,
        'survey': self.project_survey}
    self.project_survey_record = ProjectSurveyRecord(**record_values)
    self.project_survey_record.put()

    self.grading_survey = GradingProjectSurvey(**survey_values)
    self.grading_survey.put()

    record_values = {
        'user': self.student.user,
        'org': self.org,
        'project': self.old_project,
        'survey': self.grading_survey,
        'grade': True}
    self.grading_survey_record = GradingProjectSurveyRecord(**record_values)
    self.grading_survey_record.put()

    group_values = {
        'name': 'Survey Group Name',
        'grading_survey': self.grading_survey,
        'student_survey': self.project_survey,
        'link_id': 'link_id',
        'scope': self.gsoc,
        'scope_path': self.gsoc.key().id_or_name()}
    self.survey_group = GradingSurveyGroup(key_name='keyname', **group_values)
    self.survey_group.put()

    record_values = {
        'grading_survey_group': self.survey_group,
        'mentor_record': self.grading_survey_record,
        'student_record': self.project_survey_record,
        'project': self.old_project,
        'grade_decision': 'pass'}
    self.grading_record = GradingRecord(**record_values)
    self.grading_record.put()

    self.project.passed_evaluations = [self.grading_record.key()]
    self.project.put()
    self.old_project.passed_evaluations = [self.grading_record.key()]
    self.old_project.put()

  def createGSoCGSurveyGroup(self):
    """Creates a GSoCSurveyGroup
    """
    values = {
        'name': 'Survey Group Name',
        'grading_survey': self.grading_survey,
        'student_survey': self.project_survey,
        'program': self.gsoc}
    self.gsoc_survey_group = GSoCGradingSurveyGroup(**values)
    self.gsoc_survey_group.put()

  def testConvertProjectSurveyRecords(self):
    """Test conversion of ProjectSurveyRecords.
    """
    self.assertEqual(GSoCProjectSurveyRecord.all().count(1), 0)

    response = self.post(self.PSR_URL, {})

    self.assertEqual(response.status_code, httplib.OK)
    self.assertTasksInQueue(1)
    self.assertTasksInQueue(n=1, url=self.PSR_URL)
    self.assertEqual(GSoCProjectSurveyRecord.all().count(2), 1)

    record = GSoCProjectSurveyRecord.all().get()
    self.assertEqual(record.user.key(), self.project_survey_record.user.key())

  def testConvertGradingProjectSurveyRecords(self):<commit_msg>Fix tests.app.soc.modules.gsoc.tasks.test_survey_conversion. A recent change in the mentor property has broken the tests. We will now use the data seeder to seed the properties and make it more robust to accommodate future changes.<commit_after>#!/usr/bin/env python2.5
#
# Copyright 2011 the Melange authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#   http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.


__authors__ = [
  '"<NAME>" <<EMAIL>>',
  ]


import httplib
import urllib

from tests.profile_utils import GSoCProfileHelper
from tests.test_utils import DjangoTestCase
from tests.test_utils import TaskQueueTestCase

from soc.modules.gsoc.models.grading_project_survey import GradingProjectSurvey
from soc.modules.gsoc.models.grading_project_survey_record import \
    GSoCGradingProjectSurveyRecord
from soc.modules.gsoc.models.grading_project_survey_record import \
    GradingProjectSurveyRecord
from soc.modules.gsoc.models.grading_record import GSoCGradingRecord
from soc.modules.gsoc.models.grading_record import GradingRecord
from soc.modules.gsoc.models.grading_survey_group import GradingSurveyGroup
from soc.modules.gsoc.models.grading_survey_group import GSoCGradingSurveyGroup
from soc.modules.gsoc.models.student_project import StudentProject
from soc.modules.gsoc.models.project import GSoCProject
from soc.modules.gsoc.models.project_survey import ProjectSurvey
from soc.modules.gsoc.models.project_survey_record import \
    GSoCProjectSurveyRecord
from soc.modules.gsoc.models.project_survey_record import ProjectSurveyRecord

from soc.modules.seeder.logic.seeder import logic as seeder_logic

class SurveyConversionTest(DjangoTestCase, TaskQueueTestCase):
  """Test the code that converts the old surveys.
  """

  PSR_URL = '/tasks/gsoc/convert_surveys/project_survey_record'
  GPSR_URL = '/tasks/gsoc/convert_surveys/grading_project_survey_record'
  GSG_URL = '/tasks/gsoc/convert_surveys/grading_survey_group'
  GR_URL = '/tasks/gsoc/convert_surveys/grading_record'


  def setUp(self):
    super(SurveyConversionTest, self).setUp()
    self.init()
    self.createMentor()
    self.createStudent()
    self.createSurveys()

  def createMentor(self):
    """Creates a new mentor.
    """
    profile_helper = GSoCProfileHelper(self.gsoc, self.dev_test)
    profile_helper.createOtherUser('<EMAIL>')
    self.mentor = profile_helper.createMentor(self.org)

  def createStudent(self):
    """Creates a Student with a project.
    """
    profile_helper = GSoCProfileHelper(self.gsoc, self.dev_test)
    profile_helper.createOtherUser('<EMAIL>')
    self.student = profile_helper.createStudentWithProject(self.org,
                                                            self.mentor)
    self.project = GSoCProject.all().ancestor(self.student).get()

    values = {
        'link_id': 'link_id',
        'scope': self.org,
        'scope_path': self.org.key().id_or_name(),
        'student': self.student}
    for prop in self.project.properties().keys():
      values[prop] = getattr(self.project, prop)
    self.old_project = seeder_logic.seed(StudentProject, values)

  def createSurveys(self):
    """Creates the surveys and records required for the tests in the old
    format.
    """
    survey_values = {
        'author': self.founder,
        'title': 'Title',
        'modified_by': self.founder,
        'link_id': 'link_id',
        'scope': self.gsoc,
        'scope_path': self.gsoc.key().id_or_name()}

    self.project_survey = seeder_logic.seed(ProjectSurvey, survey_values)

    record_values = {
        'user': self.student.user,
        'org': self.org,
        'project': self.old_project,
        'survey': self.project_survey}
    self.project_survey_record = seeder_logic.seed(ProjectSurveyRecord,
                                                   record_values)

    self.grading_survey = seeder_logic.seed(GradingProjectSurvey, survey_values)

    record_values = {
        'user': self.student.user,
        'org': self.org,
        'project': self.old_project,
        'survey': self.grading_survey,
        'grade': True}
    self.grading_survey_record = seeder_logic.seed(GradingProjectSurveyRecord,
                                                   record_values)

    group_values = {
        'name': 'Survey Group Name',
        'grading_survey': self.grading_survey,
        'student_survey': self.project_survey,
        'link_id': 'link_id',
        'scope': self.gsoc,
        'scope_path': self.gsoc.key().id_or_name()}
    self.survey_group = seeder_logic.seed(GradingSurveyGroup, group_values)

    record_values = {
        'grading_survey_group': self.survey_group,
        'mentor_record': self.grading_survey_record,
        'student_record': self.project_survey_record,
        'project': self.old_project,
        'grade_decision': 'pass'}
    self.grading_record = seeder_logic.seed(GradingRecord, record_values)

    self.project.passed_evaluations = [self.grading_record.key()]
    self.project.put()
    self.old_project.passed_evaluations = [self.grading_record.key()]
    self.old_project.put()

  def createGSoCGSurveyGroup(self):
    """Creates a GSoCSurveyGroup
    """
    values = {
        'name': 'Survey Group Name',
        'grading_survey': self.grading_survey,
        'student_survey': self.project_survey,
        'program': self.gsoc}
    self.gsoc_survey_group = seeder_logic.seed(GSoCGradingSurveyGroup, values)

  def testConvertProjectSurveyRecords(self):
    """Test conversion of ProjectSurveyRecords.
    """
    self.assertEqual(GSoCProjectSurveyRecord.all().count(1), 0)

    response = self.post(self.PSR_URL, {})

    self.assertEqual(response.status_code, httplib.OK)
    self.assertTasksInQueue(1)
    self.assertTasksInQueue(n=1, url=self.PSR_URL)
    self.assertEqual(GSoCProjectSurveyRecord.all().count(2), 1)

    record = GSoCProjectSurveyRecord.all().get()
    self.assertEqual(record.user.key(), self.project_survey_record.user.key())

  def testConvertGradingProjectSurveyRecords(self):
{'fraction_non_alphanumeric': 0.10558271565999054, 'fraction_numerical': 0.0025232613152499604, 'mean_word_length': 3.7183779761904763, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 8, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6099937', 'n_tokens_mistral': 4052, 'n_tokens_neox': 4000, 'n_words': 773}