diff --git a/facs/utils/helpers.py b/facs/utils/helpers.py index 639dfd8..e6ba033 100644 --- a/facs/utils/helpers.py +++ b/facs/utils/helpers.py @@ -4,6 +4,7 @@ import shutil import json import warnings +import requests from contextlib import contextmanager import couchdb @@ -44,10 +45,33 @@ def generate_dummy_fastq(fname, num_reads, case=''): f.write('arvestad' * stride + os.linesep) -def send_couchdb(server, db, user, passwd, doc): +def _wake(server, retries=5): + """Try to wake up server by retrying get requests. + + This method is basically used to wake up the public database facs.iriscouch.com + used to store the tests results. + """ + status_code = requests.codes.NO_RESPONSE + while status_code != requests.codes.OK and retries: + try: + r = requests.get(db) + status_code = r.status_code + retries = 0 + except requests.ConnectionError: + if retries == 1: + raise requests.ConnectionError("There was a problem connecting to {} and the " \ + "results could not be uploaded".format(db)) + else: + retries -= 1 + pass + + +def send_couchdb(server, db, user, passwd, doc, wake_up=False): ''' Send JSON document to couchdb ''' try: + if wake_up: + _wake(server) couch = couchdb.Server(server) couch.resource.credentials = (user, passwd) db = couch[db] diff --git a/setup.py b/setup.py index 7602009..03148c9 100755 --- a/setup.py +++ b/setup.py @@ -46,6 +46,7 @@ "nose", "nose-timer", "jinja2", + "requests", "couchdb" ], entry_points=""" diff --git a/tests/test_basic.py b/tests/test_basic.py index 0a11d95..3f3f325 100644 --- a/tests/test_basic.py +++ b/tests/test_basic.py @@ -66,6 +66,18 @@ def test_1_build_ref(self): json_doc = facs.build(org, bf) self.results.append(json_doc) + def test_2_query(self): + """ Query against datasets (generated by simNGS) + """ + + for sample in glob.glob(os.path.join(self.synthetic_fastq, "*.fastq")): + for ref in os.listdir(self.reference): + qry = os.path.join(self.synthetic_fastq, sample) + bf = os.path.join(self.bloom_dir, os.path.splitext(ref)[0]+".bloom") + print(qry, bf) + json_doc = facs.query(qry, bf) + self.results.append(json_doc) + def test_3_query_custom(self): """ Query against the uncompressed FastQ files files manually deposited in data/custom folder.