Skip to content

Commit 2ad5ea3

Browse files
ayobicassidysymons
andauthored
survey association lock change (#570)
* 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]>
1 parent 773a1f1 commit 2ad5ea3

File tree

3 files changed

+86
-0
lines changed

3 files changed

+86
-0
lines changed

microsetta_private_api/repo/qiita_repo.py

+27
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,33 @@
1+
from microsetta_private_api.repo.admin_repo import AdminRepo
12
from microsetta_private_api.repo.base_repo import BaseRepo
23
from microsetta_private_api.qiita import qclient
34
from microsetta_private_api.repo.metadata_repo import retrieve_metadata
45
from microsetta_private_api.repo.metadata_repo._constants import MISSING_VALUE
6+
from microsetta_private_api.repo.survey_answers_repo import SurveyAnswersRepo
57

68

79
class QiitaRepo(BaseRepo):
10+
def lock_completed_surveys_to_barcodes(self, barcodes):
11+
# lock survey-sample association
12+
admin_repo = AdminRepo(self._transaction)
13+
sar_repo = SurveyAnswersRepo(self._transaction)
14+
15+
for sample_barcode in barcodes:
16+
ids = admin_repo._get_ids_relevant_to_barcode(sample_barcode)
17+
18+
if ids is not None:
19+
account_id = ids.get('account_id')
20+
source_id = ids.get('source_id')
21+
sample_id = ids.get('sample_id')
22+
23+
survey_ids = sar_repo.list_answered_surveys(
24+
account_id, source_id)
25+
26+
if survey_ids is not None:
27+
for survey_id in survey_ids:
28+
sar_repo.associate_answered_survey_with_sample(
29+
account_id, source_id, sample_id, survey_id)
30+
831
def push_metadata_to_qiita(self, barcodes=None):
932
"""Attempt to format and push metadata for the set of barcodes
1033
@@ -35,6 +58,7 @@ def push_metadata_to_qiita(self, barcodes=None):
3558
list
3659
Any error detail when constructing metadata
3760
"""
61+
3862
if barcodes is None:
3963
with self._transaction.cursor() as cur:
4064
# obtain all barcodes, which are part of the AG table,
@@ -84,6 +108,9 @@ def push_metadata_to_qiita(self, barcodes=None):
84108
# calls to this function if and as needed.
85109
to_push = list(barcodes - samples_in_qiita)[:1000]
86110

111+
# lock survey-sample association
112+
self.lock_completed_surveys_to_barcodes(to_push)
113+
87114
# short circuit if we do not have anything to push
88115
if len(to_push) == 0:
89116
return 0, []

microsetta_private_api/repo/tests/test_qiita.py

+58
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
from unittest import TestCase, main
22
from unittest.mock import patch
3+
from microsetta_private_api.repo.survey_answers_repo import SurveyAnswersRepo
34
from microsetta_private_api.repo.transaction import Transaction
45
from microsetta_private_api.repo.qiita_repo import QiitaRepo
56

@@ -69,6 +70,63 @@ def test_push_metadata_to_qiita(self, test_retrieve_metadata,
6970
"associated with any surveys "
7071
"matching this template id")}])
7172

73+
def test_lock_completed_surveys_to_barcodes(self):
74+
75+
test_barcode = '000069747'
76+
test_barcodes = [test_barcode]
77+
78+
with Transaction() as t:
79+
with t.dict_cursor() as cur:
80+
# first, find the ids for the barcode and survey we're using
81+
# as they are dynamically generated.
82+
cur.execute("select ag_login_id, source_id from "
83+
"ag_login_surveys a join source_barcodes_surveys b"
84+
" on a.survey_id = b.survey_id and b.barcode = "
85+
"'000069747' and survey_template_id = 1")
86+
row = cur.fetchone()
87+
account_id = row[0]
88+
source_id = row[1]
89+
90+
cur.execute("select ag_kit_barcode_id from ag_kit_barcodes "
91+
"where barcode = '000069747'")
92+
row = cur.fetchone()
93+
94+
cur.execute("SELECT * FROM source_barcodes_surveys "
95+
"WHERE barcode = '000069747'")
96+
rows_before = cur.fetchall()
97+
98+
# submit a survey for the barcode
99+
sar = SurveyAnswersRepo(t)
100+
survey_10 = {
101+
'22': 'Unspecified',
102+
'108': 'Unspecified',
103+
'109': 'Unspecified',
104+
'110': 'Unspecified',
105+
'111': 'Unspecified',
106+
'112': '1990',
107+
'113': 'Unspecified',
108+
'115': 'Unspecified',
109+
'148': 'Unspecified',
110+
'492': 'Unspecified',
111+
'493': 'Unspecified',
112+
'502': 'Male'
113+
}
114+
sar.submit_answered_survey(
115+
account_id,
116+
source_id,
117+
'en_US', 10, survey_10)
118+
119+
# now lock the barcode to the survey that was recently submitted
120+
qiita_repo = QiitaRepo(t)
121+
qiita_repo.lock_completed_surveys_to_barcodes(test_barcodes)
122+
123+
with t.dict_cursor() as cur:
124+
cur.execute("SELECT * FROM source_barcodes_surveys "
125+
"WHERE barcode = '000069747'")
126+
rows_after = cur.fetchall()
127+
128+
self.assertGreater(len(rows_after), len(rows_before))
129+
72130

73131
if __name__ == '__main__':
74132
main()

microsetta_private_api/tasks.py

+1
Original file line numberDiff line numberDiff line change
@@ -86,3 +86,4 @@ def update_qiita_metadata():
8686
{"what": "qiita metadata push errors",
8787
"content": json.dumps(error, indent=2)},
8888
EN_US)
89+
t.commit()

0 commit comments

Comments
 (0)