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