Skip to content

Commit 80d7f8f

Browse files
committed
test(unit/pusher): Cleanup actor tests
1 parent ab86ba9 commit 80d7f8f

File tree

6 files changed

+232
-407
lines changed

6 files changed

+232
-407
lines changed

tests/unit/actor/abstract_test_actor.py

Lines changed: 46 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ def recv_from_pipe(pipe, timeout):
109109
return None, None
110110

111111

112-
def define_database_content(content):
112+
def define_database_content(content: list):
113113
"""
114114
Decorator used to define database content when using an actor with database
115115
@@ -124,21 +124,52 @@ def wrap(func):
124124
return wrap
125125

126126

127-
def pytest_generate_tests_abstract(metafunc):
127+
def define_buffer_size(size: int):
128128
"""
129-
Function called by pytest when collecting a test_XXX function
129+
Define the buffer size by using the given parameter
130+
:param size: The buffer size
131+
"""
132+
133+
def wrap(func):
134+
setattr(func, '_buffer_size', size)
135+
return func
136+
137+
return wrap
130138

131-
define the content fixtures in test environment with collected the
132-
value _content if it exists or with an empty content
133139

134-
:param metafunc: the test context given by pytest
140+
def define_delay(delay: float):
141+
"""
142+
Define the delay by using the given parameter
143+
:param delay; The delay
144+
"""
145+
146+
def wrap(func):
147+
setattr(func, '_delay', delay)
148+
return func
149+
150+
return wrap
151+
152+
153+
def pytest_generate_tests(metafunc):
154+
"""
155+
Function called by pytest when collecting a test functions.
156+
Define the parameters required by the AbstractTestActorWithDB test class.
157+
:param metafunc: Test context given by pytest
135158
"""
136159
if 'content' in metafunc.fixturenames:
137160
content = getattr(metafunc.function, '_content', None)
138161
if isinstance(content, list):
139162
metafunc.parametrize('content', [content])
140-
else:
141-
metafunc.parametrize('content', [[]])
163+
164+
if 'buffer_size' in metafunc.fixturenames:
165+
buffer_size = getattr(metafunc.function, '_buffer_size', None)
166+
if isinstance(buffer_size, int):
167+
metafunc.parametrize('buffer_size', [buffer_size])
168+
169+
if 'delay' in metafunc.fixturenames:
170+
delay = getattr(metafunc.function, '_delay', None)
171+
if isinstance(delay, float):
172+
metafunc.parametrize('delay', [delay])
142173

143174

144175
@pytest.fixture
@@ -410,11 +441,16 @@ class AbstractTestActorWithDB:
410441

411442
@staticmethod
412443
@pytest.fixture
413-
def fake_db(content):
444+
def fake_db(request):
445+
content = []
446+
if hasattr(request, "param"):
447+
content = request.param.get("content", [])
448+
414449
return SilentFakeDB(content)
415450

451+
@staticmethod
416452
@pytest.fixture
417-
def actor_with_db(self, fake_db, delay, buffer_size):
453+
def actor_with_db(request, fake_db):
418454
raise NotImplementedError()
419455

420456
@staticmethod

tests/unit/conftest.py

Lines changed: 16 additions & 238 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,14 @@
2626
# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
2727
# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
2828
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29+
2930
from multiprocessing import active_children
3031

3132
import pytest
3233

34+
from powerapi.report import Report
35+
from tests.utils.db import SilentFakeDB
36+
3337

3438
@pytest.fixture
3539
def shutdown_system():
@@ -43,245 +47,19 @@ def shutdown_system():
4347

4448

4549
@pytest.fixture
46-
def power_timeline():
50+
def make_fake_database():
4751
"""
48-
Timeline of procfs report for the tests
52+
Factory fixture for creating a fake database with optional content.
4953
"""
54+
def _make(content: list[Report] | None = None) -> SilentFakeDB:
55+
return SilentFakeDB(content or [])
56+
57+
return _make
5058

51-
return [
52-
{
53-
"timestamp": "2021-09-14T12:37:37.168817",
54-
"sensor": "formula_group",
55-
"target": "all",
56-
"power": 42,
57-
},
58-
{
59-
"timestamp": "2021-09-14T12:37:37.669237",
60-
"sensor": "formula_group",
61-
"target": "all",
62-
"power": 42,
63-
},
64-
{
65-
"timestamp": "2021-09-14T12:37:38.170142",
66-
"sensor": "formula_group",
67-
"target": "all",
68-
"power": 42,
69-
},
70-
{
71-
"timestamp": "2021-09-14T12:37:38.670338",
72-
"sensor": "formula_group",
73-
"target": "all",
74-
"power": 42,
75-
},
76-
{
77-
"timestamp": "2021-09-14T12:37:39.171321",
78-
"sensor": "formula_group",
79-
"target": "all",
80-
"power": 42,
81-
},
82-
{
83-
"timestamp": "2021-09-14T12:37:39.671572",
84-
"sensor": "formula_group",
85-
"target": "all",
86-
"power": 42,
87-
},
88-
{
89-
"timestamp": "2021-09-14T12:37:40.172503",
90-
"sensor": "formula_group",
91-
"target": "all",
92-
"power": 42,
93-
},
94-
{
95-
"timestamp": "2021-09-14T12:37:40.672693",
96-
"sensor": "formula_group",
97-
"target": "all",
98-
"power": 42,
99-
},
100-
{
101-
"timestamp": "2021-09-14T12:37:41.173552",
102-
"sensor": "formula_group",
103-
"target": "all",
104-
"power": 42,
105-
},
106-
{
107-
"timestamp": "2021-09-14T12:37:41.673815",
108-
"sensor": "formula_group",
109-
"target": "all",
110-
"power": 42,
111-
},
112-
{
113-
"timestamp": "2021-09-14T12:37:42.174560",
114-
"sensor": "formula_group",
115-
"target": "all",
116-
"power": 42,
117-
},
118-
{
119-
"timestamp": "2021-09-14T12:37:42.674690",
120-
"sensor": "formula_group",
121-
"target": "all",
122-
"power": 42,
123-
},
124-
{
125-
"timestamp": "2021-09-14T12:37:43.175441",
126-
"sensor": "formula_group",
127-
"target": "all",
128-
"power": 42,
129-
},
130-
{
131-
"timestamp": "2021-09-14T12:37:43.675743",
132-
"sensor": "formula_group",
133-
"target": "all",
134-
"power": 42,
135-
},
136-
{
137-
"timestamp": "2021-09-14T12:37:44.176551",
138-
"sensor": "formula_group",
139-
"target": "all",
140-
"power": 42,
141-
},
142-
{
143-
"timestamp": "2021-09-14T12:37:44.677307",
144-
"sensor": "formula_group",
145-
"target": "all",
146-
"power": 42,
147-
},
148-
{
149-
"timestamp": "2021-09-14T12:37:45.178049",
150-
"sensor": "formula_group",
151-
"target": "all",
152-
"power": 42,
153-
},
154-
{
155-
"timestamp": "2021-09-14T12:37:45.678310",
156-
"sensor": "formula_group",
157-
"target": "all",
158-
"power": 42,
159-
},
160-
{
161-
"timestamp": "2021-09-14T12:37:46.179120",
162-
"sensor": "formula_group",
163-
"target": "all",
164-
"power": 42,
165-
},
166-
{
167-
"timestamp": "2021-09-14T12:37:46.679308",
168-
"sensor": "formula_group",
169-
"target": "all",
170-
"power": 42,
171-
},
172-
{
173-
"timestamp": "2021-09-14T12:37:47.180223",
174-
"sensor": "formula_group",
175-
"target": "all",
176-
"power": 42,
177-
},
178-
{
179-
"timestamp": "2021-09-14T12:37:47.680468",
180-
"sensor": "formula_group",
181-
"target": "all",
182-
"power": 42,
183-
},
184-
{
185-
"timestamp": "2021-09-14T12:37:48.181316",
186-
"sensor": "formula_group",
187-
"target": "all",
188-
"power": 42,
189-
},
190-
{
191-
"timestamp": "2021-09-14T12:37:48.681683",
192-
"sensor": "formula_group",
193-
"target": "all",
194-
"power": 42,
195-
},
196-
{
197-
"timestamp": "2021-09-14T12:37:49.182522",
198-
"sensor": "formula_group",
199-
"target": "all",
200-
"power": 42,
201-
},
202-
{
203-
"timestamp": "2021-09-14T12:37:49.682731",
204-
"sensor": "formula_group",
205-
"target": "all",
206-
"power": 42,
207-
},
208-
{
209-
"timestamp": "2021-09-14T12:37:50.183680",
210-
"sensor": "formula_group",
211-
"target": "all",
212-
"power": 42,
213-
},
214-
{
215-
"timestamp": "2021-09-14T12:37:50.683812",
216-
"sensor": "formula_group",
217-
"target": "all",
218-
"power": 42,
219-
},
220-
{
221-
"timestamp": "2021-09-14T12:37:51.184792",
222-
"sensor": "formula_group",
223-
"target": "all",
224-
"power": 42,
225-
},
226-
{
227-
"timestamp": "2021-09-14T12:37:51.685027",
228-
"sensor": "formula_group",
229-
"target": "all",
230-
"power": 42,
231-
},
232-
{
233-
"timestamp": "2021-09-14T12:37:52.185709",
234-
"sensor": "formula_group",
235-
"target": "all",
236-
"power": 42,
237-
},
238-
{
239-
"timestamp": "2021-09-14T12:37:52.686065",
240-
"sensor": "formula_group",
241-
"target": "all",
242-
"power": 42,
243-
},
244-
{
245-
"timestamp": "2021-09-14T12:37:53.186929",
246-
"sensor": "formula_group",
247-
"target": "all",
248-
"power": 42,
249-
},
250-
{
251-
"timestamp": "2021-09-14T12:37:53.687190",
252-
"sensor": "formula_group",
253-
"target": "all",
254-
"power": 42,
255-
},
256-
{
257-
"timestamp": "2021-09-14T12:37:54.188031",
258-
"sensor": "formula_group",
259-
"target": "all",
260-
"power": 42,
261-
},
262-
{
263-
"timestamp": "2021-09-14T12:37:54.688674",
264-
"sensor": "formula_group",
265-
"target": "all",
266-
"power": 42,
267-
},
268-
{
269-
"timestamp": "2021-09-14T12:37:55.189489",
270-
"sensor": "formula_group",
271-
"target": "all",
272-
"power": 42,
273-
},
274-
{
275-
"timestamp": "2021-09-14T12:37:55.690299",
276-
"sensor": "formula_group",
277-
"target": "all",
278-
"power": 42,
279-
},
280-
{
281-
"timestamp": "2021-09-14T12:37:56.191124",
282-
"sensor": "formula_group",
283-
"target": "all",
284-
"power": 42,
285-
},
28659

287-
]
60+
@pytest.fixture
61+
def fake_database(request) -> SilentFakeDB:
62+
"""
63+
Fixture to create a SilentFakeDB without content.
64+
"""
65+
return SilentFakeDB()

0 commit comments

Comments
 (0)