-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_resorter.py
176 lines (138 loc) · 4.96 KB
/
test_resorter.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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
import subprocess
import csv
import pandas as pd
from resorter import (
parse_input,
determine_queries,
generate_bin_edges,
BradleyTerryModel,
)
from unittest.mock import patch
class TestCodeUnderTest:
# Test parsing input with two columns
def test_parsing_input_with_two_columns(self):
# Given
df = pd.DataFrame([["item1", 10], ["item2", 20]])
# When
items, scores = parse_input(df)
# Then
assert items == ["item1", "item2"]
assert scores == {"item1": 10, "item2": 20}
# Test parsing input with one column
def test_parsing_input_with_one_column(self):
# Given
df = pd.DataFrame(["item1", "item2"], columns=["Item"])
# When
items, scores = parse_input(df)
# Then
assert items == ["item1", "item2"]
assert scores is None
# Test determining number of queries with user input
def test_determining_number_of_queries_with_user_input(self):
# Given
items = ["item1", "item2", "item3"]
args_queries = 5
# When
queries = determine_queries(items, args_queries)
# Then
assert queries == 5
# Test parsing empty input
def test_parsing_empty_input(self):
# Given
df = pd.DataFrame([], columns=["Item"])
# When
items, scores = parse_input(df)
# Then
assert items == []
assert scores is None
# Test determining number of queries with empty input
def test_determining_number_of_queries_with_empty_input(self):
# Given
items = []
args_queries = None
# When
queries = determine_queries(items, args_queries)
# Then
assert queries == 0
# Test generating bin edges with empty data
def test_generating_bin_edges_with_empty_data(self):
# Given
data = []
# When
bin_edges = generate_bin_edges(data)
# Then
assert bin_edges is None
# Test determining number of queries without user input
def test_determining_number_of_queries_without_user_input(self):
# Given
items = ["item1", "item2", "item3", "item4"]
# When
num_queries = determine_queries(items, None)
# Then
# should be using int(ceil(len(items) * np.log(len(items)) + 1))
assert num_queries == 7
# Test generating bin edges with levels
def test_generating_bin_edges_with_levels(self):
# Given
data = [1, 2, 3, 4, 5]
levels = 3
# When
bin_edges = generate_bin_edges(data, levels=levels)
# Then
assert bin_edges is not None
assert len(bin_edges) == levels + 1
assert bin_edges[0] == 1
assert bin_edges[-1] == 5
# Test generating bin edges with quantiles
def test_generating_bin_edges_with_quantiles(self):
# Given
data = [1, 2, 3, 4, 5]
quantiles = 4
# When
bin_edges = generate_bin_edges(data, quantiles=quantiles)
# Then
assert bin_edges is not None
assert len(bin_edges) == quantiles + 1
assert bin_edges[0] == 1
assert bin_edges[-1] == 5
def test_standard_error_after_random_choice(mocker):
# Given
model = BradleyTerryModel(["item1", "item2", "item3"])
with patch("builtins.input", return_value="2"):
comparison_data = model.generate_comparison_data(1)
# When
item = comparison_data[0][0] # get the first item from the first comparison
alpha, beta = model.alpha_beta[item]
computed_se = model.standard_error(alpha, beta)
expected_se = (
(alpha * beta) / ((alpha + beta) ** 2 * (alpha + beta + 1))
) ** 0.5
# Then
assert computed_se == expected_se
def test_cli_integration(self):
# Set up test input file
with open("input.csv", "w", newline="") as csvfile:
writer = csv.writer(csvfile)
writer.writerow(["Inky"])
writer.writerow(["Pinky"])
writer.writerow(["Blinky"])
# Simulate the user input for the choices
user_input = "1\n1\n1\n1\n1"
# Call the CLI script and provide the simulated user input
result = subprocess.run(
["python", "resorter.py", "--input", "input.csv", "--output", "output.csv"],
input=user_input,
capture_output=True,
text=True,
)
# Check if the script ran successfully
assert result.returncode == 0
# (Optional) Check contents of out.csv or other expected behaviors
with open("output.csv", "r", newline="") as csvfile:
csv.reader(csvfile)
# reader = csv.reader(csvfile)
# Perform assertions based on the expected output in rows
# Future TODO: assertions on input / need to control random choice
# Cleanup
subprocess.run(["rm", "input.csv"])
subprocess.run(["rm", "output.csv"])