def insert_data(self): """ Prepare tests by inserting some data into the DB. Returns ------- survey object, survey_id : str, notes_test : str and consent : dict of the newly created survey. """ # Create random string to test update happens # TODO: implement utf8 capability for notes! See issue #646 c = ascii_letters + '1234567890' notes_test = ''.join([choice(c) for i in range(40)]) survey_id = '817ff95701f4dd10' survey = Survey(2) consent = { 'login_id': 'eba20873-b7db-33cc-e040-8a80115d392c', 'survey_id': survey_id, 'participant_name': 'some name that should be ignored', 'age_range': 'ANIMAL_SURVEY', 'parent_1_name': 'ANIMAL_SURVEY', 'parent_2_name': 'ANIMAL_SURVEY', 'deceased_parent': False, 'participant_email': 'REMOVED', 'obtainer_name': 'ANIMAL_SURVEY', 'assent_obtainer': 'ANIMAL_SURVEY', 'is_juvenile': True} with_fk = [(survey_id, 128, 'Other'), (survey_id, 129, 'Wild'), (survey_id, 131, 'Male'), (survey_id, 132, 'Suburban'), (survey_id, 133, 'Normal'), (survey_id, 134, 'Omnivore'), (survey_id, 135, 'Wild food'), (survey_id, 136, 'Both'), (survey_id, 137, 'Unspecified'), (survey_id, 138, 'Lives alone with humans'), (survey_id, 139, '8+'), (survey_id, 140, 'Unspecified'), (survey_id, 141, 'Never')] without_fk = [(survey_id, 130, '["20"]'), (survey_id, 142, '["Giant ratty pet!"]'), (survey_id, 143, '["Capybara"]'), (survey_id, 144, '["%s"]' % notes_test), (survey_id, 145, '["29 - Male"]'), (survey_id, 127, '["Fluffy"]')] survey.store_survey(consent, with_fk, without_fk) return survey, survey_id, notes_test, consent
def insert_data(self): """ Prepare tests by inserting some data into the DB. Returns ------- survey object, survey_id : str, notes_test : str and consent : dict of the newly created survey. """ # Create random string to test update happens # TODO: implement utf8 capability for notes! See issue #646 c = ascii_letters + '1234567890' notes_test = ''.join([choice(c) for i in range(40)]) survey_id = '817ff95701f4dd10' survey = Survey(2) consent = { 'login_id': 'eba20873-b7db-33cc-e040-8a80115d392c', 'survey_id': survey_id, 'participant_name': 'some name that should be ignored', 'age_range': 'ANIMAL_SURVEY', 'parent_1_name': 'ANIMAL_SURVEY', 'parent_2_name': 'ANIMAL_SURVEY', 'deceased_parent': False, 'participant_email': 'REMOVED', 'obtainer_name': 'ANIMAL_SURVEY', 'assent_obtainer': 'ANIMAL_SURVEY', 'is_juvenile': True } with_fk = [(survey_id, 128, 'Other'), (survey_id, 129, 'Wild'), (survey_id, 131, 'Male'), (survey_id, 132, 'Suburban'), (survey_id, 133, 'Normal'), (survey_id, 134, 'Omnivore'), (survey_id, 135, 'Wild food'), (survey_id, 136, 'Both'), (survey_id, 137, 'Unspecified'), (survey_id, 138, 'Lives alone with humans'), (survey_id, 139, '8+'), (survey_id, 140, 'Unspecified'), (survey_id, 141, 'Never')] without_fk = [(survey_id, 130, '["20"]'), (survey_id, 142, '["Giant ratty pet!"]'), (survey_id, 143, '["Capybara"]'), (survey_id, 144, '["%s"]' % notes_test), (survey_id, 145, '["29 - Male"]'), (survey_id, 127, '["Fluffy"]')] survey.store_survey(consent, with_fk, without_fk) return survey, survey_id, notes_test, consent
def test_fetch_survey(self): survey = Survey(2) obs = survey.fetch_survey('cb367dcf9a9af7e9') self.assertEqual(obs, { 'Pet_Information_127_0': 'REMOVED', 'Pet_Information_128_0': 1, 'Pet_Information_129_0': 2, 'Pet_Information_130_0': '6', 'Pet_Information_131_0': 1, 'Pet_Information_132_0': 1, 'Pet_Information_133_0': 3, 'Pet_Information_134_0': 2, 'Pet_Information_135_0': [1], 'Pet_Information_136_0': 3, 'Pet_Information_137_0': [0], 'Pet_Information_138_0': 1, 'Pet_Information_139_0': 3, 'Pet_Information_140_0': 3, 'Pet_Information_141_0': 4, 'Pet_Information_142_0': 'REMOVED', 'Pet_Information_143_0': 'REMOVED', 'Pet_Information_144_0': 'REMOVED', 'Pet_Information_145_0': 'Female: 50; Male: 59'})
from amgut.lib.data_access.survey import Survey primary_human_survey = Survey(1) primary_animal_survey = Survey(2) fermented_survey = Survey(3) surf_survey = Survey(4) personal_microbiome_survey = Survey(5)
def test_store_survey(self): # Create new survey ID and make sure it is unused survey = Survey(2) c = ascii_letters + '1234567890' survey_id = ''.join([choice(c) for i in range(16)]) name = ''.join([choice(ascii_letters) for i in range(20)]) with self.assertRaises(ValueError): survey.fetch_survey(survey_id) consent = { 'login_id': 'd8592c74-84b1-2135-e040-8a80115d6401', 'survey_id': survey_id, 'participant_name': name, 'age_range': 'ANIMAL_SURVEY', 'parent_1_name': 'ANIMAL_SURVEY', 'parent_2_name': 'ANIMAL_SURVEY', 'deceased_parent': False, 'participant_email': 'REMOVED', 'obtainer_name': 'ANIMAL_SURVEY', 'assent_obtainer': 'ANIMAL_SURVEY', 'is_juvenile': True } with_fk = [(survey_id, 128, 'Other'), (survey_id, 129, 'Wild'), (survey_id, 131, 'Male'), (survey_id, 132, 'Suburban'), (survey_id, 133, 'Normal'), (survey_id, 134, 'Omnivore'), (survey_id, 135, 'Wild food'), (survey_id, 136, 'Both'), (survey_id, 137, 'Unspecified'), (survey_id, 138, 'Lives alone with humans'), (survey_id, 139, '8+'), (survey_id, 140, 'Unspecified'), (survey_id, 141, 'Never')] without_fk = [(survey_id, 130, '["20"]'), (survey_id, 142, unicode('["Mötley Crüe!"]', 'utf-8')), (survey_id, 143, '["Capybara"]'), (survey_id, 144, '[""]'), (survey_id, 145, '["29 - Male"]'), (survey_id, 127, '["%s"]' % name)] survey.store_survey(consent, with_fk, without_fk) obs = survey.fetch_survey(survey_id) exp = { 'Pet_Information_127_0': name, 'Pet_Information_137_0': [0], 'Pet_Information_143_0': 'Capybara', 'Pet_Information_132_0': 2, 'Pet_Information_138_0': 1, 'Pet_Information_144_0': '', 'Pet_Information_129_0': 4, 'Pet_Information_142_0': u'M\xf6tley Cr\xfce!'.encode('utf-8'), 'Pet_Information_133_0': 3, 'Pet_Information_139_0': 5, 'Pet_Information_130_0': '20', 'Pet_Information_128_0': 9, 'Pet_Information_134_0': 2, 'Pet_Information_140_0': 0, 'Pet_Information_131_0': 1, 'Pet_Information_145_0': '29 - Male', 'Pet_Information_136_0': 3, 'Pet_Information_135_0': [3], 'Pet_Information_141_0': 4 } self.assertEqual(obs, exp) obs = ag_data.getConsent(survey_id) del obs['date_signed'] consent['ag_login_id'] = consent['login_id'] del consent['login_id'] del consent['obtainer_name'] consent['deceased_parent'] = 'false' self.assertEqual(obs, consent) # revert database self.delete_survey(survey_id)
def test_fetch_survey_bad_id(self): survey = Survey(1) with self.assertRaises(ValueError): survey.fetch_survey('BAD_ID_HERE')
def test_create(self): survey = Survey(1) self.assertEqual(survey.id, 1) exp = ["<class 'amgut.lib.data_access.survey.Group'>"] * 7 self.assertEqual([str(type(x)) for x in survey.groups], exp) exp = [-1, 0, 1, 2, 3, 4, 5] self.assertEqual([x.id for x in survey.groups], exp) exp = [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 98, 99, 101, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 120, 122, 124, 126, 146, 148, 149, 150, 153, 154, 155, 156, 157, 158, 159, 160, 162, 163, 164 ] self.assertEqual(survey.questions.keys(), exp) exp = { 1: 'SINGLE', 2: 'SINGLE', 3: 'SINGLE', 4: 'SINGLE', 5: 'SINGLE', 6: 'SINGLE', 7: 'SINGLE', 8: 'SINGLE', 9: 'MULTIPLE', 11: 'SINGLE', 12: 'SINGLE', 13: 'SINGLE', 14: 'SINGLE', 15: 'SINGLE', 16: 'SINGLE', 17: 'SINGLE', 18: 'SINGLE', 19: 'SINGLE', 20: 'SINGLE', 21: 'SINGLE', 22: 'SINGLE', 23: 'SINGLE', 24: 'SINGLE', 25: 'SINGLE', 26: 'SINGLE', 27: 'SINGLE', 28: 'SINGLE', 29: 'SINGLE', 30: 'MULTIPLE', 31: 'SINGLE', 32: 'SINGLE', 33: 'SINGLE', 34: 'SINGLE', 35: 'SINGLE', 36: 'SINGLE', 37: 'SINGLE', 38: 'SINGLE', 39: 'SINGLE', 40: 'SINGLE', 41: 'SINGLE', 42: 'SINGLE', 43: 'SINGLE', 44: 'SINGLE', 45: 'SINGLE', 46: 'SINGLE', 47: 'SINGLE', 48: 'SINGLE', 49: 'SINGLE', 50: 'SINGLE', 51: 'SINGLE', 53: 'SINGLE', 54: 'MULTIPLE', 55: 'SINGLE', 56: 'SINGLE', 57: 'SINGLE', 58: 'SINGLE', 59: 'SINGLE', 60: 'SINGLE', 61: 'SINGLE', 62: 'SINGLE', 63: 'SINGLE', 64: 'SINGLE', 65: 'SINGLE', 66: 'SINGLE', 67: 'SINGLE', 68: 'SINGLE', 69: 'SINGLE', 70: 'SINGLE', 71: 'SINGLE', 72: 'SINGLE', 73: 'SINGLE', 74: 'SINGLE', 75: 'SINGLE', 76: 'SINGLE', 77: 'SINGLE', 78: 'SINGLE', 79: 'SINGLE', 80: 'SINGLE', 81: 'SINGLE', 82: 'SINGLE', 83: 'SINGLE', 84: 'SINGLE', 85: 'SINGLE', 86: 'SINGLE', 87: 'SINGLE', 88: 'SINGLE', 89: 'SINGLE', 90: 'SINGLE', 91: 'SINGLE', 92: 'SINGLE', 93: 'SINGLE', 94: 'SINGLE', 95: 'SINGLE', 96: 'SINGLE', 98: 'TEXT', 99: 'TEXT', 101: 'TEXT', 103: 'TEXT', 104: 'TEXT', 105: 'TEXT', 106: 'TEXT', 107: 'SINGLE', 108: 'STRING', 109: 'SINGLE', 110: 'SINGLE', 111: 'SINGLE', 112: 'SINGLE', 113: 'STRING', 114: 'SINGLE', 115: 'STRING', 116: 'TEXT', 117: 'TEXT', 118: 'TEXT', 119: 'TEXT', 120: 'TEXT', 122: 'TEXT', 124: 'TEXT', 126: 'TEXT', 146: 'SINGLE', 148: 'SINGLE', 149: 'SINGLE', 150: 'TEXT', 153: 'SINGLE', 154: 'MULTIPLE', 155: 'SINGLE', 156: 'SINGLE', 157: 'SINGLE', 158: 'SINGLE', 159: 'SINGLE', 160: 'SINGLE', 162: 'MULTIPLE', 163: 'SINGLE', 164: 'SINGLE' } self.assertItemsEqual(survey.question_types, exp) self.assertEqual(survey.unspecified, 'Unspecified')
def test_edit_survey(self): # creating a new participant for existing ag_login_id main_survey_id = '38792874' sec_survey_id_fermented = '3879287455' sec_survey_id_surfer = '3879287456' ag_login_id = 'd8592c74-9694-2135-e040-8a80115d6401' participant_name = 'test_dude_stefan' email = '*****@*****.**' with_fk_inserts =\ [(main_survey_id, _id, 'Unspecified') for _id in [1, 2, 3, 4, 5, 6, 7, 8, 9, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 107, 109, 110, 111, 112, 114, 146, 148, 149, 153, 154, 155, 156, 157, 158, 159, 160, 162, 163, 164]] without_fk_inserts =\ [(main_survey_id, _id, '[""]') for _id in [98, 99, 150, 103, 104, 105, 106, 108, 113, 115, 116, 117, 118, 119, 120, 122, 124, 126, 101]] self.mock_login(ag_data.ut_get_supplied_kit_id(ag_login_id)) s = Survey(main_survey_id) s.store_survey( {u'login_id': ag_login_id, u'age_range': u'18-plus', u'parent_1_name': None, u'participant_email': email, u'obtainer_name': None, u'parent_2_name': None, u'deceased_parent': u'No', u'participant_name': participant_name, u'survey_id': main_survey_id, u'is_juvenile': False}, with_fk_inserts, without_fk_inserts) # confirm that no secondary surveys are present response = self.post('/participants/%s' % participant_name, {'participant_type': 'human'}) self.assertEqual(response.code, 200) self.assertNotIn('fermented', response.body) self.assertNotIn('surf', response.body) # add a new fermented food survey data = {'questions': {'Fermented_Foods_170_0': [''], 'Fermented_Foods_173_0': [''], 'Fermented_Foods_169_0': None, 'Fermented_Foods_171_0': ['13'], 'Fermented_Foods_168_0': [''], 'Fermented_Foods_167_0': ['1'], 'Fermented_Foods_166_0': ['0'], 'Fermented_Foods_165_0': ['0'], 'Fermented_Foods_172_0': ['']}} consent = { 'login_id': ag_login_id, 'participant_name': participant_name, 'survey_id': sec_survey_id_fermented, 'secondary': True } redis.hset(sec_survey_id_fermented, 'consent', dumps(consent)) redis.hset(sec_survey_id_fermented, 0, dumps(data)) redis.expire(sec_survey_id_fermented, 86400) store_survey(fermented_survey, sec_survey_id_fermented) # confirm that now a fermented food survey is present response = self.post('/participants/%s' % participant_name, {'participant_type': 'human'}) self.assertEqual(response.code, 200) self.assertIn('fermented', response.body) self.assertNotIn('surf', response.body) self.assertIn('secondary_survey/?type=%s&participant_name=%s&survey=%s' % ('fermented', participant_name, sec_survey_id_fermented), response.body) # check human_survey_completed links: # TODO: I don't know how to set a secured cookie for # completed_survey_id and could need some help to actually test values # for the rendered page /authed/human_survey_completed/ # response = self.get('/authed/human_survey_completed/') # add a new surfers survey data = {'questions': {'Surfers_182_0': ['0'], 'Surfers_176_0': ['0'], 'Surfers_175_0': ['0'], 'Surfers_185_0': ['0'], 'Surfers_179_0': ['0'], 'Surfers_183_0': ['0'], 'Surfers_180_0': ['0'], 'Surfers_174_0': ['0'], 'Surfers_178_0': ['0'], 'Surfers_181_0': ['0'], 'Surfers_177_0': ['0'], 'Surfers_184_0': ['0']}} consent = { 'login_id': ag_login_id, 'participant_name': participant_name, 'survey_id': sec_survey_id_surfer, 'secondary': True } redis.hset(sec_survey_id_surfer, 'consent', dumps(consent)) redis.hset(sec_survey_id_surfer, 0, dumps(data)) redis.expire(sec_survey_id_surfer, 86400) store_survey(surf_survey, sec_survey_id_surfer) # confirm that now a surfers and fermented food survey is present response = self.post('/participants/%s' % participant_name, {'participant_type': 'human'}) self.assertEqual(response.code, 200) self.assertIn('fermented', response.body) self.assertIn('surf', response.body) self.assertIn('secondary_survey/?type=%s&participant_name=%s&survey=%s' % ('fermented', participant_name, sec_survey_id_fermented), response.body) self.assertIn('secondary_survey/?type=%s&participant_name=%s&survey=%s' % ('surf', participant_name, sec_survey_id_surfer), response.body)
import re from collections import defaultdict from future.utils import viewitems from amgut import text_locale from amgut.lib.data_access.survey import Survey primary_human_survey = Survey(1)
def test_store_survey_edit(self): # Create random string to test update happens c = ascii_letters + '1234567890' notes_test = ''.join([choice(c) for i in range(40)]) # Set up survey survey_id = '817ff95701f4dd10' survey = Survey(2) consent = { 'login_id': 'eba20873-b7db-33cc-e040-8a80115d392c', 'survey_id': survey_id, 'participant_name': 'some name that should be ignored', 'age_range': 'ANIMAL_SURVEY', 'parent_1_name': 'ANIMAL_SURVEY', 'parent_2_name': 'ANIMAL_SURVEY', 'deceased_parent': False, 'participant_email': 'REMOVED', 'obtainer_name': 'ANIMAL_SURVEY', 'assent_obtainer': 'ANIMAL_SURVEY', 'is_juvenile': True} with_fk = [(survey_id, 128, 'Other'), (survey_id, 129, 'Wild'), (survey_id, 131, 'Male'), (survey_id, 132, 'Suburban'), (survey_id, 133, 'Normal'), (survey_id, 134, 'Omnivore'), (survey_id, 135, 'Wild food'), (survey_id, 136, 'Both'), (survey_id, 137, 'Unspecified'), (survey_id, 138, 'Lives alone with humans'), (survey_id, 139, '8+'), (survey_id, 140, 'Unspecified'), (survey_id, 141, 'Never')] without_fk = [(survey_id, 130, '["20"]'), (survey_id, 142, '["Giant ratty pet!"]'), (survey_id, 143, '["Capybara"]'), (survey_id, 144, '["%s"]' % notes_test), (survey_id, 145, '["29 - Male"]'), (survey_id, 127, '["Fluffy"]')] survey.store_survey(consent, with_fk, without_fk) obs = survey.fetch_survey(survey_id) exp = {'Pet_Information_127_0': 'Fluffy', 'Pet_Information_137_0': [0], 'Pet_Information_143_0': 'Capybara', 'Pet_Information_132_0': 2, 'Pet_Information_138_0': 1, 'Pet_Information_144_0': notes_test, 'Pet_Information_129_0': 4, 'Pet_Information_142_0': 'Giant ratty pet!', 'Pet_Information_133_0': 3, 'Pet_Information_139_0': 5, 'Pet_Information_130_0': '20', 'Pet_Information_128_0': 9, 'Pet_Information_134_0': 2, 'Pet_Information_140_0': 0, 'Pet_Information_131_0': 1, 'Pet_Information_145_0': '29 - Male', 'Pet_Information_136_0': 3, 'Pet_Information_135_0': [3], 'Pet_Information_141_0': 4} self.assertEqual(obs, exp) obs = ag_data.getConsent(survey_id) consent['ag_login_id'] = consent['login_id'] del consent['login_id'] del consent['obtainer_name'] consent['deceased_parent'] = 'false' consent['date_signed'] = date(2015, 9, 27) consent['parent_1_name'] = 'REMOVED' consent['parent_2_name'] = 'REMOVED' consent['participant_name'] = 'REMOVED-0' consent['parent_1_name'] = 'REMOVED' self.assertEqual(obs, consent)
def test_store_survey(self): # Create new survey ID and make sure it is unused survey = Survey(2) c = ascii_letters + '1234567890' survey_id = ''.join([choice(c) for i in range(16)]) name = ''.join([choice(ascii_letters) for i in range(20)]) with self.assertRaises(ValueError): survey.fetch_survey(survey_id) consent = { 'login_id': 'd8592c74-84b1-2135-e040-8a80115d6401', 'survey_id': survey_id, 'participant_name': name, 'age_range': 'ANIMAL_SURVEY', 'parent_1_name': 'ANIMAL_SURVEY', 'parent_2_name': 'ANIMAL_SURVEY', 'deceased_parent': False, 'participant_email': 'REMOVED', 'obtainer_name': 'ANIMAL_SURVEY', 'assent_obtainer': 'ANIMAL_SURVEY', 'is_juvenile': True} with_fk = [(survey_id, 128, 'Other'), (survey_id, 129, 'Wild'), (survey_id, 131, 'Male'), (survey_id, 132, 'Suburban'), (survey_id, 133, 'Normal'), (survey_id, 134, 'Omnivore'), (survey_id, 135, 'Wild food'), (survey_id, 136, 'Both'), (survey_id, 137, 'Unspecified'), (survey_id, 138, 'Lives alone with humans'), (survey_id, 139, '8+'), (survey_id, 140, 'Unspecified'), (survey_id, 141, 'Never')] without_fk = [(survey_id, 130, '["20"]'), (survey_id, 142, unicode('["Mötley Crüe!"]', 'utf-8')), (survey_id, 143, '["Capybara"]'), (survey_id, 144, '[""]'), (survey_id, 145, '["29 - Male"]'), (survey_id, 127, '["%s"]' % name)] survey.store_survey(consent, with_fk, without_fk) obs = survey.fetch_survey(survey_id) exp = {'Pet_Information_127_0': name, 'Pet_Information_137_0': [0], 'Pet_Information_143_0': 'Capybara', 'Pet_Information_132_0': 2, 'Pet_Information_138_0': 1, 'Pet_Information_144_0': '', 'Pet_Information_129_0': 4, 'Pet_Information_142_0': u'M\xf6tley Cr\xfce!'.encode('utf-8'), 'Pet_Information_133_0': 3, 'Pet_Information_139_0': 5, 'Pet_Information_130_0': '20', 'Pet_Information_128_0': 9, 'Pet_Information_134_0': 2, 'Pet_Information_140_0': 0, 'Pet_Information_131_0': 1, 'Pet_Information_145_0': '29 - Male', 'Pet_Information_136_0': 3, 'Pet_Information_135_0': [3], 'Pet_Information_141_0': 4} self.assertEqual(obs, exp) obs = ag_data.getConsent(survey_id) del obs['date_signed'] consent['ag_login_id'] = consent['login_id'] del consent['login_id'] del consent['obtainer_name'] consent['deceased_parent'] = 'false' self.assertEqual(obs, consent)