Skip to content

Commit 04bed65

Browse files
committed
CI tests for helper functions + .txt file
1 parent d5e37ca commit 04bed65

File tree

2 files changed

+130
-0
lines changed

2 files changed

+130
-0
lines changed

tests/data/expected_structure.txt

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
2+
File structure of all samples with branch filter '':
3+
4+
---------------------------
5+
📁 Sample: test_file
6+
---------------------------
7+
8+
🌳 Tree: background
9+
├── Branches:
10+
│ ├── branch1 ; dtype: AsDtype('>f8')
11+
│ ├── branch2 ; dtype: AsDtype('>f8')
12+
13+
🌳 Tree: signal
14+
├── Branches:
15+
│ ├── branch1 ; dtype: AsDtype('>f8')

tests/test_file_peeking.py

Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
# Copyright (c) 2025, IRIS-HEP
2+
# All rights reserved.
3+
#
4+
# Redistribution and use in source and binary forms, with or without
5+
# modification, are permitted provided that the following conditions are met:
6+
#
7+
# * Redistributions of source code must retain the above copyright notice, this
8+
# list of conditions and the following disclaimer.
9+
#
10+
# * Redistributions in binary form must reproduce the above copyright notice,
11+
# this list of conditions and the following disclaimer in the documentation
12+
# and/or other materials provided with the distribution.
13+
#
14+
# * Neither the name of the copyright holder nor the names of its
15+
# contributors may be used to endorse or promote products derived from
16+
# this software without specific prior written permission.
17+
#
18+
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19+
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20+
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
21+
# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
22+
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23+
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
24+
# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
25+
# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
26+
# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27+
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28+
import pytest
29+
import uproot
30+
import awkward as ak
31+
import os
32+
import sys
33+
import numpy as np
34+
from servicex_analysis_utils import file_peeking
35+
import types
36+
import servicex
37+
import filecmp
38+
39+
40+
@pytest.fixture
41+
def build_test_samples(tmp_path):
42+
43+
test_path = str(tmp_path / "test_file1.root")
44+
# example data for two branches
45+
tree_data1 = {
46+
"branch1": np.ones(100),
47+
"branch2": np.zeros(100)
48+
}
49+
tree_data2 = {
50+
"branch1": np.ones(10),
51+
}
52+
53+
# Create tmp .root files
54+
with uproot.create(test_path) as file:
55+
file["background"] = tree_data1
56+
file["signal"] = tree_data2
57+
58+
return test_path
59+
60+
# Test helper functions: run_query, print_structure_from_str
61+
def test_encoding(build_test_samples,tmp_path, capsys):
62+
63+
path=build_test_samples
64+
query_output=file_peeking.run_query(path)
65+
66+
#Check return types
67+
assert isinstance(query_output, ak.Array), "run_query() does not produce an awkward.Array"
68+
encoded_result=query_output[0]
69+
assert isinstance(encoded_result, str), "run_query array content is not str"
70+
71+
#Check result
72+
expected_result= (
73+
"Tree: background; TBranch: branch1 ; dtype: AsDtype('>f8'), TBranch: branch2 ; dtype: AsDtype('>f8')"
74+
"\nTree: signal; TBranch: branch1 ; dtype: AsDtype('>f8')"
75+
)
76+
assert encoded_result == expected_result
77+
78+
# Produce servicex.deliver() like dict
79+
tree_data= {"branch" : query_output}
80+
with uproot.create(tmp_path / "encoded.root") as file:
81+
file["servicex"]= tree_data
82+
assert os.path.exists(tmp_path / "encoded.root"), f"servicex-like test file not found."
83+
deliver_dict={"test_file":[str(tmp_path / "encoded.root")]}
84+
85+
86+
## Test decoding structure formating
87+
# save_to_txt
88+
file_peeking.print_structure_from_str(deliver_dict,save_to_txt=True)
89+
out_txt="samples_structure.txt"
90+
assert os.path.exists(out_txt), "save_to_txt arg not producing files"
91+
92+
with open(out_txt, "r", encoding="utf-8") as f:
93+
written_str = f.read()
94+
95+
# direct return
96+
output_str=file_peeking.print_structure_from_str(deliver_dict)
97+
98+
# do_print
99+
file_peeking.print_structure_from_str(deliver_dict,do_print=True)
100+
captured = capsys.readouterr()
101+
102+
# Check if all returns match
103+
assert captured.out[0:-1] == written_str == output_str , "saved, printed and direct return formats are different"
104+
105+
# Compare with expected return
106+
test_txt="tests/data/expected_structure.txt"
107+
assert filecmp.cmp(out_txt, test_txt), "Formatted str does not match expected return"
108+
109+
# Test filter_branch
110+
filtered_str=file_peeking.print_structure_from_str(deliver_dict,filter_branch="branch2")
111+
assert "branch1" not in filtered_str, "filter_branch argument is not removing branch1"
112+
113+
114+
# Test user-facing function errors:
115+

0 commit comments

Comments
 (0)