Exemple #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()
    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())
Exemple #3
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())
    def test_position_converted_to_lat_lon(self):
        Country.objects.all().delete()

        country1 = CountryFactory.build()
        country1.geometry = MultiPolygon([
            Polygon([
                Point(10, 10, srid=settings.WGS84_SRID),
                Point(11, 10, srid=settings.WGS84_SRID),
                Point(11, 11, srid=settings.WGS84_SRID),
                Point(10, 11, srid=settings.WGS84_SRID),
                Point(10, 10, srid=settings.WGS84_SRID),
            ]),
        ])
        country1.save()

        country2 = CountryFactory.build()
        country2.geometry = MultiPolygon([
            Polygon([
                Point(20, 20, srid=settings.WGS84_SRID),
                Point(21, 20, srid=settings.WGS84_SRID),
                Point(21, 21, srid=settings.WGS84_SRID),
                Point(20, 21, srid=settings.WGS84_SRID),
                Point(20, 20, srid=settings.WGS84_SRID),
            ]),
        ])
        country2.save()

        # Create a sample point in Country 1
        sample_point = Point(10.5, 10.5, srid=settings.WGS84_SRID)

        # Convert its coordinates into the projected spatial system
        sample_point.transform(settings.PROJECTION_SRID)

        observation_data = {
            'items': [
                {
                    'time': time.time(),
                    'tile_easting_m': sample_point.coords[0],
                    'tile_northing_m': sample_point.coords[1],
                    'observations': 100,
                },
            ],
        }

        payload = json.dumps(observation_data)

        response = self.client.post(
            reverse('contributions-create'),
            payload,
            content_type='application/json',
            HTTP_AUTHORIZATION='Bearer asdf',
        )

        self.assertEqual(response.status_code, 201)

        sample_point.transform(settings.WGS84_SRID)

        contribution = Contribution.objects.get()
        self.assertEqual(contribution.point, sample_point)
    def test_position_converted_to_lat_lon(self):
        Country.objects.all().delete()

        country1 = CountryFactory.build()
        country1.geometry = MultiPolygon([
            Polygon([
                Point(10, 10, srid=settings.WGS84_SRID),
                Point(11, 10, srid=settings.WGS84_SRID),
                Point(11, 11, srid=settings.WGS84_SRID),
                Point(10, 11, srid=settings.WGS84_SRID),
                Point(10, 10, srid=settings.WGS84_SRID),
            ]),
        ])
        country1.save()

        country2 = CountryFactory.build()
        country2.geometry = MultiPolygon([
            Polygon([
                Point(20, 20, srid=settings.WGS84_SRID),
                Point(21, 20, srid=settings.WGS84_SRID),
                Point(21, 21, srid=settings.WGS84_SRID),
                Point(20, 21, srid=settings.WGS84_SRID),
                Point(20, 20, srid=settings.WGS84_SRID),
            ]),
        ])
        country2.save()

        # Create a sample point in Country 1
        sample_point = Point(10.5, 10.5, srid=settings.WGS84_SRID)

        # Convert its coordinates into the projected spatial system
        sample_point.transform(settings.PROJECTION_SRID)

        observation_data = {
            'items': [
                {
                    'time': time.time(),
                    'tile_easting_m': sample_point.coords[0],
                    'tile_northing_m': sample_point.coords[1],
                    'observations': 100,
                },
            ],
        }

        payload = json.dumps(observation_data)

        response = self.client.post(
            reverse('contributions-create'),
            payload,
            content_type='application/json',
            HTTP_AUTHORIZATION='Bearer asdf',
        )

        self.assertEqual(response.status_code, 201)

        sample_point.transform(settings.WGS84_SRID)

        contribution = Contribution.objects.get()
        self.assertEqual(contribution.point, sample_point)
Exemple #6
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'])