Skip to content

Commit

Permalink
Make tests pass. Don't assume /tmp is the OS tmpfile path.
Browse files Browse the repository at this point in the history
Make seqrepo-rest-service endpoint used in tests configurable with SEQREPO_REST_URL

Add comment to _open_for_reading. Add close to alias db handle
  • Loading branch information
theferrit32 committed Oct 6, 2023
1 parent 7a85d24 commit 701f0b3
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 16 deletions.
20 changes: 6 additions & 14 deletions src/biocommons/seqrepo/fastadir/fastadir.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ class LockableFabgzReader(contextlib.AbstractContextManager):
def __init__(self, path):
self.lock = threading.Lock()
self.fabgz_reader = FabgzReader(path)
# super().__init__(*args, **kwargs)

def __enter__(self):
self.lock.acquire()
Expand Down Expand Up @@ -151,18 +150,11 @@ def fetch(self, seq_id, start=None, end=None):
self.commit()

path = os.path.join(self._root_dir, rec["relpath"])
_logger.info(
"path: %s, seq_id: %s, start: %s, end: %s",
path, seq_id, start, end)

# fabgz = self._open_for_reading(path)
# return fabgz.fetch(seq_id, start, end)

with self._open_for_reading(path) as fabgz:
seq = fabgz.fetch(seq_id, start, end)
return seq


@functools.lru_cache(maxsize=SEQREPO_LRU_CACHE_MAXSIZE)
def fetch_seqinfo(self, seq_id):
"""fetch sequence info by seq_id"""
Expand Down Expand Up @@ -248,17 +240,17 @@ def _upgrade_db(self):

@functools.lru_cache()
def _open_for_reading(self, path):
"""
Opens a FabgzReader to path, wraps in a LockableFabgzReader for use in context managers.
Places it in an LRU cache so file is only opened once per FastaDir object. Caller must
lock the LockableFabgzReader or otherwise handle concurrent access if sharing between
in-process concurrent execution threads, such as asyncio (e.g. WSGI/ASGI web servers)
"""
_logger.debug("Opening for reading: %s", path)
if not os.path.exists(path):
_logger.error("_open_for_reading path does not exist: %s", path)
return LockableFabgzReader(path)

# def _open_for_reading(self, path):
# _logger.debug("Opening for reading: " + path)
# if not os.path.exists(path):
# _logger.error("_open_for_reading path does not exist: %s", path)
# return FabgzReader(path)

def _dump_aliases(self):
import prettytable

Expand Down
3 changes: 3 additions & 0 deletions src/biocommons/seqrepo/seqaliasdb/seqaliasdb.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,9 @@ def __init__(
# ############################################################################
# Special methods

def __del__(self):
self._db.close()

def __contains__(self, seq_id):
cursor = self._db.cursor()
cursor.execute(
Expand Down
3 changes: 2 additions & 1 deletion tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ def dataproxy():

@pytest.fixture(scope="session")
def rest_dataproxy():
return SeqRepoRESTDataProxy(base_url="http://localhost:5000/seqrepo")
url = os.environ.get("SEQREPO_REST_URL", "http://localhost:5000/seqrepo")
return SeqRepoRESTDataProxy(base_url=url)


@pytest.fixture(scope="session")
Expand Down
2 changes: 1 addition & 1 deletion tests/test_fabgz.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ def test_write_reread():

# now read them back
far = FabgzReader(fabgz_fn)
assert far.filename.startswith("/tmp/".encode())
assert far.filename.startswith(tmpdir.encode())
assert set(far.keys()) == set(sequences.keys())
assert 5 == len(far), "expected 5 sequences"
assert "l10" in far.keys()
Expand Down

0 comments on commit 701f0b3

Please # to comment.