def test_create_vioscreen_id_idempotent(self): with Transaction() as t: template_repo = SurveyTemplateRepo(t) obs1 = template_repo.create_vioscreen_id(TEST2_ACCOUNT_ID, TEST2_SOURCE_ID, TEST2_SAMPLE_ID) obs2 = template_repo.create_vioscreen_id(TEST2_ACCOUNT_ID, TEST2_SOURCE_ID, TEST2_SAMPLE_ID) self.assertEqual(obs1, obs2) obs = template_repo.create_vioscreen_id(TEST1_ACCOUNT_ID, TEST1_SOURCE_ID, TEST1_SAMPLE_ID) self.assertEqual(obs, TEST1_VIO_ID) t.rollback()
def _remote_survey_url_vioscreen(transaction, account_id, source_id, language_tag, survey_redirect_url, vioscreen_ext_sample_id): # assumes an instance of Transaction is already available acct_repo = AccountRepo(transaction) survey_template_repo = SurveyTemplateRepo(transaction) if vioscreen_ext_sample_id: # User is about to start a vioscreen survey for this sample # record this in the database. db_vioscreen_id = survey_template_repo.create_vioscreen_id( account_id, source_id, vioscreen_ext_sample_id) else: raise ValueError("Vioscreen Template requires " "vioscreen_ext_sample_id parameter.") (birth_year, gender, height, weight) = \ survey_template_repo.fetch_user_basic_physiology( account_id, source_id) account = acct_repo.get_account(account_id) country_code = account.address.country_code url = vioscreen.gen_survey_url(db_vioscreen_id, language_tag, survey_redirect_url, birth_year=birth_year, gender=gender, height=height, weight=weight, country_code=country_code) return url
def test_create_vioscreen_id_valid(self): with Transaction() as t: template_repo = SurveyTemplateRepo(t) obs = template_repo.create_vioscreen_id(TEST2_ACCOUNT_ID, TEST2_SOURCE_ID, TEST2_SAMPLE_ID) self.assertTrue(obs is not None) t.rollback()
def test_create_vioscreen_id_bad_source_or_account(self): # transaction needs to be created each time as the FK violation # disrupts the active transaction with Transaction() as t: template_repo = SurveyTemplateRepo(t) with self.assertRaises(ForeignKeyViolation): template_repo.create_vioscreen_id(str(uuid.uuid4()), TEST2_SOURCE_ID, TEST2_SAMPLE_ID) with Transaction() as t: template_repo = SurveyTemplateRepo(t) with self.assertRaises(ForeignKeyViolation): template_repo.create_vioscreen_id(TEST2_ACCOUNT_ID, str(uuid.uuid4()), TEST2_SAMPLE_ID) with Transaction() as t: template_repo = SurveyTemplateRepo(t) with self.assertRaises(KeyError): template_repo.create_vioscreen_id(TEST2_ACCOUNT_ID, TEST2_SOURCE_ID, str(uuid.uuid4()))
def read_survey_template(account_id, source_id, survey_template_id, language_tag, token_info, survey_redirect_url=None, vioscreen_ext_sample_id=None): _validate_account_access(token_info, account_id) # TODO: can we get rid of source_id? I don't have anything useful to do # with it... I guess I could check if the source is a dog before giving # out a pet information survey? with Transaction() as t: survey_template_repo = SurveyTemplateRepo(t) info = survey_template_repo.get_survey_template_link_info( survey_template_id) # For external surveys, we generate links pointing out if survey_template_id == SurveyTemplateRepo.VIOSCREEN_ID: if vioscreen_ext_sample_id: # User is about to start a vioscreen survey for this sample # record this in the database. db_vioscreen_id = survey_template_repo.create_vioscreen_id( account_id, source_id, vioscreen_ext_sample_id) else: raise ValueError("Vioscreen Template requires " "vioscreen_ext_sample_id parameter.") (birth_year, gender ) = survey_template_repo.fetch_user_birth_year_gender(account_id) url = vioscreen.gen_survey_url(db_vioscreen_id, language_tag, survey_redirect_url, birth_year=birth_year, gender=gender) # TODO FIXME HACK: This field's contents are not specified! info.survey_template_text = {"url": url} t.commit() return jsonify(info), 200 # For local surveys, we generate the json representing the survey survey_template = survey_template_repo.get_survey_template( survey_template_id, language_tag) info.survey_template_text = vue_adapter.to_vue_schema(survey_template) # TODO FIXME HACK: We need a better way to enforce validation on fields # that need it, can this be stored adjacent to the survey questions? client_side_validation = { "108": { # Height "inputType": "number", "validator": "number", "min": 0, "max": None }, "113": { # Weight "inputType": "number", "validator": "number", "min": 0, "max": None } } for group in info.survey_template_text.groups: for field in group.fields: if field.id in client_side_validation: field.set(**client_side_validation[field.id]) return jsonify(info), 200