aboutsummaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
Diffstat (limited to 'tests')
-rw-r--r--tests/integration/conftest.py9
-rw-r--r--tests/integration/test_correlation.py10
-rw-r--r--tests/performance/perf_query.py14
-rw-r--r--tests/unit/test_db_utils.py63
4 files changed, 39 insertions, 57 deletions
diff --git a/tests/integration/conftest.py b/tests/integration/conftest.py
index c953d84..795c42d 100644
--- a/tests/integration/conftest.py
+++ b/tests/integration/conftest.py
@@ -4,7 +4,7 @@ import MySQLdb
from gn3.app import create_app
from gn3.chancy import random_string
-from gn3.db_utils import parse_db_url, database_connector
+from gn3.db_utils import parse_db_url, database_connection
@pytest.fixture(scope="session")
def client():
@@ -18,17 +18,18 @@ def client():
@pytest.fixture(scope="session")
-def db_conn():
+def db_conn(client):
"""Create a db connection fixture for tests"""
# 01) Generate random string to append to all test db artifacts for the session
+ live_db_uri = client.application.config["SQL_URI"]
rand_str = random_string(15)
- live_db_details = parse_db_url()
+ live_db_details = parse_db_url(live_db_uri)
test_db_name = f"test_{live_db_details[3]}_{rand_str}"
#
# 02) Create new test db
# Use context manager to ensure the live connection is automatically
# closed on exit
- with database_connector() as live_db_conn:
+ with database_connection(live_db_uri) as live_db_conn:
with live_db_conn.cursor() as live_db_cursor:
live_db_cursor.execute(f"CREATE DATABASE {test_db_name}")
#
diff --git a/tests/integration/test_correlation.py b/tests/integration/test_correlation.py
index c1d518d..51769b5 100644
--- a/tests/integration/test_correlation.py
+++ b/tests/integration/test_correlation.py
@@ -65,15 +65,15 @@ class CorrelationIntegrationTest(TestCase):
@pytest.mark.integration_test
@mock.patch("gn3.api.correlation.compute_all_lit_correlation")
- @mock.patch("gn3.api.correlation.database_connector")
- def test_lit_correlation(self, database_connector, mock_compute_corr):
+ @mock.patch("gn3.api.correlation.database_connection")
+ def test_lit_correlation(self, database_connection, mock_compute_corr):
"""Test api/correlation/lit_corr/{species}/{gene_id}"""
mock_compute_corr.return_value = []
- database_connector.return_value = mock.Mock()
- database_connector.return_value.__enter__ = mock.Mock()
- database_connector.return_value.__exit__ = mock.Mock()
+ database_connection.return_value = mock.Mock()
+ database_connection.return_value.__enter__ = mock.Mock()
+ database_connection.return_value.__exit__ = mock.Mock()
post_data = {"1426678_at": "68031",
"1426679_at": "68036",
diff --git a/tests/performance/perf_query.py b/tests/performance/perf_query.py
index e534e9b..cdac9a9 100644
--- a/tests/performance/perf_query.py
+++ b/tests/performance/perf_query.py
@@ -7,7 +7,7 @@ from inspect import getmembers
from inspect import isfunction
from functools import wraps
-from gn3.db_utils import database_connector
+from gn3.db_utils import database_connection
def timer(func):
@@ -26,9 +26,10 @@ def timer(func):
def query_executor(query: str,
+ sql_uri: str,
fetch_all: bool = True):
"""function to execute a query"""
- with database_connector() as conn:
+ with database_connection(sql_uri) as conn:
with conn.cursor() as cursor:
cursor.execute(query)
@@ -58,22 +59,22 @@ def fetch_probeset_query(dataset_name: str):
@timer
-def perf_hc_m2_dataset():
+def perf_hc_m2_dataset(sql_uri: str):
"""test the default dataset HC_M2_0606_P"""
dataset_name = "HC_M2_0606_P"
print(f"Performance test for {dataset_name}")
- query_executor(fetch_probeset_query(dataset_name=dataset_name))
+ query_executor(fetch_probeset_query(dataset_name=dataset_name), sql_uri)
@timer
-def perf_umutaffyexon_dataset():
+def perf_umutaffyexon_dataset(sql_uri):
"""largest dataset in gn"""
dataset_name = "UMUTAffyExon_0209_RMA"
print(f"Performance test for {dataset_name}")
- query_executor(fetch_probeset_query(dataset_name=dataset_name))
+ query_executor(fetch_probeset_query(dataset_name=dataset_name), sql_uri)
def fetch_perf_functions():
@@ -102,6 +103,7 @@ def fetch_cmd_args():
if __name__ == '__main__':
+ # Figure out how to pass the database uri here... Maybe use click.
func_list = fetch_cmd_args()
for func_obj in func_list:
func_obj()
diff --git a/tests/unit/test_db_utils.py b/tests/unit/test_db_utils.py
index a319692..0211107 100644
--- a/tests/unit/test_db_utils.py
+++ b/tests/unit/test_db_utils.py
@@ -1,49 +1,28 @@
"""module contains test for db_utils"""
-
-from unittest import TestCase
from unittest import mock
-from types import SimpleNamespace
import pytest
import gn3
-from gn3.db_utils import database_connector
-from gn3.db_utils import parse_db_url
-
-@pytest.fixture(scope="class")
-def setup_app(request, fxtr_app):
- """Setup the fixtures for the class."""
- request.cls.app = fxtr_app
-
-class TestDatabase(TestCase):
- """class contains testd for db connection functions"""
-
- @pytest.mark.unit_test
- @mock.patch("gn3.db_utils.mdb")
- @mock.patch("gn3.db_utils.parse_db_url")
- def test_database_connector(self, mock_db_parser, mock_sql):
- """test for creating database connection"""
- mock_db_parser.return_value = ("localhost", "guest", "4321", "users", None)
- callable_cursor = lambda: SimpleNamespace(execute=3)
- cursor_object = SimpleNamespace(cursor=callable_cursor)
- mock_sql.connect.return_value = cursor_object
- mock_sql.close.return_value = None
- results = database_connector()
-
+from gn3.db_utils import parse_db_url, database_connection
+
+@pytest.mark.unit_test
+@mock.patch("gn3.db_utils.mdb")
+@mock.patch("gn3.db_utils.parse_db_url")
+def test_database_connection(mock_db_parser, mock_sql):
+ """test for creating database connection"""
+ print(f"MOCK SQL: {mock_sql}")
+ print(f"MOCK DB PARSER: {mock_db_parser}")
+ mock_db_parser.return_value = ("localhost", "guest", "4321", "users", None)
+
+ with database_connection("mysql://guest:4321@localhost/users") as conn:
mock_sql.connect.assert_called_with(
- "localhost", "guest", "4321", "users", port=3306)
- self.assertIsInstance(
- results, SimpleNamespace, "database not created successfully")
-
- @pytest.mark.unit_test
- @pytest.mark.usefixtures("setup_app")
- def test_parse_db_url(self):
- """test for parsing db_uri env variable"""
- print(self.__dict__)
- with self.app.app_context(), mock.patch.dict(# pylint: disable=[no-member]
- gn3.db_utils.current_app.config,
- {"SQL_URI": "mysql://username:4321@localhost/test"},
- clear=True):
- results = parse_db_url()
- expected_results = ("localhost", "username", "4321", "test", None)
- self.assertEqual(results, expected_results)
+ db="users", user="guest", passwd="4321", host="localhost",
+ port=3306)
+
+@pytest.mark.unit_test
+def test_parse_db_url():
+ """test for parsing db_uri env variable"""
+ results = parse_db_url("mysql://username:4321@localhost/test")
+ expected_results = ("localhost", "username", "4321", "test", None)
+ assert results == expected_results