mirror of
https://github.com/chanzuckerberg/cellxgene.git
synced 2026-10-09 02:18:12 +08:00
Dunitz/czi hosted test server (#2254)
update hosted unit tests to use wsgi server instead of cellxgene packages
This commit is contained in:
@@ -1,18 +1,14 @@
|
||||
import os
|
||||
import shutil
|
||||
import unittest
|
||||
import random
|
||||
from unittest import mock
|
||||
import yaml
|
||||
|
||||
from backend.test import FIXTURES_ROOT
|
||||
from backend.test.test_czi_hosted.unit import BaseTest
|
||||
|
||||
|
||||
def mockenv(**envvars):
|
||||
return mock.patch.dict(os.environ, envvars)
|
||||
|
||||
|
||||
class ConfigTests(unittest.TestCase):
|
||||
class ConfigTests(BaseTest):
|
||||
tmp_fixtures_directory = os.path.join(FIXTURES_ROOT, "tmp_dir")
|
||||
|
||||
@classmethod
|
||||
|
||||
@@ -49,6 +49,8 @@ class BaseConfigTest(ConfigTests):
|
||||
[
|
||||
("app__verbose", True, False),
|
||||
("app__flask_secret_key", "secret", None),
|
||||
('authentication__type', 'session', 'test'),
|
||||
('authentication__insecure_test_environment', False, True),
|
||||
("multi_dataset__dataroot", FIXTURES_ROOT, None),
|
||||
("multi_dataset__matrix_cache__timelimit_s", 5, 30),
|
||||
("data_locator__s3__region_name", "us-east-1", True),
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
import json
|
||||
import os
|
||||
import tempfile
|
||||
|
||||
import requests
|
||||
import unittest
|
||||
from unittest.mock import patch
|
||||
|
||||
@@ -9,7 +9,6 @@ from backend.czi_hosted.common.annotations.hosted_tiledb import AnnotationsHoste
|
||||
from backend.czi_hosted.common.annotations.local_file_csv import AnnotationsLocalFile
|
||||
from backend.czi_hosted.common.config.app_config import AppConfig
|
||||
from backend.czi_hosted.common.config.base_config import BaseConfig
|
||||
from backend.test.test_czi_hosted.unit import test_server
|
||||
from backend.test import PROJECT_ROOT, FIXTURES_ROOT
|
||||
|
||||
from backend.common.errors import ConfigurationError
|
||||
@@ -197,31 +196,35 @@ class TestDatasetConfig(ConfigTests):
|
||||
# no specializations for set3 (they get the default dataset config)
|
||||
config.complete_config()
|
||||
|
||||
with test_server(app_config=config) as server:
|
||||
session = requests.Session()
|
||||
server = self.create_app(config)
|
||||
|
||||
response = session.get(f"{server}/set1/1/2/pbmc3k.h5ad/api/v0.2/config")
|
||||
data_config = response.json()
|
||||
assert data_config["config"]["displayNames"]["dataset"] == "pbmc3k"
|
||||
assert data_config["config"]["parameters"]["annotations"] is False
|
||||
assert data_config["config"]["parameters"]["disable-diffexp"] is False
|
||||
assert data_config["config"]["parameters"]["about_legal_tos"] == "tos_set1.html"
|
||||
server.testing = True
|
||||
session = server.test_client()
|
||||
|
||||
response = session.get(f"{server}/set2/pbmc3k.cxg/api/v0.2/config")
|
||||
data_config = response.json()
|
||||
assert data_config["config"]["displayNames"]["dataset"] == "pbmc3k"
|
||||
assert data_config["config"]["parameters"]["annotations"] is True
|
||||
assert data_config["config"]["parameters"]["about_legal_tos"] == "tos_set2.html"
|
||||
response = session.get("/set1/1/2/pbmc3k.h5ad/api/v0.2/config")
|
||||
data_config = json.loads(response.data)
|
||||
|
||||
response = session.get(f"{server}/set3/pbmc3k.cxg/api/v0.2/config")
|
||||
data_config = response.json()
|
||||
assert data_config["config"]["displayNames"]["dataset"] == "pbmc3k"
|
||||
assert data_config["config"]["parameters"]["annotations"] is True
|
||||
assert data_config["config"]["parameters"]["disable-diffexp"] is False
|
||||
assert data_config["config"]["parameters"]["about_legal_tos"] == "tos_default.html"
|
||||
assert data_config["config"]["displayNames"]["dataset"] == "pbmc3k"
|
||||
assert data_config["config"]["parameters"]["annotations"] is False
|
||||
assert data_config["config"]["parameters"]["disable-diffexp"] is False
|
||||
assert data_config["config"]["parameters"]["about_legal_tos"] == "tos_set1.html"
|
||||
|
||||
response = session.get(f"{server}/health")
|
||||
assert response.json()["status"] == "pass"
|
||||
response = session.get("/set2/pbmc3k.cxg/api/v0.2/config")
|
||||
data_config = json.loads(response.data)
|
||||
assert data_config["config"]["displayNames"]["dataset"] == "pbmc3k"
|
||||
assert data_config["config"]["parameters"]["annotations"] is True
|
||||
assert data_config["config"]["parameters"]["about_legal_tos"] == "tos_set2.html"
|
||||
|
||||
response = session.get("/set3/pbmc3k.cxg/api/v0.2/config")
|
||||
data_config = json.loads(response.data)
|
||||
assert data_config["config"]["displayNames"]["dataset"] == "pbmc3k"
|
||||
assert data_config["config"]["parameters"]["annotations"] is True
|
||||
assert data_config["config"]["parameters"]["disable-diffexp"] is False
|
||||
assert data_config["config"]["parameters"]["about_legal_tos"] == "tos_default.html"
|
||||
|
||||
response = session.get("/health")
|
||||
|
||||
assert json.loads(response.data)["status"] == "pass"
|
||||
|
||||
def test_configfile_with_specialization(self):
|
||||
# test that per_dataset_config config load the default config, then the specialized config
|
||||
|
||||
@@ -1,12 +1,12 @@
|
||||
import json
|
||||
import os
|
||||
from unittest.mock import patch
|
||||
|
||||
import requests
|
||||
import yaml
|
||||
|
||||
from backend.common.errors import ConfigurationError
|
||||
from backend.czi_hosted.common.config.app_config import AppConfig
|
||||
from backend.common.utils.type_conversion_utils import convert_string_to_value
|
||||
from backend.test.test_czi_hosted.unit import test_server
|
||||
from backend.test import FIXTURES_ROOT
|
||||
from backend.test.test_czi_hosted.unit.common.config import ConfigTests
|
||||
|
||||
@@ -39,24 +39,35 @@ class TestExternalConfig(ConfigTests):
|
||||
env = os.environ
|
||||
env["DATAPATH"] = f"{FIXTURES_ROOT}/pbmc3k.cxg"
|
||||
env["DIFFEXP"] = "False"
|
||||
with test_server(command_line_args=["-c", configfile], env=env) as server:
|
||||
session = requests.Session()
|
||||
response = session.get(f"{server}/api/v0.2/config")
|
||||
data_config = response.json()
|
||||
self.assertEqual(data_config["config"]["displayNames"]["dataset"], "pbmc3k")
|
||||
self.assertTrue(data_config["config"]["parameters"]["disable-diffexp"])
|
||||
config = AppConfig()
|
||||
config.update_from_config_file(configfile)
|
||||
config.update_server_config(app__flask_secret_key="123 magic")
|
||||
|
||||
env["DATAPATH"] = f"{FIXTURES_ROOT}/a95c59b4-7f5d-4b80-ad53-a694834ca18b.h5ad"
|
||||
env["DIFFEXP"] = "True"
|
||||
with test_server(command_line_args=["-c", configfile], env=env) as server:
|
||||
session = requests.Session()
|
||||
response = session.get(f"{server}/api/v0.2/config")
|
||||
data_config = response.json()
|
||||
self.assertEqual(data_config["config"]["displayNames"]["dataset"], "a95c59b4-7f5d-4b80-ad53-a694834ca18b")
|
||||
self.assertFalse(data_config["config"]["parameters"]["disable-diffexp"])
|
||||
server = self.create_app(config)
|
||||
|
||||
server.testing = True
|
||||
session = server.test_client()
|
||||
|
||||
response = session.get("/api/v0.2/config")
|
||||
data_config = json.loads(response.data)
|
||||
self.assertEqual(data_config["config"]["displayNames"]["dataset"], "pbmc3k")
|
||||
self.assertTrue(data_config["config"]["parameters"]["disable-diffexp"])
|
||||
|
||||
os.environ["DATAPATH"] = f"{FIXTURES_ROOT}/a95c59b4-7f5d-4b80-ad53-a694834ca18b.h5ad"
|
||||
os.environ["DIFFEXP"] = "True"
|
||||
|
||||
server= self.create_app(config)
|
||||
|
||||
server.testing = True
|
||||
session = server.test_client()
|
||||
|
||||
# session = requests.Session()
|
||||
response = session.get("/api/v0.2/config")
|
||||
data_config = json.loads(response.data)
|
||||
self.assertEqual(data_config["config"]["displayNames"]["dataset"], "a95c59b4-7f5d-4b80-ad53-a694834ca18b")
|
||||
self.assertFalse(data_config["config"]["parameters"]["disable-diffexp"])
|
||||
|
||||
def test_environment_variable_errors(self):
|
||||
|
||||
# no name
|
||||
app_config = AppConfig()
|
||||
app_config.external_config.environment = [dict(required=True, path=["this", "is", "a", "path"])]
|
||||
|
||||
@@ -1,23 +1,18 @@
|
||||
import json
|
||||
import os
|
||||
import unittest
|
||||
from unittest import mock
|
||||
from unittest.mock import patch
|
||||
|
||||
|
||||
from backend.czi_hosted.common.config.base_config import BaseConfig
|
||||
from backend.common.utils.utils import find_available_port
|
||||
from backend.test.test_czi_hosted.unit import test_server
|
||||
from backend.test import PROJECT_ROOT, FIXTURES_ROOT
|
||||
import requests
|
||||
|
||||
from backend.czi_hosted.common.config.app_config import AppConfig
|
||||
from backend.common.errors import ConfigurationError
|
||||
from backend.test.test_czi_hosted.unit.common.config import ConfigTests
|
||||
|
||||
|
||||
def mockenv(**envvars):
|
||||
return mock.patch.dict(os.environ, envvars)
|
||||
|
||||
|
||||
class TestServerConfig(ConfigTests):
|
||||
def setUp(self):
|
||||
self.config_file_name = f"{unittest.TestCase.id(self).split('.')[-1]}.yml"
|
||||
@@ -147,8 +142,8 @@ class TestServerConfig(ConfigTests):
|
||||
with self.assertRaises(ConfigurationError):
|
||||
config.server_config.handle_data_source()
|
||||
|
||||
@unittest.skip("skip when running in github action")
|
||||
def test_get_api_base_url_works(self):
|
||||
|
||||
# test the api_base_url feature, and that it can contain a path
|
||||
config = AppConfig()
|
||||
backend_port = find_available_port("localhost", 10000)
|
||||
@@ -156,21 +151,22 @@ class TestServerConfig(ConfigTests):
|
||||
app__flask_secret_key="secret",
|
||||
app__api_base_url=f"http://localhost:{backend_port}/additional/path",
|
||||
multi_dataset__dataroot=f"{PROJECT_ROOT}/example-dataset",
|
||||
multi_dataset__allowed_matrix_types=["cxg"],
|
||||
)
|
||||
|
||||
config.complete_config()
|
||||
server = self.create_app(config)
|
||||
server.testing = True
|
||||
session = server.test_client()
|
||||
response = session.get(f"/additional/path/d/pbmc3k.h5ad/api/v0.2/config")
|
||||
|
||||
with test_server(["-p", str(backend_port)], app_config=config) as server:
|
||||
session = requests.Session()
|
||||
self.assertEqual(server, f"http://localhost:{backend_port}")
|
||||
response = session.get(f"{server}/additional/path/d/pbmc3k.h5ad/api/v0.2/config")
|
||||
self.assertEqual(response.status_code, 200)
|
||||
data_config = response.json()
|
||||
self.assertEqual(data_config["config"]["displayNames"]["dataset"], "pbmc3k")
|
||||
self.assertEqual(response.status_code, 200)
|
||||
data_config = json.loads(response.data)
|
||||
self.assertEqual(data_config["config"]["displayNames"]["dataset"], "pbmc3k")
|
||||
|
||||
# test the health check at the correct url
|
||||
response = session.get(f"{server}/additional/path/health")
|
||||
assert response.json()["status"] == "pass"
|
||||
# test the health check at the correct url
|
||||
response = session.get(f"/additional/path/health")
|
||||
assert json.loads(response.data)["status"] == "pass"
|
||||
|
||||
def test_get_web_base_url_works(self):
|
||||
config = self.get_config(web_base_url="www.thisisawebsite.com")
|
||||
@@ -225,7 +221,9 @@ class TestServerConfig(ConfigTests):
|
||||
)
|
||||
self.config.complete_config()
|
||||
|
||||
def test_mulitdatasets_work_e2e(self):
|
||||
@patch("backend.czi_hosted.app.app.render_template")
|
||||
def test_mulitdatasets_work_e2e(self, mock_render_template):
|
||||
mock_render_template.return_value = "something"
|
||||
# test that multi dataroots work end to end
|
||||
self.config.update_server_config(
|
||||
multi_dataset__dataroot=dict(
|
||||
@@ -251,39 +249,43 @@ class TestServerConfig(ConfigTests):
|
||||
# no specializations for set3 (they get the default dataset config)
|
||||
self.config.complete_config()
|
||||
|
||||
with test_server(app_config=self.config) as server:
|
||||
session = requests.Session()
|
||||
server = self.create_app(self.config)
|
||||
server.auth.requires_client_login = lambda: False
|
||||
server.testing = True
|
||||
session = server.test_client()
|
||||
|
||||
response = session.get(f"{server}/set1/1/2/pbmc3k.h5ad/api/v0.2/config")
|
||||
data_config = response.json()
|
||||
assert data_config["config"]["displayNames"]["dataset"] == "pbmc3k"
|
||||
assert data_config["config"]["parameters"]["annotations"] is False
|
||||
assert data_config["config"]["parameters"]["disable-diffexp"] is False
|
||||
assert data_config["config"]["parameters"]["about_legal_tos"] == "tos_set1.html"
|
||||
response = session.get(f"/set1/1/2/pbmc3k.h5ad/api/v0.2/config")
|
||||
|
||||
response = session.get(f"{server}/set2/pbmc3k.cxg/api/v0.2/config")
|
||||
data_config = response.json()
|
||||
assert data_config["config"]["displayNames"]["dataset"] == "pbmc3k"
|
||||
assert data_config["config"]["parameters"]["annotations"] is True
|
||||
assert data_config["config"]["parameters"]["about_legal_tos"] == "tos_set2.html"
|
||||
data_config = json.loads(response.data)
|
||||
assert data_config["config"]["displayNames"]["dataset"] == "pbmc3k"
|
||||
assert data_config["config"]["parameters"]["annotations"] is False
|
||||
assert data_config["config"]["parameters"]["disable-diffexp"] is False
|
||||
assert data_config["config"]["parameters"]["about_legal_tos"] == "tos_set1.html"
|
||||
|
||||
response = session.get(f"{server}/set3/pbmc3k.cxg/api/v0.2/config")
|
||||
data_config = response.json()
|
||||
assert data_config["config"]["displayNames"]["dataset"] == "pbmc3k"
|
||||
assert data_config["config"]["parameters"]["annotations"] is True
|
||||
assert data_config["config"]["parameters"]["disable-diffexp"] is False
|
||||
assert data_config["config"]["parameters"]["about_legal_tos"] == "tos_default.html"
|
||||
response = session.get("/set2/pbmc3k.cxg/api/v0.2/config")
|
||||
|
||||
response = session.get(f"{server}/health")
|
||||
assert response.json()["status"] == "pass"
|
||||
data_config = json.loads(response.data)
|
||||
assert data_config["config"]["displayNames"]["dataset"] == "pbmc3k"
|
||||
assert data_config["config"]["parameters"]["annotations"] is True
|
||||
assert data_config["config"]["parameters"]["about_legal_tos"] == "tos_set2.html"
|
||||
|
||||
# access a dataset (no slash)
|
||||
response = session.get(f"{server}/set2/pbmc3k.cxg")
|
||||
self.assertEqual(response.status_code, 200)
|
||||
response = session.get("/set3/pbmc3k.cxg/api/v0.2/config")
|
||||
data_config = json.loads(response.data)
|
||||
assert data_config["config"]["displayNames"]["dataset"] == "pbmc3k"
|
||||
assert data_config["config"]["parameters"]["annotations"] is True
|
||||
assert data_config["config"]["parameters"]["disable-diffexp"] is False
|
||||
assert data_config["config"]["parameters"]["about_legal_tos"] == "tos_default.html"
|
||||
|
||||
# access a dataset (with slash)
|
||||
response = session.get(f"{server}/set2/pbmc3k.cxg/")
|
||||
self.assertEqual(response.status_code, 200)
|
||||
response = session.get("/health")
|
||||
assert json.loads(response.data)["status"] == "pass"
|
||||
|
||||
# access a dataset (no slash)
|
||||
response = session.get("/set2/pbmc3k.cxg")
|
||||
self.assertEqual(response.status_code, 200)
|
||||
|
||||
# access a dataset (with slash)
|
||||
response = session.get("/set2/pbmc3k.cxg/")
|
||||
self.assertEqual(response.status_code, 200)
|
||||
|
||||
@patch("backend.czi_hosted.common.config.server_config.diffexp_tiledb.set_config")
|
||||
def test_handle_diffexp(self, mock_tiledb_config):
|
||||
|
||||
Reference in New Issue
Block a user