Пример #1
0
    def test_batch_participation(self):
        user = User.create(email='*****@*****.**')
        user.put()

        pc_kwargs = {
            'program_label': self.program_label,
            'cohort_label': self.cohort_label,
        }
        pcs = [
            ProjectCohort.create(**pc_kwargs),
            ProjectCohort.create(**pc_kwargs),
        ]
        ndb.put_multi(pcs)

        all_pds = []
        for pc in pcs:
            pds = mock_one_finished_one_unfinished(
                1,
                'Participant_unfinished',
                'Participant_finished',
                pc_id=pc.uid,
                code=pc.code,
                program_label=self.program_label,
                cohort_label=self.cohort_label,
            )
            all_pds += pds

        # Forbidden without allowed endpoints.
        pc_ids = [pc.uid for pc in pcs]
        self.testapp.get(
            '/api/project_cohorts/participation?uid={}&uid={}'.format(*pc_ids),
            headers=jwt_headers(user),
            status=403)

        # Running various queries works as expected.
        self.batch_participation(user, pcs)

        # Simulate a new pd being written to the first pc by clearing that
        # memcache key. The server should fall back to sql and still give the
        # same results.
        id_key = ParticipantData.participation_by_pc_cache_key(pcs[0].uid)
        code_key = ParticipantData.participation_by_pc_cache_key(pcs[0].code)
        self.assertIsNotNone(memcache.get(id_key))
        self.assertIsNotNone(memcache.get(code_key))
        memcache.delete(id_key)
        memcache.delete(code_key)
        self.batch_participation(user, pcs)

        # Now with everything cached, clearing the db and running the same
        # queries again should have the same result.
        ParticipantData.delete_multi(all_pds)
        self.batch_participation(user, pcs)
Пример #2
0
    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)