Skip to content

Commit

Permalink
[ntuple] Add more Python tests
Browse files Browse the repository at this point in the history
  • Loading branch information
hahnjo committed Jan 7, 2025
1 parent 79f67ff commit 5e17eda
Showing 1 changed file with 57 additions and 0 deletions.
57 changes: 57 additions & 0 deletions tree/ntuple/v7/test/ntuple_basics.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,63 @@ def test_write_read(self):
self.assertEqual(entry["f"], 42)
self.assertEqual(entry["mystr"], "string stored in RNTuple")

def test_write_fields(self):
"""Can create writer with on-the-fly model"""

# FIXME: This should work without make_pair...
fields = [ROOT.std.make_pair("int", "f")]
with RNTupleWriter.Recreate(fields, "ntpl", "test_ntuple_py_write_fields.root") as writer:
entry = writer.CreateEntry()
entry["f"] = 42
writer.Fill(entry)

reader = RNTupleReader.Open("ntpl", "test_ntuple_py_write_fields.root")
self.assertEqual(reader.GetNEntries(), 1)
entry = reader.CreateEntry()
reader.LoadEntry(0, entry)
self.assertEqual(entry["f"], 42)

def test_append_open(self):
"""Can append to existing TFile and open from RNTuple key."""

model = RNTupleModel.Create()
model.MakeField["int"]("f")

with ROOT.TFile.Open("test_ntuple_py_append.root", "RECREATE") as f:
with RNTupleWriter.Append(model, "ntpl", f) as writer:
entry = writer.CreateEntry()
entry["f"] = 42
writer.Fill(entry)
# The model should not have been destroyed (a clone has been used).
self.assertFalse(model.IsFrozen())

with ROOT.TFile.Open("test_ntuple_py_append.root") as f:
reader = RNTupleReader.Open(f["ntpl"])
self.assertEqual(reader.GetNEntries(), 1)
entry = reader.CreateEntry()
reader.LoadEntry(0, entry)
self.assertEqual(entry["f"], 42)

def test_read_model(self):
"""Can impose a model when reading."""

write_model = RNTupleModel.Create()
write_model.MakeField["int"]("f1")
write_model.MakeField["int"]("f2")

with RNTupleWriter.Recreate(write_model, "ntpl", "test_ntuple_py_read_model.root") as writer:
entry = writer.CreateEntry()
writer.Fill(entry)

read_model = RNTupleModel.Create()
read_model.MakeField["int"]("f1")

reader = RNTupleReader.Open(read_model, "ntpl", "test_ntuple_py_read_model.root")
entry = reader.CreateEntry()
with self.assertRaises(Exception):
# Field f2 does not exist in imposed model
entry["f2"] = 42

def test_forbid_writing_wrong_type(self):
"""Forbid writing the wrong type into an RNTuple field."""

Expand Down

0 comments on commit 5e17eda

Please sign in to comment.