<commit_before># Copyright 2013 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.

"""Unit tests for project related logic."""

import unittest

from soc.modules.gsoc.logic import project as project_logic

from soc.modules.gsoc.models import profile as profile_model
from soc.modules.gsoc.models import project as project_model

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

from tests import org_utils
from tests import program_utils


class HasMentorProjectAssignedTest(unittest.TestCase):
  """Unit tests for hasMentorProjectAssigned function."""

  def setUp(self):
    # seed a new program
    self.program = program_utils.seedGSoCProgram()

    # seed a couple of organizations
    self.organization_one = org_utils.seedSOCOrganization(self.program.key())
    self.organization_two = org_utils.seedSOCOrganization(self.program.key())

    # seed a couple of projects for the organizations
    self.project_one = seeder_logic.seed(project_model.GSoCProject,
        {'scope': self.program, 'org': self.organization_one.key.to_old_key()})

    # seed a new mentor for organization one, but without projects
    mentor_properties = {
        'is_mentor': True,
        'mentor_for': [self.organization_one.key.to_old_key()],
        'is_org_admin': False,
        'org_admin_for': [],
        'status': 'active',
    }
    self.mentor = seeder_logic.seed(
        profile_model.GSoCProfile, mentor_properties)

  def testMentorWithoutProjects(self):
    has_projects = project_logic.hasMentorProjectAssigned(self.mentor)
    self.assertFalse(has_projects)

    has_projects = project_logic.hasMentorProjectAssigned(
        self.mentor, org_key=self.organization_one.key.to_old_key())
    self.assertFalse(has_projects)

  def testMentorWithProject(self):
    # assign one project to the mentor
    self.project_one.mentors = [self.mentor.key()]
    self.project_one.put()

    # the mentor has a project
    has_projects = project_logic.hasMentorProjectAssigned(self.mentor)
    self.assertTrue(has_projects)

    # the mentor has a project for organization one
    has_projects = project_logic.hasMentorProjectAssigned(
        self.mentor, org_key=self.organization_one.key.to_old_key())
    self.assertTrue(has_projects)

    # the mentor still does not have projects for organization two
    has_projects = project_logic.hasMentorProjectAssigned(
        self.mentor, org_key=self.organization_two.key.to_old_key())
    self.assertFalse(has_projects)

  def testProjectWithMoreMentors(self):
    # seed a few extra mentors for the same project
    mentor_properties = {
        'is_mentor': True,
        'mentor_for': [self.organization_one.key.to_old_key()],
        'is_org_admin': False,
        'org_admin_for': [],
        'status': 'active',
    }
    for _ in range(5):
      other_mentor = seeder_logic.seed(
          profile_model.GSoCProfile, mentor_properties)
      self.project_one.mentors.append(other_mentor.key())
    self.project_one.put()

    # assign our mentor to the project too
    self.project_one.mentors = [self.mentor.key()]
    self.project_one.put()

    # the mentor has a project
    has_projects = project_logic.hasMentorProjectAssigned(self.mentor)
    self.assertTrue(has_projects)

    # the mentor has a project for organization one
    has_projects = project_logic.hasMentorProjectAssigned(
        self.mentor, org_key=self.organization_one.key.to_old_key())
    self.assertTrue(has_projects)

    # the mentor still does not have projects for organization two
    has_projects = project_logic.hasMentorProjectAssigned(
        self.mentor, org_key=self.organization_two.key.to_old_key())
    self.assertFalse(has_projects)

  def testMentorWithMoreProjects(self):
    # seed a few extra projects and assign our mentor
    project_properties = {
        'scope': self.program,
        'org': self.organization_one.key.to_old_key(),
        'mentors': [self.mentor.key()]
        }
    for _ in range(5):
      seeder_logic.seed(project_model.GSoCProject, project_properties)

    # the mentor has projects for organization one
    has_projects = project_logic.hasMentorProjectAssigned(
        self.mentor, org_key=self.organization_one.key.to_old_key())
    self.assertTrue(has_projects)

  def testMentorWithProjectForOtherOrg(self):
    # set our profile a mentor for organization two
    self.mentor.mentor_for.append(self.organization_two.key.to_old_key())
    self.mentor.put()

    # seed a project for organization two
    project_properties = {
        'scope': self.program,
        'org': self.organization_two.key.to_old_key(),
        'mentors': [self.mentor.key()]
        }
    seeder_logic.seed(project_model.GSoCProject, project_properties)

    # the mentor has a project
    has_projects = project_logic.hasMentorProjectAssigned(self.mentor)
    self.assertTrue(has_projects)

    # the mentor has only a project for organization two
    has_projects = project_logic.hasMentorProjectAssigned(
        self.mentor, org_key=self.organization_one.key.to_old_key())
    self.assertFalse(has_projects)<commit_msg>Unit tests for hasMentorProjectAssigned function are fixed.<commit_after># Copyright 2013 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.

"""Unit tests for project related logic."""

import unittest

from soc.modules.gsoc.logic import project as project_logic

from tests import org_utils
from tests import profile_utils
from tests import program_utils
from tests.utils import project_utils


class HasMentorProjectAssignedTest(unittest.TestCase):
  """Unit tests for hasMentorProjectAssigned function."""

  def setUp(self):
    # seed a new program
    self.program = program_utils.seedGSoCProgram()

    # seed a couple of organizations
    self.organization_one = org_utils.seedSOCOrganization(self.program.key())
    self.organization_two = org_utils.seedSOCOrganization(self.program.key())

    # seed a project for the organization one
    student = profile_utils.seedNDBStudent(self.program)
    self.project_one = project_utils.seedProject(
        student, self.program.key(), org_key=self.organization_one.key)
    self.project_one.mentors = []
    self.project_one.put()

    # seed a new mentor for organization one, but without projects
    self.mentor = profile_utils.seedNDBProfile(
        self.program.key(), mentor_for=[self.organization_one.key])

  def testMentorWithoutProjects(self):
    """Tests for a mentor with no projects."""
    has_projects = project_logic.hasMentorProjectAssigned(self.mentor)
    self.assertFalse(has_projects)

    has_projects = project_logic.hasMentorProjectAssigned(
        self.mentor, org_key=self.organization_one.key)
    self.assertFalse(has_projects)

  def testMentorWithProject(self):
    """Tests for a mentor with one project for one organization."""
    # assign one project to the mentor
    self.project_one.mentors = [self.mentor.key.to_old_key()]
    self.project_one.put()

    # the mentor has a project
    has_projects = project_logic.hasMentorProjectAssigned(self.mentor)
    self.assertTrue(has_projects)

    # the mentor has a project for organization one
    has_projects = project_logic.hasMentorProjectAssigned(
        self.mentor, org_key=self.organization_one.key)
    self.assertTrue(has_projects)

    # the mentor still does not have projects for organization two
    has_projects = project_logic.hasMentorProjectAssigned(
        self.mentor, org_key=self.organization_two.key)
    self.assertFalse(has_projects)

  def testProjectWithMoreMentors(self):
    """Tests for a mentor for project that have more mentors."""
    # seed a few extra mentors for the same project
    for _ in range(5):
      other_mentor = profile_utils.seedNDBProfile(
          self.program.key(), mentor_for=[self.organization_one.key])
      self.project_one.mentors.append(other_mentor.key.to_old_key())
    self.project_one.put()

    # assign our mentor to the project too
    self.project_one.mentors.append(self.mentor.key.to_old_key())
    self.project_one.put()

    # the mentor has a project
    has_projects = project_logic.hasMentorProjectAssigned(self.mentor)
    self.assertTrue(has_projects)

    # the mentor has a project for organization one
    has_projects = project_logic.hasMentorProjectAssigned(
        self.mentor, org_key=self.organization_one.key)
    self.assertTrue(has_projects)

    # the mentor still does not have projects for organization two
    has_projects = project_logic.hasMentorProjectAssigned(
        self.mentor, org_key=self.organization_two.key)
    self.assertFalse(has_projects)

  def testMentorWithMoreProjects(self):
    """Tests for a mentor with more projects."""
    # seed a few extra projects and assign our mentor
    for _ in range(5):
      student = profile_utils.seedNDBStudent(self.program)
      self.project_one = project_utils.seedProject(
          student, self.program.key(), org_key=self.organization_one.key,
          mentor_key=self.mentor.key)

    # the mentor has projects for organization one
    has_projects = project_logic.hasMentorProjectAssigned(
        self.mentor, org_key=self.organization_one.key)
    self.assertTrue(has_projects)

  def testMentorWithProjectForOtherOrg(self):
    """Tests for a mentor who has a project for another organization."""
    # set our profile a mentor for organization two
    self.mentor.mentor_for.append(self.organization_two.key)
    self.mentor.put()

    # seed a project for organization two
    student = profile_utils.seedNDBStudent(self.program)
    project_utils.seedProject(
        student, self.program.key(), org_key=self.organization_two.key,
        mentor_key=self.mentor.key)

    # the mentor has a project
    has_projects = project_logic.hasMentorProjectAssigned(self.mentor)
    self.assertTrue(has_projects)

    # the mentor has only a project for organization two
    has_projects = project_logic.hasMentorProjectAssigned(
        self.mentor, org_key=self.organization_one.key)
    self.assertFalse(has_projects)
{'fraction_non_alphanumeric': 0.08398190045248868, 'fraction_numerical': 0.0018099547511312218, 'mean_word_length': 3.9423076923076925, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '5037639', 'n_tokens_mistral': 3390, 'n_tokens_neox': 3245, 'n_words': 824}