-
Notifications
You must be signed in to change notification settings - Fork 33
Expand file tree
/
Copy pathtest_12_functions_advanced.py
More file actions
351 lines (251 loc) · 9.45 KB
/
test_12_functions_advanced.py
File metadata and controls
351 lines (251 loc) · 9.45 KB
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
import pathlib
import random
import re
import time
import typing as t
from math import isclose
from string import ascii_lowercase as lowercase
from string import ascii_uppercase as uppercase # noqa: F401
from string import digits, punctuation # noqa: F401
import pytest
#
# Example: Randomize list
#
def reference_randomize_list(my_list: list[int]) -> list[int]:
return sorted(my_list, key=lambda x: random.random())
@pytest.mark.parametrize(
"my_list",
[
([1, 2, 3, 4]),
(list(range(100))),
],
)
def test_randomize_list(
function_to_test: t.Callable,
my_list: list[int],
):
# Skip randomization tests for trivial lists
if len(my_list) <= 1:
return
# Run the function multiple times to ensure randomization
results = []
for _ in range(10): # Run multiple times
# Use a copy to prevent the function from modifying the original
test_list = my_list.copy()
result = function_to_test(test_list)
# Check that the elements are preserved
assert sorted(result) == sorted(my_list)
results.append(result)
# For lists with enough elements, very unlikely to get the same result twice
# 1. Convert each of the results to tuples
# 2. {tuple(r) for r in results} create a set that deduplicates identical orderings
# 3. len(...) > 1 checks that we've seen at least two different orderings
if len(my_list) > 2:
# Check there's at least some variation in results
assert len({tuple(r) for r in results}) > 1, (
"Your function doesn't appear to randomize. You should not use random.seed()."
)
#
# Exercise: Password checker factory
#
def reference_password_checker_factory(
min_up: int, min_low: int, min_pun: int, min_dig: int
) -> t.Callable:
"""Password checker factory"""
# The `string` module contains a number of useful constants
import string
# The `sub` function from the operator module can be used to subtract two numbers
# sub(x, y) is equivalent to x - y
from operator import sub
def password_checker(password: str) -> tuple[bool, dict]:
"""Password checker function"""
# Counts the number of chars for each class in a password
counts = [
sum(1 for char in password if char in _class)
for _class in (
string.ascii_uppercase,
string.ascii_lowercase,
string.punctuation,
string.digits,
)
]
# Compare with requirements and calculate the differences
diffs = [
sub(*pair)
for pair in zip(counts, (min_up, min_low, min_pun, min_dig), strict=False)
]
result = dict(
zip(
("uppercase", "lowercase", "punctuation", "digits"), diffs, strict=False
)
)
return all(diff >= 0 for diff in diffs), result
return password_checker
def test_password_checker_factory_no_min_no_pw(function_to_test: t.Callable):
pc = function_to_test(0, 0, 0, 0)
result, details = pc("")
assert result
assert len(details) == 4
for value in details.values():
assert value == 0
def test_password_checker_factory_no_min_some_pw(function_to_test: t.Callable):
pc = function_to_test(0, 0, 0, 0)
result, details = pc("ABCDefgh!@#$1234")
assert result
assert len(details) == 4
for value in details.values():
assert value == 4
def test_password_checker_factory_simple_good(function_to_test: t.Callable):
pc = function_to_test(1, 2, 3, 4)
result, details = pc("Abc!@#1234")
assert result
assert len(details) == 4
for value in details.values():
assert value == 0
def test_password_checker_factory_simple_bad(function_to_test: t.Callable):
pc = function_to_test(1, 2, 3, 4)
result, details = pc("b!#234")
assert not result
assert len(details) == 4
for value in details.values():
assert value == -1
@pytest.mark.parametrize("onlyset", ["uppercase", "lowercase", "punctuation", "digits"])
def test_password_checker_factory_only_set_one(onlyset, function_to_test: t.Callable):
for source in ["uppercase", "lowercase", "punctuation", "digits"]:
if onlyset == source:
pw = globals()[source][:4]
pc = function_to_test(4, 4, 4, 4)
result, details = pc(pw) # type: ignore
assert not result
assert len(details) == 4
for key, value in details.items():
if key == onlyset:
assert value == 0
else:
assert value == -4
@pytest.mark.parametrize(
"donotset", ["uppercase", "lowercase", "punctuation", "digits"]
)
def test_password_checker_factory_only_ignore_one(
donotset, function_to_test: t.Callable
):
pw = ""
for source in ["uppercase", "lowercase", "punctuation", "digits"]:
if donotset == source:
continue
pw += globals()[source][:4]
pc = function_to_test(4, 4, 4, 4)
result, details = pc(pw)
assert not result
assert len(details) == 4
for key, value in details.items():
if key == donotset:
assert value == -4
else:
assert value == 0
#
# Exercise: Once per minute
#
def hello(name):
return f"Hello {name}!"
def reference_once(allowed_time: int = 15) -> t.Callable:
"""Decorator to run a function at most once"""
class TooSoonError(RuntimeError):
def __init__(self, wait: float):
super().__init__(f"Wait another {wait:.2f} seconds")
def decorator(func: t.Callable) -> t.Callable:
timer = 0.0
def wrapper(*args, **kwargs) -> t.Any:
"""Wrapper"""
nonlocal timer
if not timer:
timer = time.perf_counter()
return func(*args, **kwargs)
if (stop := time.perf_counter()) - timer < allowed_time:
raise TooSoonError(allowed_time - (stop - timer))
timer = time.perf_counter()
return func(*args, **kwargs)
return wrapper
return decorator
def test_once_simple(function_to_test: t.Callable) -> None:
_hello = function_to_test(5)(hello)
assert _hello("world") == "Hello world!"
def test_once_twice(function_to_test: t.Callable) -> None:
allowed_time = 5
_hello = function_to_test(allowed_time)(hello)
time.sleep(allowed_time)
assert _hello("world") == "Hello world!"
with pytest.raises(RuntimeError) as err:
_hello("world 2")
assert err.type is RuntimeError
assert "Wait another" in err.value.args[0]
wait_time = re.search(r"[\d.]+", err.value.args[0])
assert wait_time and isclose(float(wait_time.group()), 5.0, abs_tol=1e-2)
def test_once_waiting_not_enough_time(function_to_test: t.Callable) -> None:
allowed_time = 10
_hello = function_to_test(allowed_time)(hello)
time.sleep(allowed_time)
assert _hello("world") == "Hello world!"
time.sleep(allowed_time - 1)
with pytest.raises(RuntimeError) as err:
_hello("world 2")
assert err.type is RuntimeError
assert "Wait another" in err.value.args[0]
wait_time = re.search(r"[\d.]+", err.value.args[0])
assert wait_time and isclose(float(wait_time.group()), 1.0, abs_tol=1e-2)
def test_once_waiting_enough_time(function_to_test: t.Callable) -> None:
# Test that waiting the allowed time lets the function run again
allowed_time = 2
_hello = function_to_test(allowed_time)(hello)
# First call should work
assert _hello("world") == "Hello world!"
# Wait for the full allowed time
time.sleep(allowed_time + 0.1) # Add small buffer to avoid timing issues
# Second call should work
assert _hello("world 2") == "Hello world 2!"
#
# Exercise: String range
#
def reference_str_range(start: str, end: str, step: int = 1) -> t.Iterator[str]:
"""Return an iterator of strings from start to end, inclusive"""
for i in range(ord(start), ord(end) + (1 if step > 0 else -1), step):
yield chr(i)
def test_str_range_same_start_end(function_to_test: t.Callable):
r = function_to_test("a", "a")
assert iter(r) == r
assert "".join(list(r)) == "a"
def test_str_range_simple(function_to_test: t.Callable):
r = function_to_test("a", "c")
assert "".join(list(r)) == "abc"
def test_str_range_simple_with_step(function_to_test: t.Callable):
r = function_to_test("a", "c", 2)
assert "".join(list(r)) == "ac"
def test_str_range_simple_with_negativestep(function_to_test: t.Callable):
r = function_to_test("c", "a", -2)
assert "".join(list(r)) == "ca"
def test_str_range_hebrew(function_to_test: t.Callable):
r = function_to_test("א", "ז", 2)
assert "".join(list(r)) == "אגהז"
#
# Exercise Read n lines
#
def reference_read_n_lines(filename: str, lines: int):
with open(filename) as file:
while True:
first_line = file.readline()
if not first_line:
break
yield first_line + "".join(file.readline() for _ in range(lines - 1))
def create_alphabet_file(tmp_path: pathlib.Path):
d = tmp_path / "sub"
d.mkdir()
p = d / "alphabet.txt"
text = "\n".join(f"{one_letter * 20}" for one_letter in lowercase) + "\n"
p.write_text(text)
return p
@pytest.mark.parametrize("n,expected", [(1, 26), (2, 13), (3, 9), (4, 7)])
def test_read_n_lines(tmp_path, n, expected, function_to_test: t.Callable):
p = create_alphabet_file(tmp_path)
i = function_to_test(p, n)
assert i == iter(i)
assert len(list(i)) == expected