Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

survey association lock change #570

Merged
merged 21 commits into from
Oct 2, 2024
Merged
Show file tree
Hide file tree
Changes from 15 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions microsetta_private_api/repo/qiita_repo.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,34 @@
from microsetta_private_api.repo.admin_repo import AdminRepo
from microsetta_private_api.repo.base_repo import BaseRepo
from microsetta_private_api.qiita import qclient
from microsetta_private_api.repo.metadata_repo import retrieve_metadata
from microsetta_private_api.repo.metadata_repo._constants import MISSING_VALUE
from microsetta_private_api.repo.survey_answers_repo import SurveyAnswersRepo


class QiitaRepo(BaseRepo):
def lock_completed_surveys_to_barcodes(self, barcodes):
# lock survey-sample association
with self._transaction as t:
admin_repo = AdminRepo(t)
sar_repo = SurveyAnswersRepo(t)
ayobi marked this conversation as resolved.
Show resolved Hide resolved

for sample_barcode in barcodes:
ids = admin_repo._get_ids_relevant_to_barcode(sample_barcode)

if ids is not None:
account_id = ids.get('account_id')
source_id = ids.get('source_id')
sample_id = ids.get('sample_id')

survey_ids = sar_repo.list_answered_surveys(
account_id, source_id)

if survey_ids is not None:
for survey_id in survey_ids:
sar_repo.associate_answered_survey_with_sample(
account_id, source_id, sample_id, survey_id)

def push_metadata_to_qiita(self, barcodes=None):
"""Attempt to format and push metadata for the set of barcodes

Expand Down Expand Up @@ -35,6 +59,7 @@ def push_metadata_to_qiita(self, barcodes=None):
list
Any error detail when constructing metadata
"""

if barcodes is None:
with self._transaction.cursor() as cur:
# obtain all barcodes, which are part of the AG table,
Expand Down Expand Up @@ -84,6 +109,9 @@ def push_metadata_to_qiita(self, barcodes=None):
# calls to this function if and as needed.
to_push = list(barcodes - samples_in_qiita)[:1000]

# lock survey-sample association
self.lock_completed_surveys_to_barcodes(to_push)

# short circuit if we do not have anything to push
if len(to_push) == 0:
return 0, []
Expand Down
59 changes: 59 additions & 0 deletions microsetta_private_api/repo/tests/test_qiita.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from unittest import TestCase, main
from unittest.mock import patch
from microsetta_private_api.repo.survey_answers_repo import SurveyAnswersRepo
from microsetta_private_api.repo.transaction import Transaction
from microsetta_private_api.repo.qiita_repo import QiitaRepo

Expand Down Expand Up @@ -69,6 +70,64 @@ def test_push_metadata_to_qiita(self, test_retrieve_metadata,
"associated with any surveys "
"matching this template id")}])

def test_lock_completed_surveys_to_barcodes(self):

test_barcode = '000069747'
test_barcodes = [test_barcode]

with Transaction() as t:
with t.dict_cursor() as cur:
# first, find the ids for the barcode and survey we're using
# as they are dynamically generated.
cur.execute("select ag_login_id, source_id from "
"ag_login_surveys a join source_barcodes_surveys b"
" on a.survey_id = b.survey_id and b.barcode = "
"'000069747' and survey_template_id = 1")
row = cur.fetchone()
account_id = row[0]
source_id = row[1]

cur.execute("select ag_kit_barcode_id from ag_kit_barcodes "
"where barcode = '000069747'")
row = cur.fetchone()

cur.execute("SELECT * FROM source_barcodes_surveys "
"WHERE barcode = '000069747'")
rows_before = cur.fetchall()

# submit a survey for the barcode
sar = SurveyAnswersRepo(t)
survey_10 = {
'22': 'Unspecified',
'108': 'Unspecified',
'109': 'Unspecified',
'110': 'Unspecified',
'111': 'Unspecified',
'112': '1990',
'113': 'Unspecified',
'115': 'Unspecified',
'148': 'Unspecified',
'492': 'Unspecified',
'493': 'Unspecified',
'502': 'Male'
}
sar.submit_answered_survey(
account_id,
source_id,
'en_US', 10, survey_10)

# now lock the barcode to the survey that was recently submitted
qiita_repo = QiitaRepo(t)
qiita_repo.lock_completed_surveys_to_barcodes(test_barcodes)

with t.dict_cursor() as cur:
cur.execute("SELECT * FROM source_barcodes_surveys "
"WHERE barcode = '000069747'")
rows_after = cur.fetchall()
t.commit()
ayobi marked this conversation as resolved.
Show resolved Hide resolved

self.assertGreater(len(rows_after), len(rows_before))


if __name__ == '__main__':
main()
Loading