Skip to content

Commit a33caa6

Browse files
committed
refactor(tests): simplify fixtures with partial
fixture returns a function taking a name that, when called, returns a function taking a module that, when called, patches the name in the module prior to this refactor, every fixture in the conftest returned a function taking a module that, when called, patched a name in the module that was hardcoded this refactor centralizes the logic and allows for simpler fixtures in the conftest
1 parent eb9fa59 commit a33caa6

File tree

1 file changed

+53
-100
lines changed

1 file changed

+53
-100
lines changed

tests/conftest.py

+53-100
Original file line numberDiff line numberDiff line change
@@ -1,174 +1,127 @@
11
import pytest
22

3+
from compiler_admin.commands import RESULT_SUCCESS
4+
35

46
@pytest.fixture
5-
def mock_commands_create(mocker):
6-
"""Fixture returns a function that patches commands.create in a given module."""
7+
def mock_module_name(mocker):
8+
"""Fixture returns a function taking a name, that returns a function taking a module,
9+
patching the given name in the given module.
710
8-
def _mock_commands_create(module, **kwargs):
9-
return mocker.patch(f"{module}.create", **kwargs)
11+
By default, the patched object is given a return_value = RESULT_SUCCESS.
12+
"""
1013

11-
return _mock_commands_create
14+
def _mock_module_name(name):
15+
def __mock_module_name(module):
16+
patched = mocker.patch(f"{module}.{name}")
17+
patched.return_value = RESULT_SUCCESS
18+
return patched
1219

20+
return __mock_module_name
1321

14-
@pytest.fixture
15-
def mock_commands_convert(mocker):
16-
"""Fixture returns a function that patches commands.convert in a given module."""
22+
return _mock_module_name
1723

18-
def _mock_commands_convert(module, **kwargs):
19-
return mocker.patch(f"{module}.convert", **kwargs)
2024

21-
return _mock_commands_convert
25+
@pytest.fixture
26+
def mock_commands_create(mock_module_name):
27+
"""Fixture returns a function that patches commands.create in a given module."""
28+
return mock_module_name("create")
2229

2330

2431
@pytest.fixture
25-
def mock_commands_delete(mocker):
26-
"""Fixture returns a function that patches commands.delete in a given module."""
32+
def mock_commands_convert(mock_module_name):
33+
"""Fixture returns a function that patches commands.convert in a given module."""
34+
return mock_module_name("convert")
2735

28-
def _mock_commands_delete(module, **kwargs):
29-
return mocker.patch(f"{module}.delete", **kwargs)
3036

31-
return _mock_commands_delete
37+
@pytest.fixture
38+
def mock_commands_delete(mock_module_name):
39+
"""Fixture returns a function that patches commands.delete in a given module."""
40+
return mock_module_name("delete")
3241

3342

3443
@pytest.fixture
35-
def mock_commands_info(mocker):
44+
def mock_commands_info(mock_module_name):
3645
"""Fixture returns a function that patches commands.info in a given module."""
37-
38-
def _mock_commands_info(module, **kwargs):
39-
return mocker.patch(f"{module}.info", **kwargs)
40-
41-
return _mock_commands_info
46+
return mock_module_name("info")
4247

4348

4449
@pytest.fixture
45-
def mock_commands_init(mocker):
50+
def mock_commands_init(mock_module_name):
4651
"""Fixture returns a function that patches commands.init in a given module."""
47-
48-
def _mock_commands_init(module, **kwargs):
49-
return mocker.patch(f"{module}.init", **kwargs)
50-
51-
return _mock_commands_init
52+
return mock_module_name("init")
5253

5354

5455
@pytest.fixture
55-
def mock_commands_offboard(mocker):
56+
def mock_commands_offboard(mock_module_name):
5657
"""Fixture returns a function that patches commands.offboard in a given module."""
57-
58-
def _mock_commands_offboard(module, **kwargs):
59-
return mocker.patch(f"{module}.offboard", **kwargs)
60-
61-
return _mock_commands_offboard
58+
return mock_module_name("offboard")
6259

6360

6461
@pytest.fixture
65-
def mock_commands_restore(mocker):
62+
def mock_commands_restore(mock_module_name):
6663
"""Fixture returns a function that patches commands.restore in a given module."""
67-
68-
def _mock_commands_restore(module, **kwargs):
69-
return mocker.patch(f"{module}.restore", **kwargs)
70-
71-
return _mock_commands_restore
64+
return mock_module_name("restore")
7265

7366

7467
@pytest.fixture
75-
def mock_commands_signout(mocker):
68+
def mock_commands_signout(mock_module_name):
7669
"""Fixture returns a function that patches commands.signout in a given module."""
77-
78-
def _mock_commands_signout(module, **kwargs):
79-
return mocker.patch(f"{module}.signout", **kwargs)
80-
81-
return _mock_commands_signout
70+
return mock_module_name("signout")
8271

8372

8473
@pytest.fixture
85-
def mock_google_CallGAMCommand(mocker):
74+
def mock_google_CallGAMCommand(mock_module_name):
8675
"""Fixture returns a function that patches the CallGAMCommand function from a given module."""
87-
88-
def _mock_google_CallGAMCommand(module, **kwargs):
89-
return mocker.patch(f"{module}.CallGAMCommand", **kwargs)
90-
91-
return _mock_google_CallGAMCommand
76+
return mock_module_name("CallGAMCommand")
9277

9378

9479
@pytest.fixture
95-
def mock_google_CallGYBCommand(mocker):
80+
def mock_google_CallGYBCommand(mock_module_name):
9681
"""Fixture returns a function that patches the CallGYBCommand function from a given module."""
97-
98-
def _mock_google_CallGYBCommand(module, **kwargs):
99-
return mocker.patch(f"{module}.CallGYBCommand", **kwargs)
100-
101-
return _mock_google_CallGYBCommand
82+
return mock_module_name("CallGYBCommand")
10283

10384

10485
@pytest.fixture
105-
def mock_google_add_user_to_group(mocker):
86+
def mock_google_add_user_to_group(mock_module_name):
10687
"""Fixture returns a function that patches the add_user_to_group function from a given module."""
107-
108-
def _mock_google_add_user_to_group(module, **kwargs):
109-
return mocker.patch(f"{module}.add_user_to_group", **kwargs)
110-
111-
return _mock_google_add_user_to_group
88+
return mock_module_name("add_user_to_group")
11289

11390

11491
@pytest.fixture
115-
def mock_google_move_user_ou(mocker):
92+
def mock_google_move_user_ou(mock_module_name):
11693
"""Fixture returns a function that patches the move_user_ou function from a given module."""
117-
118-
def _mock_google_move_user_ou(module, **kwargs):
119-
return mocker.patch(f"{module}.move_user_ou", **kwargs)
120-
121-
return _mock_google_move_user_ou
94+
return mock_module_name("move_user_ou")
12295

12396

12497
@pytest.fixture
125-
def mock_google_remove_user_from_group(mocker):
98+
def mock_google_remove_user_from_group(mock_module_name):
12699
"""Fixture returns a function that patches the remove_user_from_group function from a given module."""
127-
128-
def _mock_google_remove_user_from_group(module, **kwargs):
129-
return mocker.patch(f"{module}.remove_user_from_group", **kwargs)
130-
131-
return _mock_google_remove_user_from_group
100+
return mock_module_name("remove_user_from_group")
132101

133102

134103
@pytest.fixture
135-
def mock_google_user_exists(mocker):
104+
def mock_google_user_exists(mock_module_name):
136105
"""Fixture returns a function that patches the user_exists function from a given module."""
137-
138-
def _mock_google_user_exists(module, **kwargs):
139-
return mocker.patch(f"{module}.user_exists", **kwargs)
140-
141-
return _mock_google_user_exists
106+
return mock_module_name("user_exists")
142107

143108

144109
@pytest.fixture
145-
def mock_google_user_in_group(mocker):
110+
def mock_google_user_in_group(mock_module_name):
146111
"""Fixture returns a function that patches the user_in_group function from a given module."""
147-
148-
def _mock_google_user_in_group(module, **kwargs):
149-
return mocker.patch(f"{module}.user_in_group", **kwargs)
150-
151-
return _mock_google_user_in_group
112+
return mock_module_name("user_in_group")
152113

153114

154115
@pytest.fixture
155-
def mock_google_user_is_partner(mocker):
116+
def mock_google_user_is_partner(mock_module_name):
156117
"""Fixture returns a function that patches the user_is_partner function from a given module."""
157-
158-
def _mock_google_user_is_partner(module, **kwargs):
159-
return mocker.patch(f"{module}.user_is_partner", **kwargs)
160-
161-
return _mock_google_user_is_partner
118+
return mock_module_name("user_is_partner")
162119

163120

164121
@pytest.fixture
165-
def mock_google_user_is_staff(mocker):
122+
def mock_google_user_is_staff(mock_module_name):
166123
"""Fixture returns a function that patches the user_is_staff function from a given module."""
167-
168-
def _mock_google_user_is_staff(module, **kwargs):
169-
return mocker.patch(f"{module}.user_is_staff", **kwargs)
170-
171-
return _mock_google_user_is_staff
124+
return mock_module_name("user_is_staff")
172125

173126

174127
@pytest.fixture

0 commit comments

Comments
 (0)