Skip to content

Commit 7e56aa0

Browse files
committed
[tests] Add test for the process function
1 parent 861f31d commit 7e56aa0

File tree

1 file changed

+74
-36
lines changed

1 file changed

+74
-36
lines changed

tests/test_compute.py

Lines changed: 74 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -42,76 +42,114 @@ def executeChunks(node, tmpPath, size):
4242
return logFiles
4343

4444

45+
_INPUTS = [
46+
desc.IntParam(
47+
name="input",
48+
label="Input",
49+
description="input",
50+
value=0,
51+
),
52+
]
53+
_OUTPUTS = [
54+
desc.IntParam(
55+
name="output",
56+
label="Output",
57+
description="Output",
58+
value=None,
59+
),
60+
]
61+
4562
class TestNodeA(desc.BaseNode):
63+
"""
64+
Test process with chunks
65+
"""
4666
__test__ = False
47-
67+
_size = 2
4868
size = desc.StaticNodeSize(2)
4969
parallelization = desc.Parallelization(blockSize=1)
50-
51-
inputs = [
52-
desc.IntParam(
53-
name="input",
54-
label="Input",
55-
description="input",
56-
value=0,
57-
),
58-
]
59-
outputs = [
60-
desc.IntParam(
61-
name="output",
62-
label="Output",
63-
description="Output",
64-
value=None,
65-
),
66-
]
70+
inputs = _INPUTS
71+
outputs = _OUTPUTS
6772

6873
def processChunk(self, chunk):
6974
chunk.logManager.start("info")
70-
chunk.logger.info("> Message A")
71-
LOGGER.info(f"> Message B")
75+
iteration = chunk.range.iteration
76+
nbBlocks = chunk.range.nbBlocks
77+
chunk.logger.info(f"> (chunk.logger) {chunk.node.name}")
78+
LOGGER.info(f"> (root logger) {iteration}/{nbBlocks}")
7279
chunk.logManager.end()
7380

7481

7582
class TestNodeB(TestNodeA):
83+
"""
84+
Test process with 1 chunk but still implementing processChunk
85+
"""
86+
__test__ = False
87+
_size = 1
88+
size = desc.StaticNodeSize(1)
89+
parallelization = None
90+
91+
92+
class TestNodeC(desc.BaseNode):
93+
"""
94+
Test process without chunks and without processChunk
95+
"""
96+
__test__ = False
7697
size = desc.StaticNodeSize(1)
7798
parallelization = None
99+
inputs = _INPUTS
100+
outputs = _OUTPUTS
101+
102+
def process(self, node):
103+
LOGGER.info(f"> {node.name}")
78104

79105

80106
class TestNodeLogger:
81107

82-
reA = re.compile(r"\[\d{2}:\d{2}:\d{2}\.\d{3}\]\[info\] > Message A")
83-
reB = re.compile(r"\[\d{2}:\d{2}:\d{2}\.\d{3}\]\[info\] > Message B")
84-
108+
logPrefix = r"\[\d{2}:\d{2}:\d{2}\.\d{3}\]\[info\] > "
109+
85110
@classmethod
86111
def setup_class(cls):
87112
registerNodeDesc(TestNodeA)
88113
registerNodeDesc(TestNodeB)
114+
registerNodeDesc(TestNodeC)
89115

90116
@classmethod
91117
def teardown_class(cls):
92118
unregisterNodeDesc(TestNodeA)
93119
unregisterNodeDesc(TestNodeB)
94-
95-
def test_nodeWithChunks(self, tmp_path):
120+
unregisterNodeDesc(TestNodeC)
121+
122+
def test_processChunks(self, tmp_path):
96123
graph = Graph("")
97124
graph._cacheDir = tmp_path
98-
node = graph.addNewNode(TestNodeA.__name__)
99-
# Compute
100-
logFiles = executeChunks(node, tmp_path, 2)
101-
for chunkId, logFile in logFiles.items():
125+
# TestNodeA : multiple chunks
126+
nodeA = graph.addNewNode(TestNodeA.__name__)
127+
logFiles = executeChunks(nodeA, tmp_path, 2)
128+
for chunkIndex, logFile in logFiles.items():
129+
with open(logFile, "r") as f:
130+
content = f.read()
131+
reg = re.compile(self.logPrefix + r"\(chunk.logger\) TestNodeA_1")
132+
assert len(reg.findall(content)) == 1
133+
reg = re.compile(self.logPrefix + r"\(root logger\) " + f"{chunkIndex}/2")
134+
assert len(reg.findall(content)) == 1
135+
# TestNodeA : single chunk
136+
nodeB = graph.addNewNode(TestNodeB.__name__)
137+
logFiles = executeChunks(nodeB, tmp_path, 1)
138+
for chunkIndex, logFile in logFiles.items():
102139
with open(logFile, "r") as f:
103140
content = f.read()
104-
assert len(self.reA.findall(content)) == 1
105-
assert len(self.reB.findall(content)) == 1
141+
reg = re.compile(self.logPrefix + r"\(chunk.logger\) TestNodeB_1")
142+
assert len(reg.findall(content)) == 1
143+
reg = re.compile(self.logPrefix + r"\(root logger\) 0/0")
144+
assert len(reg.findall(content)) == 1
106145

107-
def test_nodeWithoutChunks(self, tmp_path):
146+
def test_process(self, tmp_path):
108147
graph = Graph("")
109148
graph._cacheDir = tmp_path
110-
node = graph.addNewNode(TestNodeB.__name__)
111-
# Compute
149+
node = graph.addNewNode(TestNodeC.__name__)
112150
logFiles = executeChunks(node, tmp_path, 1)
113151
for _, logFile in logFiles.items():
114152
with open(logFile, "r") as f:
115153
content = f.read()
116-
assert len(self.reA.findall(content)) == 1
117-
assert len(self.reB.findall(content)) == 1
154+
reg = re.compile(self.logPrefix + "TestNodeC_1")
155+
assert len(reg.findall(content)) == 1

0 commit comments

Comments
 (0)