-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2 from CIMAC-CIDC/connect-to-db
Connect to the database from cloud functions
- Loading branch information
Showing
10 changed files
with
138 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
# Environment variable configs for local testing | ||
POSTGRES_URI='postgresql://cidcdev:1234@localhost:5432/cidctest' | ||
GOOGLE_SECRETS_BUCKET='cidc-secrets-staging' | ||
GOOGLE_CLOUD_PROJECT='cidc-dfci-staging' | ||
GOOGLE_UPLOAD_BUCKET='cidc-uploads-staging' | ||
GOOGLE_UPLOAD_TOPIC='uploads' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -85,7 +85,6 @@ celerybeat-schedule | |
*.sage.py | ||
|
||
# Environments | ||
.env | ||
.venv | ||
env/ | ||
venv/ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,5 @@ | ||
# The cidc_api_models package | ||
flask==1.1.1 | ||
flask-sqlalchemy==2.4.0 | ||
psycopg2-binary==2.8.3 | ||
# The cidc_api_modules package | ||
git+https://github.com/CIMAC-CIDC/cidc-api-gae |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import pytest | ||
|
||
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() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,39 @@ | ||
from tests.util import make_pubsub_event | ||
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 | ||
|
||
|
||
def test_ingest_upload(): | ||
"""Test stub event-processing functionality""" | ||
job_id = "1" | ||
successful_upload_event = make_pubsub_event(job_id) | ||
ingest_upload(successful_upload_event, None) | ||
@with_app_context | ||
def test_ingest_upload(db_session, monkeypatch): | ||
"""Test upload data transfer functionality""" | ||
|
||
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] | ||
|
||
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,22 @@ | ||
import base64 | ||
from functools import wraps | ||
|
||
from flask import Flask | ||
|
||
|
||
def make_pubsub_event(data: str) -> dict: | ||
"""Make pubsub event dictionary with base64-encoded data.""" | ||
b64data = base64.encodebytes(bytes(data, "utf-8")) | ||
return {"data": b64data} | ||
|
||
|
||
def with_app_context(f): | ||
"""Run `f` inside a default Flask app context""" | ||
|
||
@wraps(f) | ||
def wrapped(*args, **kwargs): | ||
app = Flask("test-app") | ||
with app.app_context(): | ||
return f(*args, **kwargs) | ||
|
||
return wrapped |