Skip to content

Commit 4c9f5c0

Browse files
add tests for regex
1 parent b6c756c commit 4c9f5c0

File tree

2 files changed

+52
-1
lines changed

2 files changed

+52
-1
lines changed

tests/scalability/test_scalabilityHandler.py

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
import pytest
44
import tempfile, json
5-
from feelpp.benchmarking.reframe.scalability import ScalabilityHandler, CsvExtractor,TsvExtractor,JsonExtractor,Extractor,ExtractorFactory
5+
from feelpp.benchmarking.reframe.scalability import ScalabilityHandler, CsvExtractor,TsvExtractor,JsonExtractor,RegexExtractor,Extractor,ExtractorFactory
66
import numpy as np
77

88
class StageMocker:
@@ -83,6 +83,33 @@ def test_extractTsv(self):
8383
file.close()
8484

8585

86+
87+
def test_extractRegex(self):
88+
""" Test extracting performance variables using regex from a file """
89+
90+
file = tempfile.NamedTemporaryFile(mode="w+")
91+
content = "assembly: 0.012\nsolve: 1.42\npostprocess: 0.08"
92+
file.write(content)
93+
file.flush()
94+
95+
pattern = r"^(?P<name>[^:]+):\s*(?P<value>[\d.]+)$"
96+
extractor = RegexExtractor(
97+
filepath=file.name,
98+
stage_name="timers",
99+
pattern=pattern,
100+
variable_name_group="name",
101+
variable_value_group="value",
102+
units={"*":"s"}
103+
)
104+
perfvars = extractor.extract()
105+
assert perfvars["timers_assembly"].evaluate() == 0.012
106+
assert perfvars["timers_solve"].evaluate() == 1.42
107+
assert perfvars["timers_postprocess"].evaluate() == 0.08
108+
109+
file.close()
110+
111+
112+
86113
def test_extractJson(self):
87114
""" Test performance variable extraction for JSON files"""
88115
file = tempfile.NamedTemporaryFile()

tests/scalability/test_scalabilityValidation.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,5 +25,29 @@ def test_format(self):
2525
stage = Stage(**{"name":"test_stage","filepath":"test_filepath","format":"csv"})
2626
assert stage.variables_path == []
2727

28+
def test_regex_validation(self):
29+
""" Tests mandatory regex fields and named/numbered groups """
30+
# Missing pattern
31+
with pytest.raises(ValidationError, match="regex must be specified if format == regex"):
32+
Stage(**{"name": "r_stage", "filepath": "file.txt", "format": "regex", "variable_value_group": "value"})
33+
34+
# Missing variable_value_group
35+
with pytest.raises(ValidationError, match="variable_value_group must be specified if format == regex"):
36+
Stage(**{"name": "r_stage", "filepath": "file.txt", "format": "regex", "pattern": ".*"})
37+
38+
# Valid named capture groups
39+
stage = Stage(
40+
name="r_stage",
41+
filepath="file.txt",
42+
format="regex",
43+
pattern="^(?P<name>[^:]+):\\s*(?P<value>[\\d.]+)$",
44+
variable_name_group="name",
45+
variable_value_group="value"
46+
)
47+
assert stage.format == "regex"
48+
assert stage.variable_name_group == "name"
49+
assert stage.variable_value_group == "value"
50+
51+
2852
class TestAppOutput:
2953
pass

0 commit comments

Comments
 (0)