blob: 42d8075374b500f83a0aa476348b16dcc6d0a9d7 [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 project logic."""
import unittest
from google.appengine.ext import ndb
from summerofcode.logic import project as project_logic
from summerofcode.models import project as project_model
from tests import org_utils
from tests import profile_utils
from tests import program_utils
from tests.utils import project_utils
_TEST_NUMER_OF_PROJECTS = 3
class QueryAcceptedProjectsTest(unittest.TestCase):
"""Unit tests for queryAcceptedProjects function."""
def setUp(self):
"""See unittest.UnitTest.setUp for specification."""
self.program = program_utils.seedGSoCProgram()
# seed a few accepted, withdrawn and failed projects
self.accepted_project_keys = set()
for _ in range(_TEST_NUMER_OF_PROJECTS):
student = profile_utils.seedNDBStudent(self.program)
self.accepted_project_keys.add(
project_utils.seedNDBProject(student, self.program.key()).key)
student = profile_utils.seedNDBStudent(self.program)
project_utils.seedNDBProject(
student, self.program.key(), status=project_model.Status.WITHDRAWN)
student = profile_utils.seedNDBStudent(self.program)
project_utils.seedNDBProject(
student, self.program.key(), status=project_model.Status.FAILED)
# seed a few accepted projects for another program
other_program = program_utils.seedGSoCProgram()
for _ in range(_TEST_NUMER_OF_PROJECTS):
student = profile_utils.seedNDBStudent(other_program)
project_utils.seedNDBProject(student, other_program.key())
def testsFetchedProjects(self):
"""Tests that only accepted projects for the program are fetched."""
query = project_logic.queryAcceptedProjects(self.program.key())
project_keys = query.fetch(1000, keys_only=True)
self.assertSetEqual(set(project_keys), self.accepted_project_keys)
class QueryAllProjectsTest(unittest.TestCase):
"""Unit tests for queryAllProjects function."""
def setUp(self):
"""See unittest.UnitTest.setUp for specification."""
self.program = program_utils.seedGSoCProgram()
# seed a few accepted, withdrawn and failed projects
self.accepted_project_keys = set()
for _ in range(_TEST_NUMER_OF_PROJECTS):
student = profile_utils.seedNDBStudent(self.program)
self.accepted_project_keys.add(
project_utils.seedNDBProject(student, self.program.key()).key)
student = profile_utils.seedNDBStudent(self.program)
self.accepted_project_keys.add(
project_utils.seedNDBProject(
student, self.program.key(),
status=project_model.Status.WITHDRAWN).key)
student = profile_utils.seedNDBStudent(self.program)
self.accepted_project_keys.add(
project_utils.seedNDBProject(
student, self.program.key(),
status=project_model.Status.FAILED).key)
# seed a few accepted projects for another program
other_program = program_utils.seedGSoCProgram()
for _ in range(_TEST_NUMER_OF_PROJECTS):
student = profile_utils.seedNDBStudent(other_program)
project_utils.seedNDBProject(student, other_program.key())
def testsFetchedProjects(self):
"""Tests that only accepted projects for the program are fetched."""
query = project_logic.queryAllProjects(self.program.key())
project_keys = query.fetch(1000, keys_only=True)
self.assertSetEqual(set(project_keys), self.accepted_project_keys)
class QueryAcceptedProjectsForOrgTest(unittest.TestCase):
"""Unit tests for queryAcceptedProjectsForOrg function."""
def setUp(self):
"""See unittest.UnitTest.setUp for specification."""
program = program_utils.seedGSoCProgram()
self.org = org_utils.seedSOCOrganization(program.key())
# seed a few accepted, withdrawn and failed projects for the organization
self.accepted_project_keys = set()
for _ in range(_TEST_NUMER_OF_PROJECTS):
student = profile_utils.seedNDBStudent(program)
self.accepted_project_keys.add(
project_utils.seedNDBProject(
student, program.key(), org_key=self.org.key).key)
student = profile_utils.seedNDBStudent(program)
project_utils.seedNDBProject(
student, program.key(), status=project_model.Status.WITHDRAWN,
org_key=self.org.key)
student = profile_utils.seedNDBStudent(program)
project_utils.seedNDBProject(
student, program.key(), status=project_model.Status.FAILED,
org_key=self.org.key)
# seed a few accepted projects for other organizations
for _ in range(_TEST_NUMER_OF_PROJECTS):
student = profile_utils.seedNDBStudent(program)
project_utils.seedNDBProject(student, program.key())
def testsFetchedProjects(self):
"""Tests that only accepted projects for the organization are fetched."""
query = project_logic.queryAcceptedProjectsForOrg(self.org.key)
project_keys = query.fetch(1000, keys_only=True)
self.assertSetEqual(set(project_keys), self.accepted_project_keys)
class QueryAllProjectsForStudentTest(unittest.TestCase):
"""Unit tests for queryAllProjectsForStudent function."""
def setUp(self):
"""See unittest.UnitTest.setUp for specification."""
program = program_utils.seedGSoCProgram()
self.student = profile_utils.seedNDBStudent(program)
# seed a few accepted, withdrawn and failed projects for the student
self.accepted_project_keys = set()
for _ in range(_TEST_NUMER_OF_PROJECTS):
self.accepted_project_keys.add(
project_utils.seedNDBProject(self.student, program.key()).key)
self.accepted_project_keys.add(
project_utils.seedNDBProject(
self.student, program.key(),
status=project_model.Status.WITHDRAWN).key)
self.accepted_project_keys.add(
project_utils.seedNDBProject(
self.student, program.key(),
status=project_model.Status.FAILED).key)
# seed a few accepted projects for other students
for _ in range(_TEST_NUMER_OF_PROJECTS):
student = profile_utils.seedNDBStudent(program)
project_utils.seedNDBProject(student, program.key())
def testsFetchedProjects(self):
"""Tests that only accepted projects for the organization are fetched."""
query = project_logic.queryAllProjectsForStudent(self.student.key)
project_keys = query.fetch(1000, keys_only=True)
self.assertSetEqual(set(project_keys), self.accepted_project_keys)
class queryAllProjectsForOrgMemberTest(unittest.TestCase):
"""Unit tests for queryAllProjectsForOrgMember function."""
def setUp(self):
"""See unittest.UnitTest.setUp for specification."""
program = program_utils.seedGSoCProgram()
org = org_utils.seedSOCOrganization(program.key())
self.mentor = profile_utils.seedNDBProfile(
program.key(), mentor_for=[org.key])
# seed a few accepted, withdrawn and failed projects for the mentor
self.accepted_project_keys = set()
for _ in range(_TEST_NUMER_OF_PROJECTS):
student = profile_utils.seedNDBStudent(program)
self.accepted_project_keys.add(
project_utils.seedNDBProject(
student, program.key(), org_key=org.key,
mentor_key=self.mentor.key).key)
self.accepted_project_keys.add(
project_utils.seedNDBProject(
student, program.key(), mentor_key=self.mentor.key,
status=project_model.Status.WITHDRAWN, org_key=org.key).key)
self.accepted_project_keys.add(
project_utils.seedNDBProject(
student, program.key(), mentor_key=self.mentor.key,
status=project_model.Status.FAILED, org_key=org.key).key)
# seed a few accepted projects for other mentors
for _ in range(_TEST_NUMER_OF_PROJECTS):
student = profile_utils.seedNDBStudent(program)
project_utils.seedNDBProject(student, program.key(), org_key=org.key)
def testsFetchedProjects(self):
"""Tests that only accepted projects for the organization are fetched."""
query = project_logic.queryAllProjectsForOrgMember(self.mentor.key)
project_keys = query.fetch(1000, keys_only=True)
self.assertSetEqual(set(project_keys), self.accepted_project_keys)
_OTHER_PROJECT_PROPERTIES = {
'title': u'Other Title',
'abstract': u'Other Abstract',
'content': u'Other Content',
'additional_info': u'http://www.other.additional.info.com'
}
class UpdateProjectTest(unittest.TestCase):
"""Unit tests for updateProject function."""
def setUp(self):
"""See unittest.TestCase.setUp for specification."""
self.program = program_utils.seedGSoCProgram()
self.student = profile_utils.seedSOCStudent(self.program)
self.org = org_utils.seedSOCOrganization(self.program.key())
self.project = project_utils.seedNDBProject(
self.student, self.program.key(), org_key=self.org.key)
def testProjectIsUpdated(self):
"""Tests that project is updated properly."""
# set expected properties, after the update
expected_properties = self.project.to_dict()
expected_properties.update(_OTHER_PROJECT_PROPERTIES)
result = project_logic.updateProject(
self.project.key, _OTHER_PROJECT_PROPERTIES)
self.assertTrue(result)
# check that updated properties are updated
project = self.project.key.get()
actual_properties = project.to_dict()
self.assertDictEqual(expected_properties, actual_properties)
def testIllegalProperty(self):
"""Tests that an error is raised when an illegal property is updated."""
# check for trying to change program
other_program = program_utils.seedGSoCProgram()
project_properties = _OTHER_PROJECT_PROPERTIES.copy()
project_properties['program'] = ndb.Key.from_old_key(other_program.key())
with self.assertRaises(ValueError):
project_logic.updateProject(
self.project.key, project_properties)
# check for trying to change organization
other_org = org_utils.seedSOCOrganization(self.program.key())
project_properties = _OTHER_PROJECT_PROPERTIES.copy()
project_properties['organization'] = other_org.key
with self.assertRaises(ValueError):
project_logic.updateProject(
self.project.key, project_properties)
class WithdrawProjectTest(unittest.TestCase):
"""Unit tests for withdrawProject function."""
def setUp(self):
"""See unittest.TestCase.setUp for specification."""
self.program = program_utils.seedGSoCProgram()
self.student = profile_utils.seedSOCStudent(self.program)
self.org = org_utils.seedSOCOrganization(self.program.key())
def testAcceptedProjectIsWithdrawn(self):
"""Tests that an accepted project is withdrawn properly."""
project = project_utils.seedNDBProject(
self.student, self.program.key(), org_key=self.org.key)
result = project_logic.withdrawProject(project.key)
self.assertTrue(result)
# check that the project is withdrawn
project = project.key.get()
self.assertEqual(project.status, project_model.Status.WITHDRAWN)
# check that the number of projects is updated
updated_student = self.student.key.get()
self.assertEqual(
updated_student.student_data.number_of_projects,
self.student.student_data.number_of_projects - 1)
def testWithdrawnProjectIsWithdrawn(self):
"""Tests that a withdrawn project stays withdrawn."""
project = project_utils.seedNDBProject(
self.student, self.program.key(), org_key=self.org.key,
status=project_model.Status.WITHDRAWN)
result = project_logic.withdrawProject(project.key)
self.assertTrue(result)
# check that the project is withdrawn
project = project.key.get()
self.assertEqual(project.status, project_model.Status.WITHDRAWN)
# check that the number of projects is not changed
updated_student = self.student.key.get()
self.assertEqual(
updated_student.student_data.number_of_projects,
self.student.student_data.number_of_projects)
def testFailedProjectIsNotWithdrawn(self):
"""Tests that a failed project is not withdrawn."""
project = project_utils.seedNDBProject(
self.student, self.program.key(), org_key=self.org.key,
status=project_model.Status.FAILED)
result = project_logic.withdrawProject(project.key)
self.assertFalse(result)
self.assertEqual(result.extra, project_logic.ILLEGAL_PROJECT_STATE)
# check that the project is failed
project = project.key.get()
self.assertEqual(project.status, project_model.Status.FAILED)
# check that the number of projects is not changed
updated_student = self.student.key.get()
self.assertEqual(
updated_student.student_data.number_of_projects,
self.student.student_data.number_of_projects)
class AcceptProjectTest(unittest.TestCase):
"""Unit tests for acceptProject function."""
def setUp(self):
"""See unittest.TestCase.setUp for specification."""
self.program = program_utils.seedGSoCProgram()
self.student = profile_utils.seedSOCStudent(self.program)
self.org = org_utils.seedSOCOrganization(self.program.key())
def testAcceptedProjectIsAccepted(self):
"""Tests that an accepted project stays accepted."""
project = project_utils.seedNDBProject(
self.student, self.program.key(), org_key=self.org.key)
result = project_logic.acceptProject(project.key)
self.assertTrue(result)
# check that the project is accepted
project = project.key.get()
self.assertEqual(project.status, project_model.Status.ACCEPTED)
# check that the number of projects is not changed
updated_student = self.student.key.get()
self.assertEqual(
updated_student.student_data.number_of_projects,
self.student.student_data.number_of_projects)
def testWithdrawnProjectIsAccepted(self):
"""Tests that a withdrawn project is accepted properly."""
project = project_utils.seedNDBProject(
self.student, self.program.key(), org_key=self.org.key,
status=project_model.Status.WITHDRAWN)
result = project_logic.acceptProject(project.key)
self.assertTrue(result)
# check that the project is accepted
project = project.key.get()
self.assertEqual(project.status, project_model.Status.ACCEPTED)
# check that the number of projects is updated
updated_student = self.student.key.get()
self.assertEqual(
updated_student.student_data.number_of_projects,
self.student.student_data.number_of_projects + 1)
def testFailedProjectIsNotAccepted(self):
"""Tests that a failed project is not accepted."""
project = project_utils.seedNDBProject(
self.student, self.program.key(), org_key=self.org.key,
status=project_model.Status.FAILED)
result = project_logic.acceptProject(project.key)
self.assertFalse(result)
self.assertEqual(result.extra, project_logic.ILLEGAL_PROJECT_STATE)
# check that the project is failed
project = project.key.get()
self.assertEqual(project.status, project_model.Status.FAILED)
# check that the number of projects is not changed
updated_student = self.student.key.get()
self.assertEqual(
updated_student.student_data.number_of_projects,
self.student.student_data.number_of_projects)