From ab8b96c9603aa5c2d55bfab12b28a28af9c14e82 Mon Sep 17 00:00:00 2001 From: Jacob Lurye Date: Mon, 29 Jul 2019 09:27:45 -0400 Subject: [PATCH] Don't actually modify the database in tests --- functions/uploads.py | 2 +- tests/conftest.py | 2 ++ tests/functions/test_uploads.py | 24 +++++++++++++++++++----- 3 files changed, 22 insertions(+), 6 deletions(-) diff --git a/functions/uploads.py b/functions/uploads.py index d1e8d50..794debd 100644 --- a/functions/uploads.py +++ b/functions/uploads.py @@ -15,7 +15,7 @@ def ingest_upload(event: dict, context: BackgroundContext): TODO: actually implement the above functionality. """ - job_id = extract_pubsub_data(event) + job_id = int(extract_pubsub_data(event)) session = get_db_session() job: UploadJobs = UploadJobs.find_by_id(job_id, session=session) diff --git a/tests/conftest.py b/tests/conftest.py index f514ec0..140a645 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -3,6 +3,8 @@ from functions.util import get_db_session +# TODO: set up database migrations for this project +# so that tests can actually modify the test database instance. @pytest.fixture def db_session(): return get_db_session() diff --git a/tests/functions/test_uploads.py b/tests/functions/test_uploads.py index e383302..4ead26f 100644 --- a/tests/functions/test_uploads.py +++ b/tests/functions/test_uploads.py @@ -1,3 +1,5 @@ +from unittest.mock import MagicMock + from cidc_api.models import UploadJobs from tests.util import make_pubsub_event, with_app_context @@ -5,21 +7,33 @@ @with_app_context -def test_ingest_upload(db_session): +def test_ingest_upload(db_session, monkeypatch): """Test upload data transfer functionality""" - EMAIL = "test@email.com" + JOB_ID = 1 URI1 = "/path/to/file1" URI2 = "/path/to/deeper/file2" TS_AND_PATH = "/1234/local_path1.txt" FILE_URIS = [URI1 + TS_AND_PATH, URI2 + TS_AND_PATH] - METADATA_PATCH = {"lead_organization_study_id": "CIMAC-12345"} - # Add a test job to the database - job = UploadJobs.create(EMAIL, FILE_URIS, METADATA_PATCH, session=db_session) + job = UploadJobs( + id=JOB_ID, + uploader_email="test@email.com", + gcs_file_uris=FILE_URIS, + metadata_json_patch={"lead_organization_study_id": "CIMAC-12345"}, + status="completed", + ) + + # Since the test database isn't yet set up with migrations, + # it won't have the correct relations in it, so we can't actually + # store or retrieve data from it. + find_by_id = MagicMock() + find_by_id.return_value = job + monkeypatch.setattr(UploadJobs, "find_by_id", find_by_id) successful_upload_event = make_pubsub_event(str(job.id)) response = ingest_upload(successful_upload_event, None) assert response.json[URI1 + TS_AND_PATH] == URI1 assert response.json[URI2 + TS_AND_PATH] == URI2 + find_by_id.assert_called_once_with(JOB_ID, session=db_session)