|
2 | 2 |
|
3 | 3 | import awkward as ak |
4 | 4 | import pytest |
| 5 | +import uproot |
5 | 6 | from distributed import Client |
6 | 7 |
|
7 | 8 | from coffea.nanoevents import NanoAODSchema, NanoEventsFactory |
@@ -246,3 +247,47 @@ def test_access_log(tests_directory, mode): |
246 | 247 | _ = ak.materialize(events.Muon.pt) |
247 | 248 | branches = {entry.branch for entry in factory.access_log} |
248 | 249 | assert branches == {"nMuon", "Muon_pt"} |
| 250 | + |
| 251 | + |
| 252 | +@pytest.mark.parametrize("mode", ["eager", "virtual"]) |
| 253 | +def test_file_handle_from_path(tests_directory, mode): |
| 254 | + """Test that file_handle is available when opening from path string.""" |
| 255 | + path = f"{tests_directory}/samples/nano_dy.root:Events" |
| 256 | + |
| 257 | + factory = NanoEventsFactory.from_root( |
| 258 | + path, |
| 259 | + schemaclass=NanoAODSchema, |
| 260 | + mode=mode, |
| 261 | + ) |
| 262 | + |
| 263 | + # file_handle should be ReadOnlyFile when opened from path |
| 264 | + assert factory.file_handle is not None |
| 265 | + assert isinstance(factory.file_handle, uproot.reading.ReadOnlyFile) |
| 266 | + |
| 267 | + _ = factory.events() |
| 268 | + |
| 269 | + # file_handle still accessible after events() call |
| 270 | + assert factory.file_handle is not None |
| 271 | + |
| 272 | + |
| 273 | +@pytest.mark.parametrize("mode", ["eager", "virtual"]) |
| 274 | +def test_file_handle_from_directory(tests_directory, mode): |
| 275 | + """Test that file_handle is available when passing ReadOnlyDirectory.""" |
| 276 | + filepath = f"{tests_directory}/samples/nano_dy.root" |
| 277 | + |
| 278 | + with uproot.open(filepath) as file: |
| 279 | + factory = NanoEventsFactory.from_root( |
| 280 | + file, |
| 281 | + treepath="Events", |
| 282 | + schemaclass=NanoAODSchema, |
| 283 | + mode=mode, |
| 284 | + ) |
| 285 | + |
| 286 | + # file_handle should be ReadOnlyDirectory when passed directly |
| 287 | + assert factory.file_handle is not None |
| 288 | + assert isinstance(factory.file_handle, uproot.ReadOnlyDirectory) |
| 289 | + |
| 290 | + _ = factory.events() |
| 291 | + |
| 292 | + # file_handle still accessible after events() call |
| 293 | + assert factory.file_handle is not None |
0 commit comments