mirror of
https://github.com/Novartis/cellxgene-gateway.git
synced 2026-10-10 04:40:56 +08:00
fixed unit tests
This commit is contained in:
@@ -10,14 +10,16 @@
|
|||||||
import logging
|
import logging
|
||||||
import time
|
import time
|
||||||
|
|
||||||
from cellxgene_gateway.env import ttl
|
from cellxgene_gateway import env
|
||||||
from cellxgene_gateway.util import current_time_stamp
|
from cellxgene_gateway import util
|
||||||
|
|
||||||
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
|
||||||
class PruneProcessCache:
|
class PruneProcessCache:
|
||||||
def __init__(self, cache):
|
def __init__(self, cache):
|
||||||
self.cache = cache
|
self.cache = cache
|
||||||
self.expire_seconds = 3600 if ttl is None else int(ttl)
|
self.expire_seconds = 3600 if env.ttl is None else int(env.ttl)
|
||||||
|
|
||||||
def __call__(self):
|
def __call__(self):
|
||||||
while True:
|
while True:
|
||||||
@@ -25,7 +27,7 @@ class PruneProcessCache:
|
|||||||
self.prune()
|
self.prune()
|
||||||
|
|
||||||
def prune(self):
|
def prune(self):
|
||||||
timestamp = current_time_stamp()
|
timestamp = util.current_time_stamp()
|
||||||
cutoff = timestamp - self.expire_seconds
|
cutoff = timestamp - self.expire_seconds
|
||||||
processes_to_delete = [
|
processes_to_delete = [
|
||||||
p for p in self.cache.entry_list if p.timestamp < cutoff
|
p for p in self.cache.entry_list if p.timestamp < cutoff
|
||||||
@@ -33,9 +35,9 @@ class PruneProcessCache:
|
|||||||
processes_to_keep = [
|
processes_to_keep = [
|
||||||
p for p in self.cache.entry_list if not p.timestamp < cutoff
|
p for p in self.cache.entry_list if not p.timestamp < cutoff
|
||||||
]
|
]
|
||||||
logger = logging.getLogger("cellxgene_gateway")
|
|
||||||
logger.debug(
|
logger.debug(
|
||||||
f"Cutoff {cutoff} = timestamp {timestamp} - expire seconds {self.expire_seconds} , keeping {processes_to_keep}"
|
f"Cutoff {cutoff} = timestamp {timestamp} - expire seconds {self.expire_seconds} , keeping {processes_to_keep}, pruning {processes_to_delete}"
|
||||||
)
|
)
|
||||||
|
|
||||||
for process in processes_to_delete:
|
for process in processes_to_delete:
|
||||||
|
|||||||
@@ -1,11 +1,19 @@
|
|||||||
import unittest
|
import unittest
|
||||||
|
from flask import Flask
|
||||||
from cellxgene_gateway.cache_entry import CacheEntry, CacheEntryStatus
|
from cellxgene_gateway.cache_entry import CacheEntry, CacheEntryStatus
|
||||||
from cellxgene_gateway.cache_key import CacheKey
|
from cellxgene_gateway.cache_key import CacheKey
|
||||||
|
from cellxgene_gateway.gateway import app
|
||||||
|
|
||||||
key = CacheKey("czi/pbmc3k.h5ad", "pbmc3k.h5ad", "tmp.csv")
|
key = CacheKey("czi/pbmc3k.h5ad", "pbmc3k.h5ad", "tmp.csv")
|
||||||
|
|
||||||
|
|
||||||
class TestRenderEntry(unittest.TestCase):
|
class TestRenderEntry(unittest.TestCase):
|
||||||
|
def setUp(self):
|
||||||
|
self.app = app
|
||||||
|
self.app_context = self.app.test_request_context()
|
||||||
|
self.app_context.push()
|
||||||
|
self.client = self.app.test_client()
|
||||||
|
|
||||||
def test_GIVEN_key_and_port_THEN_returns_loading_CacheEntry(self):
|
def test_GIVEN_key_and_port_THEN_returns_loading_CacheEntry(self):
|
||||||
entry = CacheEntry.for_key("some-key", 1)
|
entry = CacheEntry.for_key("some-key", 1)
|
||||||
self.assertEqual(entry.status, CacheEntryStatus.loading)
|
self.assertEqual(entry.status, CacheEntryStatus.loading)
|
||||||
@@ -14,16 +22,14 @@ class TestRenderEntry(unittest.TestCase):
|
|||||||
actual = CacheEntry.for_key(key, 8000).rewrite_text_content(
|
actual = CacheEntry.for_key(key, 8000).rewrite_text_content(
|
||||||
"src:url(/static/assets/"
|
"src:url(/static/assets/"
|
||||||
)
|
)
|
||||||
expected = (
|
expected = "src:url(/view/czi/pbmc3k.h5ad/static/assets/"
|
||||||
"src:url(http://localhost:5005/view/czi/pbmc3k.h5ad/static/assets/"
|
|
||||||
)
|
|
||||||
self.assertEqual(actual, expected)
|
self.assertEqual(actual, expected)
|
||||||
|
|
||||||
def test_GIVEN_absolute_src_THEN_include_path(self):
|
def test_GIVEN_absolute_src_THEN_include_path(self):
|
||||||
actual = CacheEntry.for_key(key, 8000).rewrite_text_content(
|
actual = CacheEntry.for_key(key, 8000).rewrite_text_content(
|
||||||
'<link rel="shortcut icon" href="/static/assets/favicon.ico">'
|
'<link rel="shortcut icon" href="/static/assets/favicon.ico">'
|
||||||
)
|
)
|
||||||
expected = '<link rel="shortcut icon" href="http://localhost:5005/view/czi/pbmc3k.h5ad/static/assets/favicon.ico">'
|
expected = '<link rel="shortcut icon" href="/view/czi/pbmc3k.h5ad/static/assets/favicon.ico">'
|
||||||
self.assertEqual(actual, expected)
|
self.assertEqual(actual, expected)
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user