def test_enrolled_vs_inquiry_studies_by_user(self): u = self.construct_user(email="*****@*****.**") s1 = self.construct_study(title="Super Study") s2 = self.construct_study(title="Amazing Study") s3 = self.construct_study(title="Phenomenal Study") su1 = StudyUser(study=s1, user=u, status=StudyUserStatus.inquiry_sent) su2 = StudyUser(study=s3, user=u, status=StudyUserStatus.inquiry_sent) su3 = StudyUser(study=s2, user=u, status=StudyUserStatus.enrolled) db.session.add_all([su1, su2, su3]) db.session.commit() rv = self.app.get('/api/user/%i/study' % u.id, content_type="application/json", headers=self.logged_in_headers()) self.assert_success(rv) response = json.loads(rv.get_data(as_text=True)) self.assertEqual(3, len(response)) rv = self.app.get('/api/user/%i/inquiry/study' % u.id, content_type="application/json", headers=self.logged_in_headers()) self.assert_success(rv) response = json.loads(rv.get_data(as_text=True)) self.assertEqual(2, len(response)) self.assertNotEqual(s2.id, response[0]["study_id"]) self.assertNotEqual(s2.id, response[1]["study_id"]) rv = self.app.get('/api/user/%i/enrolled/study' % u.id, content_type="application/json", headers=self.logged_in_headers()) self.assert_success(rv) response = json.loads(rv.get_data(as_text=True)) self.assertEqual(1, len(response)) self.assertEqual(s2.id, response[0]["study_id"])
def test_get_study_by_user_includes_user_details(self): u = self.construct_user(email="c1") u2 = self.construct_user(email="c2") s = self.construct_study() su = StudyUser(study=s, user=u, status=StudyUserStatus.inquiry_sent) su2 = StudyUser(study=s, user=u2, status=StudyUserStatus.enrolled) db.session.add_all([su, su2]) db.session.commit() rv = self.app.get('/api/user/%i/study' % u.id, content_type="application/json", headers=self.logged_in_headers()) self.assert_success(rv) response = json.loads(rv.get_data(as_text=True)) self.assertEqual(s.id, response[0]["study_id"]) self.assertEqual(2, len(response[0]["study"]["study_users"])) self.assertEqual( "c1", response[0]["study"]["study_users"][0]["user"]["email"])
def post(self, study_id): request_data = request.get_json() study_users = self.schema.load(request_data, many=True).data db.session.query(StudyUser).filter_by(study_id=study_id).delete() for c in study_users: db.session.add(StudyUser(study_id=study_id, user_id=c.user_id)) db.session.commit() return self.get(study_id)
def test_get_study_by_user(self): u = self.construct_user() s = self.construct_study() su = StudyUser(study=s, user=u, status=StudyUserStatus.inquiry_sent) db.session.add(su) db.session.commit() rv = self.app.get('/api/user/%i/study' % u.id, content_type="application/json", headers=self.logged_in_headers()) self.assert_success(rv) response = json.loads(rv.get_data(as_text=True)) self.assertEqual(1, len(response)) self.assertEqual(s.id, response[0]["study_id"]) self.assertEqual(s.description, response[0]["study"]["description"])
def construct_everything(self): self.construct_all_questionnaires() cat = self.construct_category() org = self.construct_organization() self.construct_resource() study = self.construct_study() location = self.construct_location() event = self.construct_event() self.construct_location_category(location.id, cat.name) self.construct_study_category(study.id, cat.name) self.construct_zip_code() investigator = Investigator(name="Sam I am", organization_id=org.id) db.session.add(StudyInvestigator(study = study, investigator = investigator)) db.session.add(StudyUser(study=study, user=self.construct_user())) db.session.add(AdminNote(user_id=self.construct_user().id, resource_id=self.construct_resource().id, note='')) db.session.add(investigator) db.session.add(EmailLog()) db.session.add(StepLog()) db.session.commit()
def post(self): request_data = request.get_json() user_id = request_data['user_id'] study_id = request_data['study_id'] user = db.session.query(User).filter_by(id=user_id).first() study = db.session.query(Study).filter_by(id=study_id).first() if user and study: tracking_code = email_service.study_inquiry_email(study=study, user=user) log = EmailLog(user_id=user.id, type="study_inquiry_email", tracking_code=tracking_code) su = StudyUser(study_id=study.id, user_id=user.id, status=StudyUserStatus.inquiry_sent) db.session.add(log) db.session.add(su) db.session.commit() return '' else: raise RestException(RestException.STUDY_INQUIRY_ERROR)