def test_put_clears_cache(self): pc_id = 'ProjectCohort_foo' survey_id = 'Survey_foo' code = 'foo bar' start1 = datetime.datetime.today() end1 = start1 + datetime.timedelta(days=1) start2 = start1 + datetime.timedelta(days=2) end2 = start1 + datetime.timedelta(days=3) cache_data = { ParticipantData.participation_cache_key(pc_id): { ParticipantData.date_key(start1, end1): ['result1'], ParticipantData.date_key(start2, end2): ['result2'], }, ParticipantData.participation_cache_key(survey_id): { ParticipantData.date_key(start1, end1): ['result1'], ParticipantData.date_key(start2, end2): ['result2'], }, ParticipantData.participation_by_pc_cache_key(pc_id): { ParticipantData.date_key(start1, end1): ['result1'], ParticipantData.date_key(start2, end2): ['result2'], }, ParticipantData.participation_by_pc_cache_key(code): { ParticipantData.date_key(start1, end1): ['result1'], ParticipantData.date_key(start2, end2): ['result2'], }, } memcache.set_multi(cache_data) # Write a pd that relates to the pc and survey, falling in the first # date range. That date range should clear, the other should remain. pd = ParticipantData.create( key='progress', value=1, participant_id='Participant_foo', program_label='demo-program', project_id='Project_foo', cohort_label='2019', project_cohort_id=pc_id, code=code, survey_id=survey_id, survey_ordinal=1, ) ParticipantData.put_for_index(pd, 'participant-survey-key') for cache_key in cache_data.keys(): self.assertEqual(len(memcache.get(cache_key)), 1)
def mock_one_finished_one_unfinished(survey_ordinal, unfinished_id, finished_id, pc_id='ProjectCohort_foo', code='cool cat', program_label=None, cohort_label=None, organization_id=None): """Simulating one who finished and one who didn't.""" survey = Survey.create( [], ordinal=survey_ordinal, program_label=program_label, organization_id=organization_id or 'Org_foo', ) survey.put() kwargs = { 'key': 'progress', 'program_label': program_label, 'project_id': 'Project_12345678', 'cohort_label': cohort_label or cohort_label, 'project_cohort_id': pc_id, 'code': code, 'survey_id': survey.uid, 'survey_ordinal': survey.ordinal, } pd1 = ParticipantData.create(participant_id=unfinished_id, value=1, **kwargs) pd2 = ParticipantData.create(participant_id=finished_id, value=1, **kwargs) pd3 = ParticipantData.create(participant_id=finished_id, value=100, **kwargs) return [ ParticipantData.put_for_index(pd1, 'participant-survey-key'), ParticipantData.put_for_index(pd2, 'participant-survey-key'), # Writing this third one should should update the second, so the third # uid will never get to the db. ParticipantData.put_for_index(pd3, 'participant-survey-key'), ]
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_for_index_update_duplicate(self): """put_for_index() existing uid but matches an index raises.""" params1 = dict(self.context_params, value='1', survey_id='Survey_1') pd1 = ParticipantData.create(**params1) pd1.put() params2 = dict(self.context_params, value='1', survey_id='Survey_2') pd2 = ParticipantData.create(**params2) pd2.put() with self.assertRaises(IntegrityError): # Now changing 1 so that it collides with 2. pd1.survey_id = 'Survey_2' synced_pd1 = ParticipantData.put_for_index( pd1, 'participant-survey-key')
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())