Skip to content
Snippets Groups Projects

Convert scripts to entry_points

Open Duncan Macleod requested to merge duncanmmacleod/dqsegdb-client:entry-points into master
1 unresolved thread
Files
2
+ 108
0
# -*- python -*-
"""Tests for :mod:`dqsegdb.tool.ligolw_segment_insert`.
"""
import subprocess
import sys
from pathlib import Path
import pytest
from dqsegdb.tools import ligolw_segment_insert
__author__ = "Duncan Macleod <duncan.macleod@ligo.org>"
# -- utilities --------------
# homebrew tmp_path fixture for old pytest
if pytest.__version__ < "3.9.1":
if pytest.__version__ < "3.0.0":
yield_fixture = pytest.yield_fixture
else:
yield_fixture = pytest.fixture
@yield_fixture
def tmp_path():
import tempfile
with tempfile.TemporaryDirectory() as tmpdir:
yield Path(tmpdir)
def load_segment_tables(path):
"""Load the three segment tables from the given XML file path
Returns
-------
segdeftable : `glue.ligolw.lsctables.SegmentDefTable`
segsumtable : `glue.ligolw.lsctables.SegmentSumTable`
segtable : `glue.ligolw.lsctables.SegmentTable`
"""
from glue.ligolw import lsctables
from glue.ligolw.ligolw import LIGOLWContentHandler
from glue.ligolw.utils import load_filename
@lsctables.use_in
class ContentHandler(LIGOLWContentHandler):
pass
xmldoc = load_filename(path, contenthandler=ContentHandler)
return (
lsctables.SegmentDefTable.get_table(xmldoc),
lsctables.SegmentSumTable.get_table(xmldoc),
lsctables.SegmentTable.get_table(xmldoc),
)
# -- tests ------------------
@pytest.mark.xfail(
sys.version_info >= (3, 10),
reason="Can't read files with glue.ligolw on Python >= 3.10",
strict=True,
)
def test_ligolw_segment_insert_output(tmp_path):
"""Test that ligolw_segment_insert correctly formats a file
This isn't a very thorough test, but it's better than nothing.
"""
# write known and active segments to a file
known = [
(0, 10),
(20, 30),
]
active = [
(0, 2),
(3, 5),
(25, 30),
]
for name, seglist in (
("known.txt", known),
("active.txt", active),
):
with open(tmp_path / name, "w") as file:
for seg in seglist:
print(f"{seg[0]} {seg[1]}", file=file)
outfile = tmp_path / "result.xml"
# run ligolw_segment_insert_dqsegdb
ligolw_segment_insert.main([
"--insert",
"--ifos=X1",
"--name=TEST",
"--version=0",
"--segment-url=http://example.com",
f"--summary-file={tmp_path}/known.txt",
f"--segment-file={tmp_path}/active.txt",
f"--output={outfile}",
"--explain='test'",
"--comment='test'",
])
# validate the output
segdef, segsum, seg = load_segment_tables(outfile)
assert len(segdef) == 1
assert [(s.start_time, s.end_time) for s in segsum] == known
assert [(s.start_time, s.end_time) for s in seg] == active
Loading