Coverage for tests/model_tests/test_algorithm.py: 100%
89 statements
« prev ^ index » next coverage.py v7.8.0, created at 2025-05-05 14:02 +0000
« prev ^ index » next coverage.py v7.8.0, created at 2025-05-05 14:02 +0000
1"""Tests for the matching algorithm."""
3import os
4import sys
6# flake8: noqa: F811
8sys.path.append(
9 os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
10)
12from algorithm.matching import Matching
15def test_basic_matching_without_conflicts():
16 """Tests a basic matching without any conflicts."""
17 students_preference = {
18 "Student_1": ["company_1"],
19 "Student_2": ["company_2"],
20 "Student_3": ["company_3"],
21 }
22 employer_preference = {
23 "company_1": {"positions": 1, "Student_1": 1},
24 "company_2": {"positions": 1, "Student_2": 1},
25 "company_3": {"positions": 1, "Student_3": 1},
26 }
27 match = Matching(students_preference, employer_preference)
28 result = match.find_best_match()
29 expected = (
30 [],
31 {
32 "company_1": ["Student_1"],
33 "company_2": ["Student_2"],
34 "company_3": ["Student_3"],
35 },
36 )
38 assert result == expected
41def test_get_matches_function():
42 """Tests a basic matching without any conflicts."""
43 students_preference = {
44 "Student_1": ["company_1"],
45 "Student_2": ["company_2"],
46 "Student_3": ["company_3"],
47 }
48 employer_preference = {
49 "company_1": {"positions": 1, "Student_1": 1},
50 "company_2": {"positions": 1, "Student_2": 1},
51 "company_3": {"positions": 1, "Student_3": 1},
52 }
53 match = Matching(students_preference, employer_preference)
54 result = match.find_best_match()
55 expected = (
56 [],
57 {
58 "company_1": ["Student_1"],
59 "company_2": ["Student_2"],
60 "company_3": ["Student_3"],
61 },
62 )
63 matches = match.get_matches()
65 assert result == expected == matches
68def test_students_exceed_company_positions():
69 """Tests a scenario where students exceed the number of positions available."""
70 students_preference = {
71 "Student_1": ["company_1"],
72 "Student_2": ["company_1"],
73 "Student_3": ["company_1"],
74 }
75 employer_preference = {
76 "company_1": {
77 "positions": 1,
78 "Student_1": 2,
79 "Student_2": 1,
80 "Student_3": 3,
81 },
82 }
83 match = Matching(students_preference, employer_preference)
84 result = match.find_best_match()
85 expected = (
86 ["Student_1", "Student_3"],
87 {
88 "company_1": ["Student_2"],
89 },
90 )
92 assert result == expected
95def test_replacing_weaker_match():
96 """Tests a scenario where a weaker match is replaced by a stronger one."""
97 students_preference = {
98 "Student_1": ["company_1"],
99 "Student_2": ["company_1"],
100 }
101 employer_preference = {
102 "company_1": {"positions": 1, "Student_2": 1, "Student_1": 2},
103 }
104 match = Matching(students_preference, employer_preference)
105 result = match.find_best_match()
106 expected = (
107 ["Student_1"],
108 {
109 "company_1": ["Student_2"],
110 },
111 )
113 assert result == expected
116def test_no_positions_left_for_students():
117 """Tests a scenario where there are no positions left for students."""
118 students_preference = {
119 "Student_1": ["company_1"],
120 "Student_2": ["company_1"],
121 "Student_3": ["company_2"],
122 }
123 employer_preference = {
124 "company_1": {"positions": 1, "Student_1": 1, "Student_2": 2},
125 "company_2": {"positions": 0},
126 }
127 match = Matching(students_preference, employer_preference)
128 result = match.find_best_match()
129 expected = (
130 ["Student_2", "Student_3"],
131 {
132 "company_1": ["Student_1"],
133 },
134 )
136 assert result == expected
139def test_matching_with_multiple_preferences():
140 """Tests a scenario where students and employers have multiple preferences."""
141 students_preference = {
142 "Student_1": ["company_1", "company_2"],
143 "Student_2": ["company_1", "company_2"],
144 "Student_3": ["company_2"],
145 "Student_4": ["company_3", "company_1"],
146 }
147 employer_preference = {
148 "company_1": {"positions": 1, "Student_1": 1, "Student_2": 2},
149 "company_2": {
150 "positions": 2,
151 "Student_3": 1,
152 "Student_2": 2,
153 "Student_1": 3,
154 },
155 "company_3": {"positions": 1, "Student_4": 1},
156 }
157 match = Matching(students_preference, employer_preference)
158 result = match.find_best_match()
159 expected = (
160 [],
161 {
162 "company_1": ["Student_1"],
163 "company_2": ["Student_2", "Student_3"],
164 "company_3": ["Student_4"],
165 },
166 )
168 assert result == expected
171def test_student_reassigned_due_to_higher_priority():
172 """Tests a scenario where a student is reassigned due to higher priority."""
173 students_preference = {
174 "Student_1": ["company_1"],
175 "Student_2": ["company_1"],
176 "Student_3": ["company_1"],
177 }
178 employer_preference = {
179 "company_1": {
180 "positions": 1,
181 "Student_1": 1,
182 "Student_2": 3,
183 "Student_3": 2,
184 },
185 }
186 match = Matching(students_preference, employer_preference)
187 result = match.find_best_match()
188 expected = (
189 ["Student_2", "Student_3"],
190 {
191 "company_1": ["Student_1"],
192 },
193 )
195 assert result == expected
198def test_unequal_positions_distribution():
199 """Tests a scenario where employers have unequal positions."""
200 students_preference = {
201 "Student_1": ["company_1"],
202 "Student_2": ["company_1", "company_2"],
203 "Student_4": ["company_2"],
204 "Student_3": ["company_2"],
205 }
206 employer_preference = {
207 "company_1": {"positions": 2, "Student_1": 1, "Student_2": 2},
208 "company_2": {"positions": 1, "Student_3": 1, "Student_4": 2},
209 }
210 match = Matching(students_preference, employer_preference)
211 result = match.find_best_match()
212 expected = (
213 ["Student_4"],
214 {
215 "company_1": ["Student_1", "Student_2"],
216 "company_2": ["Student_3"],
217 },
218 )
220 assert result == expected
223def test_student_prefers_multiple_companies():
224 """Tests a scenario where a student prefers multiple companies."""
225 students_preference = {
226 "Student_1": ["company_1", "company_2"],
227 "Student_2": ["company_1", "company_2"],
228 }
229 employer_preference = {
230 "company_1": {"positions": 1, "Student_2": 1, "Student_1": 2},
231 "company_2": {"positions": 1, "Student_1": 1, "Student_2": 2},
232 }
233 match = Matching(students_preference, employer_preference)
234 result = match.find_best_match()
235 expected = (
236 [],
237 {
238 "company_1": ["Student_2"],
239 "company_2": ["Student_1"],
240 },
241 )
243 assert result == expected
246def test_confusing_preference_leading_to_correct_match():
247 """Tests a scenario where a student's preference is confusing."""
248 students_preference = {
249 "Student_1": ["company_2", "company_1"],
250 "Student_2": ["company_2"],
251 "Student_3": ["company_1"],
252 "Student_4": ["company_1"],
253 }
254 employer_preference = {
255 "company_1": {"positions": 2, "Student_3": 1, "Student_4": 2},
256 "company_2": {"positions": 1, "Student_1": 1, "Student_2": 2},
257 }
258 match = Matching(students_preference, employer_preference)
259 result = match.find_best_match()
260 expected = (
261 ["Student_2"],
262 {
263 "company_1": ["Student_3", "Student_4"],
264 "company_2": ["Student_1"],
265 },
266 )
268 assert result == expected
271def test_multiple_unmapped_students():
272 """Test when there are multiple students that could not be mapped."""
273 students_preference = {
274 "Student_1": ["company_2", "company_3"],
275 "Student_2": ["company_1", "company_4", "company_5"],
276 "Student_3": ["company_1", "company_2", "company_6"],
277 "Student_4": ["company_3", "company_5"],
278 "Student_5": ["company_2", "company_3", "company_7"],
279 "Student_6": ["company_5", "company_8"],
280 "Student_7": ["company_1", "company_4"],
281 "Student_8": ["company_7", "company_9", "company_5"],
282 "Student_9": ["company_1", "company_2"],
283 "Student_10": ["company_8", "company_10", "company_9"],
284 }
286 employer_preference = {
287 "company_1": {
288 "positions": 2,
289 "Student_2": 1,
290 "Student_3": 2,
291 "Student_9": 3,
292 "Student_5": 4,
293 "Student_1": 5,
294 },
295 "company_2": {
296 "positions": 0,
297 "Student_1": 1,
298 "Student_5": 2,
299 "Student_3": 3,
300 "Student_10": 4,
301 "Student_8": 5,
302 "Student_4": 6,
303 },
304 "company_3": {
305 "positions": 0,
306 "Student_4": 1,
307 "Student_1": 2,
308 "Student_2": 3,
309 "Student_9": 4,
310 "Student_8": 5,
311 },
312 "company_4": {
313 "positions": 0,
314 "Student_2": 1,
315 "Student_7": 2,
316 "Student_5": 3,
317 "Student_1": 4,
318 "Student_3": 5,
319 },
320 "company_5": {
321 "positions": 1,
322 "Student_6": 1,
323 "Student_4": 2,
324 "Student_2": 3,
325 "Student_10": 4,
326 },
327 "company_6": {
328 "positions": 1,
329 "Student_3": 1,
330 "Student_1": 2,
331 "Student_4": 3,
332 "Student_9": 4,
333 },
334 "company_7": {
335 "positions": 1,
336 "Student_5": 1,
337 "Student_8": 2,
338 "Student_1": 3,
339 "Student_3": 4,
340 },
341 "company_8": {
342 "positions": 0,
343 "Student_10": 1,
344 "Student_6": 2,
345 "Student_2": 3,
346 "Student_3": 4,
347 },
348 "company_9": {
349 "positions": 1,
350 "Student_2": 1,
351 "Student_4": 2,
352 "Student_1": 3,
353 },
354 "company_10": {
355 "positions": 1,
356 "Student_10": 1,
357 "Student_5": 2,
358 "Student_6": 3,
359 "Student_8": 4,
360 },
361 }
363 match = Matching(students_preference, employer_preference)
364 result = match.find_best_match()
366 expected = (
367 ["Student_1", "Student_4", "Student_7", "Student_8", "Student_9"],
368 {
369 "company_1": ["Student_2", "Student_3"],
370 "company_5": ["Student_6"],
371 "company_7": ["Student_5"],
372 "company_10": ["Student_10"],
373 },
374 )
376 assert result == expected
379def test_larger_set_matching_scenario():
380 """Test a larger set of students and employers to find the best match."""
381 students_preference = {
382 "Student_1": ["company_2", "company_3", "company_6", "company_7"],
383 "Student_2": ["company_1", "company_4", "company_5", "company_9"],
384 "Student_3": ["company_1", "company_2", "company_6", "company_10"],
385 "Student_4": ["company_3", "company_5", "company_6", "company_9"],
386 "Student_5": ["company_2", "company_7", "company_8"],
387 "Student_6": ["company_5", "company_8", "company_10"],
388 "Student_7": ["company_1", "company_4", "company_7"],
389 "Student_8": ["company_7", "company_9", "company_5", "company_3"],
390 "Student_9": ["company_1", "company_2", "company_8"],
391 "Student_10": ["company_8", "company_10", "company_9"],
392 "Student_11": ["company_2", "company_6", "company_5"],
393 "Student_12": ["company_10", "company_4", "company_7", "company_8"],
394 "Student_13": ["company_1", "company_6", "company_7"],
395 "Student_14": ["company_4", "company_5", "company_10"],
396 "Student_15": ["company_6", "company_8", "company_9", "company_3"],
397 }
399 employer_preference = {
400 "company_1": {
401 "positions": 3,
402 "Student_2": 1,
403 "Student_3": 2,
404 "Student_9": 3,
405 "Student_7": 4,
406 "Student_13": 5,
407 },
408 "company_2": {
409 "positions": 1,
410 "Student_1": 1,
411 "Student_3": 2,
412 "Student_11": 3,
413 "Student_9": 4,
414 "Student_5": 5,
415 },
416 "company_3": {
417 "positions": 1,
418 "Student_4": 1,
419 "Student_8": 2,
420 "Student_1": 3,
421 "Student_15": 4,
422 },
423 "company_4": {
424 "positions": 2,
425 "Student_2": 1,
426 "Student_7": 2,
427 "Student_12": 3,
428 "Student_14": 4,
429 },
430 "company_5": {
431 "positions": 1,
432 "Student_4": 1,
433 "Student_6": 2,
434 "Student_14": 3,
435 "Student_8": 4,
436 "Student_15": 5,
437 },
438 "company_6": {
439 "positions": 1,
440 "Student_3": 1,
441 "Student_1": 2,
442 "Student_11": 3,
443 "Student_13": 4,
444 },
445 "company_7": {
446 "positions": 2,
447 "Student_5": 1,
448 "Student_12": 2,
449 "Student_1": 3,
450 "Student_8": 4,
451 },
452 "company_8": {
453 "positions": 2,
454 "Student_6": 1,
455 "Student_5": 2,
456 "Student_10": 3,
457 "Student_15": 4,
458 },
459 "company_9": {
460 "positions": 1,
461 "Student_2": 1,
462 "Student_4": 2,
463 "Student_10": 3,
464 "Student_8": 4,
465 },
466 "company_10": {
467 "positions": 2,
468 "Student_10": 1,
469 "Student_12": 2,
470 "Student_14": 3,
471 "Student_6": 4,
472 },
473 }
475 match = Matching(students_preference, employer_preference)
476 result = match.find_best_match()
478 expected = (
479 ["Student_13"],
480 {
481 "company_2": ["Student_1"],
482 "company_1": ["Student_2", "Student_3", "Student_9"],
483 "company_3": ["Student_4"],
484 "company_7": ["Student_5", "Student_8"],
485 "company_5": ["Student_6"],
486 "company_4": ["Student_7", "Student_14"],
487 "company_8": ["Student_10", "Student_15"],
488 "company_6": ["Student_11"],
489 "company_10": ["Student_12"],
490 },
491 )
493 assert result == expected