-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpts-6.py
116 lines (95 loc) · 3.19 KB
/
pts-6.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
# Python Thoughts Snippet #6 - Test Driven Development
# Python 3.7
# 2019/12/02
# post: https://pythonicthoughtssnippets.github.io/#6-test-driven-development
# THIS CODE IS NOT MEANT TO BE FUNCTIONAL OR EXECUTABLE,
# IT IS A REPRESENTATION OF AN IDEA AND AN EXAMPLE TO RAISE DISCUSSION.
# As a common practice in my Pythonic Thoughts Snippets,
# I am not going into the details of the minor implementations,
# on doubts please search elsewhere on the web, there are countless of
# amazing explanations; here, we focus on the broader concept.
def random_fragment(iterable, fragsize=None):
"""
Generate a slice object that reflects a random fragment from iterable.
(iterable, int -> slice object)
Parameters
----------
iterable : iterable-type
An interable: string, list, tuple, etc.
fragsize : int or NoneType
The size of the fragment to generate.
Returns
-------
slice object
A slice object reflecting a random fragment of the `iterable`
of size `fragsize`.
If `fragsize` is ``None``, returns a full range slice object.
"""
# this is separate from the try: block to account input of for types
# that are now iterable and have not to do with the usage
# of fragsize=None
len_ = len(iterable)
try:
start = random.randint(0, len_ - fragsize)
except TypeError: # fragsize is None
return slice(None, None, None)
else:
return slice(start, start + fragsize, None)
# And these are the tests I wrote.
"""Test libutil."""
import pytest
# I know libutil is a very bad name. What does that mean? That other
# libs are not *util* ? :-P
from project.libs import libutil as UTIL
def test_random_fragment_return():
"""Test return type is slice object."""
result = UTIL.random_fragment([1, 2])
assert isinstance(result, slice)
@pytest.mark.parametrize(
'in1,fragsize,expected',
[
(list(range(1000)), 7, 7),
(list(range(1000)), 0, 0),
(list(range(1000)), None, 1000),
([], None, 0),
([], 0, 0),
('abcdefgh', 3, 3),
((1,2,3,4), 1, 1),
],
)
def test_random_fragment(in1, fragsize, expected):
"""
Test fragment has expected length.
Parametrize
-----------
1: list with fragsize < len(list)
2: list with fragsize == 0, should return empty list
3: list with fragsize is None, should return whole list
4: empty list with fragsize is None, should return empty list
5: empty list with fragsize is None, should return empty list
6: functionality for strings
7: functionality for tuples
"""
result = UTIL.random_fragment(in1, fragsize)
assert len(in1[result]) == expected
@pytest.mark.parametrize(
'in1,fragsize,error',
[
([], 7, ValueError),
(list(range(100)), 700, ValueError),
(9, 2, TypeError),
(9.0, 2, TypeError),
],
)
def test_random_fragment_errors(in1, fragsize, error):
"""
Test errors raised with input.
Parametrize
-----------
1: Empty list with fragsize > 0
2: list with fragsize > len(list)
3: int
4: float
"""
with pytest.raises(error):
UTIL.random_fragment(in1, fragsize)