blob: 2d2e3e2e31f84e5ab85969a56076f3b7ed04999d [file] [log] [blame]
# Copyright 2014 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 profile logic."""
import unittest
from melange.models import profile as profile_model
from summerofcode.logic import profile as profile_logic
from summerofcode.models import proposal as proposal_model
from tests import org_utils
from tests import profile_utils
from tests import program_utils
from tests.utils import proposal_utils
class HasProjectTest(unittest.TestCase):
"""Unit tests for hasProject function."""
def setUp(self):
"""See unittest.TestCase.setUp for specification."""
program = program_utils.seedProgram()
self.profile = profile_utils.seedSOCStudent(program)
def testForStudentWithNoProjects(self):
"""Tests for student with no projects."""
self.profile.student_data.number_of_projects = 0
has_project = profile_logic.hasProject(self.profile)
self.assertFalse(has_project)
def testForStudentWithOneProjects(self):
"""Tests for student with one project."""
self.profile.student_data.number_of_projects = 1
has_project = profile_logic.hasProject(self.profile)
self.assertTrue(has_project)
class GetProfilesForNewProposalNotificationTest(unittest.TestCase):
"""Unit tests for getProfilesForNewProposalNotification class."""
def setUp(self):
"""See unittest.TestCase.setUp for specification."""
self.program = program_utils.seedProgram()
self.org = org_utils.seedOrganization(self.program.key())
def testNonMentorNotReturned(self):
"""Tests that a profile with no mentor role for the org is not returned."""
other_org = org_utils.seedOrganization(self.program.key())
profile_utils.seedNDBProfile(
self.program.key(), mentor_for=[other_org.key])
profiles = profile_logic.getProfilesForNewProposalNotification(self.org.key)
self.assertListEqual(profiles, [])
def testOrgMembersWithEnabledNotificationsReturned(self):
"""Tests that profiles with roles for the org are returned."""
mentor = profile_utils.seedNDBProfile(
self.program.key(), mentor_for=[self.org.key])
admin = profile_utils.seedNDBProfile(
self.program.key(), admin_for=[self.org.key])
profiles = profile_logic.getProfilesForNewProposalNotification(self.org.key)
self.assertIn(mentor.key, [profile.key for profile in profiles])
self.assertIn(admin.key, [profile.key for profile in profiles])
def testOrgMemersWithDisabledNotificationsNotReturned(self):
"""Tests that a org members who disabled notifications is not returned."""
notification_settings = profile_model.NotificationSettings(
org_new_proposals=False)
profile_utils.seedNDBProfile(
self.program.key(), mentor_for=[self.org.key],
notification_settings=notification_settings)
profile_utils.seedNDBProfile(
self.program.key(), mentor_for=[self.org.key],
notification_settings=notification_settings)
profiles = profile_logic.getProfilesForNewProposalNotification(self.org.key)
self.assertListEqual(profiles, [])
_TEST_NUMBER_OF_ORGS = 4
_TEST_SLOT_ALLOCATION = 3
_TEST_HIGH_SCORE = 4
_TEST_LOW_SCORE = 2
class SetToBeAcceptedProposalsTest(unittest.TestCase):
"""Unit tests for setToBeAcceptedProposals function."""
def setUp(self):
"""See unittest.TestCase.setUp for specification."""
self.program = program_utils.seedProgram()
self.student = profile_utils.seedNDBStudent(self.program)
self.orgs = []
for _ in range(_TEST_NUMBER_OF_ORGS):
self.orgs.append(
org_utils.seedSOCOrganization(
self.program.key(), slot_allocation=_TEST_SLOT_ALLOCATION))
# seed _TEST_SLOT_ALLOCATION - 1 to-be-accepted proposals for each org
for org in self.orgs:
mentor = profile_utils.seedNDBProfile(
self.program.key(), mentor_for=[org.key])
for _ in range(_TEST_SLOT_ALLOCATION - 1):
student = profile_utils.seedNDBStudent(self.program)
score = proposal_model.Score(author=mentor.key, value=_TEST_HIGH_SCORE)
proposal_utils.seedNDBProposal(
student.key, self.program.key(), org_key=org.key,
mentor_key=mentor.key, scores=[score], accept_as_project=True,
status=proposal_model.Status.PENDING)
def testNoProposals(self):
"""Tests that the property is set to false for no proposals."""
profile_logic.setToBeAcceptedProposals(self.student.key)
student = self.student.key.get()
self.assertFalse(student.student_data.has_duplicates)
def testOneProposalToAccept(self):
"""Tests that the property is set to false for one proposal to accept."""
# seed one to-be-accepted proposal for the student
mentor = profile_utils.seedNDBProfile(
self.program.key(), mentor_for=[self.orgs[0].key])
score = proposal_model.Score(author=mentor.key, value=_TEST_HIGH_SCORE)
proposal_utils.seedNDBProposal(
self.student.key, self.program.key(), org_key=self.orgs[0].key,
mentor_key=mentor.key, scores=[score], accept_as_project=True,
status=proposal_model.Status.PENDING)
# check the there are not duplicates
profile_logic.setToBeAcceptedProposals(self.student.key)
student = self.student.key.get()
self.assertFalse(student.student_data.has_duplicates)
# seed to-be-rejected proposals to all organizations
for org in self.orgs:
proposal_utils.seedNDBProposal(
self.student.key, self.program.key(), org_key=org.key,
mentor_key=mentor.key, accept_as_project=False,
status=proposal_model.Status.PENDING)
# check the there are not duplicates
profile_logic.setToBeAcceptedProposals(self.student.key)
student = self.student.key.get()
self.assertFalse(student.student_data.has_duplicates)
# seed one more to-be-accepted proposals to all organization
# one by another student (with higher score) and one by the student
# (with lower score); there are still no duplicates because there is
# no enough allocation to accept student's proposals
for org in self.orgs:
mentor = profile_utils.seedNDBProfile(
self.program.key(), mentor_for=[org.key])
student = profile_utils.seedNDBStudent(self.program)
score = proposal_model.Score(author=mentor.key, value=_TEST_HIGH_SCORE)
proposal_utils.seedNDBProposal(
student.key, self.program.key(), org_key=org.key,
mentor_key=mentor.key, scores=[score], accept_as_project=True,
status=proposal_model.Status.PENDING)
score = proposal_model.Score(author=mentor.key, value=_TEST_LOW_SCORE)
proposal_utils.seedNDBProposal(
self.student.key, self.program.key(), org_key=org.key,
mentor_key=mentor.key, scores=[score], accept_as_project=True,
status=proposal_model.Status.PENDING)
# check the there are not duplicates
profile_logic.setToBeAcceptedProposals(self.student.key)
student = self.student.key.get()
self.assertFalse(student.student_data.has_duplicates)
def testTwoProposalsToAccept(self):
"""Tests that the property is set to true for two proposals to accept."""
# seed two to-be-accepted proposal for the students
mentor = profile_utils.seedNDBProfile(
self.program.key(), mentor_for=[self.orgs[0].key, self.orgs[-1].key])
score = proposal_model.Score(author=mentor.key, value=_TEST_HIGH_SCORE)
proposal_utils.seedNDBProposal(
self.student.key, self.program.key(), org_key=self.orgs[0].key,
mentor_key=mentor.key, scores=[score], accept_as_project=True,
status=proposal_model.Status.PENDING)
proposal_utils.seedNDBProposal(
self.student.key, self.program.key(), org_key=self.orgs[-1].key,
mentor_key=mentor.key, scores=[score], accept_as_project=True,
status=proposal_model.Status.PENDING)
# check the there are duplicates
profile_logic.setToBeAcceptedProposals(self.student.key)
student = self.student.key.get()
self.assertTrue(student.student_data.has_duplicates)
def testTwoProposalsForTheSameOrgToAccept(self):
"""Tests that the property is set to true for two proposals to accept."""
# increase slot allocation for one organization
self.orgs[0].slot_allocation = 3 * _TEST_SLOT_ALLOCATION
self.orgs[0].put()
# seed two to-be-accepted proposal for the students
mentor = profile_utils.seedNDBProfile(
self.program.key(), mentor_for=[self.orgs[0].key, self.orgs[-1].key])
score = proposal_model.Score(author=mentor.key, value=_TEST_HIGH_SCORE)
proposal_utils.seedNDBProposal(
self.student.key, self.program.key(), org_key=self.orgs[0].key,
mentor_key=mentor.key, scores=[score], accept_as_project=True,
status=proposal_model.Status.PENDING)
proposal_utils.seedNDBProposal(
self.student.key, self.program.key(), org_key=self.orgs[0].key,
mentor_key=mentor.key, scores=[score], accept_as_project=True,
status=proposal_model.Status.PENDING)
# check the there are duplicates
profile_logic.setToBeAcceptedProposals(self.student.key)
student = self.student.key.get()
self.assertTrue(student.student_data.has_duplicates)
def testManyProposalsToAccept(self):
"""Tests that the property is set to true for two proposals to accept."""
for org in self.orgs:
mentor = profile_utils.seedNDBProfile(
self.program.key(), mentor_for=[org.key])
score = proposal_model.Score(author=mentor.key, value=_TEST_HIGH_SCORE)
proposal_utils.seedNDBProposal(
self.student.key, self.program.key(), org_key=org.key,
mentor_key=mentor.key, scores=[score], accept_as_project=True,
status=proposal_model.Status.PENDING)
# check the there are duplicates
profile_logic.setToBeAcceptedProposals(self.student.key)
student = self.student.key.get()
self.assertTrue(student.student_data.has_duplicates)
_TEST_NUMBER_OF_STUDENTS = 3
class GetProfilesWithDuplicatesTest(unittest.TestCase):
"""Unit tests for getProfilesWithDuplicates function."""
def setUp(self):
"""See unittest.TestCase.setUp for specification."""
self.program = program_utils.seedProgram()
def testProfilesWithDuplicatesReturned(self):
"""Tests that only students who have duplicates are returned."""
# seed students with duplicates
expected = set()
for _ in range(_TEST_NUMBER_OF_STUDENTS):
student = profile_utils.seedNDBStudent(self.program)
# seed two proposals for the student
proposal = proposal_utils.seedNDBProposal(
student.key, self.program.key(), accept_as_project=True)
student.student_data.to_be_accepted_proposals.append(proposal.key)
proposal = proposal_utils.seedNDBProposal(
student.key, self.program.key(), accept_as_project=True)
student.student_data.to_be_accepted_proposals.append(proposal.key)
student.put()
expected.add(student.key)
# seed students with no duplicates
for _ in range(_TEST_NUMBER_OF_STUDENTS):
profile_utils.seedNDBStudent(self.program)
actual = set(
profile.key for profile
in profile_logic.getProfilesWithDuplicates(self.program.key()))
self.assertSetEqual(actual, expected)