Skip to content

Commit

Permalink
survey association lock change (#570)
Browse files Browse the repository at this point in the history
* added lock to qiita repo

* changed where to get survey ids

* moved lock sample to func in qiita repo and added test

* changed location of lock

* added dissociate in test

* added comments

* changed test acct

* removed test in qiita in favor of test_associate_sample_and_survey

* added test

* changes per suggestions

* updated test

* additional changes

* added back commit

* towards moving test into one transaction

* remove commit

* added suggestion

* add commit to qitta metadata update

* Update microsetta_private_api/tasks.py

---------

Co-authored-by: Cassidy Symons <[email protected]>
  • Loading branch information
ayobi and cassidysymons authored Oct 2, 2024
1 parent 773a1f1 commit 2ad5ea3
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 0 deletions.
27 changes: 27 additions & 0 deletions microsetta_private_api/repo/qiita_repo.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,33 @@
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
admin_repo = AdminRepo(self._transaction)
sar_repo = SurveyAnswersRepo(self._transaction)

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 +58,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 +108,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
58 changes: 58 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,63 @@ 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()

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


if __name__ == '__main__':
main()
1 change: 1 addition & 0 deletions microsetta_private_api/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,3 +86,4 @@ def update_qiita_metadata():
{"what": "qiita metadata push errors",
"content": json.dumps(error, indent=2)},
EN_US)
t.commit()

0 comments on commit 2ad5ea3

Please sign in to comment.