2
2
''' pytest tests for namedtuple_maker.py
3
3
4
4
Usage:
5
- pytest test_namedtuple_maker.py namedtuple_maker .py
5
+ pytest tests/ test_namedtuple_maker.py
6
6
'''
7
7
8
8
# Imports
9
9
from namedtuple_maker .namedtuple_maker import named_tuple_converter , \
10
- make_named_tuple , TEST_DATA
10
+ make_named_tuple , TEST_DATA
11
11
from collections import namedtuple
12
12
from pytest import mark
13
13
from typing import Iterable
14
14
from unittest .mock import patch
15
15
16
16
# Constants
17
+ # Create a namedtuple object for synthetic testing
17
18
TEST_PERSON_INFO = namedtuple (
18
19
typename = 'NamedTuple' ,
19
20
field_names = TEST_DATA .keys ()
20
21
)
22
+
23
+ # Instantiate an object from the testing namedtuple, add test data values
21
24
TEST_EXPECTED_RESULT = TEST_PERSON_INFO (** TEST_DATA )
25
+
26
+ # Create a tuple of attribute names with invalid characters
22
27
TEST_INVALID_ATTRIBUTE_NAMES_DATA = (
23
28
'_first_name' ,
24
29
'$1_las*t_nam)(e' ,
25
30
'+_age' ,
26
31
'hair color #' ,
27
32
' 4eye color !@'
28
33
)
29
- TEST_AUTO_NAMED_ATTRIBUTED_DATA = (f'index_{ i } ' for i in range (5 ))
34
+
35
+ # Capture the number of values in the TEST_DATA object
36
+ TEST_DATA_LENGTH = len (TEST_DATA .values ())
37
+
38
+ # Create a set of auto-generated attribute names (TEST_DATA_LENGTH # of items)
39
+ TEST_AUTO_ATTRIBUTE_NAME_DATA = (f'index_{ i } ' for i in range (TEST_DATA_LENGTH ))
30
40
31
41
32
42
@mark .parametrize (
43
+
44
+ # Test data argument names (descriptions in test function docstring)
33
45
argnames = [
34
46
'iter_input' ,
35
- 'att_names' ,
36
- 'iter_return' ,
37
- 'att_return'
47
+ 'att_names' , # namedtuple attribute names
48
+ 'iter_return' , # Expected attribute value names
49
+ 'att_return' # Expected attribute name results
38
50
],
51
+
52
+ # Test data argument values
39
53
argvalues = [
54
+
55
+ # Test #1 values, to test normal operation
40
56
[
41
57
TEST_DATA .values (),
42
58
TEST_DATA .keys (),
43
59
TEST_DATA .values (),
44
60
TEST_DATA .keys ()
45
61
],
62
+
63
+ # Test #2 values, to test auto-modification of invalid attribute names
46
64
[
47
65
TEST_DATA .values (),
48
66
TEST_INVALID_ATTRIBUTE_NAMES_DATA ,
@@ -58,76 +76,93 @@ def test_named_tuple_converter(
58
76
att_return
59
77
) -> None :
60
78
''' Test of the named_tuple_converter decorator function to determine if
61
- the function accepts a tuple argument and returns a namedtuple
62
- with the original tuple data. Pass the namedtuple field names
63
- as an argument.
79
+ the function accepts an iterable argument and returns a namedtuple
80
+ with the original iterable data values. Collect the namedtuple field
81
+ names from an iterable of names passed an argument.
64
82
65
83
Args:
66
- None.
84
+ iter_input (Iterable):
85
+ Iterable input to be converted to namedtuple attribute values.
86
+
87
+ att_names (Iterable):
88
+ Iterable input of namedtuple attribute names.
89
+
90
+ iter_return (Iterable):
91
+ Iterable output of expected namedtuple attribute values.
92
+
93
+ att_return (Iterable):
94
+ Iterable output of expected namedtuple attribute names.
67
95
68
96
Returns:
69
97
None.
70
98
'''
71
99
72
- # Get a result to test
100
+ # Get a namedtuple result to test
73
101
test_result = make_named_tuple (
74
102
iterable_input = iter_input ,
75
103
attribute_names = att_names
76
104
)
77
105
78
- # Verify the result is of type NamedTuple
106
+ # Verify the test result object is of type NamedTuple
79
107
assert 'NamedTuple' in str (test_result .__class__ )
80
108
81
- # Verify the test tuple input data equals the namedtuple attribute values
109
+ # Verify the iterable input equals the namedtuple attribute values
82
110
assert tuple (iter_return ) == tuple (test_result ._asdict ().values ())
83
111
84
- # Verify the field names input data equals the namedtuple attribute names
112
+ # Verify the attribute names input equals the namedtuple attribute names
85
113
assert tuple (att_return ) == test_result ._fields
86
114
87
115
88
116
@patch (
117
+
118
+ # Create mock namedtuple attribute name data for the input() function
89
119
'builtins.input' ,
90
120
side_effect = TEST_DATA .keys ()
91
121
)
92
122
def test_named_tuple_converter_input (side_effects ) -> None :
93
123
''' Test of the named_tuple_converter decorator function to determine if
94
- the function accepts a tuple argument and returns a namedtuple
95
- with the original tuple data. Collect the field names with the
96
- input() function.
124
+ the function accepts an iterable argument and returns a namedtuple
125
+ with the original iterable data values . Collect the namedtuple
126
+ attribute names with the input() function.
97
127
98
128
Args:
99
- None.
129
+ side_effects (unittest.mock.patch):
130
+ namedtuple attribute mock data for the input() function.
100
131
101
132
Returns:
102
133
None.
103
134
'''
104
135
105
- # Get a result to test
136
+ # Get a namedtuple result to test
106
137
test_result = make_named_tuple (
107
138
iterable_input = TEST_DATA .values (),
108
139
)
109
140
110
- # Verify the result is of type NamedTuple
141
+ # Verify the test result object is of type NamedTuple
111
142
assert 'NamedTuple' in str (test_result .__class__ )
112
143
113
- # Verify the test tuple input data equals the namedtuple attribute values
144
+ # Verify the iterable input equals the namedtuple attribute values
114
145
assert tuple (TEST_DATA .values ()) == tuple (test_result ._asdict ().values ())
115
146
116
- # Verify the field names input data equals the namedtuple attribute names
147
+ # Verify the attribute names input equals the namedtuple attribute names
117
148
assert tuple (TEST_DATA .keys ()) == test_result ._fields
118
149
119
150
120
151
@mark .parametrize (
152
+
153
+ # Test data argument names (descriptions in test function docstring)
121
154
argnames = [
122
155
'iter_input' ,
123
156
'iter_return' ,
124
157
'att_return'
125
158
],
159
+
160
+ # Test #1 values, to test normal operation
126
161
argvalues = [
127
162
[
128
163
TEST_DATA .values (),
129
164
TEST_DATA .values (),
130
- TEST_AUTO_NAMED_ATTRIBUTED_DATA
165
+ TEST_AUTO_ATTRIBUTE_NAME_DATA
131
166
]
132
167
]
133
168
)
@@ -137,14 +172,21 @@ def test_named_tuple_converter_custom_function_auto_name_attributes(
137
172
att_return
138
173
) -> None :
139
174
''' Test of the named_tuple_converter decorator function using a custom
140
- function to determine if the function accepts a tuple argument
141
- and returns a namedtuple with the original tuple data, and if the
175
+ function to determine if the function accepts an iterable argument
176
+ and returns a namedtuple with the original iterable data, and if the
142
177
decorator function automatically names the namedtuple attributes
143
- with the auto_attribute_names parameter set to True. Pass the
144
- namedtuple field names as an argument.
178
+ with the auto_attribute_names parameter set to True. Collect the
179
+ namedtuple field names from an iterable of names passed an argument.
145
180
146
181
Args:
147
- None.
182
+ iter_input (Iterable):
183
+ Iterable input to be converted to namedtuple attribute values.
184
+
185
+ att_names (Iterable):
186
+ Iterable input of namedtuple attribute names.
187
+
188
+ att_return (Iterable):
189
+ Iterable output of expected namedtuple attribute names.
148
190
149
191
Returns:
150
192
None.
@@ -156,22 +198,32 @@ def custom_function(
156
198
iterable_input : Iterable ,
157
199
auto_attribute_names : bool
158
200
) -> tuple :
159
- '''
201
+ ''' Test function that accepts an iterable object as input,
202
+ and returns the iterable as a tuple object.
203
+
204
+ Args:
205
+ iterable_input (Iterable):
206
+ Iterable object to convert to a tuple object.
207
+
208
+ auto_attribute_names (bool):
209
+ Boolean to specify whether or not namedtuple attribute
210
+ names are automatically generated or provided as a
211
+ separate argument.
160
212
'''
161
213
162
214
return tuple (iter_input )
163
215
164
- # Get a result to test
216
+ # Get a namedtuple result to test
165
217
test_result = custom_function (
166
218
iterable_input = iter_input ,
167
219
auto_attribute_names = True
168
220
)
169
221
170
- # Verify the result is of type NamedTuple
222
+ # Verify the test result object is of type NamedTuple
171
223
assert 'NamedTuple' in str (test_result .__class__ )
172
224
173
- # Verify the test tuple input data equals the namedtuple attribute values
225
+ # Verify the iterable input equals the namedtuple attribute values
174
226
assert tuple (iter_return ) == tuple (test_result ._asdict ().values ())
175
227
176
- # Verify the field names input data equals the namedtuple attribute names
228
+ # Verify the attribute names input equals the namedtuple attribute names
177
229
assert tuple (att_return ) == test_result ._fields
0 commit comments