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.
19 '"Sverre Rabbelier" <sverre@rabbelier.nl>',
25 from soc
.logic
import allocations
28 class Student(object):
29 """Mocker for Student object.
32 def __init__(self
, id):
33 """Simple init that stores id for later use.
38 def __eq__(self
, other
):
39 """Simple eq that compares ids.
42 return self
.id == other
.id
45 """Simple str that returns str(id).
51 """Simple repr that returns repr(id).
57 class AllocationsTest(unittest
.TestCase
):
58 """Tests related to the slot allocation algorithm.
62 """Set up required for the slot allocation tests.
66 self
.max_slots_per_org
= 40
67 self
.min_slots_per_org
= 2
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.
94 self
.allocater
.allocate(locked_slots
)
96 def testLockedSlotsAllocation(self
):
97 """Test that an allocation with an org locked does not crash.
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
,
111 def testInitialAllocationBelowMaxSlots(self
):
112 """Test that the initial allocation is below the max slot count.
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
)
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
)
142 actual
= result
['git']
144 self
.failUnlessEqual(expected
, actual
)