From 701f0b36c1d0dbd1d4bc134bc0a8dd966de03798 Mon Sep 17 00:00:00 2001 From: Kyle Ferriter Date: Mon, 28 Aug 2023 00:05:15 -0400 Subject: [PATCH] Make tests pass. Don't assume /tmp is the OS tmpfile path. 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 --- src/biocommons/seqrepo/fastadir/fastadir.py | 20 ++++++------------- .../seqrepo/seqaliasdb/seqaliasdb.py | 3 +++ tests/conftest.py | 3 ++- tests/test_fabgz.py | 2 +- 4 files changed, 12 insertions(+), 16 deletions(-) diff --git a/src/biocommons/seqrepo/fastadir/fastadir.py b/src/biocommons/seqrepo/fastadir/fastadir.py index f1f011c..50de156 100644 --- a/src/biocommons/seqrepo/fastadir/fastadir.py +++ b/src/biocommons/seqrepo/fastadir/fastadir.py @@ -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() @@ -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""" @@ -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 diff --git a/src/biocommons/seqrepo/seqaliasdb/seqaliasdb.py b/src/biocommons/seqrepo/seqaliasdb/seqaliasdb.py index ae1ee8e..ffc001d 100644 --- a/src/biocommons/seqrepo/seqaliasdb/seqaliasdb.py +++ b/src/biocommons/seqrepo/seqaliasdb/seqaliasdb.py @@ -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( diff --git a/tests/conftest.py b/tests/conftest.py index 293f0bb..5231cd4 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -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") diff --git a/tests/test_fabgz.py b/tests/test_fabgz.py index 39fc334..56cfd3b 100644 --- a/tests/test_fabgz.py +++ b/tests/test_fabgz.py @@ -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()