Files
galaxy/test/unit/util/test_utils.py
T
John Chilton 22ddea4337 Better packaging of unit test utilities.
clean_multiline_string was used in multiple places across modules in different packages so I simply refactored it into galaxy.util and added a test case (used in multiple places and has tests a lower bar then some other code in galaxy.util).

The tempfilecache abstraction was only used in one place, so I've refactored it right into that file.

Together these should simplify the package tests for galaxy-data (and hopefully other packages also).
2020-07-03 13:32:32 -04:00

94 lines
2.8 KiB
Python

import errno
import os
import tempfile
import pytest
from galaxy import util
SECTION_XML = """<?xml version="1.0" ?>
<section id="fasta_fastq_manipulation" name="Fasta Fastq Manipulation" version="">
<tool file="toolshed.g2.bx.psu.edu/repos/peterjc/seq_filter_by_id/fb1313d79396/seq_filter_by_id/tools/seq_filter_by_id/seq_filter_by_id.xml" guid="toolshed.g2.bx.psu.edu/repos/peterjc/seq_filter_by_id/seq_filter_by_id/0.2.5">
<tool_shed>
toolshed.g2.bx.psu.edu
</tool_shed>
</tool>
</section>
"""
def test_strip_control_characters():
s = '\x00bla'
assert util.strip_control_characters(s) == 'bla'
def test_strip_control_characters_nested():
s = '\x00bla'
stripped_s = 'bla'
l = [s]
t = (s, 'blub')
d = {42: s}
assert util.strip_control_characters_nested(l)[0] == stripped_s
assert util.strip_control_characters_nested(t)[0] == stripped_s
assert util.strip_control_characters_nested(d)[42] == stripped_s
def test_parse_xml_string():
section = util.parse_xml_string(SECTION_XML)
_verify_section(section)
def test_parse_xml_file():
with tempfile.NamedTemporaryFile(mode='w') as tmp:
tmp.write(SECTION_XML)
tmp.flush()
section = util.parse_xml(tmp.name).getroot()
_verify_section(section)
def _verify_section(section):
tool = next(iter(section))
assert sorted(tool.items()) == [
('file',
'toolshed.g2.bx.psu.edu/repos/peterjc/seq_filter_by_id/fb1313d79396/seq_filter_by_id/tools/seq_filter_by_id/seq_filter_by_id.xml'),
('guid',
'toolshed.g2.bx.psu.edu/repos/peterjc/seq_filter_by_id/seq_filter_by_id/0.2.5')
]
assert next(iter(tool)).text == 'toolshed.g2.bx.psu.edu'
def test_xml_to_string():
section = util.parse_xml_string(SECTION_XML)
s = util.xml_to_string(section)
assert len(s.split('\n')) == 1
def test_xml_to_string_pretty():
section = util.parse_xml_string(SECTION_XML)
s = util.xml_to_string(section, pretty=True)
PRETTY = """<?xml version="1.0" ?>
<section id="fasta_fastq_manipulation" name="Fasta Fastq Manipulation" version="">
<tool file="toolshed.g2.bx.psu.edu/repos/peterjc/seq_filter_by_id/fb1313d79396/seq_filter_by_id/tools/seq_filter_by_id/seq_filter_by_id.xml" guid="toolshed.g2.bx.psu.edu/repos/peterjc/seq_filter_by_id/seq_filter_by_id/0.2.5">
<tool_shed>toolshed.g2.bx.psu.edu</tool_shed>
</tool>
</section>"""
assert s == PRETTY
def test_parse_xml_enoent():
fd, path = tempfile.mkstemp()
os.close(fd)
os.remove(path)
with pytest.raises(IOError) as excinfo:
util.parse_xml(path)
assert excinfo.value.errno == errno.ENOENT
def test_clean_multiline_string():
x = util.clean_multiline_string("""
a
b
c
""")
assert x == "a\nb\nc\n"