Skip to content

Commit

Permalink
Don't actually modify the database in tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jacoblurye committed Jul 29, 2019
1 parent dc147bf commit ab8b96c
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 6 deletions.
2 changes: 1 addition & 1 deletion functions/uploads.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 2 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
24 changes: 19 additions & 5 deletions tests/functions/test_uploads.py
Original file line number Diff line number Diff line change
@@ -1,25 +1,39 @@
from unittest.mock import MagicMock

from cidc_api.models import UploadJobs

from tests.util import make_pubsub_event, with_app_context
from functions.uploads import ingest_upload


@with_app_context
def test_ingest_upload(db_session):
def test_ingest_upload(db_session, monkeypatch):
"""Test upload data transfer functionality"""

EMAIL = "[email protected]"
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="[email protected]",
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)

0 comments on commit ab8b96c

Please sign in to comment.