Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-30 09:19:26

0001 # test_convert.py
0002 
0003 import pytest
0004 from click.testing import CliRunner
0005 from pyrobird.cli.convert import convert  # Import your convert function
0006 import os
0007 import json
0008 from pyrobird.cli.convert import guess_output_name
0009 
0010 # Assuming the small ROOT file is named 'test_data.root' and is placed in the 'tests' directory
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     # Use a temporary directory for the output file
0021     with runner.isolated_filesystem():
0022         # Copy the test ROOT file into the isolated filesystem
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         # Run the convert command without specifying output (default behavior)
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         # Check that the output file was created
0034         expected_output_file = 'test_data.firebird.json'
0035         assert os.path.exists(expected_output_file), "Output file was not created"
0036 
0037         # Optionally, check the content of the output file
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             # Add more assertions based on expected content
0042 
0043 
0044 def test_convert_specified_output(runner):
0045     with runner.isolated_filesystem():
0046         # Copy the test ROOT file
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         # Specify an output file name
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         # Copy the test ROOT file
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         # Output to stdout
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         # The output should be the JSON data
0073         assert result.output.strip().startswith('{'), "Output is not JSON data"
0074         # Optionally, parse the JSON and perform assertions
0075         data = json.loads(result.output)
0076         assert isinstance(data, dict), "Output JSON is not a dictionary"
0077         # Add more assertions based on expected content
0078 
0079 
0080 def test_convert_invalid_entry(runner):
0081     with runner.isolated_filesystem():
0082         # Copy the test ROOT file
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         # Test file doesn't have event 1000
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         # The output should be the JSON data
0093         assert isinstance(result.exception, ValueError)
0094 
0095 
0096 def test_convert_missing_file(runner):
0097     # Attempt to convert a non-existent file
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         # Test cases with default output_extension
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         # Test cases with custom output_extension
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)