Repository: cancervariants/therapy-normalization (language: python)

Issue:
Use local DynamoDB by default
We currently have the production database as the default, but we should switch to using the local database. 


Discussion / hints:

Fix patch:
diff --git a/README.md b/README.md
index 0b8de245..a4c05e2a 100644
--- a/README.md
+++ b/README.md
@@ -11,7 +11,7 @@ for direction on installing pipenv in your compute environment.
  
 Once installed, from the project root dir, just run:
 
-```shell script
+```commandline
 pipenv sync
 ```
 
@@ -34,7 +34,7 @@ This ensures:
 
 Before first commit run:
 
-```
+```commandline
 pre-commit install
 ```
 
@@ -43,11 +43,11 @@ pre-commit install
 
 Running unit tests is as easy as pytest.
 
-```
+```commandline
 pipenv run pytest
 ```
 
-### Updating the gene normalization database
+### Updating the therapy normalization database
 
 Before you use the CLI to update the database, run the following in a separate terminal to start DynamoDB on `port 8000`:
 
@@ -71,7 +71,7 @@ To update one source, simply set `--normalizer` to the source you wish to update
 
 From the project root, run the following to update the ChEMBL source:
 
-```
+```commandline
 python3 -m therapy.cli --normalizer="chembl"
 ```
 
@@ -83,31 +83,30 @@ To update all sources, use the `--update_all` flag.
 
 From the project root, run the following to update all sources:
 
-```
+```commandline
 python3 -m therapy.cli --update_all
 ```
 
 #### Specifying the database URL endpoint
+The default URL endpoint is `http://localhost:8000`.
+There are two different ways to specify the database URL endpoint.
 
-To specify the database URL endpoint, simply set `--db_url` to the endpoint you want to use. You must also include `--normalizer` or `--update_all`.
-
-From the project root, run the following to update all sources from the database with URL endpoint `http://localhost:8001`:
-
-```
+The first way is to set the `--db_url` flag to the URL endpoint.
+```commandline
 python3 -m therapy.cli --update_all --db_url="http://localhost:8001"
 ```
 
-From the project root, run the following to update all sources from the database with URL endpoint `http://localhost:8000`:
-```
-python3 -m therapy.cli --update_all --dev
+The second way is to set the `THERAPY_NORM_DB_URL` to the URL endpoint.
+```commandline
+export THERAPY_NORM_DB_URL="http://localhost:8001"
+python3 -m therapy.cli --update_all
 ```
 
-
 ### Starting the therapy normalization service
 
 From the project root, run the following:
 
-```shell script
+```commandline
  uvicorn therapy.main:app --reload
 ```
 
diff --git a/therapy/cli.py b/therapy/cli.py
index 65e472d7..1a759ae6 100644
--- a/therapy/cli.py
+++ b/therapy/cli.py
@@ -7,7 +7,6 @@
 from timeit import default_timer as timer
 from therapy.database import Database
 from boto3.dynamodb.conditions import Key
-import sys
 from os import environ
 
 
@@ -20,9 +19,9 @@ class CLI:
         help="The normalizer(s) you wish to update separated by spaces."
     )
     @click.option(
-        '--dev',
+        '--prod',
         is_flag=True,
-        help="Working in development environment on localhost port 8000."
+        help="Working in production environment."
     )
     @click.option(
         '--db_url',
@@ -39,7 +38,7 @@ class CLI:
         help='Update concepts for normalize endpoint. Must select either'
              '--update_all or include Mondo as a normalizer source argument.'
     )
-    def update_normalizer_db(normalizer, dev, db_url, update_all,
+    def update_normalizer_db(normalizer, prod, db_url, update_all,
                              update_merged):
         """Update select normalizer source(s) in the therapy database."""
         sources = {
@@ -51,21 +50,17 @@ def update_normalizer_db(normalizer, dev, db_url, update_all,
             'rxnorm': RxNorm,
         }
 
-        if dev:
-            db: Database = Database(db_url='http://localhost:8000')
-        elif db_url:
-            db: Database = Database(db_url=db_url)
-        elif 'THERAPY_NORM_DB_URL' in environ.keys():
-            # environment variable will be picked up by DB instance
+        if prod:
+            environ['THERAPY_NORM_PROD'] = "TRUE"
             db: Database = Database()
         else:
-            if click.confirm("Are you sure you want to update"
-                             " the production database?", default=False):
-                click.echo("Updating production db...")
-                db: Database = Database()
+            if db_url:
+                endpoint_url = db_url
+            elif 'THERAPY_NORM_DB_URL' in environ.keys():
+                endpoint_url = environ['THERAPY_NORM_DB_URL']
             else:
-                click.echo("Exiting CLI.")
-                sys.exit()
+                endpoint_url = 'http://localhost:8000'
+            db: Database = Database(db_url=endpoint_url)
 
         if update_all:
             normalizers = list(src for src in sources)
diff --git a/therapy/database.py b/therapy/database.py
index 9b7b8776..adaa6020 100644
--- a/therapy/database.py
+++ b/therapy/database.py
@@ -6,6 +6,8 @@
 from os import environ
 from typing import Optional, Dict, List, Any
 import logging
+import click
+import sys
 
 logger = logging.getLogger('therapy')
 logger.setLevel(logging.DEBUG)
@@ -26,25 +28,37 @@ def __init__(self, db_url: str = '', region_name: str = 'us-east-2'):
         :param str db_url: database endpoint URL to connect to
         :param str region_name: AWS region name to use
         """
-        if db_url:
+        if 'THERAPY_NORM_PROD' in environ.keys():
             boto_params = {
-                'region_name': region_name,
-                'endpoint_url': db_url
-            }
-        elif 'THERAPY_NORM_DB_URL' in environ.keys():
-            boto_params = {
-                'region_name': region_name,
-                'endpoint_url': environ['THERAPY_NORM_DB_URL']
+                'region_name': region_name
             }
+            if 'THERAPY_NORM_EB_PROD' not in environ.keys():
+                # EB Instance should not have to confirm.
+                # This is used only for using production via CLI
+                if click.confirm("Are you sure you want to use the "
+                                 "production database?", default=False):
+                    click.echo("***PRODUCTION DATABASE IN USE***")
+                else:
+                    click.echo("Exiting.")
+                    sys.exit()
         else:
+            if db_url:
+                endpoint_url = db_url
+            elif 'THERAPY_NORM_DB_URL' in environ.keys():
+                endpoint_url = environ['THERAPY_NORM_DB_URL']
+            else:
+                endpoint_url = 'http://localhost:8000'
+            click.echo(f"***Using Database Endpoint: {endpoint_url}***")
             boto_params = {
-                'region_name': region_name
+                'region_name': region_name,
+                'endpoint_url': endpoint_url
             }
+
         self.dynamodb = boto3.resource('dynamodb', **boto_params)
         self.dynamodb_client = boto3.client('dynamodb', **boto_params)
 
-        # create tables if nonexistant if not connecting to remote database
-        if db_url or 'THERAPY_NORM_DB_URL' in environ.keys():
+        # create tables if nonexistent if not connecting to remote database
+        if 'THERAPY_NORM_PROD' not in environ.keys():
             existing_tables = self.dynamodb_client.list_tables()['TableNames']
             self.create_therapies_table(existing_tables)
             self.create_meta_data_table(existing_tables)


Test patch:
diff --git a/tests/unit/test_database.py b/tests/unit/test_database.py
index 2a27ce76..c974f991 100644
--- a/tests/unit/test_database.py
+++ b/tests/unit/test_database.py
@@ -11,7 +11,7 @@ def db():
     """Create a DynamoDB test fixture."""
     class DB:
         def __init__(self):
-            self.db = Database()
+            self.db = Database(db_url=os.environ['THERAPY_NORM_DB_URL'])
             if os.environ.get('TEST') is not None:
                 self.load_test_data()
 
@@ -32,11 +32,11 @@ def load_test_data(self):
                         batch.put_item(Item=m)
                 f.close()
 
-    return DB()
+    return DB().db
 
 
 def test_tables_created(db):
     """Check that therapy_concepts and therapy_metadata are created."""
-    existing_tables = db.db.dynamodb_client.list_tables()['TableNames']
+    existing_tables = db.dynamodb_client.list_tables()['TableNames']
     assert 'therapy_concepts' in existing_tables
     assert 'therapy_metadata' in existing_tables