<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.

"""Tests for withdraw projects view.
"""


from tests.profile_utils import GSoCProfileHelper
from tests.test_utils import GSoCDjangoTestCase


class WithdrawProjectsTest(GSoCDjangoTestCase):
  """Test withdraw projects page
  """

  def setUp(self):
    self.init()

  def assertWithdrawProjects(self, response):
    """Asserts that all templates from the withdraw projects page were used
    and all contexts were passed
    """
    self.assertTrue('base_layout' in response.context)
    self.assertGSoCTemplatesUsed(response)
    self.assertEqual(response.context['base_layout'],
        'v2/modules/gsoc/base.html')

    self.assertTemplateUsed(response,
        'v2/modules/gsoc/accept_withdraw_projects/base.html')
    self.assertTemplateUsed(response,
        'v2/modules/gsoc/accept_withdraw_projects/base.html')

  def testWithdrawProjects(self):
    self.data.createHost()
    self.timeline.studentsAnnounced()

    url = '/gsoc/withdraw_projects/' + self.gsoc.key().name()
    response = self.get(url)

    self.assertWithdrawProjects(response)

    # list response without any projects
    response = self.getListResponse(url, 0)
    self.assertIsJsonResponse(response)
    data = response.context['data']['']
    self.assertEqual(0, len(data))

    # list response with projects
    mentor_profile_helper = GSoCProfileHelper(self.gsoc, self.dev_test)
    mentor_profile_helper.createOtherUser('<EMAIL>')
    mentor = mentor_profile_helper.createMentor(self.org)
    self.data.createStudentWithProposal(self.org, mentor)
    self.data.createStudentWithProject(self.org, mentor)

    response = self.getListResponse(url, 0)
    self.assertIsJsonResponse(response)
    data = response.context['data']['']
    self.assertEqual(1, len(data))
<commit_msg>Add tests to ensure withdrawn projects have all the entity properties updated as expected.<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.

"""Tests for withdraw projects view.
"""


from google.appengine.ext import db

from django.utils import simplejson

from soc.modules.gsoc.models import project as project_model
from soc.modules.gsoc.models import proposal as proposal_model

from tests.profile_utils import GSoCProfileHelper
from tests.test_utils import GSoCDjangoTestCase


class WithdrawProjectsTest(GSoCDjangoTestCase):
  """Test withdraw projects page
  """

  def setUp(self):
    self.init()

  def assertWithdrawProjects(self, response):
    """Asserts that all templates from the withdraw projects page were used
    and all contexts were passed
    """
    self.assertTrue('base_layout' in response.context)
    self.assertGSoCTemplatesUsed(response)
    self.assertEqual(response.context['base_layout'],
        'v2/modules/gsoc/base.html')

    self.assertTemplateUsed(response,
        'v2/modules/gsoc/accept_withdraw_projects/base.html')
    self.assertTemplateUsed(response,
        'v2/modules/gsoc/accept_withdraw_projects/base.html')

  def testWithdrawProjects(self):
    self.data.createHost()
    self.timeline.studentsAnnounced()

    url = '/gsoc/withdraw_projects/' + self.gsoc.key().name()
    response = self.get(url)

    self.assertWithdrawProjects(response)

    # list response without any projects
    response = self.getListResponse(url, 0)
    self.assertIsJsonResponse(response)
    data = response.context['data']['']
    self.assertEqual(0, len(data))

    # list response with projects
    mentor_profile_helper = GSoCProfileHelper(self.gsoc, self.dev_test)
    mentor_profile_helper.createOtherUser('<EMAIL>')
    mentor = mentor_profile_helper.createMentor(self.org)
    self.data.createStudentWithProposal(self.org, mentor)
    self.data.createStudentWithProject(self.org, mentor)

    response = self.getListResponse(url, 0)
    self.assertIsJsonResponse(response)
    data = response.context['data']['']
    self.assertEqual(1, len(data))

  def testWithdrawProject(self):
    """Test if withdrawing a project updates all the datastore properties."""
    self.data.createHost()
    self.timeline.studentsAnnounced()

    # list response with projects
    mentor_profile_helper = GSoCProfileHelper(self.gsoc, self.dev_test)
    mentor_profile_helper.createOtherUser('<EMAIL>')
    mentor = mentor_profile_helper.createMentor(self.org)
    self.data.createStudentWithProposal(self.org, mentor)
    student = self.data.createStudentWithProject(self.org, mentor)
    student_key = student.key()
    orig_number_of_projects = student.student_info.number_of_projects
    orig_project_for_orgs = student.student_info.project_for_orgs

    project_q = project_model.GSoCProject.all().ancestor(student)
    project = project_q.get()

    data_payload = [{
        'full_project_key': str(project.key()),
        }
    ]

    json_data = simplejson.dumps(data_payload)

    postdata = {
        'data': json_data,
        'button_id': 'withdraw'
        }

    list_idx = 0
    url = '/gsoc/withdraw_projects/%s?fmt=json&marker=1&&idx=%s' % (
        self.gsoc.key().name(), str(list_idx))

    response = self.post(url, postdata)
    self.assertResponseOK(response)

    student = db.get(student_key)
    project_q = project_model.GSoCProject.all().ancestor(student)
    project = project_q.get()

    proposal_q = proposal_model.GSoCProposal.all().ancestor(student)
    proposal = proposal_q.get()

    self.assertEqual(project.status, 'withdrawn')
    self.assertEqual(proposal.status, 'withdrawn')

    self.assertEqual(student.student_info.number_of_projects,
                     orig_number_of_projects - 1)

    orig_project_for_orgs.remove(self.org.key())
    self.assertEqual(list(student.student_info.project_for_orgs),
                     orig_project_for_orgs)

{'fraction_non_alphanumeric': 0.10081466395112017, 'fraction_numerical': 0.005382601105615362, 'mean_word_length': 4.126771066368382, 'pattern_counts': {'":': 0, '<': 6, '<?xml version=': 0, '>': 6, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2273531', 'n_tokens_mistral': 2084, 'n_tokens_neox': 2019, 'n_words': 474}