Skip to content

Commit 72ef53e

Browse files
committed
removed rpath - and simplified test funcitons
1 parent 422d123 commit 72ef53e

File tree

1 file changed

+41
-44
lines changed

1 file changed

+41
-44
lines changed

tests/test_materialization.py

Lines changed: 41 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -33,12 +33,14 @@
3333
import os
3434
import sys
3535
import numpy as np
36-
37-
#Setting rpath
38-
sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), "..")))
3936
from servicex_analysis_utils.materialization import to_awk
4037

41-
def build_test_samples():
38+
39+
@pytest.fixture
40+
def build_test_samples(tmp_path):
41+
42+
test_path1 = tmp_path / "test_file1.root"
43+
test_path2 = tmp_path / "test_file2.root"
4244
# example data for two branches
4345
tree_data1 = {
4446
"branch1": np.ones(100),
@@ -54,71 +56,66 @@ def build_test_samples():
5456
with uproot.create(test_path2) as file:
5557
file["Tree"] = tree_data2
5658

57-
#Initial test configuration
58-
@pytest.fixture(scope="function", autouse=True)
59-
def init(tmp_path):
60-
#Setting global variables to be used in the tests and helper function
61-
global test_path1, test_path2, \
62-
result, result_da, result_filtered
63-
64-
test_path1 = tmp_path / "test_file1.root"
65-
test_path2 = tmp_path / "test_file2.root"
66-
67-
#Building dumy test files
68-
if not os.path.exists(test_path1) or not os.path.exists(test_path2):
69-
build_test_samples()
70-
7159
#Dict like servicex.deliver() output
7260
sx_dict = {"Test-Sample1": test_path1, "Test-Sample2": test_path2}
7361

74-
#Executing to_awk() and saving results for tests
75-
result = to_awk(sx_dict)
76-
result_da = to_awk(sx_dict, dask=True, step_size=10) #uproot.dask step_size kwarg
77-
result_filtered = to_awk(sx_dict, expressions="branch1") #uproot.iterate expressions kwarg
62+
return sx_dict
7863

79-
#Test functions
80-
def test_to_awk_instances():
81-
arr1=result["Test-Sample1"]
82-
da_arr1=result_da["Test-Sample1"]
8364

84-
#Testing returned types
85-
assert isinstance(arr1, ak.Array), "to_awk() does not produce an awkward.Array instance"
86-
assert isinstance(da_arr1, dak.Array), "to_awk(dask=True) does not produce a dask_awkward.Array instance"
87-
88-
def test_to_awk_collection():
89-
arr1=result["Test-Sample1"]
90-
arr2=result["Test-Sample2"]
65+
#Test functions
66+
def test_to_awk_collection(build_test_samples):
67+
sx_dict = build_test_samples
68+
result = to_awk(sx_dict) #uproot.iterate expressions kwarg
9169

9270
#Collecting all samples
9371
assert list(result.keys())==["Test-Sample1", "Test-Sample2"]
72+
arr1=result["Test-Sample1"]
73+
arr2=result["Test-Sample2"]
9474

9575
#Collecting all branches
9676
assert ak.fields(arr1) == ['branch1', 'branch2']
9777
assert ak.fields(arr2) == ['branch1']
98-
78+
79+
assert isinstance(arr1, ak.Array), "to_awk() does not produce an awkward.Array instance"
80+
assert isinstance(arr2, ak.Array), "to_awk() does not produce an awkward.Array instance"
81+
9982
#Collecting all elements per branch
10083
assert ak.all(arr1['branch2'] == ak.from_numpy(np.zeros(100)))
10184
assert ak.all(arr2['branch1'] == ak.from_numpy(np.ones(10)))
10285

103-
def test_to_awk_dask():
86+
#Checking kwargs
87+
result_filtered = to_awk(sx_dict, expressions="branch1") #uproot.iterate expressions kwarg
88+
arr1_filtered=result_filtered["Test-Sample1"]
89+
assert ak.fields(arr1_filtered) == ['branch1'] #branch2 should be filtered out
90+
91+
92+
def test_to_awk_dask(build_test_samples):
93+
sx_dict = build_test_samples
94+
result_da = to_awk(sx_dict, dask=True, step_size=10) #uproot.dask step_size kwarg
95+
96+
#Collecting all samples
97+
assert list(result_da.keys())==["Test-Sample1", "Test-Sample2"]
10498
arr1=result_da["Test-Sample1"]
10599
arr2=result_da["Test-Sample2"]
106100

107-
#Testing if dask.compute() leads to same results
108-
assert ak.almost_equal(arr1.compute(), result["Test-Sample1"])
109-
assert ak.almost_equal(arr2.compute(), result["Test-Sample2"])
101+
#Checking instance
102+
assert isinstance(arr1, dak.Array), "to_awk(dask=True) does not produce an dak.Array instance"
103+
assert isinstance(arr2, dak.Array), "to_awk(dask=True) does not produce an dak.Array instance"
110104

111105
#Testing partitionning kwarg
112106
assert arr1.npartitions == 10
113107
assert arr2.npartitions == 1
114108

115-
def test_to_awk_filter():
116-
arr1=result_filtered["Test-Sample1"]
117-
arr2=result_filtered["Test-Sample2"]
109+
#Collecting all branches
110+
assert ak.fields(arr1) == ['branch1', 'branch2']
111+
assert ak.fields(arr2) == ['branch1']
112+
113+
#Collecting all elements per branch
114+
assert ak.all(arr1['branch2'].compute() == ak.from_numpy(np.zeros(100)))
115+
assert ak.all(arr2['branch1'].compute() == ak.from_numpy(np.ones(10)))
116+
117+
118118

119-
#Testing if filtering kwargs are passed to uproot.iterate()
120-
assert ak.fields(arr1) == ['branch1'] #branch2 should be filtered out
121-
assert ak.fields(arr2) == ['branch1']
122119

123120

124121

0 commit comments

Comments
 (0)