Skip to content

Commit

Permalink
changed db.query() method to immediately return result to simplify in…
Browse files Browse the repository at this point in the history
…terface and remove potential bugs from side effects
  • Loading branch information
nickdelgrosso committed Jan 26, 2019
1 parent 5d37f99 commit 937bec3
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 19 deletions.
2 changes: 0 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,8 @@ from proteindb import ProteinDB
>> db.populate('uniprot1.fasta')

>> db.query(uniprot_id='DFAFDA1')
>> db.result
'SDFYUONNAJDMAMDFHAABWQ'

>> db.query(sequence='SDFYUONNAJDMAMDFHAABWQ')
>> db.result
'DFAFDA1'
```
4 changes: 2 additions & 2 deletions proteindb/proteindb.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ def setup_db(filename):
class ProteinDB:
def __init__(self, filename='protein.db'):
self._conn = setup_db(filename)
self.result = None

def populate(self, fasta_filename):
cursor = self._conn.cursor()
Expand All @@ -47,4 +46,5 @@ def query(self, uniprot_id=None, sequence=None):

assert len(results[0]) == 1
assert len(results) == 1
self.result = results[0][0]
result = results[0][0]
return result
10 changes: 4 additions & 6 deletions tests/test_database.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,19 +77,17 @@ def test_db_contains_correct_records(populated_db, fasta_records):


def test_querying_uniprot_id_works(populated_db, fasta_records):
assert not populated_db.result

for record in fasta_records:
sequence = str(record.seq)
uniprot_id = str(record.id.split('|')[1])
populated_db.query(uniprot_id=uniprot_id)
assert populated_db.result == sequence
result = populated_db.query(uniprot_id=uniprot_id)
assert result == sequence

def test_querying_sequence_works(populated_db, fasta_records):
assert not populated_db.result

for record in fasta_records:
sequence = str(record.seq)
uniprot_id = str(record.id.split('|')[1])
populated_db.query(sequence=sequence)
assert populated_db.result == uniprot_id
result = populated_db.query(sequence=sequence)
assert result == uniprot_id
26 changes: 17 additions & 9 deletions tests/test_protein_database.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,18 @@
import pytest
from pytest_bdd import scenario, given, when, then, scenarios
from proteindb import ProteinDB
from os import path


@pytest.fixture
def Workspace():
"""A singleton object representing the variables in memory in a session."""
class Workspace:
pass
return Workspace

@given("I have a database populated with data from Fasta file named <filename>.")
def db(filename):

db = ProteinDB()
db.populate(path.join('data', filename))
return db
Expand All @@ -22,13 +30,13 @@ def uniprot_id(uniprot_id):


@when("I search for <uniprot_id> in a database")
def step_impl(db, uniprot_id):
db.query(uniprot_id=uniprot_id)
def step_impl(db, uniprot_id, Workspace):
Workspace.result = db.query(uniprot_id=uniprot_id)


@then("I will see the corresponding <sequence>")
def step_impl(db, sequence):
assert db.result == sequence
def step_impl(Workspace, sequence):
assert Workspace.result == sequence


#############
Expand All @@ -43,11 +51,11 @@ def sequence(sequence):


@when("I search for the <sequence> in the database")
def step_impl(db, sequence):
db.query(sequence=sequence)
def step_impl(db, sequence, Workspace):
Workspace.result = db.query(sequence=sequence)


@then("I will see the corresponding <uniprot_id>")
def step_impl(db, uniprot_id):
assert db.result == uniprot_id
def step_impl(uniprot_id, Workspace):
assert Workspace.result == uniprot_id

0 comments on commit 937bec3

Please sign in to comment.