def test_get_survey_participation(self):
        """Get count of particpants at each marker."""
        pds = self.mock_one_finished_one_unfinished(1)
        survey_id = pds[0].survey_id
        start = datetime.date.today()
        end = start + datetime.timedelta(days=1)
        result = ParticipantData.participation(survey_id=survey_id,
                                               start=start,
                                               end=end)
        expected = [
            {
                'value': '1',
                'n': 1,
                'survey_ordinal': 1
            },
            {
                'value': '100',
                'n': 1,
                'survey_ordinal': 1
            },
        ]
        self.assertEqual(result, expected)

        # The same result should also now be available in memcache, so if we
        # clear the db the result should be the same.
        ParticipantData.delete_multi(pds)
        result = ParticipantData.participation(survey_id=survey_id,
                                               start=start,
                                               end=end)
        self.assertEqual(result, expected)
    def test_get_project_cohort_participation(self):
        """Get stats for all surveys in the project cohort."""
        pds1 = self.mock_one_finished_one_unfinished(1)
        pds2 = self.mock_one_finished_one_unfinished(2)
        project_cohort_id = pds1[0].project_cohort_id
        ProjectCohort(
            id=project_cohort_id,
            program_label=self.program_label,
        ).put()
        start = datetime.date.today()
        end = start + datetime.timedelta(days=1)

        expected = [
            {
                'value': '1',
                'n': 1,
                'survey_ordinal': 1
            },
            {
                'value': '100',
                'n': 1,
                'survey_ordinal': 1
            },
            {
                'value': '1',
                'n': 1,
                'survey_ordinal': 2
            },
            {
                'value': '100',
                'n': 1,
                'survey_ordinal': 2
            },
        ]

        result = ParticipantData.participation(
            project_cohort_id=project_cohort_id, start=start, end=end)
        self.assertEqual(result, expected)

        # The same result should also now be available in memcache, so if we
        # clear the db the result should be the same.
        ParticipantData.delete_multi(pds1 + pds2)
        result = ParticipantData.participation(
            project_cohort_id=project_cohort_id, start=start, end=end)
        self.assertEqual(result, expected)

        # It should also work if some other kwargs are set with value None.
        result = ParticipantData.participation(
            project_cohort_id=project_cohort_id,
            start=start,
            end=end,
            cohort_label=None)
        self.assertEqual(result, expected)
Esempio n. 3
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)
Esempio n. 4
0
 def test_delete_returns_affected_rows(self):
     params = dict(self.context_params, value='1')
     pd = ParticipantData.create(**params)
     ParticipantData.put(pd)
     affected_rows = ParticipantData.delete_multi([pd])
     self.assertEqual(affected_rows, 1)