Skip to content

Commit

Permalink
Adds an OSError to kdb.database when the input file does not exist.
Browse files Browse the repository at this point in the history
  • Loading branch information
MatthewRalston committed Dec 29, 2020
1 parent 851cb0e commit c1bbfc6
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 6 deletions.
4 changes: 3 additions & 1 deletion kdb/database.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,9 @@ class SqliteKdb:
def __init__(self, filename:str, k:int):

if type(filename) is not str:
raise TypeError("kdb.database.build_sqlite_file expects a str as its first positional argument")
raise TypeError("kdb.database.SqliteKdb expects a str as its first positional argument")
elif not os.path.exists(filename):
raise OSError("kdb.database.SqliteKdb expects an existing .kdb file as its first positional argument")
elif type(k) is not int:
raise TypeError("kdb.database.build_sqlite_file expects an int as its second positional argument")
self._max_records = 4**k
Expand Down
5 changes: 4 additions & 1 deletion test/test_database.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import sys
import os
import unittest
import tempfile

Expand Down Expand Up @@ -35,7 +36,9 @@ def test_filepath_exists(self):
SqliteKdb throws an OSError if it receives a filepath that doesnt exist
"""
with self.assertRaises(OSError):
database.SqliteKdb("helloworld.txt", self.k)
db = database.SqliteKdb("helloworld.txt", self.k)
db.conn.close()
os.unlink("helloworld.txt")

def test_k_is_int_only(self):
"""
Expand Down
11 changes: 7 additions & 4 deletions test/test_parsefile.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import sys
import os
import unittest

sys.path.append("..")
Expand Down Expand Up @@ -33,7 +34,9 @@ def test_filepath_exists(self):
parsefile throws an OSError if it receives a filepath that doesnt exist
"""
with self.assertRaises(OSError):
parse.parsefile("helloworld.txt", self.k)
db, header = parse.parsefile("foobar.txt", self.k)
db.conn.close()
os.unlink("foobar.txt")

def test_k_is_int_only(self):
"""
Expand Down Expand Up @@ -111,11 +114,11 @@ def test_returns_database_and_header_dictionary(self):
testdb, headerDict = parse.parsefile(self.fasta, self.k)
self.assertDictEqual({'filename': 'test/data/Ctetani_E88.fasta.gz',
'md5': '3a8682befcdba8ca6405a4bc238e6f06',
'mononucleotides': {'A': 988498, 'C': 410071, 'G': 394694, 'T': 1005988},
'mononucleotides': {'A': 1016563, 'C': 418556, 'G': 404380, 'T': 1033834},
'nullomers': 0,
'sha256': '438446b1081c6aa65c88cc81fbe5e65c85077f1c46951e52793f8f4bc5fe7b67',
'total_kmers': 148160,
'total_reads': 3,
'total_kmers': 5746658,
'total_reads': 2,
'unique_kmers': 64}
, headerDict)
self.assertTrue(isinstance(testdb, database.SqliteKdb))
Expand Down

0 comments on commit c1bbfc6

Please sign in to comment.