File indexing completed on 2025-01-30 09:19:26
0001
0002
0003 import pytest
0004 from click.testing import CliRunner
0005 from pyrobird.cli.convert import convert
0006 import os
0007 import json
0008 from pyrobird.cli.convert import guess_output_name
0009
0010
0011 TEST_ROOT_FILE = os.path.join(os.path.dirname(__file__), 'data', 'reco_2024-09_craterlake_2evt.edm4eic.root')
0012
0013
0014 @pytest.fixture
0015 def runner():
0016 return CliRunner()
0017
0018
0019 def test_convert_default_output(runner, tmp_path):
0020
0021 with runner.isolated_filesystem():
0022
0023 test_root_file = 'test_data.root'
0024 with open(TEST_ROOT_FILE, 'rb') as src_file:
0025 with open(test_root_file, 'wb') as dst_file:
0026 dst_file.write(src_file.read())
0027
0028
0029 result = runner.invoke(convert, [test_root_file])
0030
0031 assert result.exit_code == 0, f"Command failed with exit code {result.exit_code}"
0032
0033
0034 expected_output_file = 'test_data.firebird.json'
0035 assert os.path.exists(expected_output_file), "Output file was not created"
0036
0037
0038 with open(expected_output_file, 'r') as f:
0039 data = json.load(f)
0040 assert isinstance(data, dict), "Output JSON is not a dictionary"
0041
0042
0043
0044 def test_convert_specified_output(runner):
0045 with runner.isolated_filesystem():
0046
0047 test_root_file = 'test_data.root'
0048 with open(TEST_ROOT_FILE, 'rb') as src_file:
0049 with open(test_root_file, 'wb') as dst_file:
0050 dst_file.write(src_file.read())
0051
0052
0053 output_file = 'custom_output.json'
0054 result = runner.invoke(convert, [test_root_file, '--output', output_file])
0055
0056 assert result.exit_code == 0, f"Command failed with exit code {result.exit_code}"
0057 assert os.path.exists(output_file), "Specified output file was not created"
0058
0059
0060 def test_convert_output_to_stdout(runner):
0061 with runner.isolated_filesystem():
0062
0063 test_root_file = 'test_data.root'
0064 with open(TEST_ROOT_FILE, 'rb') as src_file:
0065 with open(test_root_file, 'wb') as dst_file:
0066 dst_file.write(src_file.read())
0067
0068
0069 result = runner.invoke(convert, [test_root_file, '--output', '-'])
0070
0071 assert result.exit_code == 0, f"Command failed with exit code {result.exit_code}"
0072
0073 assert result.output.strip().startswith('{'), "Output is not JSON data"
0074
0075 data = json.loads(result.output)
0076 assert isinstance(data, dict), "Output JSON is not a dictionary"
0077
0078
0079
0080 def test_convert_invalid_entry(runner):
0081 with runner.isolated_filesystem():
0082
0083 test_root_file = 'test_data.root'
0084 with open(TEST_ROOT_FILE, 'rb') as src_file:
0085 with open(test_root_file, 'wb') as dst_file:
0086 dst_file.write(src_file.read())
0087
0088
0089 result = runner.invoke(convert, [test_root_file, '--output', '-', '-e', '1000'])
0090
0091 assert result.exit_code == 1, f"Command failed with exit code {result.exit_code}"
0092
0093 assert isinstance(result.exception, ValueError)
0094
0095
0096 def test_convert_missing_file(runner):
0097
0098 result = runner.invoke(convert, ['nonexistent.root'])
0099 assert result.exit_code != 0, "Command should fail with non-existent file"
0100 assert isinstance(result.exception, FileNotFoundError)
0101
0102
0103 @pytest.mark.parametrize(
0104 "input_entry, expected_output, output_extension",
0105 [
0106
0107 ('filename.txt', 'filename.firebird.json', '.firebird.json'),
0108 ('filename', 'filename.firebird.json', '.firebird.json'),
0109 ('root://filename.txt', 'filename.firebird.json', '.firebird.json'),
0110 ('http://filename', 'filename.firebird.json', '.firebird.json'),
0111 ('protocol1://protocol2://filename.ext', 'filename.firebird.json', '.firebird.json'),
0112 ('C://path/to/filename.ext', 'path/to/filename.firebird.json', '.firebird.json'),
0113 ('', '.firebird.json', '.firebird.json'),
0114
0115
0116 ('filename.dat', 'filename.custom', '.custom'),
0117 ('ftp://filename.bin', 'filename.custom', '.custom'),
0118 ]
0119 )
0120 def test_guess_output_name(input_entry, expected_output, output_extension):
0121 assert guess_output_name(input_entry, output_extension) == expected_output
0122
0123
0124 def test_guess_output_name_none_input():
0125 with pytest.raises(TypeError):
0126 guess_output_name(None)