示例#1
0
    def setUp(self):
        self.geom_with_published_project = GeometryStore()
        self.geom_with_published_project.save()
        point = PointGeometry(geom=Point(20, 30))
        point.save()
        self.geom_with_published_project.points.add(point)
        self.published_project = ProjectFactory(
            published=True,
            countries=[CountryFactory(), CountryFactory()],
            total_cost=1200000)
        self.published_project.geo = self.geom_with_published_project
        self.published_project.save()

        self.geom_with_unpublished_project = GeometryStore()
        self.geom_with_unpublished_project.save()
        point = PointGeometry(geom=Point(40, 50))
        point.save()
        self.geom_with_unpublished_project.points.add(point)
        self.unpublished_project = ProjectFactory(published=False)
        self.unpublished_project.geo = self.geom_with_unpublished_project
        self.unpublished_project.save()

        self.list_url = reverse('api:geostore-centroids-list')

        self.user = User(username='******', password='******')
        self.user.save()
示例#2
0
 def setUp(self):
     super().setUp()
     self.china = CountryFactory(name='China', slug='china')
     self.india = CountryFactory(name='India', slug='india')
     self.china_project = factories.ProjectFactory(published=True)
     self.china_project.countries.add(self.china)
     self.india_project = factories.ProjectFactory(published=True)
     self.india_project.countries.add(self.india)
     self.all_project = factories.ProjectFactory(published=True)
     self.all_project.countries.add(self.china, self.india)
示例#3
0
 def test_plant_countries(self):
     """Ensure multiple countries are in CSV export"""
     country1 = CountryFactory()
     country2 = CountryFactory()
     power_plant = factories.PowerPlantFactory(countries=(country1, country2))
     response = self.client.get(self.url)
     stream = io.StringIO(response.content.decode('utf-8'))
     results = csv.DictReader(stream)
     for row in results:
         if row['plant_id'] == str(power_plant.id):
             self.assertTrue(country1.name in row['plant_countries'])
             self.assertTrue(country2.name in row['plant_countries'])
示例#4
0
    def setUp(self):
        self.geometry_store = GeometryStore()
        self.geometry_store.save()
        point = PointGeometryFactory()
        self.geometry_store.points.add(point)
        self.url = reverse('api:geometrystore-detail',
                           args=[self.geometry_store.identifier])
        # View ignores GeometryStores without projects, so add one
        self.project = ProjectFactory(
            countries=[CountryFactory(), CountryFactory()])
        self.project.geo = self.geometry_store
        self.project.save()

        self.user = User(username='******', password='******')
        self.user.save()
示例#5
0
    def test_power_plant_name(self):
        """Ensure Power Plant name is in export"""

        project1 = factories.ProjectFactory(power_plant=factories.PowerPlantFactory(),
                                            countries=(CountryFactory(),))
        for row in self.get_results():
            if row['identifier'] == str(project1.identifier):
                self.assertEqual(project1.power_plant.name, row['power_plant_name'])
示例#6
0
    def test_project_manufacturers(self):
        """Ensure related manufacturers are included in CSV export"""

        org1 = OrganizationFactory()
        org2 = OrganizationFactory()
        project1 = factories.ProjectFactory(manufacturers=(org1, org2),
                                            countries=(CountryFactory(),))
        for row in self.get_results():
            if row['identifier'] == str(project1.identifier):
                self.assertTrue(org1.name in row['manufacturers'])
                self.assertTrue(org2.name in row['manufacturers'])
示例#7
0
    def test_project_fuel(self):
        """Ensure related fuel relations are included in CSV export"""

        fuel1 = factories.FuelFactory()
        fuel2 = factories.FuelFactory()
        project1 = factories.ProjectFactory(fuels=(fuel1, fuel2),
                                            countries=(CountryFactory(),))
        for row in self.get_results():
            if row['identifier'] == str(project1.identifier):
                self.assertTrue(fuel1.name in row['fuel_type'])
                self.assertTrue(fuel2.name in row['fuel_type'])
                self.assertTrue(fuel1.fuel_category.name in row['fuel_category'])
                self.assertTrue(fuel2.fuel_category.name in row['fuel_category'])
示例#8
0
    def setUp(self):
        super().setUp()
        self.url = reverse('search:search')
        self.index_patch = patch.object(SiteSearch, 'index',
                                        TEST_SEARCH['default']['index'])
        self.index_patch.start()
        india = CountryFactory(name='India')
        china = CountryFactory(name='China')
        rail_type = InfrastructureTypeFactory(name='Railroad')
        road_type = InfrastructureTypeFactory(name='Road')

        self.china_rail = ProjectFactory(name='China Rail Project',
                                         infrastructure_type=rail_type,
                                         countries=[
                                             china,
                                         ])
        self.china_road = ProjectFactory(name='China Road Project',
                                         infrastructure_type=road_type,
                                         countries=[
                                             china,
                                         ])
        self.india_rail = ProjectFactory(name='India Rail Project',
                                         infrastructure_type=rail_type,
                                         countries=[
                                             india,
                                         ])
        self.india_road = ProjectFactory(name='India Road Project',
                                         infrastructure_type=road_type,
                                         countries=[
                                             india,
                                         ])
        serializer = ProjectSerializer()
        serializer.create_document(self.china_rail).save()
        serializer.create_document(self.china_road).save()
        serializer.create_document(self.india_rail).save()
        serializer.create_document(self.india_road).save()
        self.index.refresh()
    def test_countries_regions_created(self):
        """Countries and Regions are created correctly."""
        country_existing = CountryFactory(
            name=iso3166.countries.get('France').name,
            numeric=iso3166.countries.get('France').numeric,
            alpha_3=iso3166.countries.get('France').alpha3,
        )
        region_existing = RegionFactory(name='Existing Region')

        self.call_command(
            filename='power_plant_import/tests/data/six_rows.csv')

        # Get the PowerPlants that were created during the import
        (powerplant_ouessant, powerplant_ilarionas,
         powerplant_tonstad) = self.get_created_plants()
        # Get the Projects that were created during the import
        (project_ouessant1, project_ouessant2,
         project_liaoning) = self.get_created_projects()
        # The Countries and Regions have been assigned to the correct PowerPlants and Projects
        self.assertEqual(Country.objects.count(), 4)
        self.assertEqual(Region.objects.count(), 3)
        greece = Country.objects.get(name='Greece')
        china = Country.objects.get(name='China')
        norway = Country.objects.get(name='Norway')
        mediterranean = Region.objects.get(name='Gulf and Mediterranean')
        northeast_asia = Region.objects.get(name='Northeast Asia')
        self.assertEqual(set(powerplant_ouessant.countries.all()),
                         set([country_existing]))
        self.assertEqual(set(powerplant_ouessant.regions.all()),
                         set([region_existing]))
        self.assertEqual(set(project_ouessant1.countries.all()),
                         set([country_existing]))
        self.assertEqual(set(project_ouessant1.regions.all()),
                         set([region_existing]))
        self.assertEqual(set(project_ouessant1.countries.all()),
                         set([country_existing]))
        self.assertEqual(set(project_ouessant1.regions.all()),
                         set([region_existing]))
        self.assertEqual(set(powerplant_ilarionas.countries.all()),
                         set([greece]))
        self.assertEqual(set(powerplant_ilarionas.regions.all()),
                         set([mediterranean]))
        self.assertEqual(set(project_liaoning.countries.all()), set([china]))
        self.assertEqual(set(project_liaoning.regions.all()),
                         set([northeast_asia]))
        self.assertEqual(set(powerplant_tonstad.countries.all()),
                         set([norway]))
        self.assertEqual(set(powerplant_tonstad.regions.all()),
                         set([region_existing]))
示例#10
0
    def test_project_substations(self):
        """Ensure related substations are included in CSV export"""
        project1 = factories.ProjectFactory(countries=(CountryFactory(),))
        sub_station1 = factories.ProjectSubstationFactory(project=project1)
        sub_station2 = factories.ProjectSubstationFactory(project=project1)

        for row in self.get_results():
            if row['identifier'] == str(project1.identifier):
                substations = list(zip(row['substation_name'].split(','),
                                       row['substation_capacity'].split(','),
                                       row['substation_voltage'].split(','))
                                   )

                with self.subTest('Substation 1'):
                    self.assertTrue(sub_station1.name in substations[0][0])
                    self.assertTrue(str(sub_station1.capacity) in substations[0][1])
                    self.assertTrue(str(sub_station1.voltage) in substations[0][2])

                with self.subTest('Substation 2'):
                    self.assertTrue(sub_station2.name in substations[1][0])
                    self.assertTrue(str(sub_station2.capacity) in substations[1][1])
                    self.assertTrue(str(sub_station2.voltage) in substations[1][2])
示例#11
0
    def test_filters(self):
        included_org = OrganizationFactory()
        excluded_org = OrganizationFactory()

        with self.subTest('no filters'):
            response = self.client.get(self.url)
            returned_orgs = [
                result['name']
                for result in json.loads(response.content.decode())['results']
            ]
            self.assertIn(included_org.name, returned_orgs)
            self.assertIn(excluded_org.name, returned_orgs)

        with self.subTest('filter by leader'):
            leader = PersonFactory()
            included_org.leaders.add(leader)
            params = {'leaders': leader.id}
            response = self.client.get(self.url, params)
            returned_orgs = [
                result['name']
                for result in json.loads(response.content.decode())['results']
            ]
            self.assertIn(included_org.name, returned_orgs)
            self.assertNotIn(excluded_org.name, returned_orgs)

        with self.subTest('filter by parent'):
            parent = OrganizationFactory()
            included_org.parent = parent
            included_org.save()
            params = {'parent': parent.id}
            response = self.client.get(self.url, params)
            returned_orgs = [
                result['name']
                for result in json.loads(response.content.decode())['results']
            ]
            self.assertIn(included_org.name, returned_orgs)
            self.assertNotIn(excluded_org.name, returned_orgs)

        with self.subTest('filter by country (single)'):
            country = CountryFactory()
            country.save()
            included_org.countries.add(country)
            params = {'country': country.id}
            response = self.client.get(self.url, params)
            returned_orgs = [
                result['name']
                for result in json.loads(response.content.decode())['results']
            ]
            self.assertIn(included_org.name, returned_orgs)
            self.assertNotIn(excluded_org.name, returned_orgs)

        with self.subTest('filter by countries (multiple)'):
            country1 = CountryFactory()
            country2 = CountryFactory()
            included_org.countries.add(country1)
            extra_included_org = OrganizationFactory()
            extra_included_org.countries.add(country2)
            params = {'countries': [country1.id, country2.id]}
            response = self.client.get(self.url, params)
            returned_orgs = [
                result['name']
                for result in json.loads(response.content.decode())['results']
            ]
            self.assertIn(included_org.name, returned_orgs)
            self.assertIn(extra_included_org.name, returned_orgs)
            self.assertNotIn(excluded_org.name, returned_orgs)

        with self.subTest('filter by principal initiative'):
            principal_initiative = InitiativeFactory(
                principal_agent=included_org)
            params = {'principal_initiatives': principal_initiative.id}
            response = self.client.get(self.url, params)
            returned_orgs = [
                result['name']
                for result in json.loads(response.content.decode())['results']
            ]
            self.assertIn(included_org.name, returned_orgs)
            self.assertNotIn(excluded_org.name, returned_orgs)

        with self.subTest('filter by principal initiative existance'):
            params = {'principal_initiatives__isnull': 'False'}
            response = self.client.get(self.url, params)
            returned_orgs = [
                result['name']
                for result in json.loads(response.content.decode())['results']
            ]
            self.assertIn(included_org.name, returned_orgs)
            self.assertNotIn(excluded_org.name, returned_orgs)