tests/app/soc/logic/test_allocations.py
author Sverre Rabbelier <srabbelier@gmail.com>
Sat, 07 Mar 2009 18:16:54 +0000
changeset 1721 9acf4fe1b9bb
parent 1718 ca34f4a8c61b
child 1751 17c7a7a48dc7
permissions -rw-r--r--
Adjust the total_popularity after ranging This way 'off the scale' orgs (many applicants, with only few mentors available) will not pull the total amount of allocated slots down as much as before. Patch by: Sverre Rabbelier
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
1056
d0c82bdc2de2 Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
     1
#!/usr/bin/python2.5
d0c82bdc2de2 Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
     2
#
1308
35b75ffcbb37 Partially reverted "Update the copyright notice for 2009."
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1056
diff changeset
     3
# Copyright 2009 the Melange authors.
1056
d0c82bdc2de2 Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
     4
#
d0c82bdc2de2 Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
     5
# Licensed under the Apache License, Version 2.0 (the "License");
d0c82bdc2de2 Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
     6
# you may not use this file except in compliance with the License.
d0c82bdc2de2 Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
     7
# You may obtain a copy of the License at
d0c82bdc2de2 Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
     8
#
d0c82bdc2de2 Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
     9
#   http://www.apache.org/licenses/LICENSE-2.0
d0c82bdc2de2 Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    10
#
d0c82bdc2de2 Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    11
# Unless required by applicable law or agreed to in writing, software
d0c82bdc2de2 Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    12
# distributed under the License is distributed on an "AS IS" BASIS,
d0c82bdc2de2 Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    13
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
d0c82bdc2de2 Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    14
# See the License for the specific language governing permissions and
d0c82bdc2de2 Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    15
# limitations under the License.
d0c82bdc2de2 Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    16
d0c82bdc2de2 Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    17
d0c82bdc2de2 Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    18
__authors__ = [
d0c82bdc2de2 Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    19
  '"Sverre Rabbelier" <sverre@rabbelier.nl>',
d0c82bdc2de2 Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    20
  ]
d0c82bdc2de2 Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    21
d0c82bdc2de2 Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    22
d0c82bdc2de2 Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    23
import unittest
d0c82bdc2de2 Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    24
d0c82bdc2de2 Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    25
from soc.logic import allocations
d0c82bdc2de2 Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    26
d0c82bdc2de2 Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    27
d0c82bdc2de2 Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    28
class Student(object):
d0c82bdc2de2 Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    29
  """Mocker for Student object.
d0c82bdc2de2 Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    30
  """
d0c82bdc2de2 Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    31
d0c82bdc2de2 Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    32
  def __init__(self, id):
d0c82bdc2de2 Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    33
    """Simple init that stores id for later use.
d0c82bdc2de2 Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    34
    """
d0c82bdc2de2 Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    35
d0c82bdc2de2 Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    36
    self.id = id
d0c82bdc2de2 Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    37
d0c82bdc2de2 Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    38
  def __eq__(self, other):
d0c82bdc2de2 Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    39
    """Simple eq that compares ids.
d0c82bdc2de2 Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    40
    """
d0c82bdc2de2 Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    41
d0c82bdc2de2 Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    42
    return self.id == other.id
d0c82bdc2de2 Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    43
d0c82bdc2de2 Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    44
  def __str__(self):
d0c82bdc2de2 Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    45
    """Simple str that returns str(id).
d0c82bdc2de2 Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    46
    """
d0c82bdc2de2 Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    47
d0c82bdc2de2 Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    48
    return str(self.id)
d0c82bdc2de2 Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    49
d0c82bdc2de2 Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    50
  def __repr__(self):
d0c82bdc2de2 Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    51
    """Simple repr that returns repr(id).
d0c82bdc2de2 Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    52
    """
d0c82bdc2de2 Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    53
d0c82bdc2de2 Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    54
    return repr(self.id)
d0c82bdc2de2 Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    55
d0c82bdc2de2 Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    56
d0c82bdc2de2 Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    57
class AllocationsTest(unittest.TestCase):
d0c82bdc2de2 Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    58
  """Tests related to the slot allocation algorithm.
d0c82bdc2de2 Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    59
  """
d0c82bdc2de2 Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    60
d0c82bdc2de2 Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    61
  def setUp(self):
d0c82bdc2de2 Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    62
    """Set up required for the slot allocation tests.
d0c82bdc2de2 Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    63
    """
d0c82bdc2de2 Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    64
d0c82bdc2de2 Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    65
    self.slots = 60
d0c82bdc2de2 Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    66
    self.max_slots_per_org = 40
1717
d4c4c8668871 Added assigned mentors and amount of minimum slots per org
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1308
diff changeset
    67
    self.min_slots_per_org = 2
1056
d0c82bdc2de2 Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    68
    self.allocated = 0
1718
ca34f4a8c61b Added an pre-processing algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1717
diff changeset
    69
    self.iterative = False
1056
d0c82bdc2de2 Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    70
1717
d4c4c8668871 Added assigned mentors and amount of minimum slots per org
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1308
diff changeset
    71
    apps = {
d4c4c8668871 Added assigned mentors and amount of minimum slots per org
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1308
diff changeset
    72
        'asf': self.allocate(20, 20),
d4c4c8668871 Added assigned mentors and amount of minimum slots per org
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1308
diff changeset
    73
        'gcc': self.allocate(15, 30),
d4c4c8668871 Added assigned mentors and amount of minimum slots per org
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1308
diff changeset
    74
        'git': self.allocate(6, 6),
d4c4c8668871 Added assigned mentors and amount of minimum slots per org
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1308
diff changeset
    75
        'google': self.allocate(3, 10),
d4c4c8668871 Added assigned mentors and amount of minimum slots per org
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1308
diff changeset
    76
        'melange': self.allocate(100, 3),
1056
d0c82bdc2de2 Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    77
        }
d0c82bdc2de2 Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    78
1717
d4c4c8668871 Added assigned mentors and amount of minimum slots per org
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1308
diff changeset
    79
    self.applications = dict([(k,a) for k, (m, a) in apps.iteritems()])
d4c4c8668871 Added assigned mentors and amount of minimum slots per org
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1308
diff changeset
    80
    self.mentors = dict([(k,m) for k, (m, a) in apps.iteritems()])
d4c4c8668871 Added assigned mentors and amount of minimum slots per org
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1308
diff changeset
    81
1056
d0c82bdc2de2 Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    82
    self.orgs = self.applications.keys()
d0c82bdc2de2 Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    83
1717
d4c4c8668871 Added assigned mentors and amount of minimum slots per org
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1308
diff changeset
    84
    self.allocater = allocations.Allocator(
d4c4c8668871 Added assigned mentors and amount of minimum slots per org
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1308
diff changeset
    85
        self.orgs, self.applications, self.mentors, self.slots,
d4c4c8668871 Added assigned mentors and amount of minimum slots per org
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1308
diff changeset
    86
        self.max_slots_per_org, self.min_slots_per_org, self.iterative)
1056
d0c82bdc2de2 Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    87
1717
d4c4c8668871 Added assigned mentors and amount of minimum slots per org
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1308
diff changeset
    88
  def allocate(self, count, max):
1056
d0c82bdc2de2 Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    89
    """Returns a list with count new student objects.
d0c82bdc2de2 Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    90
    """
d0c82bdc2de2 Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    91
d0c82bdc2de2 Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    92
    i = self.allocated
d0c82bdc2de2 Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    93
    j = i + count
d0c82bdc2de2 Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    94
    self.allocated += count
d0c82bdc2de2 Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    95
1717
d4c4c8668871 Added assigned mentors and amount of minimum slots per org
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1308
diff changeset
    96
    return max, [Student(i) for i in range(i,j)]
1056
d0c82bdc2de2 Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    97
d0c82bdc2de2 Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    98
  def testAllocate(self):
d0c82bdc2de2 Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    99
    """Test that the allocate helper works properly.
d0c82bdc2de2 Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   100
d0c82bdc2de2 Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   101
    A meta-test, it never hurts to be certain.
d0c82bdc2de2 Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   102
    """
d0c82bdc2de2 Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   103
d0c82bdc2de2 Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   104
    stash = self.allocated
d0c82bdc2de2 Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   105
    self.allocated = 0
d0c82bdc2de2 Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   106
d0c82bdc2de2 Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   107
    expected = [Student(0), Student(1), Student(2)]
1717
d4c4c8668871 Added assigned mentors and amount of minimum slots per org
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1308
diff changeset
   108
    count, actual = self.allocate(3, 0)
1056
d0c82bdc2de2 Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   109
    self.failUnlessEqual(expected, actual)
1717
d4c4c8668871 Added assigned mentors and amount of minimum slots per org
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1308
diff changeset
   110
    self.failUnlessEqual(count, 0)
1056
d0c82bdc2de2 Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   111
d0c82bdc2de2 Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   112
    expected = []
1717
d4c4c8668871 Added assigned mentors and amount of minimum slots per org
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1308
diff changeset
   113
    count, actual = self.allocate(0, 10)
1056
d0c82bdc2de2 Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   114
    self.failUnlessEqual(expected, actual)
1717
d4c4c8668871 Added assigned mentors and amount of minimum slots per org
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1308
diff changeset
   115
    self.failUnlessEqual(count, 10)
1056
d0c82bdc2de2 Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   116
d0c82bdc2de2 Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   117
    expected = [Student(3)]
1717
d4c4c8668871 Added assigned mentors and amount of minimum slots per org
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1308
diff changeset
   118
    count, actual = self.allocate(1, 5)
1056
d0c82bdc2de2 Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   119
    self.failUnlessEqual(expected, actual)
1717
d4c4c8668871 Added assigned mentors and amount of minimum slots per org
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1308
diff changeset
   120
    self.failUnlessEqual(count, 5)
1056
d0c82bdc2de2 Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   121
d0c82bdc2de2 Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   122
    self.allocated = stash
d0c82bdc2de2 Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   123
d0c82bdc2de2 Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   124
  def testInitialAllocation(self):
d0c82bdc2de2 Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   125
    """Test that an allocation with no arguments does not crash.
d0c82bdc2de2 Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   126
    """
d0c82bdc2de2 Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   127
d0c82bdc2de2 Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   128
    locked_slots = {}
d0c82bdc2de2 Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   129
    adjusted_slots = {}
d0c82bdc2de2 Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   130
    self.allocater.allocate(locked_slots, adjusted_slots)
d0c82bdc2de2 Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   131
d0c82bdc2de2 Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   132
  def testLockedSlotsAllocation(self):
d0c82bdc2de2 Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   133
    """Test that an allocation with an org locked does not crash.
d0c82bdc2de2 Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   134
    """
d0c82bdc2de2 Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   135
d0c82bdc2de2 Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   136
    locked_slots = {'melange': 3}
d0c82bdc2de2 Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   137
    adjusted_slots = {}
d0c82bdc2de2 Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   138
    self.allocater.allocate(locked_slots, adjusted_slots)
d0c82bdc2de2 Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   139
d0c82bdc2de2 Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   140
  def testAdjustedSlotsAllocation(self):
d0c82bdc2de2 Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   141
    """Test that an allocation with an org adjusted does not crash.
d0c82bdc2de2 Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   142
    """
d0c82bdc2de2 Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   143
d0c82bdc2de2 Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   144
    locked_slots = {}
d0c82bdc2de2 Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   145
    adjusted_slots = {'google': -1}
d0c82bdc2de2 Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   146
    self.allocater.allocate(locked_slots, adjusted_slots)
d0c82bdc2de2 Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   147
d0c82bdc2de2 Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   148
  def testInvalidSlotsAllocation(self):
d0c82bdc2de2 Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   149
    """Test that an allocation with an org locked and adjusted errors out.
d0c82bdc2de2 Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   150
    """
d0c82bdc2de2 Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   151
d0c82bdc2de2 Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   152
    locked_slots = {'git': 1}
d0c82bdc2de2 Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   153
    adjusted_slots = {'git': 1}
d0c82bdc2de2 Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   154
    self.failUnlessRaises(allocations.Error, self.allocater.allocate,
d0c82bdc2de2 Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   155
                          locked_slots, adjusted_slots)
d0c82bdc2de2 Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   156
d0c82bdc2de2 Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   157
  def testNonExistantOrgAllocation1(self):
d0c82bdc2de2 Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   158
    """Test that locking a non-existing org errors out.
d0c82bdc2de2 Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   159
    """
d0c82bdc2de2 Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   160
d0c82bdc2de2 Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   161
    locked_slots = {'gnome': 1}
d0c82bdc2de2 Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   162
    adjusted_slots = {}
d0c82bdc2de2 Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   163
    self.failUnlessRaises(allocations.Error, self.allocater.allocate,
d0c82bdc2de2 Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   164
                          locked_slots, adjusted_slots)
d0c82bdc2de2 Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   165
d0c82bdc2de2 Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   166
  def testNonExistantOrgAllocation2(self):
d0c82bdc2de2 Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   167
    """Test that adjusting a non-existing org errors out.
d0c82bdc2de2 Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   168
    """
d0c82bdc2de2 Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   169
d0c82bdc2de2 Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   170
    locked_slots = {}
d0c82bdc2de2 Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   171
    adjusted_slots = {'gnome': 1}
d0c82bdc2de2 Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   172
    self.failUnlessRaises(allocations.Error, self.allocater.allocate,
d0c82bdc2de2 Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   173
                          locked_slots, adjusted_slots)
d0c82bdc2de2 Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   174
d0c82bdc2de2 Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   175
  def testInitialAllocationBelowMaxSlots(self):
d0c82bdc2de2 Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   176
    """Test that the initial allocation is below the max slot count.
d0c82bdc2de2 Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   177
    """
d0c82bdc2de2 Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   178
d0c82bdc2de2 Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   179
    locked_slots = {}
d0c82bdc2de2 Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   180
    adjusted_slots = {}
d0c82bdc2de2 Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   181
d0c82bdc2de2 Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   182
    result = self.allocater.allocate(locked_slots, adjusted_slots)
d0c82bdc2de2 Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   183
    self.failIf(sum(result.values()) > self.slots)
d0c82bdc2de2 Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   184
d0c82bdc2de2 Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   185
  def testLockedAllocationCorrect(self):
d0c82bdc2de2 Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   186
    """Test that locking an allocation assigns the org the allocation.
d0c82bdc2de2 Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   187
    """
d0c82bdc2de2 Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   188
d0c82bdc2de2 Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   189
    locked_slots = {'git': 6}
d0c82bdc2de2 Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   190
    adjusted_slots = {}
d0c82bdc2de2 Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   191
d0c82bdc2de2 Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   192
    result = self.allocater.allocate(locked_slots, adjusted_slots)
d0c82bdc2de2 Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   193
d0c82bdc2de2 Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   194
    expected = 6
d0c82bdc2de2 Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   195
    actual = result['git']
d0c82bdc2de2 Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   196
d0c82bdc2de2 Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   197
    self.failUnlessEqual(expected, actual)
d0c82bdc2de2 Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   198
d0c82bdc2de2 Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   199
  def testOverassignedAllocationCorrect(self):
d0c82bdc2de2 Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   200
    """Test that over-assigned allocation are cut down.
d0c82bdc2de2 Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   201
    """
d0c82bdc2de2 Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   202
d0c82bdc2de2 Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   203
    locked_slots = {'git': 20}
d0c82bdc2de2 Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   204
    adjusted_slots = {}
d0c82bdc2de2 Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   205
d0c82bdc2de2 Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   206
    result = self.allocater.allocate(locked_slots, adjusted_slots)
d0c82bdc2de2 Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   207
d0c82bdc2de2 Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   208
    expected = 6
d0c82bdc2de2 Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   209
    actual = result['git']
d0c82bdc2de2 Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   210
d0c82bdc2de2 Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   211
    self.failUnlessEqual(expected, actual)
d0c82bdc2de2 Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   212
d0c82bdc2de2 Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   213
  def testAdjustedAllocationCorrect(self):
d0c82bdc2de2 Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   214
    """Test that locking an allocation assigns the org the allocation.
d0c82bdc2de2 Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   215
    """
d0c82bdc2de2 Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   216
d0c82bdc2de2 Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   217
    locked_slots = {}
1717
d4c4c8668871 Added assigned mentors and amount of minimum slots per org
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1308
diff changeset
   218
    adjusted_slots = {'gcc': 10}
1056
d0c82bdc2de2 Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   219
d0c82bdc2de2 Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   220
    with_adjusting = self.allocater.allocate(locked_slots, adjusted_slots)
d0c82bdc2de2 Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   221
    without_adjusting = self.allocater.allocate(locked_slots, {})
d0c82bdc2de2 Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   222
1721
9acf4fe1b9bb Adjust the total_popularity after ranging
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1718
diff changeset
   223
    expected = without_adjusting['gcc']
1717
d4c4c8668871 Added assigned mentors and amount of minimum slots per org
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1308
diff changeset
   224
    actual = with_adjusting['gcc']
1056
d0c82bdc2de2 Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   225
1721
9acf4fe1b9bb Adjust the total_popularity after ranging
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1718
diff changeset
   226
    self.failUnless(actual > expected)