|
| 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 json |
| 31 | +import os |
| 32 | +from servicex_analysis_utils import file_peeking |
| 33 | +from pathlib import Path |
| 34 | + |
| 35 | + |
| 36 | +@pytest.fixture |
| 37 | +def build_test_samples(tmp_path): |
| 38 | + |
| 39 | + test_path = str(tmp_path / "test_metadata.root") |
| 40 | + # example data for two branches |
| 41 | + tree_data = { |
| 42 | + "FileMetaDataAuxDyn.test_100": [100], |
| 43 | + "FileMetaDataAuxDyn.test_abc": ["abc"], |
| 44 | + } |
| 45 | + |
| 46 | + # Create tmp .root files |
| 47 | + with uproot.create(test_path) as file: |
| 48 | + file["MetaData"] = tree_data |
| 49 | + |
| 50 | + return test_path |
| 51 | + |
| 52 | + |
| 53 | +# Test run_query and print_structure_from_str |
| 54 | +def test_metadata_retrieval(build_test_samples, tmp_path, capsys): |
| 55 | + |
| 56 | + path = build_test_samples |
| 57 | + query_output = file_peeking.run_query(path) |
| 58 | + # Check result |
| 59 | + expected_result = { |
| 60 | + "FileMetaData": {"test_100": "100", "test_abc": "abc"}, |
| 61 | + "MetaData": { |
| 62 | + "FileMetaDataAuxDyn.test_100": "AsDtype('>i8')", |
| 63 | + "FileMetaDataAuxDyn.test_abc": "AsStrings()", |
| 64 | + }, |
| 65 | + } |
| 66 | + encoded_result = json.loads(query_output[0]) |
| 67 | + |
| 68 | + assert encoded_result == expected_result |
| 69 | + |
| 70 | + # Produce servicex.deliver() like dict |
| 71 | + # i.e {"Sample Name":"Path"} |
| 72 | + tree_data = {"branch": query_output} |
| 73 | + with uproot.create(tmp_path / "encoded.root") as file: |
| 74 | + file["servicex"] = tree_data |
| 75 | + assert os.path.exists( |
| 76 | + tmp_path / "encoded.root" |
| 77 | + ), f"servicex-like test file not found." |
| 78 | + deliver_dict = {"test_file": [str(tmp_path / "encoded.root")]} |
| 79 | + |
| 80 | + ## Test str formating |
| 81 | + output_str = file_peeking.print_structure_from_str(deliver_dict) |
| 82 | + |
| 83 | + expected_path = Path("tests/data/expected_metadata.txt") |
| 84 | + expected = expected_path.read_text(encoding="utf-8") |
| 85 | + |
| 86 | + assert ( |
| 87 | + expected == output_str |
| 88 | + ), f"Output does not match expected.\n Output: {output_str}" |
0 commit comments