33
33
import os
34
34
import sys
35
35
import numpy as np
36
-
37
- #Setting rpath
38
- sys .path .insert (0 , os .path .abspath (os .path .join (os .path .dirname (__file__ ), ".." )))
39
36
from servicex_analysis_utils .materialization import to_awk
40
37
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"
42
44
# example data for two branches
43
45
tree_data1 = {
44
46
"branch1" : np .ones (100 ),
@@ -54,71 +56,66 @@ def build_test_samples():
54
56
with uproot .create (test_path2 ) as file :
55
57
file ["Tree" ] = tree_data2
56
58
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
-
71
59
#Dict like servicex.deliver() output
72
60
sx_dict = {"Test-Sample1" : test_path1 , "Test-Sample2" : test_path2 }
73
61
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
78
63
79
- #Test functions
80
- def test_to_awk_instances ():
81
- arr1 = result ["Test-Sample1" ]
82
- da_arr1 = result_da ["Test-Sample1" ]
83
64
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
91
69
92
70
#Collecting all samples
93
71
assert list (result .keys ())== ["Test-Sample1" , "Test-Sample2" ]
72
+ arr1 = result ["Test-Sample1" ]
73
+ arr2 = result ["Test-Sample2" ]
94
74
95
75
#Collecting all branches
96
76
assert ak .fields (arr1 ) == ['branch1' , 'branch2' ]
97
77
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
+
99
82
#Collecting all elements per branch
100
83
assert ak .all (arr1 ['branch2' ] == ak .from_numpy (np .zeros (100 )))
101
84
assert ak .all (arr2 ['branch1' ] == ak .from_numpy (np .ones (10 )))
102
85
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" ]
104
98
arr1 = result_da ["Test-Sample1" ]
105
99
arr2 = result_da ["Test-Sample2" ]
106
100
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"
110
104
111
105
#Testing partitionning kwarg
112
106
assert arr1 .npartitions == 10
113
107
assert arr2 .npartitions == 1
114
108
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
+
118
118
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' ]
122
119
123
120
124
121
0 commit comments