Exemplo n.º 1
0
    def setUp(self):
        # There are two countries
        self.country1 = CountryFactory()
        self.country2 = CountryFactory()

        # And there are three contributors
        self.contributor1 = ContributorFactory()
        self.contributor2 = ContributorFactory()

        # Contributor3 makes no contributions
        self.contributor3 = ContributorFactory()

        # Contributor1 contributes 2 times to self.country1
        for i in range(2):
            self.create_contribution(self.contributor1, self.country1)

        # Contributor1 contributes 5 times to self.country2
        for i in range(5):
            self.create_contribution(self.contributor1, self.country2)

        # Contributor2 contributes 3 times to self.country1
        for i in range(3):
            self.create_contribution(self.contributor2, self.country1)

        # Contributor2 contributes 3 times to self.country2
        for i in range(3):
            self.create_contribution(self.contributor2, self.country2)

        # Compute the global and country ranks
        ContributorRank.compute_ranks()
Exemplo n.º 2
0
    def test_compute_ranks_matches_point_to_nearest_country(self):
        contributor = ContributorFactory()

        # Create a dummy country to find a point outside existing countries
        dummy_country = CountryFactory()
        point_outside_countries = dummy_country.geometry.point_on_surface
        dummy_country.delete()

        # The point is outside all existing countries
        self.assertFalse(
            Country.objects.filter(
                geometry__contains=point_outside_countries).exists())

        # The point is closer to country2
        self.assertGreater(
            self.country1.geometry.distance(point_outside_countries),
            self.country2.geometry.distance(point_outside_countries),
        )

        ContributionFactory(contributor=contributor,
                            point=point_outside_countries)

        self.assertFalse(
            ContributorRank.objects.filter(contributor=contributor).exists())

        ContributorRank.compute_ranks()

        self.assertTrue(
            ContributorRank.objects.filter(contributor=contributor,
                                           country=self.country2).exists())

        self.assertFalse(
            ContributorRank.objects.filter(contributor=contributor,
                                           country=self.country1).exists())
Exemplo n.º 3
0
    def test_contribution_set_frozen_during_rank_computation(self):
        contributor = ContributorFactory()
        country = CountryFactory()

        self.assertEqual(Contribution.objects.count(), 0)

        for i in range(3):
            self.create_contribution(contributor, country)

        self.assertEqual(Contribution.objects.count(), 3)

        contributions = list(Contribution.objects.all().select_related())
        self.assertEqual(Contribution.objects.count(), 3)

        for i in range(3):
            self.create_contribution(contributor, country)

        self.assertEqual(Contribution.objects.count(), 6)

        ContributorRank._compute_ranks(contributions)

        self.assertEqual(
            ContributorRank.objects.get(contributor=contributor,
                                        country=None).observations, 3)
        self.assertEqual(Contribution.objects.count(), 3)
    def test_list_countries_returns_country_data(self):
        today = datetime.date.today()

        countries = [CountryFactory() for i in range(3)]

        # A country with no contributions should not appear
        # in the results
        CountryFactory()

        for country in countries:
            for contributor_i in range(3):
                contributor = ContributorFactory()

                for contribution_i in range(10):
                    Contribution.objects.create(
                        contributor=contributor,
                        country=country,
                        date=today,
                        observations=1,
                    )

        ContributorRank.compute_ranks()

        response = self.client.get(reverse('countries-list'))

        self.assertEqual(response.status_code, 200)

        countries_data = json.loads(response.content)
        self.assertEqual(len(countries_data), len(countries))

        for country in countries:
            self.assertIn(
                {
                    'iso2':
                    country.iso2,
                    'name':
                    country.name,
                    'observations':
                    30,
                    'leaders_url':
                    reverse(
                        'leaders-country-list',
                        kwargs={'country_id': country.iso2},
                    ),
                }, countries_data)
 def setUp(self):
     super(SubmitContributionTests, self).setUp()
     fxa_profile_data = self.setup_profile_call()
     self.setup_verify_call(uid=fxa_profile_data['uid'])
     self.country = CountryFactory()
     self.contributor = ContributorFactory(fxa_uid=fxa_profile_data['uid'])