blob: a9c102db010293e23373df69bae7277a266fe942 [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 slot transfer logic."""
import unittest
from summerofcode.logic import slot_transfer as slot_transfer_logic
from summerofcode.models import slot_transfer as slot_transfer_model
from tests import org_utils
from tests import program_utils
_TEST_QUANTITY = 4
_TEST_SLOT_ALLOCATION = 7
class CreateSlotTransferTest(unittest.TestCase):
"""Unit tests for createSlotTransfer function."""
def setUp(self):
"""See unittest.TestCase.setUp for specification."""
self.program = program_utils.seedProgram()
self.org = org_utils.seedSOCOrganization(
self.program.key(), slot_allocation=_TEST_SLOT_ALLOCATION)
def testSlotTransferCreated(self):
"""Tests that slot transfer entity is created successfully."""
result = slot_transfer_logic.createSlotTransfer(
self.org.key, _TEST_QUANTITY)
# check that slot transfer entity is created
self.assertTrue(result)
slot_transfer = result.extra.key.get()
# check properties of the slot transfer entity
self.assertEqual(slot_transfer.status, slot_transfer_model.Status.PENDING)
self.assertEqual(slot_transfer.key.parent(), self.org.key)
self.assertEqual(slot_transfer.quantity, _TEST_QUANTITY)
# check that slot transfer is not created for the second time
result = slot_transfer_logic.createSlotTransfer(
self.org.key, _TEST_QUANTITY)
self.assertFalse(result)
self.assertEqual(
result.extra, slot_transfer_logic.PENDING_SLOT_TRANSFER_EXISTS)
def testTransferAllSlots(self):
"""Tests that a new entity is created when all slots are transferred."""
result = slot_transfer_logic.createSlotTransfer(
self.org.key, _TEST_SLOT_ALLOCATION)
self.assertTrue(result)
def testTransferInvalidNumberOfSlots(self):
"""Tests that no entity is created when invalid number is transferred."""
# too many slots
result = slot_transfer_logic.createSlotTransfer(
self.org.key, _TEST_SLOT_ALLOCATION + 1)
self.assertFalse(result)
self.assertEqual(
result.extra, slot_transfer_logic.NOT_ENOUGH_SLOTS_ALLOCATED)
# zero slots
result = slot_transfer_logic.createSlotTransfer(self.org.key, 0)
self.assertFalse(result)
self.assertEqual(
result.extra, slot_transfer_logic.NOT_ENOUGH_SLOTS_TRANSFERRED)
# negative slots
result = slot_transfer_logic.createSlotTransfer(self.org.key, -1)
self.assertFalse(result)
self.assertEqual(
result.extra, slot_transfer_logic.NOT_ENOUGH_SLOTS_TRANSFERRED)
_TEST_SLOT_TRANSFER_NUMBER = 2
class HasPendingSlotTransferTest(unittest.TestCase):
"""Unit tests for hasPendingSlotTransfer function."""
def setUp(self):
"""See unittest.TestCase.setUp for specification."""
self.program = program_utils.seedProgram()
self.org = org_utils.seedSOCOrganization(self.program.key())
def testNoSlotTransferRequests(self):
"""Tests that false is returned if no slot transfer requests exist."""
result = slot_transfer_logic.hasPendingSlotTransfer(self.org.key)
self.assertFalse(result)
def testNonPendingSlotTransferRequests(self):
"""Tests that false is returned when non-pending slot transfers exist."""
# seed a couple of accepted and rejected slot transfer requests
for _ in range(_TEST_SLOT_TRANSFER_NUMBER):
org_utils.seedSlotTransfer(
self.org, status=slot_transfer_model.Status.ACCEPTED)
org_utils.seedSlotTransfer(
self.org, status=slot_transfer_model.Status.REJECTED)
result = slot_transfer_logic.hasPendingSlotTransfer(self.org.key)
self.assertFalse(result)
def testPendingRequest(self):
"""Tests that true is returned when a pending slot transfer exists."""
org_utils.seedSlotTransfer(
self.org, status=slot_transfer_model.Status.PENDING)
result = slot_transfer_logic.hasPendingSlotTransfer(self.org.key)
self.assertTrue(result)
class AcceptSlotTransferTest(unittest.TestCase):
"""Unit tests for acceptSlotTransfer function."""
def setUp(self):
"""See unittest.TestCase.setUp for specification."""
self.program = program_utils.seedProgram()
self.org = org_utils.seedSOCOrganization(
self.program.key(), slot_allocation=_TEST_SLOT_ALLOCATION)
def testSlotTransferAccepted(self):
"""Tests that the slot transfer is accepted properly."""
slot_transfer = org_utils.seedSlotTransfer(
self.org, quantity=_TEST_QUANTITY)
result = slot_transfer_logic.acceptSlotTransfer(slot_transfer.key)
self.assertTrue(result)
slot_transfer = slot_transfer.key.get()
self.assertEqual(slot_transfer.status, slot_transfer_model.Status.ACCEPTED)
org = self.org.key.get()
self.assertEqual(
org.slot_allocation, _TEST_SLOT_ALLOCATION - _TEST_QUANTITY)
def testTooManySlotsTransferred(self):
"""Tests that the slot allocation does not drop below zero."""
slot_transfer = org_utils.seedSlotTransfer(
self.org, quantity=_TEST_SLOT_ALLOCATION + 1)
result = slot_transfer_logic.acceptSlotTransfer(slot_transfer.key)
self.assertTrue(result)
slot_transfer = slot_transfer.key.get()
self.assertEqual(slot_transfer.status, slot_transfer_model.Status.ACCEPTED)
org = self.org.key.get()
self.assertEqual(org.slot_allocation, 0)
class RejectSlotTransferTest(unittest.TestCase):
"""Unit tests for rejectSlotTransfer function."""
def setUp(self):
"""See unittest.TestCase.setUp for specification."""
self.program = program_utils.seedProgram()
self.org = org_utils.seedSOCOrganization(
self.program.key(), slot_allocation=_TEST_SLOT_ALLOCATION)
def testSlotTransferRejected(self):
"""Tests that the slot transfer is rejected properly."""
slot_transfer = org_utils.seedSlotTransfer(
self.org, quantity=_TEST_QUANTITY)
result = slot_transfer_logic.rejectSlotTransfer(slot_transfer.key)
self.assertTrue(result)
slot_transfer = slot_transfer.key.get()
self.assertEqual(slot_transfer.status, slot_transfer_model.Status.REJECTED)
org = self.org.key.get()
self.assertEqual(org.slot_allocation, _TEST_SLOT_ALLOCATION)