29
29
import uproot
30
30
import awkward as ak
31
31
import os
32
- import sys
33
32
import numpy as np
34
33
from servicex_analysis_utils import file_peeking
35
34
import types
36
- import servicex
37
35
import filecmp
36
+ import re
38
37
39
38
40
39
@pytest .fixture
@@ -57,7 +56,7 @@ def build_test_samples(tmp_path):
57
56
58
57
return test_path
59
58
60
- # Test helper functions: run_query, print_structure_from_str
59
+ # Test run_query and print_structure_from_str
61
60
def test_encoding (build_test_samples ,tmp_path , capsys ):
62
61
63
62
path = build_test_samples
@@ -110,6 +109,51 @@ def test_encoding(build_test_samples,tmp_path, capsys):
110
109
filtered_str = file_peeking .print_structure_from_str (deliver_dict ,filter_branch = "branch2" )
111
110
assert "branch1" not in filtered_str , "filter_branch argument is not removing branch1"
112
111
112
+ # Test spec builder for deliver
113
+ def test_spec_builder ():
114
+ #Get spec
115
+ test_did_str = "random_space:did"
116
+ spec = file_peeking .build_deliver_spec (test_did_str )
117
+
118
+ #Check return type
119
+ assert isinstance (spec , dict ), "build_deliver_spec does not return a dict"
120
+ assert "Sample" in spec , "Key 'Sample' is missing in the returned dict"
121
+ assert isinstance (spec ["Sample" ], list ), "'Sample' should be a list"
122
+
123
+ #Get return size
124
+ size = len (spec ["Sample" ])
125
+ assert size == 1 , f"Only one did given but sample item of spec is not len 1: { size } "
126
+
127
+ #Check first sample
128
+ first_entry = spec ["Sample" ][0 ]
129
+ assert isinstance (first_entry , dict ), "Each entry in 'Sample' should be a dict"
130
+
131
+ #Check each key type
132
+ assert isinstance (first_entry ["NFiles" ], int ), "'NFiles' should be an integer"
133
+ assert isinstance (first_entry ["Name" ], str ), "'Name' should be a string"
134
+
135
+ from servicex .dataset_identifier import RucioDatasetIdentifier
136
+ assert isinstance (first_entry ["Dataset" ], RucioDatasetIdentifier ), "'Dataset' should be a RucioDatasetIdentifier"
137
+
138
+ from servicex .python_dataset import PythonFunction
139
+ assert isinstance (first_entry ["Query" ], PythonFunction ), "'Query' should be a PythonFunction"
140
+
141
+ ##Different input types
142
+ #list with two DID
143
+ test_did_list = [test_did_str , test_did_str + "2" ]
144
+ spec_from_list = file_peeking .build_deliver_spec (test_did_list )
145
+ assert len (spec_from_list ["Sample" ])== 2 , "Wrong number of samples in deliver configuration"
146
+
147
+ #dict with sample name
148
+ test_did_dict = {"Custom-Name" :test_did_str }
149
+ spec_from_dict = file_peeking .build_deliver_spec (test_did_dict )
150
+ assert spec_from_dict ["Sample" ][0 ]["Name" ]== "Custom-Name"
151
+
152
+ #wrong input type
153
+ wrong_did = 1234
154
+ with pytest .raises (ValueError , match = re .escape (f"Unsupported dataset input type: { type (wrong_did )} .\n Input must be dict ('sample_name':'dataset_id'), str or list of str" )):
155
+ file_peeking .build_deliver_spec (wrong_did )
156
+
157
+
113
158
114
- # Test user-facing function errors:
115
159
0 commit comments