def test_organization_can_have_hierarchy(self): parent_org = OrganizationFactory() child_org = OrganizationFactory(parent=parent_org) grandchild_org = OrganizationFactory(parent=child_org) self.assertEqual(child_org.parent, parent_org) self.assertEqual(grandchild_org.parent, child_org) self.assertIn(grandchild_org, parent_org.get_descendants().all())
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'])
def test_plant_operators(self): """Ensure multiple operators are in CSV export""" operator1 = OrganizationFactory() operator2 = OrganizationFactory() power_plant = factories.PowerPlantFactory(operators=(operator1, operator2)) 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(operator1.name in row['plant_operators']) self.assertTrue(operator2.name in row['plant_operators'])
def test_consultants_created(self): """Consultants (Organizations) are created correctly for Projects.""" # Currently, there is just 1 Organization in the database, the org_existing org_existing = OrganizationFactory(name='Existing Organization') self.assertEqual(Organization.objects.count(), 1) self.call_command( filename='power_plant_import/tests/data/six_rows.csv') # Get the Projects that were created during the import (project_ouessant1, project_ouessant2, project_liaoning) = self.get_created_projects() # The CSV file mentions 1 consultant for the project_liaoning self.assertEqual(set(project_liaoning.consultants.all()), set([org_existing])) for project in [project_ouessant1, project_ouessant2]: self.assertEqual(project.consultants.count(), 0)
def test_operators_created(self): """Implementers (Organizations) are created correctly for PowerPlants.""" # Currently, there is just 1 Organization in the database, the org_existing org_existing = OrganizationFactory(name='Existing Organization') self.assertEqual(Organization.objects.count(), 1) 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() # The CSV file mentions 2 operators for the powerplant_tonstad operator1 = Organization.objects.get(name='Operator One') self.assertEqual(set(powerplant_tonstad.operators.all()), set([operator1, org_existing])) for power_plant in [powerplant_ouessant, powerplant_ilarionas]: self.assertEqual(power_plant.operators.count(), 0)
def test_funders_created(self): """Funders are created correctly for Projects.""" # Currently, there is 1 ProjectFunding object in the database org_existing = OrganizationFactory(name='Existing Organization') funder_existing = ProjectFundingFactory() funder_existing.sources.add(org_existing) self.assertEqual(ProjectFunding.objects.count(), 1) self.call_command( filename='power_plant_import/tests/data/six_rows.csv') # Get the Projects that were created during the import (project_ouessant1, project_ouessant2, project_liaoning) = self.get_created_projects() # The project_ouessant1 has 2 funders, and the project_ouessant2 has 1 funder. # The funder_existing also still exists. self.assertEqual(ProjectFunding.objects.count(), 4) self.assertEqual(project_ouessant1.funding.count(), 2) self.assertEqual( ProjectFunding.objects.filter( project=project_ouessant1, amount=100, currency='USD', ).count(), 1) self.assertEqual( ProjectFunding.objects.filter( project=project_ouessant1, amount=200, currency='RUB', ).count(), 1) self.assertEqual(project_ouessant2.funding.count(), 1) self.assertEqual( ProjectFunding.objects.filter( project=project_ouessant2, amount=None, currency=None, ).count(), 1) self.assertEqual(project_liaoning.funding.count(), 0) # The org_existing is funding 3 Projects: the one for the funder_existing, # the project_ouessant1 and the project_ouessant2 self.assertEqual(org_existing.projectfunding_set.count(), 3)
def test_contractors_created(self): """Contractors (Organizations) are created correctly for Projects.""" # Currently, there is just 1 Organization in the database, the org_existing OrganizationFactory(name='Existing Organization') self.assertEqual(Organization.objects.count(), 1) self.call_command( filename='power_plant_import/tests/data/six_rows.csv') # Get the Projects that were created during the import (project_ouessant1, project_ouessant2, project_liaoning) = self.get_created_projects() # The CSV file mentions two contractors, 1 for the project_ouessant1, and # 2 for the project_ouessant2 contractor1 = Organization.objects.get(name='Contractor One') contractor2 = Organization.objects.get(name='Contractor Two') self.assertEqual(set(project_ouessant1.contractors.all()), set([contractor1])) self.assertEqual(set(project_ouessant2.contractors.all()), set([contractor1, contractor2])) self.assertEqual(project_liaoning.contractors.count(), 0)
def test_manufacturers_created(self): """Manufacturers (Organizations) are created correctly for Projects.""" # Currently, there is just 1 Organization in the database, the org_existing org_existing = OrganizationFactory(name='Existing Organization') self.assertEqual(Organization.objects.count(), 1) self.call_command( filename='power_plant_import/tests/data/six_rows.csv') # Get the Projects that were created during the import (project_ouessant1, project_ouessant2, project_liaoning) = self.get_created_projects() # The CSV file mentions 3 manufacturers, 1 for the project_ouessant1, and # 2 for project_liaoning manufacturer1 = Organization.objects.get(name='Manufacturer One') manufacturer_vestas = Organization.objects.get( name='Vestas Wind Systems A/S') self.assertEqual(set(project_ouessant1.manufacturers.all()), set([org_existing])) self.assertEqual(set(project_liaoning.manufacturers.all()), set([manufacturer1, manufacturer_vestas])) self.assertEqual(project_ouessant2.manufacturers.count(), 0)
def test_owners_ownerstakes_created(self): """Owners and OwnerStakes are created correctly for PowerPlants.""" org_existing = OrganizationFactory(name='Existing Organization') # Currently, there is 1 owner (Organization) and 0 OwnerStakes objects # in the database self.assertEqual(Organization.objects.count(), 1) self.assertEqual(PlantOwnerStake.objects.count(), 0) 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() # The OwnerStakes have been assigned to the correct PowerPlants self.assertEqual(PlantOwnerStake.objects.count(), 4) owner_sabella = Organization.objects.get(name='Sabella SAS') owner_ppc = Organization.objects.get( name='Public Power Corporation SA') owner_stake_ouessant1 = PlantOwnerStake.objects.get( owner=owner_sabella, power_plant=powerplant_ouessant) self.assertEqual(owner_stake_ouessant1.percent_owned, 50) owner_stake_ouessant2 = PlantOwnerStake.objects.get( owner=org_existing, power_plant=powerplant_ouessant) self.assertEqual(owner_stake_ouessant2.percent_owned, 30) owner_stake_ilarionas1 = PlantOwnerStake.objects.get( owner=owner_ppc, power_plant=powerplant_ilarionas) self.assertIsNone(owner_stake_ilarionas1.percent_owned) owner_stake_ilarionas2 = PlantOwnerStake.objects.get( owner=org_existing, power_plant=powerplant_ilarionas) self.assertIsNone(owner_stake_ilarionas2.percent_owned) self.assertEqual(set(powerplant_ouessant.plant_owner_stakes.all()), set([owner_stake_ouessant1, owner_stake_ouessant2])) self.assertEqual(set(powerplant_ilarionas.plant_owner_stakes.all()), set([owner_stake_ilarionas1, owner_stake_ilarionas2])) self.assertEqual(powerplant_tonstad.plant_owner_stakes.count(), 0)
def test_organization_string_is_name(self): obj = OrganizationFactory() self.assertEqual(obj.__str__(), obj.name)
def test_organization_can_have_no_headquarters(self): obj = OrganizationFactory() self.assertIsNone(obj.headquarters)
def test_organization_can_be_leaderless(self): obj = OrganizationFactory() self.assertEqual(obj.leaders.count(), 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)
def test_organization_can_have_hierarchy(self): parent_org = OrganizationFactory(name='Parent') child_org = OrganizationFactory(parent=parent_org, name='Child') grandchild_org = OrganizationFactory(parent=child_org, name='Grandchild') # Refresh the tree state of the newly created orgs parent_org.refresh_from_db() child_org.refresh_from_db() grandchild_org.refresh_from_db() self.assertEqual(child_org.parent, parent_org) self.assertEqual(grandchild_org.parent, child_org) self.assertIn(grandchild_org, parent_org.get_descendants().all())