Skip to content

Commit

Permalink
Issue saketkc#26 test_case added
Browse files Browse the repository at this point in the history
  • Loading branch information
DaasDaham committed Mar 2, 2020
1 parent c5c29c4 commit b7be4d4
Show file tree
Hide file tree
Showing 5 changed files with 105 additions and 12 deletions.
6 changes: 3 additions & 3 deletions pysradb/basedb.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@ def __init__(self, sqlite_file):

def open(self):
"""Open sqlite connection."""
sqlite_file_path = os.getcwd()
check_file = "{}/{}".format(sqlite_file_path, self.sqlite_file) # str(sqlite_file_path)+"/"+str(self.sqlite_file)
self.db = sqlite3.connect('file:{}?mode=rw'.format(check_file), uri=True)
# Originally sqlite3.connect(self.sqlite_file)
self.sqlite_file = self.sqlite_file.replace('?','')
self.db = sqlite3.connect('file:{}?mode=rw'.format(self.sqlite_file), uri=True)
self.db.text_factory = str

def close(self):
Expand Down
2 changes: 2 additions & 0 deletions pysradb/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ def _print_save_df(df, saveto=None):
to_print = []
for line in to_print_split:
to_print.append(line.lstrip())
print("sajgndsfsdfdsfsdfdfasfadsfasdfsdfsdfsdfsgsgdasljgnasflnsa")
print(sys.getsizeof(to_print_split))
print(("{}".format(os.linesep)).join(to_print))


Expand Down
11 changes: 2 additions & 9 deletions pysradb/sradb.py
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ def _verify_srametadb(filepath):
db = BASEdb(filepath)
except:
print(
"{} not a valid SRAmetadb.sqlite file.\n".format(filepath)
"{} not a valid SRAmetadb.sqlite file or path.\n".format(filepath)
+ "Please download one using `pysradb metadb`."
)
sys.exit(1)
Expand All @@ -177,15 +177,8 @@ def __init__(self, sqlite_file):
"""
try:
super(SRAdb, self).__init__(sqlite_file)
except:
print(
"{} not a valid SRAmetadb.sqlite file.\n".format(sqlite_file)
+ "Please download one using `pysradb metadb`."
)
sys.exit(1)
_verify_srametadb(sqlite_file)
super(SRAdb, self).__init__(sqlite_file)
self._db_type = "SRA"
self.valid_in_acc_type = [
"SRA",
Expand Down
21 changes: 21 additions & 0 deletions testingdb.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
from pysradb import SRAdb
import os
#db = SRAdb('SRAmetadb.sqlite')

def test_list_tables(sradb_connection):
fields = sradb_connection.list_fields("sqlite_sequence")
print(fields)

def test_changed_paths2():
wrong_filename = "SRAme'tadb2.sql.ite"
path = os.path.join(os.getcwd(), "data", "{}".format(wrong_filename))
try:
db = SRAdb(path)
except:
pass
assert os.path.isfile(path) == False
#assert os.path.isfile(path) == False



test_changed_paths2()
77 changes: 77 additions & 0 deletions tests/_test_sradb.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,9 @@ def test_desc_table(sradb_connection):
def test_all_row_counts(sradb_connection):
assert sradb_connection.all_row_counts().loc["metaInfo", "count"] == 2

def test_all_row_counts2(sradb_connection):
assert len(sradb_connection.all_row_counts()) == 13


def test_sra_metadata(sradb_connection):
df = sradb_connection.sra_metadata("SRP017942")
Expand Down Expand Up @@ -148,3 +151,77 @@ def test_strain_type(sradb_connection):
"s288c",
"s288c",
]

def test_srp_to_srx(sradb_connection):
assert len(sradb_connection.srp_to_srx("SRP082570")) == 14


def test_srp_to_srr(sradb_connection):
df = sradb_connection.srp_to_srr("SRP091987")
assert sorted(list(df["run_accession"])[:3]) == [
'SRR4447104',
'SRR4447105',
'SRR4447106'
]


def test_srp_to_gse(sradb_connection):
gse_id = sradb_connection.srp_to_gse("SRP050443").iloc[0,1]
df = sradb_connection.gse_to_gsm(gse_id)
assert("GSM1557451" in df["experiment_alias"].to_list())


def test_gsm_to_gse(sradb_connection):
df = sradb_connection.gsm_to_gse(["GSM1020651","GSM1020664","GSM1020771"])
assert set(list(df["study_alias"])) == {"GSE41637"}


@pytest.mark.xfail(raises=ValueError)
def test_wrong_input_metadata(sradb_connection):
df = sradb_connection.sra_metadata("should_throw_error")


def test_search_by_expt_id(sradb_connection):
srx_id = "SRX116363"
df_expt = sradb_connection.search_by_expt_id(srx_id)
sra_id = df_expt["submission_accession"].loc[0]
df = sradb_connection.sra_metadata(sra_id)
connected_srp = sradb_connection.srx_to_srp("SRX116363").iloc[0,1]
assert(srx_id in df["experiment_accession"].to_list()) and (connected_srp == "SRP010374")


def test_changed_paths():
wrong_filename = "SRAmet?adb.?sql?ite"
path = os.path.join(os.getcwd(), "data", "{}".format(wrong_filename))
wrong_path = os.path.join(os.getcwd(), "data", "SRAmet")
try:
db = SRAdb(path)
except:
pass
assert os.path.isfile(wrong_path) == False


def test_changed_paths2():
wrong_filename = "SRAme'tadb2.sql.ite"
path = os.path.join(os.getcwd(), "data", "{}".format(wrong_filename))
try:
db = SRAdb(path)
except:
pass
assert os.path.isfile(path) == False


def test_wrong_path_exists():
wrong_filename = "wrongdb.sqlite"
path = os.path.join(os.getcwd(), "data")
wrongfile_path = os.path.join(path, wrong_filename)
with open(wrongfile_path, 'w') as f:
pass
try:
db = SRAdb(wrongfile_path)
assert False
except Exception as e:
assert True
finally:
os.remove(wrongfile_path)

0 comments on commit b7be4d4

Please sign in to comment.