From 1e1de1987815f02fc8802dba9e3332936af5702b Mon Sep 17 00:00:00 2001 From: Alok Saldanha Date: Mon, 16 Sep 2019 10:58:02 -0400 Subject: [PATCH] added unit test --- Readme.md | 11 +++++++++-- cellxgene_gateway/env.py | 31 +++++++++++++++--------------- cellxgene_gateway/extra_scripts.py | 3 ++- cellxgene_gateway/gateway.py | 1 + tests/test_extra_scripts.py | 23 ++++++++++++++++++++++ 5 files changed, 51 insertions(+), 18 deletions(-) create mode 100644 tests/test_extra_scripts.py diff --git a/Readme.md b/Readme.md index da71c39..87985bc 100644 --- a/Readme.md +++ b/Readme.md @@ -65,6 +65,7 @@ Here's what the environment variables mean: * `CELLXGENE_DATA` - a directory that can contain subdirectories with `.h5ad` data files, *without* trailing slash, e.g. `/mnt/cellxgene_data` * `GATEWAY_HOST` - the hostname and port that the gateway will run on, typically `localhost:5005` if running locally * `GATEWAY_PROTOCOL` - typically http when running locally, can be https when deployed if the gateway is behind a load balancer or reverse proxy. +* `GATEWAY_EXTRA_SCRIPTS` - (optional) JSON array of script paths, will be embedded into each page and forwarded with `--scripts` to cellxgene server The defaults should be fine if you set up a venv and cellxgene_data folder as above. @@ -73,11 +74,11 @@ The defaults should be fine if you set up a venv and cellxgene_data folder as ab The current paradigm for customization is to modify files during a build or deployment phase: * To modify CSS or JS on particular gateway pages, overwrite or append to the templates -* To add script tags such as for user analytics to all pages, overwrite the extra_scripts.py file. +* To add script tags such as for user analytics to all pages, set GATEWAY_EXTRA_SCRIPTS * these scripts will also be run on the pages served by cellxgene server via the --scripts parameter * See https://github.com/chanzuckerberg/cellxgene/pull/680 for details on --scripts parameter -Currently we use a build.sh that copies the gateway to a "build" directory before modifying with sed and the like. +Currently we use a bash script that copies the gateway to a "build" directory before modifying templates with sed and the like. There is probably a better way. # Development @@ -106,6 +107,12 @@ python setup.py develop For convenience, the code repo includes a `run.sh.example` shell script to run the gateway. +## Running Tests + +```bash + python -m unittest discover tests +``` + ## Running Linters pip install isort flake8 black diff --git a/cellxgene_gateway/env.py b/cellxgene_gateway/env.py index 3eecea6..9ad02f0 100644 --- a/cellxgene_gateway/env.py +++ b/cellxgene_gateway/env.py @@ -24,21 +24,22 @@ env_vars = { "GATEWAY_IP": ip, } -if not all(env_vars.values()): - raise ValueError( - f""" -Please ensure that environment variables are set correctly. -The ones with None below are missing and need to be set. +def validate(): + if not all(env_vars.values()): + raise ValueError( + f""" + Please ensure that environment variables are set correctly. + The ones with None below are missing and need to be set. -{env_vars} + {env_vars} -Set them at the terminal before running the gateway. -An example is: + Set them at the terminal before running the gateway. + An example is: - export CELLXGENE_LOCATION=~/anaconda/envs/cellxgene-dev/bin/cellxgene - export CELLXGENE_DATA=../cellxgene_data - export GATEWAY_HOST=localhost:5005 - export GATEWAY_PROTOCOL=http - export GATEWAY_IP=127.0.0.1 -""" - ) + export CELLXGENE_LOCATION=~/anaconda/envs/cellxgene-dev/bin/cellxgene + export CELLXGENE_DATA=../cellxgene_data + export GATEWAY_HOST=localhost:5005 + export GATEWAY_PROTOCOL=http + export GATEWAY_IP=127.0.0.1 + """ + ) diff --git a/cellxgene_gateway/extra_scripts.py b/cellxgene_gateway/extra_scripts.py index 7f758fd..14c2c30 100644 --- a/cellxgene_gateway/extra_scripts.py +++ b/cellxgene_gateway/extra_scripts.py @@ -8,10 +8,11 @@ # the specific language governing permissions and limitations under the License. from cellxgene_gateway import env +from json import loads def get_extra_scripts(): # can be array of script tags to inject on every page, e.g. for google analytics could be # ['https://www.googletagmanager.com/gtag/js?id=UA-123456-2', # f"{env.gateway_protocol}://{env.gateway_host}/static/js/google_ua.js"] # where google_ua.js is a script you add to the static/js folder prior to deployment. - return [] if env.extra_scripts is None else [p.strip() for p in env.extra_scripts.split(",") if len(p.strip()) > 0] + return [] if env.extra_scripts is None else loads(env.extra_scripts) diff --git a/cellxgene_gateway/gateway.py b/cellxgene_gateway/gateway.py index 958a731..dd2277b 100644 --- a/cellxgene_gateway/gateway.py +++ b/cellxgene_gateway/gateway.py @@ -184,6 +184,7 @@ def do_GET(path): def main(): + env.validate() background_thread = Thread(target=PruneProcessCache(cache)) background_thread.start() diff --git a/tests/test_extra_scripts.py b/tests/test_extra_scripts.py new file mode 100644 index 0000000..4e63c89 --- /dev/null +++ b/tests/test_extra_scripts.py @@ -0,0 +1,23 @@ +import unittest +from unittest.mock import MagicMock, patch +from cellxgene_gateway.extra_scripts import get_extra_scripts + +class TestExtraScripts(unittest.TestCase): + @patch('cellxgene_gateway.env.extra_scripts', new='["abc","def"]') + def test_GIVEN_two_scripts_THEN_returns_two_strings(self): + self.assertEqual(get_extra_scripts(), ['abc', 'def']) + + @patch('cellxgene_gateway.env.extra_scripts', new='["abc", "def"]') + def test_GIVEN_two_scripts_space_THEN_returns_two_strings(self): + self.assertEqual(get_extra_scripts(), ['abc', 'def']) + + @patch('cellxgene_gateway.env.extra_scripts', new=None) + def test_GIVEN_none_THEN_returns_empty_array(self): + self.assertEqual(get_extra_scripts(), []) + + @patch('cellxgene_gateway.env.extra_scripts', new='[]') + def test_GIVEN_empty_string_THEN_returns_empty_array(self): + self.assertEqual(get_extra_scripts(), []) + +if __name__ == '__main__': + unittest.main() \ No newline at end of file