-
Notifications
You must be signed in to change notification settings - Fork 1
/
test_selection.py
140 lines (113 loc) · 5.63 KB
/
test_selection.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
import logging
import unittest
from selection import UserSelector
class TestUserSelector(unittest.TestCase):
def setUp(self):
logging.basicConfig(level=logging.INFO)
def test_select_from_list_with_duplicates(self):
selector = UserSelector()
users = ["Alice", "Bob", "Charlie", "Alice"] # With duplicates
selected = selector.select(users)
self.assertIn(selected, users)
def test_select_from_list_without_duplicates(self):
selector = UserSelector()
users = ["Alice", "Bob", "Charlie", "Diana"] # Without duplicates
selected = selector.select(users)
self.assertIn(selected, users)
def test_select_empty_list(self):
selector = UserSelector()
with self.assertRaises(ValueError):
selector.select([])
def test_select_single_user(self):
selector = UserSelector()
users = ["Alice"]
selected = selector.select(users)
self.assertEqual(selected, "Alice")
def test_select_non_excluded_from_2_users_with_2_selection_gap(self):
selector = UserSelector(exclude_gap=2)
users = ["Alice", "Bob", "Bob", "Bob", "Bob"]
selector.history_manager.history = ["Alice", "Bob", "Alice"]
expected_selections = ["Bob", "Alice", "Bob", "Alice"]
for expected_selection in expected_selections:
selected = selector.select(users)
self.assertEqual(selected, expected_selection)
def test_select_non_excluded_3_users_with_2_selection_gap(self):
selector = UserSelector(exclude_gap=2)
users = ["Alice", "Alice", "Bob", "Bob", "Charlie", "Charlie", "Charlie"]
selector.history_manager.history = ["Alice", "Bob", "Charlie"]
expected_selections = ["Alice", "Bob", "Charlie", "Alice", "Bob", "Charlie"]
for expected_selection in expected_selections:
selected = selector.select(users)
self.assertEqual(selected, expected_selection)
def test_select_non_excluded_from_2_users_with_0_selection_gap(self):
selector = UserSelector(exclude_gap=0)
users = ["Alice", "Bob", "Bob", "Bob", "Bob"]
selector.history_manager.history = ["Alice", "Bob", "Alice"]
selected = selector.select(users)
self.assertIn(selected, ["Alice", "Bob"])
def test_select_non_excluded_3_users_with_0_selection_gap__(self):
count = 0
for _ in range(0, 100):
selector = UserSelector(exclude_gap=0)
users = ["Alice", "Alice", "Bob", "Bob", "Charlie", "Charlie", "Charlie"]
selector.history_manager.history = ["Alice", "Bob", "Charlie"]
selected = selector.select(users)
if selected in ["Bob", "Charlie"]:
count += 1
self.assertGreater(count, 0)
def test_select_non_excluded_3_users_with_1_selection_gap__(self):
count = 0
for _ in range(0, 100):
selector = UserSelector(exclude_gap=2)
users = ["Alice", "Alice", "Bob", "Bob", "Charlie", "Charlie", "Charlie"]
selector.history_manager.history = ["Alice", "Bob", "Charlie"]
selected = selector.select(users)
if selected in ["Charlie"]:
count += 1
self.assertEqual(count, 0)
def test_select_non_excluded_3_users_with_2_selection_gap__(self):
count = 0
for _ in range(0, 100):
selector = UserSelector(exclude_gap=2)
users = ["Alice", "Alice", "Bob", "Bob", "Charlie", "Charlie", "Charlie"]
selector.history_manager.history = ["Alice", "Bob", "Charlie"]
selected = selector.select(users)
if selected in ["Bob", "Charlie"]:
count += 1
self.assertEqual(count, 0)
def test_select_non_excluded_3_users_with_3_selection_gap__(self):
count = 0
for _ in range(0, 100):
selector = UserSelector(exclude_gap=3)
users = ["Alice", "Alice", "Bob", "Bob", "Charlie", "Charlie", "Charlie", "Dave"]
selector.history_manager.history = ["Alice", "Bob", "Charlie"]
selected = selector.select(users)
if selected in ["Alice", "Bob", "Charlie"]:
count += 1
self.assertEqual(count, 0)
def test_select_non_excluded_3_users_with_ids(self):
selector = UserSelector(exclude_gap=2)
users = [(1, "Alice"), (1, "Alice"), (2, "Bob"), (2, "Bob"), (3, "Charlie"), (3, "Charlie"), (3, "Charlie")]
selector.history_manager.history = [(1, "Alice"), (2, "Bob"), (3, "Charlie")]
expected_selections = [(1, "Alice"), (2, "Bob"), (3, "Charlie"), (1, "Alice"), (2, "Bob"), (3, "Charlie")]
for expected_selection in expected_selections:
selected = selector.select(users)
self.assertEqual(selected, expected_selection)
def test_select_never_picks_last_n_from_history(self):
for _ in range(100):
selector = UserSelector(exclude_gap=2)
users = \
["Alice", "Alice", "Bob", "Bob", "Charlie", "Charlie", "Charlie", "Diana", "Diana", "David", "David"]
selector.history_manager.history = ["Alice", "Bob", "Charlie", "Diana"]
selected = selector.select(users)
self.assertIn(selected, ["Alice", "Bob", "David"])
def test_history_maintenance(self):
selector = UserSelector(exclude_gap=2)
users = ["Alice", "Bob", "Charlie", "Diana"]
selections = []
for _ in range(100):
selected = selector.select(users)
selections.append(selected)
self.assertEqual(selector.history_manager.history, selections[-10:])
if __name__ == "__main__":
unittest.main()