def test_scrub_bad_source(self): with Transaction() as t: sar = SurveyAnswersRepo(t) with self.assertRaises(RepoException): sar.scrub(HUMAN_SOURCE.account_id, HUMAN_SOURCE.account_id, SURVEY_ID)
def test_scrub(self): with Transaction() as t: sar = SurveyAnswersRepo(t) sar.scrub(HUMAN_SOURCE.account_id, HUMAN_SOURCE.id, SURVEY_ID) obs = sar.get_answered_survey(HUMAN_SOURCE.account_id, HUMAN_SOURCE.id, SURVEY_ID, 'en_US') self.assertEqual(obs['3'], SURVEY_ANSWERS['3']) self.assertNotEqual(obs['116'], SURVEY_ANSWERS['116'])
def delete_account(account_id, token_info): validate_admin_access(token_info) with Transaction() as t: acct_repo = AccountRepo(t) src_repo = SourceRepo(t) samp_repo = SampleRepo(t) sar_repo = SurveyAnswersRepo(t) acct = acct_repo.get_account(account_id) if acct is None: return jsonify(message="Account not found", code=404), 404 else: # the account is already scrubbed so let's stop early if acct.account_type == 'deleted': return None, 204 sample_count = 0 sources = src_repo.get_sources_in_account(account_id) for source in sources: samples = samp_repo.get_samples_by_source(account_id, source.id) has_samples = len(samples) > 0 sample_count += len(samples) for sample in samples: # we scrub rather than disassociate in the event that the # sample is in our freezers but not with an up-to-date scan samp_repo.scrub(account_id, source.id, sample.id) surveys = sar_repo.list_answered_surveys(account_id, source.id) if has_samples: # if we have samples, we need to scrub survey / source # free text for survey_id in surveys: sar_repo.scrub(account_id, source.id, survey_id) src_repo.scrub(account_id, source.id) else: # if we do not have associated samples, then the source # is safe to delete for survey_id in surveys: sar_repo.delete_answered_survey(account_id, survey_id) src_repo.delete_source(account_id, source.id) # an account is safe to delete if there are no associated samples if sample_count > 0: acct_repo.scrub(account_id) else: acct_repo.delete_account(account_id) t.commit() return None, 204