| 1 |
import copy |
| 2 |
import datetime |
| 3 |
import json |
| 4 |
import logging |
| 5 |
import os.path |
| 6 |
import tempfile |
| 7 |
import uuid |
| 8 |
|
| 9 |
from pbcommand.models import FileTypes, DataStore, DataStoreFile, PacBioAlarm |
| 10 |
from pbcommand.models.common import _datetime_to_string |
| 11 |
|
| 12 |
log = logging.getLogger(__name__) |
| 13 |
|
| 14 |
|
| 15 |
class TestLoadFileTypes: |
| 16 |
|
| 17 |
def test_file_types(self): |
| 18 |
|
| 19 |
ft = FileTypes.DS_ALIGN |
| 20 |
assert ft is not None |
| 21 |
assert ft == copy.deepcopy(FileTypes.DS_ALIGN) |
| 22 |
assert ft != FileTypes.DS_ALIGN_CCS |
| 23 |
|
| 24 |
def test_is_valid(self): |
| 25 |
ft = FileTypes.DS_ALIGN |
| 26 |
assert FileTypes.is_valid_id(ft.file_type_id) |
| 27 |
|
| 28 |
|
| 29 |
class TestDataStore: |
| 30 |
|
| 31 |
def test_datastore_file(self): |
| 32 |
tmpfile = tempfile.NamedTemporaryFile(suffix=".subreadset.xml").name |
| 33 |
ds = DataStoreFile(str(uuid.uuid4()), |
| 34 |
"pbcommand.tasks.dev_task", |
| 35 |
FileTypes.DS_SUBREADS.file_type_id, |
| 36 |
tmpfile, |
| 37 |
False, |
| 38 |
"Subreads", |
| 39 |
"Subread DataSet XML") |
| 40 |
log.info("DataStoreFile: {s}".format(s=ds)) |
| 41 |
ds2 = DataStoreFile.from_dict(ds.to_dict()) |
| 42 |
for attr in ["uuid", "file_type_id", "file_id", "path", "is_chunked", "name", "description"]: |
| 43 |
assert getattr(ds2, attr) == getattr(ds, attr) |
| 44 |
assert ds.file_type == FileTypes.DS_SUBREADS |
| 45 |
|
| 46 |
def test_datastore_paths(self): |
| 47 |
tmpfile = tempfile.NamedTemporaryFile(suffix=".subreadset.xml").name |
| 48 |
base_dir = os.path.dirname(tmpfile) |
| 49 |
tmp_ds = os.path.join(base_dir, "datastore.json") |
| 50 |
dsf = DataStoreFile(str(uuid.uuid4()), |
| 51 |
"pbcommand.tasks.dev_task", |
| 52 |
FileTypes.DS_SUBREADS.file_type_id, |
| 53 |
os.path.basename(tmpfile), |
| 54 |
False, |
| 55 |
"Subreads", |
| 56 |
"Subread DataSet XML") |
| 57 |
ds = DataStore([dsf]) |
| 58 |
ds.write_json(tmp_ds) |
| 59 |
with open(tmp_ds) as json_in: |
| 60 |
d = json.loads(json_in.read()) |
| 61 |
assert not os.path.isabs(d['files'][0]['path']) |
| 62 |
ds = DataStore.load_from_json(tmp_ds) |
| 63 |
assert list(ds.files.values())[0].path == tmpfile |
| 64 |
|
| 65 |
|
| 66 |
class TestAlarm: |
| 67 |
|
| 68 |
def test_pacbio_alarm(self): |
| 69 |
json_tmp = tempfile.NamedTemporaryFile(suffix=".json").name |
| 70 |
d = { |
| 71 |
"exception": "IOError", |
| 72 |
"info": "this would usually be a Python traceback", |
| 73 |
"message": "Something broke!", |
| 74 |
"name": "IOError", |
| 75 |
"severity": "ERROR", |
| 76 |
"owner": "python", |
| 77 |
"createdAt": _datetime_to_string(datetime.datetime.now()), |
| 78 |
"id": str(uuid.uuid4()) |
| 79 |
} |
| 80 |
a = PacBioAlarm.from_dict(d) |
| 81 |
assert a.log_level == logging.ERROR |
| 82 |
a.to_json(json_tmp) |
| 83 |
a = PacBioAlarm.from_json(json_tmp) |
| 84 |
assert a.log_level == logging.ERROR |