def test_project_fdi_value(fdi_value_id, expected):
    """Tests that project fdi value gets default value when fdi value is null."""
    InvestmentProjectFactory(fdi_value_id=fdi_value_id, )
    etl = ETLInvestmentProjects(destination=MIInvestmentProject)

    updated, created = etl.load()
    assert (0, 1) == (updated, created)

    mi_investment_project = MIInvestmentProject.objects.values(
        *etl.COLUMNS).first()
    assert mi_investment_project['project_fdi_value'] == expected
def test_pipeline_can_query_investmentprojects():
    """Test that pipeline can query investment projects."""
    try:
        etl = ETLInvestmentProjects(destination=MIInvestmentProject)
        etl.get_source_query().first()
    except (FieldDoesNotExist, FieldError) as exc:
        pytest.fail(
            'Missing or incorrect column type in InvestmentProject model breaks the '
            'MI Dashboard pipeline. Please ensure that the change to InvestmentProject is '
            'reflected in the MI Dashboard pipeline and relevant MI dashboards. Message: '
            f'{str(exc)}.', )
def test_load_investment_projects():
    """Tests that investment projects are loaded to FDIDashboard table."""
    InvestmentProjectFactory.create_batch(10)
    etl = ETLInvestmentProjects(destination=MIInvestmentProject)

    updated, created = etl.load()
    assert (0, 10) == (updated, created)

    dashboard = MIInvestmentProject.objects.values(*etl.COLUMNS).all()
    for row in dashboard:
        source_row = etl.get_rows().get(pk=row['dh_fdi_project_id'])
        assert source_row == row
def test_number_new_jobs(number_new_jobs, expected):
    """Tests that number new jobs with zero is zero when source value is null."""
    InvestmentProjectFactory(number_new_jobs=number_new_jobs, )
    etl = ETLInvestmentProjects(destination=MIInvestmentProject)

    updated, created = etl.load()
    assert (0, 1) == (updated, created)

    mi_investment_project = MIInvestmentProject.objects.values(
        *etl.COLUMNS).first()
    assert mi_investment_project['number_new_jobs'] == number_new_jobs
    assert mi_investment_project['number_new_jobs_with_zero'] == expected
def test_total_investment(total_investment, expected):
    """Tests that total investment with zero is zero when source value is null."""
    InvestmentProjectFactory(total_investment=total_investment, )
    etl = ETLInvestmentProjects(destination=MIInvestmentProject)

    updated, created = etl.load()
    assert (0, 1) == (updated, created)

    mi_investment_project = MIInvestmentProject.objects.values(
        *etl.COLUMNS).first()
    assert mi_investment_project['total_investment'] == total_investment
    assert mi_investment_project['total_investment_with_zero'] == expected
def test_investor_company_country(country_id, expected):
    """Tests that if investor country is not set investor_company_country is empty."""
    investor_company = CompanyFactory(address_country_id=country_id, )
    InvestmentProjectFactory(investor_company=investor_company, )
    etl = ETLInvestmentProjects(destination=MIInvestmentProject)

    updated, created = etl.load()
    assert (0, 1) == (updated, created)

    mi_investment_project = MIInvestmentProject.objects.values(
        *etl.COLUMNS).first()
    assert mi_investment_project['investor_company_country'] == expected
示例#7
0
def test_country_of_origin(country_id, expected):
    """
    Tests that investor_company_country is populated when country_investment_originates_from
    is set.
    """
    InvestmentProjectFactory(country_investment_originates_from_id=country_id)
    etl = ETLInvestmentProjects(destination=MIInvestmentProject)

    updated, created = etl.load()
    assert (0, 1) == (updated, created)

    mi_investment_project = MIInvestmentProject.objects.values(
        *etl.COLUMNS).first()
    assert mi_investment_project['investor_company_country'] == expected
def test_run_mi_investment_project_etl_pipeline():
    """Tests that run_mi_investment_project_etl_pipeline copy data to MIInvestmentProject table."""
    InvestmentProjectFactory.create_batch(5, actual_land_date=date(2018, 4, 1))
    InvestmentProjectFactory.create_batch(5,
                                          actual_land_date=date(2018, 3, 31))

    updated, created = run_mi_investment_project_etl_pipeline()
    assert (0, 10) == (updated, created)

    etl = ETLInvestmentProjects(destination=MIInvestmentProject)
    dashboard = MIInvestmentProject.objects.values(
        *ETLInvestmentProjects.COLUMNS).all()
    for row in dashboard:
        source_row = etl.get_rows().get(pk=row['dh_fdi_project_id'])
        assert source_row == row
def test_actual_uk_region_names(uk_region_id, expected):
    """Tests that the field actual uk region names has correct default value."""
    investment_project = InvestmentProjectFactory()
    if uk_region_id:
        investment_project.actual_uk_regions.add(parse_uuid(uk_region_id))

    etl = ETLInvestmentProjects(destination=MIInvestmentProject)

    updated, created = etl.load()
    assert (0, 1) == (updated, created)

    mi_investment_project = MIInvestmentProject.objects.values(
        *etl.COLUMNS).first()

    assert mi_investment_project['actual_uk_region_names'] == expected
def test_top_level_sector(sector_id, expected):
    """Tests that top level sector has correct default value."""
    InvestmentProjectFactory(sector_id=sector_id, )

    etl = ETLInvestmentProjects(destination=MIInvestmentProject)

    updated, created = etl.load()
    assert (0, 1) == (updated, created)

    mi_investment_project = MIInvestmentProject.objects.values(
        *etl.COLUMNS).first()

    assert mi_investment_project['sector_name'] == expected
    assert mi_investment_project['top_level_sector_name'] == expected.split(
        ' :', maxsplit=1)[0]
def test_land_date(actual_land_date, estimated_land_date, expected_land_date):
    """Tests that land date becomes either actual land date or estimated land date."""
    InvestmentProjectFactory(
        actual_land_date=actual_land_date,
        estimated_land_date=estimated_land_date,
    )
    etl = ETLInvestmentProjects(destination=MIInvestmentProject)

    updated, created = etl.load()
    assert (0, 1) == (updated, created)

    mi_investment_project = MIInvestmentProject.objects.values(
        *etl.COLUMNS).first()
    assert mi_investment_project['actual_land_date'] == actual_land_date
    assert mi_investment_project['estimated_land_date'] == estimated_land_date
    assert mi_investment_project['land_date'] == expected_land_date
def test_sector_cluster(sector_cluster_id, expected):
    """Tests that sector cluster has correct default value."""
    parent_sector = SectorFactory(
        segment='Cats',
        sector_cluster_id=sector_cluster_id,
    )
    sector = SectorFactory(segment='Rockets', parent=parent_sector)
    InvestmentProjectFactory(sector_id=sector.id, )

    etl = ETLInvestmentProjects(destination=MIInvestmentProject)

    updated, created = etl.load()
    assert (0, 1) == (updated, created)

    mi_investment_project = MIInvestmentProject.objects.values(
        *etl.COLUMNS).first()
    assert mi_investment_project['sector_cluster'] == expected
示例#13
0
def test_country_url(country_id):
    """Tests that if country investment originates from is not set country url is empty."""
    InvestmentProjectFactory(country_investment_originates_from_id=country_id)
    etl = ETLInvestmentProjects(destination=MIInvestmentProject)

    updated, created = etl.load()
    assert (0, 1) == (updated, created)

    if country_id is None:
        expected = ''
    else:
        key = 'mi_fdi_dashboard_country'
        expected = f'{settings.DATAHUB_FRONTEND_URL_PREFIXES[key]}{country_id}'

    mi_investment_project = MIInvestmentProject.objects.values(
        *etl.COLUMNS).first()
    assert mi_investment_project['country_url'] == expected
def test_run_pipeline(caplog):
    """Tests that run_pipeline command copy investment projects to MIInvestmentProject table."""
    caplog.set_level('INFO')

    InvestmentProjectFactory.create_batch(5)

    management.call_command(run_pipeline.Command())

    assert 'Updated "0" and created "5" investment projects.' in caplog.text
    assert len(caplog.records) == 1

    etl = ETLInvestmentProjects(destination=MIInvestmentProject)
    dashboard = MIInvestmentProject.objects.values(
        *ETLInvestmentProjects.COLUMNS)
    for row in dashboard:
        source_row = etl.get_rows().get(pk=row['dh_fdi_project_id'])
        assert source_row == row
def test_investment_projects_get_updated():
    """Tests that investment projects get updated when running load again."""
    investment_projects = InvestmentProjectFactory.create_batch(10)
    extract = ETLInvestmentProjects(destination=MIInvestmentProject)

    updated, created = extract.load()
    assert (0, 10) == (updated, created)

    dashboard = MIInvestmentProject.objects.values(*extract.COLUMNS).all()
    for row in dashboard:
        source_row = extract.get_rows().get(pk=row['dh_fdi_project_id'])
        assert source_row == row

    for investment_project in investment_projects:
        investment_project.number_new_jobs = 100000000
        investment_project.save()

    updated, created = extract.load()
    assert (10, 0) == (updated, created)

    dashboard = MIInvestmentProject.objects.values(*extract.COLUMNS).all()
    for row in dashboard:
        source_row = extract.get_rows().get(pk=row['dh_fdi_project_id'])
        assert row['number_new_jobs'] == 100000000
        assert source_row == row