Skip to content

Commit

Permalink
changes based on feedback/added tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ayobi committed Sep 24, 2024
1 parent 19473d2 commit 7698a49
Show file tree
Hide file tree
Showing 6 changed files with 76 additions and 54 deletions.
10 changes: 7 additions & 3 deletions microsetta_private_api/api/_survey.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,13 @@ def read_survey_templates(account_id, source_id, language_tag, token_info):
# permission to see Skin Scoring App survey
sample_repo = SampleRepo(t)
samples = sample_repo.get_samples_by_source(account_id, source_id)
has_skin_sample = any(
s.project_id == SurveyTemplateRepo.SBI_PROJECT_ID
for s in samples) if samples else False
if samples:
has_skin_sample = any(
SurveyTemplateRepo.SBI_PROJECT_ID
in s.project_id for s in samples
)
else:
has_skin_sample = False

template_ids = [
SurveyTemplateRepo.VIOSCREEN_ID,
Expand Down
2 changes: 0 additions & 2 deletions microsetta_private_api/api/tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -384,8 +384,6 @@ def delete_dummy_accts():
template_repo.delete_polyphenol_ffq(curr_acct_id,
curr_source.id)
template_repo.delete_spain_ffq(curr_acct_id, curr_source.id)
template_repo.delete_skin_scoring_app(curr_acct_id,
curr_source.id)

# Dissociate all samples linked to this source from all
# answered surveys linked to this source, then delete all
Expand Down
10 changes: 6 additions & 4 deletions microsetta_private_api/repo/sample_repo.py
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,7 @@ def get_samples_by_source(self, account_id, source_id,
sample.kit_id = self._get_supplied_kit_id_by_sample(
sample.barcode
)
sample.project_id = self._get_project_id_by_sample(
sample.project_id = self._get_project_ids_by_sample(
sample.barcode
)
samples.append(sample)
Expand Down Expand Up @@ -407,16 +407,18 @@ def _get_supplied_kit_id_by_sample(self, sample_barcode):
row = cur.fetchone()
return row[0]

def _get_project_id_by_sample(self, sample_barcode):
def _get_project_ids_by_sample(self, sample_barcode):
with self._transaction.cursor() as cur:
cur.execute(
"SELECT project_id "
"FROM barcodes.project_barcode "
"WHERE barcode = %s",
(sample_barcode, )
)
row = cur.fetchone()
return row[0]
rows = cur.fetchall()

project_ids = [row[0] for row in rows]
return project_ids

def scrub(self, account_id, source_id, sample_id):
"""Wipe out free text information for a sample
Expand Down
49 changes: 4 additions & 45 deletions microsetta_private_api/repo/survey_template_repo.py
Original file line number Diff line number Diff line change
Expand Up @@ -788,7 +788,7 @@ def create_skin_scoring_app_entry(self,
"""
characters = string.ascii_lowercase + string.digits

for attempt in range(5):
while True:
skin_scoring_app_id = ''.join(random.choices(characters, k=8))

try:
Expand All @@ -810,21 +810,12 @@ def create_skin_scoring_app_entry(self,
"VALUES(%s, %s, %s, %s, %s)",
(account_id, skin_scoring_app_id, None,
source_id,
SurveyTemplateRepo.SKIN_SCORING_APP_ID))
SurveyTemplateRepo.SKIN_SCORING_APP_ID))

return skin_scoring_app_id
except psycopg2.IntegrityError as e:
except psycopg2.IntegrityError:
self._transaction.rollback()

if e.pgcode == '23505':
print(f"Duplicate ID '{skin_scoring_app_id}' "
f"detected. Retrying... (Attempt {attempt + 1}/{5})")
else:
raise

raise Exception("Unable to generate a unique "
"Skin Scoring App ID after 5 attempts")

def get_skin_scoring_app_id_if_exists(self,
account_id,
source_id):
Expand Down Expand Up @@ -853,39 +844,7 @@ def get_skin_scoring_app_id_if_exists(self,
if res is None:
return None
else:
return res

def delete_skin_scoring_app(self, account_id, source_id):
"""Intended for admin use, remove Skin Scoring App entries
This method is idempotent.
This method deletes ALL Skin Scoring App surveys associated with an
account and source
This is a hard delete, we REMOVE rows rather than setting a flag
Parameters
----------
account_id : str, UUID
The account UUID
source_id : str, UUID
The source UUID
"""
with self._transaction.cursor() as cur:
existing = \
self.get_skin_scoring_app_id_if_exists(account_id,
source_id)
if existing is not None:
cur.execute("""DELETE FROM ag.ag_login_surveys
WHERE ag_login_id=%s
AND source_id=%s
AND survey_id=%s""",
(account_id, source_id, existing))
cur.execute("""DELETE FROM ag.skin_scoring_app_registry
WHERE account_id=%s
AND source_id=%s""",
(account_id, source_id))
return res[0]

def get_vioscreen_sample_to_user(self):
"""Obtain a mapping of sample barcode to vioscreen user"""
Expand Down
24 changes: 24 additions & 0 deletions microsetta_private_api/repo/tests/test_sample.py
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,30 @@ def test_get_supplied_kit_id_by_sample(self):
)
self.assertEqual(kit_id, supplied_kit_id)

def test_get_project_ids_by_sample(self):
with Transaction() as t:
# First we'll create a kit so that we have a kit_id/barcode combo
# to test with
admin_repo = AdminRepo(t)
res = admin_repo.create_kits(
1,
1,
"UNITTEST",
[1, 2]
)

# Extract the info from results. We know we created 1 kit with 1
# sample so we don't need to iterate
kit_info = res['created'][0]
sample_barcode = kit_info['sample_barcodes'][0]

# Verify that the function returns the correct project_id
sample_repo = SampleRepo(t)
project_ids = sample_repo._get_project_ids_by_sample(
sample_barcode
)
self.assertEqual([1, 2], project_ids)


if __name__ == '__main__':
unittest.main()
35 changes: 35 additions & 0 deletions microsetta_private_api/repo/tests/test_survey_template_repo.py
Original file line number Diff line number Diff line change
Expand Up @@ -395,6 +395,41 @@ def test_get_spain_ffq_id_if_exists_false(self):
TEST1_SOURCE_ID)
self.assertEqual(obs, None)

def test_create_skin_scoring_app_entry_valid(self):
with Transaction() as t:
template_repo = SurveyTemplateRepo(t)
obs = template_repo.create_skin_scoring_app_entry(TEST1_ACCOUNT_ID,
TEST1_SOURCE_ID,
'en_US')
self.assertEqual(len(obs), 8)

def test_create_skin_scoring_app_entry_invalid(self):
with Transaction() as t:
template_repo = SurveyTemplateRepo(t)
with self.assertRaises(InvalidTextRepresentation):
template_repo.create_skin_scoring_app_entry('',
TEST1_SOURCE_ID,
'en_US')

def test_get_skin_scoring_app_id_if_exists_true(self):
with Transaction() as t:
template_repo = SurveyTemplateRepo(t)
test_ssa_id = template_repo.create_skin_scoring_app_entry(
TEST1_ACCOUNT_ID, TEST1_SOURCE_ID, 'en_US'
)
obs = template_repo.get_skin_scoring_app_id_if_exists(
TEST1_ACCOUNT_ID, TEST1_SOURCE_ID
)
self.assertEqual(test_ssa_id, obs)

def test_get_skin_scoring_app_id_if_exists_false(self):
with Transaction() as t:
template_repo = SurveyTemplateRepo(t)
obs = template_repo.get_skin_scoring_app_id_if_exists(
TEST1_ACCOUNT_ID, TEST1_SOURCE_ID
)
self.assertEqual(obs, None)

def test_create_vioscreen_id_valid(self):
with Transaction() as t:
template_repo = SurveyTemplateRepo(t)
Expand Down

0 comments on commit 7698a49

Please sign in to comment.