1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
|
import logging
import os
import os.path as op
import tempfile
import traceback
import pytest
from base_utils import get_data_file_from_subdir
from pbcommand.validators import *
log = logging.getLogger(__name__)
def _to_report(name):
return get_data_file_from_subdir("example-reports", name)
class TestValidators:
def test_validate_file(self):
"""
Test: Return abspath if file found
"""
file_path = validate_file(op.relpath(__file__, "."))
assert file_path == __file__
def test_validate_file_fails(self):
"""
Test: Raise IOError if file not found
"""
with pytest.raises(IOError):
validate_file("this_file_does_not_exist")
def test_validate_nonempty_file(self):
"""
Test: Return abspath if file found and not empty
"""
file_path = validate_nonempty_file(op.relpath(__file__, os.getcwd()))
assert file_path == __file__
def test_validate_nonempty_file_fails(self):
"""
Test: Raise IOError if file found but empty
"""
f = tempfile.NamedTemporaryFile(suffix="empty").name
with pytest.raises(IOError):
validate_nonempty_file(f)
def test_validate_output_dir(self):
"""
Test: Raise IOError if output does not exist
"""
try:
validate_output_dir(op.dirname(__file__))
except BaseException:
log.error(traceback.format_exc())
raise Exception("Directory validation failed")
with pytest.raises(IOError):
validate_output_dir('/whatev')
def test_validate_report_file(self):
"""
Test: Raise ValueError if report has path sep
"""
try:
# we know this gets made
validate_report_file('foo')
except BaseException:
log.error(traceback.format_exc())
raise Exception("Filename validation failed")
with pytest.raises(ValueError):
validate_report_file('/foo')
def test_validate_report(self):
rpt = _to_report("test_report.json")
rpt2 = validate_report(rpt)
def test_validate_report_fails(self):
rpt = _to_report("test_report2.json")
with pytest.raises(ValueError):
validate_report(rpt)
|