Skip to content

Commit c059158

Browse files
authored
Merge pull request #13 from timothyhull:timothyhull/issue12
pytest documentation updates
2 parents 9200f2c + ad97dd0 commit c059158

File tree

2 files changed

+95
-42
lines changed

2 files changed

+95
-42
lines changed

.devcontainer/devcontainer.json

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,16 @@
55
"context": ".."
66
},
77
"extensions": [
8-
"davidanson.vscode-markdownlint",
9-
"docsmsft.docs-markdown",
10-
"docsmsft.docs-yaml",
11-
"ms-azuretools.vscode-docker",
12-
"ms-python.python",
13-
"ms-python.vscode-pylance",
14-
"redhat.vscode-yaml",
15-
"streetsidesoftware.code-spell-checker"
16-
],
8+
"davidanson.vscode-markdownlint",
9+
"docsmsft.docs-markdown",
10+
"docsmsft.docs-yaml",
11+
"github.vscode-pull-request-github",
12+
"ms-azuretools.vscode-docker",
13+
"ms-python.python",
14+
"ms-python.vscode-pylance",
15+
"redhat.vscode-yaml",
16+
"streetsidesoftware.code-spell-checker",
17+
],
1718
"settings": {
1819
"#terminal.integrated.defaultProfile.linux#": "/bin/bash",
1920
"[yaml]": {

tests/test_namedtuple_maker.py

Lines changed: 85 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -2,47 +2,65 @@
22
''' pytest tests for namedtuple_maker.py
33
44
Usage:
5-
pytest test_namedtuple_maker.py namedtuple_maker.py
5+
pytest tests/test_namedtuple_maker.py
66
'''
77

88
# Imports
99
from namedtuple_maker.namedtuple_maker import named_tuple_converter, \
10-
make_named_tuple, TEST_DATA
10+
make_named_tuple, TEST_DATA
1111
from collections import namedtuple
1212
from pytest import mark
1313
from typing import Iterable
1414
from unittest.mock import patch
1515

1616
# Constants
17+
# Create a namedtuple object for synthetic testing
1718
TEST_PERSON_INFO = namedtuple(
1819
typename='NamedTuple',
1920
field_names=TEST_DATA.keys()
2021
)
22+
23+
# Instantiate an object from the testing namedtuple, add test data values
2124
TEST_EXPECTED_RESULT = TEST_PERSON_INFO(**TEST_DATA)
25+
26+
# Create a tuple of attribute names with invalid characters
2227
TEST_INVALID_ATTRIBUTE_NAMES_DATA = (
2328
'_first_name',
2429
'$1_las*t_nam)(e',
2530
'+_age',
2631
'hair color #',
2732
' 4eye color !@'
2833
)
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))
3040

3141

3242
@mark.parametrize(
43+
44+
# Test data argument names (descriptions in test function docstring)
3345
argnames=[
3446
'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
3850
],
51+
52+
# Test data argument values
3953
argvalues=[
54+
55+
# Test #1 values, to test normal operation
4056
[
4157
TEST_DATA.values(),
4258
TEST_DATA.keys(),
4359
TEST_DATA.values(),
4460
TEST_DATA.keys()
4561
],
62+
63+
# Test #2 values, to test auto-modification of invalid attribute names
4664
[
4765
TEST_DATA.values(),
4866
TEST_INVALID_ATTRIBUTE_NAMES_DATA,
@@ -58,76 +76,93 @@ def test_named_tuple_converter(
5876
att_return
5977
) -> None:
6078
''' 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.
6482
6583
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.
6795
6896
Returns:
6997
None.
7098
'''
7199

72-
# Get a result to test
100+
# Get a namedtuple result to test
73101
test_result = make_named_tuple(
74102
iterable_input=iter_input,
75103
attribute_names=att_names
76104
)
77105

78-
# Verify the result is of type NamedTuple
106+
# Verify the test result object is of type NamedTuple
79107
assert 'NamedTuple' in str(test_result.__class__)
80108

81-
# Verify the test tuple input data equals the namedtuple attribute values
109+
# Verify the iterable input equals the namedtuple attribute values
82110
assert tuple(iter_return) == tuple(test_result._asdict().values())
83111

84-
# Verify the field names input data equals the namedtuple attribute names
112+
# Verify the attribute names input equals the namedtuple attribute names
85113
assert tuple(att_return) == test_result._fields
86114

87115

88116
@patch(
117+
118+
# Create mock namedtuple attribute name data for the input() function
89119
'builtins.input',
90120
side_effect=TEST_DATA.keys()
91121
)
92122
def test_named_tuple_converter_input(side_effects) -> None:
93123
''' 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.
97127
98128
Args:
99-
None.
129+
side_effects (unittest.mock.patch):
130+
namedtuple attribute mock data for the input() function.
100131
101132
Returns:
102133
None.
103134
'''
104135

105-
# Get a result to test
136+
# Get a namedtuple result to test
106137
test_result = make_named_tuple(
107138
iterable_input=TEST_DATA.values(),
108139
)
109140

110-
# Verify the result is of type NamedTuple
141+
# Verify the test result object is of type NamedTuple
111142
assert 'NamedTuple' in str(test_result.__class__)
112143

113-
# Verify the test tuple input data equals the namedtuple attribute values
144+
# Verify the iterable input equals the namedtuple attribute values
114145
assert tuple(TEST_DATA.values()) == tuple(test_result._asdict().values())
115146

116-
# Verify the field names input data equals the namedtuple attribute names
147+
# Verify the attribute names input equals the namedtuple attribute names
117148
assert tuple(TEST_DATA.keys()) == test_result._fields
118149

119150

120151
@mark.parametrize(
152+
153+
# Test data argument names (descriptions in test function docstring)
121154
argnames=[
122155
'iter_input',
123156
'iter_return',
124157
'att_return'
125158
],
159+
160+
# Test #1 values, to test normal operation
126161
argvalues=[
127162
[
128163
TEST_DATA.values(),
129164
TEST_DATA.values(),
130-
TEST_AUTO_NAMED_ATTRIBUTED_DATA
165+
TEST_AUTO_ATTRIBUTE_NAME_DATA
131166
]
132167
]
133168
)
@@ -137,14 +172,21 @@ def test_named_tuple_converter_custom_function_auto_name_attributes(
137172
att_return
138173
) -> None:
139174
''' 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
142177
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.
145180
146181
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.
148190
149191
Returns:
150192
None.
@@ -156,22 +198,32 @@ def custom_function(
156198
iterable_input: Iterable,
157199
auto_attribute_names: bool
158200
) -> 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.
160212
'''
161213

162214
return tuple(iter_input)
163215

164-
# Get a result to test
216+
# Get a namedtuple result to test
165217
test_result = custom_function(
166218
iterable_input=iter_input,
167219
auto_attribute_names=True
168220
)
169221

170-
# Verify the result is of type NamedTuple
222+
# Verify the test result object is of type NamedTuple
171223
assert 'NamedTuple' in str(test_result.__class__)
172224

173-
# Verify the test tuple input data equals the namedtuple attribute values
225+
# Verify the iterable input equals the namedtuple attribute values
174226
assert tuple(iter_return) == tuple(test_result._asdict().values())
175227

176-
# Verify the field names input data equals the namedtuple attribute names
228+
# Verify the attribute names input equals the namedtuple attribute names
177229
assert tuple(att_return) == test_result._fields

0 commit comments

Comments
 (0)