def test_put_multi(self): """If no indexes collide: succeeds, otherwise: raises.""" params1 = dict(self.context_params, value='1', survey_id='Survey_1') pd1 = ParticipantData.create(**params1) params2 = dict(self.context_params, value='1', survey_id='Survey_2') pd2 = ParticipantData.create(**params2) affected_rows = ParticipantData.put_multi([pd1, pd2]) self.assertEqual(affected_rows, 2) self.assertIsNotNone(ParticipantData.get_by_id(pd1.uid)) self.assertIsNotNone(ParticipantData.get_by_id(pd2.uid)) pd3 = ParticipantData.create(**params1) with self.assertRaises(IntegrityError): ParticipantData.put_multi([pd3])
def test_put_insert(self): """put() a new uid: succeeds.""" params = dict(self.context_params, value='1') pd = ParticipantData.create(**params) pd.put() # Db shows saved values, and db-provided defaults like timestamps # are present in the saved object. fetched = ParticipantData.get_by_id(pd.uid) self.assertEqual(pd.to_dict(), fetched.to_dict())
def test_put_for_index_insert(self): """put_for_index() a new uid: succeeds.""" params = dict(self.context_params, value='1') pd = ParticipantData.create(**params) synced_pd = ParticipantData.put_for_index(pd, 'participant-survey-key') # Returns same uid. self.assertEqual(pd.uid, synced_pd.uid) # Db shows values. fetched = ParticipantData.get_by_id(pd.uid) self.assertEqual(synced_pd.to_dict(), fetched.to_dict())
def test_put_update(self): """put() an exisiting uid: succeeds.""" params = dict(self.context_params, value='1') pd = ParticipantData.create(**params) pd.put() pd.value = '2' pd.put() # Db shows values. fetched = ParticipantData.get_by_id(pd.uid) self.assertEqual(pd.to_dict(), fetched.to_dict())
def test_put_for_index_insert_duplicate(self): """put_for_index() a new uid but matching an index: succeeds.""" params = dict(self.context_params, value='1') pd = ParticipantData.create(**params) pd.put() dupe_params = dict(self.context_params, value='2') dupe_pd = ParticipantData.create(**params) synced_pd = ParticipantData.put_for_index(dupe_pd, 'participant-survey-key') # Returns original uid, not the new one. self.assertEqual(synced_pd.uid, pd.uid) # Db shows values fetched = ParticipantData.get_by_id(pd.uid) self.assertEqual(synced_pd.to_dict(), fetched.to_dict())
def test_update_local_pd(self): pc, survey, participant = self.create_pd_context() def write_value(value): return self.testapp.post_json( '/api/participants/{participant_id}/data/{key}'.format( participant_id=participant.uid, key='progress', ), { 'value': value, 'survey_id': survey.uid }, ) original_id = json.loads(write_value('1').body)['uid'] updated_id = json.loads(write_value('100').body)['uid'] self.assertEqual(original_id, updated_id) pd = ParticipantData.get_by_id(original_id) self.assertEqual(pd.value, '100')
def test_update_local_pd_with_descriptor(self): """Uses a descriptor two ways: in a param, and in the survey id.""" pc, survey, participant = self.create_pd_context() survey_descriptor = 'cycle-1' # Using the param. response = self.testapp.post_json( '/api/participants/{participant_id}/data/{key}'.format( participant_id=participant.uid, key='progress', ), { 'value': '1', 'survey_id': survey.uid, 'survey_descriptor': survey_descriptor }, ) original_id = json.loads(response.body)['uid'] # With the descriptor combined in the id. response = self.testapp.post_json( '/api/participants/{participant_id}/data/{key}'.format( participant_id=participant.uid, key='progress', ), { 'value': '100', 'survey_id': '{}:{}'.format(survey.uid, survey_descriptor) }, ) updated_id = json.loads(response.body)['uid'] self.assertEqual(original_id, updated_id) pd = ParticipantData.get_by_id(original_id) self.assertEqual(pd.value, '100') self.assertEqual(pd.survey_id, '{}:{}'.format(survey.uid, survey_descriptor))