Skip to content

Commit

Permalink
using mock for runtime network calls
Browse files Browse the repository at this point in the history
  • Loading branch information
carlosp420 committed Nov 13, 2015
1 parent d649b34 commit 1973747
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 11 deletions.
7 changes: 3 additions & 4 deletions voseq/blast_local/utils.py
Expand Up @@ -148,12 +148,11 @@ def create_blast_db(self):

def save_query_to_file(self):
b = Sequences.objects.get(code_id=self.voucher_code, gene_code=self.gene_code)
this_id = b.code_id + '|' + b.gene_code
this_id = '{0}|{1}'.format(b.code_id, b.gene_code)
seq = self.strip_question_marks(b.sequences)

if seq != '':
seq_record = SeqRecord(Seq(seq),
id=this_id)
if seq:
seq_record = SeqRecord(Seq(seq), id=this_id)
SeqIO.write(seq_record, self.query_file, "fasta")

def do_blast(self):
Expand Down
17 changes: 12 additions & 5 deletions voseq/blast_ncbi/tests/tests_blast_ncbi.py
@@ -1,4 +1,7 @@
from io import StringIO
import os
import unittest
from unittest.mock import patch

from django.conf import settings
from django.test import TestCase
Expand All @@ -7,20 +10,24 @@
from blast_ncbi.utils import BLASTNcbi


TEST_PATH = os.path.abspath(os.path.dirname(__file__))
with open(os.path.join(TEST_PATH, "CP100-10_COI-begin.xml"), "r") as handle:
ncbi_return_handle = StringIO(handle.read())


class TestNcbiBlast(TestCase):
def setUp(self):
args = []
opts = {'dumpfile': 'test_db_dump.xml', 'verbosity': 0}
opts = {'dumpfile': 'test_db_dump2.xml', 'verbosity': 0}
cmd = 'migrate_db'
call_command(cmd, *args, **opts)

voucher_code = 'CP100-10'
gene_code = 'COI'
gene_code = 'COI-begin'
self.blast = BLASTNcbi(voucher_code, gene_code)

@unittest.skipIf(settings.TRAVIS is True,
'Testing using BLASTNcbi fails due to network problems')
def test_blast_with_accession_number_in_header(self):
@patch("Bio.Blast.NCBIWWW.qblast", return_value=ncbi_return_handle)
def test_blast_with_accession_number_in_header(self, mock_qblast):
self.blast.save_query_to_file()
self.blast.do_blast()
result = self.blast.parse_blast_output()
Expand Down
6 changes: 4 additions & 2 deletions voseq/blast_ncbi/utils.py
Expand Up @@ -7,7 +7,8 @@


class BLASTNcbi(BLAST):
"""Handles duties related to blast against sequences in NCBI Genbank.
"""Handles duties related to blast against sequences in NCBI GenBank.
"""
def __init__(self, voucher_code, gene_code):
self.voucher_code = voucher_code
Expand All @@ -22,7 +23,8 @@ def __init__(self, voucher_code, gene_code):
"output_{0}.xml".format(uuid.uuid4().hex))

def do_blast(self):
"""Does a blast against NCBI and saves returned XML file to local disk.
"""Blasts against NCBI and saves returned XML file to local disk.
"""
with open(self.query_file) as handle:
fasta_string = handle.read()
Expand Down

0 comments on commit 1973747

Please sign in to comment.