Skip to content

Commit 139708e

Browse files
committed
adding more tests for deliver spec builder
1 parent fe6c414 commit 139708e

File tree

1 file changed

+48
-4
lines changed

1 file changed

+48
-4
lines changed

tests/test_file_peeking.py

Lines changed: 48 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,11 @@
2929
import uproot
3030
import awkward as ak
3131
import os
32-
import sys
3332
import numpy as np
3433
from servicex_analysis_utils import file_peeking
3534
import types
36-
import servicex
3735
import filecmp
36+
import re
3837

3938

4039
@pytest.fixture
@@ -57,7 +56,7 @@ def build_test_samples(tmp_path):
5756

5857
return test_path
5958

60-
# Test helper functions: run_query, print_structure_from_str
59+
# Test run_query and print_structure_from_str
6160
def test_encoding(build_test_samples,tmp_path, capsys):
6261

6362
path=build_test_samples
@@ -110,6 +109,51 @@ def test_encoding(build_test_samples,tmp_path, capsys):
110109
filtered_str=file_peeking.print_structure_from_str(deliver_dict,filter_branch="branch2")
111110
assert "branch1" not in filtered_str, "filter_branch argument is not removing branch1"
112111

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)}.\nInput must be dict ('sample_name':'dataset_id'), str or list of str")):
155+
file_peeking.build_deliver_spec(wrong_did)
156+
157+
113158

114-
# Test user-facing function errors:
115159

0 commit comments

Comments
 (0)