Example #1
0
 def test_single_org(self):
     url = '/api/v1/orgs/%d/' % self.uganda.pk
     response = self.client.get(url)
     self.assertEqual(response.status_code, status.HTTP_200_OK)
     org = self.uganda
     logo_url = generate_absolute_url_from_file(response, org.logo) if org.logo else None
     self.assertDictEqual(response.data, dict(id=org.pk,
                                              logo_url=logo_url,
                                              name=org.name,
                                              language=org.language,
                                              subdomain=org.subdomain,
                                              domain=org.domain,
                                              timezone=org.timezone))
Example #2
0
    def test_single_org(self, mock_cache_get):
        mock_cache_get.return_value = None

        url = '/api/v1/orgs/%d/' % self.uganda.pk
        response = self.client.get(url)
        self.assertEqual(response.status_code, status.HTTP_200_OK)
        org = self.uganda
        logo_url = generate_absolute_url_from_file(response, org.logo) if org.logo else None
        gender_stats = response.data.pop('gender_stats')
        age_stats = response.data.pop('age_stats')
        registration_stats = response.data.pop('registration_stats')
        occupation_stats = response.data.pop('occupation_stats')
        reporters_count = response.data.pop('reporters_count')
        self.assertDictEqual(response.data, dict(id=org.pk,
                                                 logo_url=logo_url,
                                                 name=org.name,
                                                 language=org.language,
                                                 subdomain=org.subdomain,
                                                 domain=org.domain,
                                                 timezone=org.timezone))

        self.assertEquals(gender_stats, dict(female_count=0, female_percentage="---",
                                             male_count=0, male_percentage="---"))

        self.assertEqual(age_stats, [dict(name='0-14', y=0), dict(name='15-19', y=0), dict(name='20-24', y=0),
                                     dict(name='25-30', y=0), dict(name='31-34', y=0), dict(name='35+', y=0)])
        self.assertEquals(reporters_count, 0)
        self.assertEquals(occupation_stats, [])

        ReportersCounter.objects.create(org=org, type='gender:f', count=2)
        ReportersCounter.objects.create(org=org, type='gender:m', count=2)
        ReportersCounter.objects.create(org=org, type='gender:m', count=1)

        now = timezone.now()
        now_year = now.year

        two_years_ago = now_year - 2
        five_years_ago = now_year - 5
        twelve_years_ago = now_year - 12
        forthy_five_years_ago = now_year - 45

        ReportersCounter.objects.create(org=org, type='born:%s' % two_years_ago, count=2)
        ReportersCounter.objects.create(org=org, type='born:%s' % five_years_ago, count=1)
        ReportersCounter.objects.create(org=org, type='born:%s' % twelve_years_ago, count=3)
        ReportersCounter.objects.create(org=org, type='born:%s' % twelve_years_ago, count=2)
        ReportersCounter.objects.create(org=org, type='born:%s' % forthy_five_years_ago, count=2)

        ReportersCounter.objects.create(org=org, type='born:10', count=10)
        ReportersCounter.objects.create(org=org, type='born:732837', count=20)

        ReportersCounter.objects.create(org=org, type='total-reporters', count=5)

        ReportersCounter.objects.create(org=org, type='occupation:student', count=5)
        ReportersCounter.objects.create(org=org, type='occupation:writer', count=2)
        ReportersCounter.objects.create(org=org, type='occupation:all responses', count=13)

        response = self.client.get(url)


        gender_stats = response.data.pop('gender_stats')
        self.assertEqual(gender_stats, dict(female_count=2, female_percentage="40%",
                                            male_count=3, male_percentage="60%"))

        age_stats = response.data.pop('age_stats')
        self.assertEqual(age_stats, [dict(name='0-14', y=80), dict(name='15-19', y=0), dict(name='20-24', y=0),
                                     dict(name='25-30', y=0), dict(name='31-34', y=0), dict(name='35+', y=20)])

        reporters_count = response.data.pop('reporters_count')
        self.assertEqual(reporters_count, 5)

        occupation_stats = response.data.pop('occupation_stats')
        self.assertEqual(occupation_stats, [dict(label='student', count=5), dict(label='writer', count=2)])

        tz = pytz.timezone('UTC')

        with patch.object(timezone, 'now', return_value=tz.localize(datetime(2015, 9, 4, 3, 4, 5, 6))):

            for entry in registration_stats:
                self.assertEqual(entry['count'], 0)

            ReportersCounter.objects.create(org=org, type='registered_on:2015-08-27', count=3)
            ReportersCounter.objects.create(org=org, type='registered_on:2015-08-25', count=2)
            ReportersCounter.objects.create(org=org, type='registered_on:2015-08-25', count=3)
            ReportersCounter.objects.create(org=org, type='registered_on:2015-08-25', count=1)
            ReportersCounter.objects.create(org=org, type='registered_on:2015-06-30', count=2)
            ReportersCounter.objects.create(org=org, type='registered_on:2015-06-30', count=2)
            ReportersCounter.objects.create(org=org, type='registered_on:2014-11-25', count=6)

            response = self.client.get(url)

            stats = response.data.pop('registration_stats')

            non_zero_keys = {'08/24/15': 9, '06/29/15': 4}

            for entry in stats:
                self.assertFalse(entry['label'].endswith('14'))
                if entry['count'] != 0:
                    self.assertTrue(entry['label'] in non_zero_keys.keys())
                    self.assertEqual(entry['count'], non_zero_keys[entry['label']])
Example #3
0
    def test_single_org(self, mock_cache_get):
        mock_cache_get.return_value = None

        url = "/api/v1/orgs/%d/" % self.uganda.pk
        response = self.client.get(url)
        self.assertEqual(response.status_code, status.HTTP_200_OK)
        org = self.uganda
        logo_url = generate_absolute_url_from_file(response, org.logo) if org.logo else None
        gender_stats = response.data.pop("gender_stats")
        age_stats = response.data.pop("age_stats")
        registration_stats = response.data.pop("registration_stats")
        occupation_stats = response.data.pop("occupation_stats")
        reporters_count = response.data.pop("reporters_count")
        self.assertDictEqual(
            response.data,
            dict(
                id=org.pk,
                logo_url=logo_url,
                name=org.name,
                language=org.language,
                subdomain=org.subdomain,
                domain=org.domain,
                timezone=six.text_type(org.timezone),
            ),
        )

        self.assertEqual(
            gender_stats, dict(female_count=0, female_percentage="---", male_count=0, male_percentage="---")
        )

        self.assertEqual(
            age_stats,
            [
                dict(name="0-14", y=0),
                dict(name="15-19", y=0),
                dict(name="20-24", y=0),
                dict(name="25-30", y=0),
                dict(name="31-34", y=0),
                dict(name="35+", y=0),
            ],
        )
        self.assertEqual(reporters_count, 0)
        self.assertEqual(occupation_stats, [])

        ReportersCounter.objects.create(org=org, type="gender:f", count=2)
        ReportersCounter.objects.create(org=org, type="gender:m", count=2)
        ReportersCounter.objects.create(org=org, type="gender:m", count=1)

        now = timezone.now()
        now_year = now.year

        two_years_ago = now_year - 2
        five_years_ago = now_year - 5
        twelve_years_ago = now_year - 12
        forthy_five_years_ago = now_year - 45

        ReportersCounter.objects.create(org=org, type="born:%s" % two_years_ago, count=2)
        ReportersCounter.objects.create(org=org, type="born:%s" % five_years_ago, count=1)
        ReportersCounter.objects.create(org=org, type="born:%s" % twelve_years_ago, count=3)
        ReportersCounter.objects.create(org=org, type="born:%s" % twelve_years_ago, count=2)
        ReportersCounter.objects.create(org=org, type="born:%s" % forthy_five_years_ago, count=2)

        ReportersCounter.objects.create(org=org, type="born:10", count=10)
        ReportersCounter.objects.create(org=org, type="born:732837", count=20)

        ReportersCounter.objects.create(org=org, type="total-reporters", count=5)

        ReportersCounter.objects.create(org=org, type="occupation:student", count=5)
        ReportersCounter.objects.create(org=org, type="occupation:writer", count=2)
        ReportersCounter.objects.create(org=org, type="occupation:all responses", count=13)

        response = self.client.get(url)

        gender_stats = response.data.pop("gender_stats")
        self.assertEqual(
            gender_stats, dict(female_count=2, female_percentage="40%", male_count=3, male_percentage="60%")
        )

        age_stats = response.data.pop("age_stats")
        self.assertEqual(
            age_stats,
            [
                dict(name="0-14", y=80),
                dict(name="15-19", y=0),
                dict(name="20-24", y=0),
                dict(name="25-30", y=0),
                dict(name="31-34", y=0),
                dict(name="35+", y=20),
            ],
        )

        reporters_count = response.data.pop("reporters_count")
        self.assertEqual(reporters_count, 5)

        occupation_stats = response.data.pop("occupation_stats")
        self.assertEqual(occupation_stats, [dict(label="student", count=5), dict(label="writer", count=2)])

        tz = pytz.timezone("UTC")

        with patch.object(timezone, "now", return_value=tz.localize(datetime(2015, 9, 4, 3, 4, 5, 6))):

            for entry in registration_stats:
                self.assertEqual(entry["count"], 0)

            ReportersCounter.objects.create(org=org, type="registered_on:2015-08-27", count=3)
            ReportersCounter.objects.create(org=org, type="registered_on:2015-08-25", count=2)
            ReportersCounter.objects.create(org=org, type="registered_on:2015-08-25", count=3)
            ReportersCounter.objects.create(org=org, type="registered_on:2015-08-25", count=1)
            ReportersCounter.objects.create(org=org, type="registered_on:2015-06-30", count=2)
            ReportersCounter.objects.create(org=org, type="registered_on:2015-06-30", count=2)
            ReportersCounter.objects.create(org=org, type="registered_on:2014-11-25", count=6)

            response = self.client.get(url)

            stats = response.data.pop("registration_stats")

            non_zero_keys = {"08/24/15": 9, "06/29/15": 4}

            for entry in stats:
                self.assertFalse(entry["label"].endswith("14"))
                if entry["count"] != 0:
                    self.assertTrue(entry["label"] in non_zero_keys.keys())
                    self.assertEqual(entry["count"], non_zero_keys[entry["label"]])
Example #4
0
    def test_single_org(self):
        url = '/api/v1/orgs/%d/' % self.uganda.pk
        response = self.client.get(url)
        self.assertEqual(response.status_code, status.HTTP_200_OK)
        org = self.uganda
        logo_url = generate_absolute_url_from_file(
            response, org.logo) if org.logo else None
        gender_stats = response.data.pop('gender_stats')
        age_stats = response.data.pop('age_stats')
        registration_stats = response.data.pop('registration_stats')
        occupation_stats = response.data.pop('occupation_stats')
        reporters_count = response.data.pop('reporters_count')
        self.assertDictEqual(
            response.data,
            dict(id=org.pk,
                 logo_url=logo_url,
                 name=org.name,
                 language=org.language,
                 subdomain=org.subdomain,
                 domain=org.domain,
                 timezone=org.timezone))

        self.assertEquals(
            gender_stats,
            dict(female_count=0,
                 female_percentage="---",
                 male_count=0,
                 male_percentage="---"))

        self.assertEqual(age_stats, [
            dict(name='0-14', y=0),
            dict(name='15-19', y=0),
            dict(name='20-24', y=0),
            dict(name='25-30', y=0),
            dict(name='31-34', y=0),
            dict(name='35+', y=0)
        ])
        self.assertEquals(reporters_count, 0)
        self.assertEquals(occupation_stats, [])

        ReportersCounter.objects.create(org=org, type='gender:f', count=2)
        ReportersCounter.objects.create(org=org, type='gender:m', count=2)
        ReportersCounter.objects.create(org=org, type='gender:m', count=1)

        now = timezone.now()
        now_year = now.year

        two_years_ago = now_year - 2
        five_years_ago = now_year - 5
        twelve_years_ago = now_year - 12
        forthy_five_years_ago = now_year - 45

        ReportersCounter.objects.create(org=org,
                                        type='born:%s' % two_years_ago,
                                        count=2)
        ReportersCounter.objects.create(org=org,
                                        type='born:%s' % five_years_ago,
                                        count=1)
        ReportersCounter.objects.create(org=org,
                                        type='born:%s' % twelve_years_ago,
                                        count=3)
        ReportersCounter.objects.create(org=org,
                                        type='born:%s' % twelve_years_ago,
                                        count=2)
        ReportersCounter.objects.create(org=org,
                                        type='born:%s' % forthy_five_years_ago,
                                        count=2)

        ReportersCounter.objects.create(org=org, type='born:10', count=10)
        ReportersCounter.objects.create(org=org, type='born:732837', count=20)

        ReportersCounter.objects.create(org=org,
                                        type='total-reporters',
                                        count=5)

        ReportersCounter.objects.create(org=org,
                                        type='occupation:student',
                                        count=5)
        ReportersCounter.objects.create(org=org,
                                        type='occupation:writer',
                                        count=2)
        ReportersCounter.objects.create(org=org,
                                        type='occupation:all responses',
                                        count=13)

        response = self.client.get(url)

        gender_stats = response.data.pop('gender_stats')
        self.assertEqual(
            gender_stats,
            dict(female_count=2,
                 female_percentage="40%",
                 male_count=3,
                 male_percentage="60%"))

        age_stats = response.data.pop('age_stats')
        self.assertEqual(age_stats, [
            dict(name='0-14', y=80),
            dict(name='15-19', y=0),
            dict(name='20-24', y=0),
            dict(name='25-30', y=0),
            dict(name='31-34', y=0),
            dict(name='35+', y=20)
        ])

        reporters_count = response.data.pop('reporters_count')
        self.assertEqual(reporters_count, 5)

        occupation_stats = response.data.pop('occupation_stats')
        self.assertEqual(
            occupation_stats,
            [dict(label='student', count=5),
             dict(label='writer', count=2)])

        tz = pytz.timezone('UTC')

        with patch.object(timezone,
                          'now',
                          return_value=tz.localize(
                              datetime(2015, 9, 4, 3, 4, 5, 6))):

            for entry in registration_stats:
                self.assertEqual(entry['count'], 0)

            ReportersCounter.objects.create(org=org,
                                            type='registered_on:2015-08-27',
                                            count=3)
            ReportersCounter.objects.create(org=org,
                                            type='registered_on:2015-08-25',
                                            count=2)
            ReportersCounter.objects.create(org=org,
                                            type='registered_on:2015-08-25',
                                            count=3)
            ReportersCounter.objects.create(org=org,
                                            type='registered_on:2015-08-25',
                                            count=1)
            ReportersCounter.objects.create(org=org,
                                            type='registered_on:2015-06-30',
                                            count=2)
            ReportersCounter.objects.create(org=org,
                                            type='registered_on:2015-06-30',
                                            count=2)
            ReportersCounter.objects.create(org=org,
                                            type='registered_on:2014-11-25',
                                            count=6)

            response = self.client.get(url)

            stats = response.data.pop('registration_stats')

            non_zero_keys = {'08/24/15': 9, '06/29/15': 4}

            for entry in stats:
                self.assertFalse(entry['label'].endswith('14'))
                if entry['count'] != 0:
                    self.assertTrue(entry['label'] in non_zero_keys.keys())
                    self.assertEqual(entry['count'],
                                     non_zero_keys[entry['label']])