Change iterative to algorithm in slot allocator
[Melange.git] / tests / app / soc / logic / test_allocations.py
blob75eb17ffb71bc3e447462bc6f59f0162b64f8860
1 #!/usr/bin/python2.5
3 # Copyright 2009 the Melange authors.
5 # Licensed under the Apache License, Version 2.0 (the "License");
6 # you may not use this file except in compliance with the License.
7 # You may obtain a copy of the License at
9 # http://www.apache.org/licenses/LICENSE-2.0
11 # Unless required by applicable law or agreed to in writing, software
12 # distributed under the License is distributed on an "AS IS" BASIS,
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
18 __authors__ = [
19 '"Sverre Rabbelier" <sverre@rabbelier.nl>',
23 import unittest
25 from soc.logic import allocations
28 class Student(object):
29 """Mocker for Student object.
30 """
32 def __init__(self, id):
33 """Simple init that stores id for later use.
34 """
36 self.id = id
38 def __eq__(self, other):
39 """Simple eq that compares ids.
40 """
42 return self.id == other.id
44 def __str__(self):
45 """Simple str that returns str(id).
46 """
48 return str(self.id)
50 def __repr__(self):
51 """Simple repr that returns repr(id).
52 """
54 return repr(self.id)
57 class AllocationsTest(unittest.TestCase):
58 """Tests related to the slot allocation algorithm.
59 """
61 def setUp(self):
62 """Set up required for the slot allocation tests.
63 """
65 self.slots = 60
66 self.max_slots_per_org = 40
67 self.min_slots_per_org = 2
68 self.allocated = 0
69 self.algorithm = 1
71 apps = {
72 'asf': (20, 20),
73 'gcc': (15, 50),
74 'git': (6, 6),
75 'google': (3, 10),
76 'melange': (100, 3),
79 self.popularity = dict([(k,a) for k, (a, m) in apps.iteritems()])
80 self.mentors = dict([(k,m) for k, (a, m) in apps.iteritems()])
82 self.orgs = self.popularity.keys()
84 self.allocater = allocations.Allocator(
85 self.orgs, self.popularity, self.mentors, self.slots,
86 self.max_slots_per_org, self.min_slots_per_org, self.algorithm)
88 def testInitialAllocation(self):
89 """Test that an allocation with no arguments does not crash.
90 """
92 locked_slots = {}
93 adjusted_slots = {}
94 self.allocater.allocate(locked_slots)
96 def testLockedSlotsAllocation(self):
97 """Test that an allocation with an org locked does not crash.
98 """
100 locked_slots = {'melange': 3}
101 self.allocater.allocate(locked_slots)
103 def testNonExistantOrgAllocation(self):
104 """Test that locking a non-existing org errors out.
107 locked_slots = {'gnome': 1}
108 self.failUnlessRaises(allocations.Error, self.allocater.allocate,
109 locked_slots)
111 def testInitialAllocationBelowMaxSlots(self):
112 """Test that the initial allocation is below the max slot count.
115 locked_slots = {}
117 result = self.allocater.allocate(locked_slots)
118 self.failIf(sum(result.values()) > self.slots)
120 def testLockedAllocationCorrect(self):
121 """Test that locking an allocation assigns the org the allocation.
124 locked_slots = {'git': 6}
126 result = self.allocater.allocate(locked_slots)
128 expected = 6
129 actual = result['git']
131 self.failUnlessEqual(expected, actual)
133 def testOverassignedAllocationCorrect(self):
134 """Test that over-assigned allocation are cut down.
137 locked_slots = {'git': 20}
139 result = self.allocater.allocate(locked_slots)
141 expected = 6
142 actual = result['git']
144 self.failUnlessEqual(expected, actual)